<?php
declare (strict_types=1);

namespace app\api\controller;

use cores\exception\BaseException;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\response\Json;

class Identity extends Controller
{
    /**
     * @notes:会员身份价格列表
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException|BaseException
     * @author: wanghousheng
     */
    public function userPriceList(): Json
    {
        $client = $this->request->post('client');
        if (!$client) {
            return $this->renderError('客户端不能为空');
        }
        $model = new \app\api\service\Identity();
        $list = $model->userCenter($client);
        return $this->renderSuccess(compact('list'));
    }

    /**
     * @notes:分销身份价格列表
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException|BaseException
     * @author: wanghousheng
     */
    public function dealerPriceList(): Json
    {
        $client = $this->request->post('client');
        if (!$client) {
            return $this->renderError('客户端不能为空');
        }
        $model = new \app\api\service\Identity();
        $list = $model->dealerCenter($client);
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 确认充值订单
     * @return Json
     * @throws BaseException
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function submit(): Json
    {
        $method = $this->request->post('method');
        if (!$method) {
            return $this->renderError('支付方式不能为空');
        }
        $client = $this->request->post('client');
        if (!$client) {
            return $this->renderError('客户端不能为空');
        }
        $identityId = intval($this->request->post('identity_id'));
        if (!$identityId) {
            return $this->renderError('缺少必要参数');
        }
        $service = new \app\api\service\Identity();
        $data = $service->setMethod($method)
            ->setClient($client)
            ->orderPay($identityId);
        return $this->renderSuccess($data, $service->getMessage() ?: '下单成功');
    }

    /**
     * 查询第三方支付订单是否付款成功
     * @return Json
     * @throws BaseException
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function tradeQuery(): Json
    {
        $method = $this->request->post('method');
        if (!$method) {
            return $this->renderError('支付方式不能为空');
        }
        $order_no = $this->request->post('order_no');
        if (!$order_no) {
            return $this->renderError('订单号不能为空');
        }
        $client = $this->request->post('client');
        if (!$client) {
            return $this->renderError('客户端不能为空');
        }
        $service = new \app\api\service\Identity();
        $result = $service->setMethod($method)->setClient($client)->tradeQuery($order_no);
        $message = $result ? '恭喜您,开通成功' : ($service->getError() ?: '很抱歉,订单未支付,请重新发起');
        return $this->renderSuccess(['isPay' => $result], $message);
    }
}