<?php

namespace app\api\service\wholesaler;

use app\api\model\Payment as PaymentModel;
use app\api\model\wholesaler\Set;
use app\api\service\User as UserService;
use app\common\service\BaseService;
use cores\exception\BaseException;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class Wholesaler extends BaseService
{
    // 提示信息
    private string $message = '';

    // 支付方式 (微信支付、支付宝、余额)
    private string $method;

    // 下单的客户端
    private string $client;

    /**
     * 设置当前支付方式
     * @param string $method 支付方式
     * @return $this
     */
    public function setMethod(string $method): Wholesaler
    {
        $this->method = $method;
        return $this;
    }

    /**
     * 设置下单的客户端
     * @param string $client 客户端
     * @return $this
     */
    public function setClient(string $client): Wholesaler
    {
        $this->client = $client;
        return $this;
    }

    /**
     * 开通批发商页面数据
     * @return array
     * @throws BaseException
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function center(): array
    {
        // 当期用户信息
        $userInfo = UserService::getCurrentLoginUser(true);
        // 获取充值方案列表
        $info = Set::detail();
        if (!$info->isEmpty()) {
            $info = $info->toArray();
        }
        //计算优惠价格
        // 根据指定客户端获取可用的支付方式
        $PaymentModel = new PaymentModel;
        $methods = $PaymentModel->getMethodsByClient($this->client);
        // 返回数据
        return [
            'personal' => $userInfo,
            'info' => $info,
            'paymentMethods' => $methods
        ];
    }

    /**
     * 确认订单支付事件
     * @param array $data
     * @param array $extra 附加数据
     * @return array[]
     * @throws BaseException
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function orderPay(array $data, array $extra = []): array
    {
        $PaymentService = new Payment();
        $result = $PaymentService->setMethod($this->method)
            ->setClient($this->client)
            ->orderPay($data, $extra);
        $this->message = $PaymentService->getMessage();
        return $result;
    }

    /**
     * 返回消息提示
     * @return string
     */
    public function getMessage(): string
    {
        return $this->message;
    }
}