<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\api\controller;

use think\response\Json;
use app\api\model\Order as OrderModel;
use app\api\model\Setting as SettingModel;
use app\api\service\User as UserService;
use app\api\service\Order as OrderService;
use app\common\service\qrcode\Extract as ExtractQRcode;
use cores\exception\BaseException;

/**
 * 我的订单控制器
 * Class Order
 * @package app\api\controller
 */
class Order extends Controller
{
    /**
     * 我的订单列表
     * @param string $dataType 订单类型
     * @return Json
     * @throws BaseException
     * @throws \think\db\exception\DbException
     */
    public function list(string $dataType): Json
    {
        $model = new OrderModel;
        $list = $model->getList($dataType);
//        return $this->renderSuccess(compact('list'));
        $list = $this->renderSuccess(compact('list'));

        return $list;
    }

    public function del()
    {
        $model = new OrderModel;
        $res = $model->del();
        if ($res) {
            return $this->renderSuccess('删除成功');
        }
        return $this->renderError('删除失败');
    }

    public function orderfast()
    {
        $model = new OrderModel;
        $res = $model->orderfast();
        if ($res) {
            return $this->renderSuccess('催单成功');
        }
        return $this->renderError('催单失败');
    }

    /**
     * 订单详情信息
     * @param int $orderId 订单ID
     * @return Json
     * @throws BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function detail(int $orderId): Json
    {
        // 订单详情
        $model = OrderModel::getUserOrderDetail($orderId);
        //自提时间
//        $model->ziti_time = '2024-03-02 22:22:22';
//        $model->ziti_status = 0;//0没提 1已提货
//        $model->ziti_image = ['https://www.saas.njrenzhou.com/uploads/10001/20240116/18adc22ad9ea021ab2586878fe1035b0.png'];

        return $this->renderSuccess([
            'order' => $model,  // 订单详情
            'setting' => [
                // 积分名称
                'points_name' => SettingModel::getPointsName(),
            ],
        ]);
    }

    /**
     * 获取物流跟踪信息
     * @param int $orderId 订单ID
     * @return Json
     * @throws BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function express(int $orderId): Json
    {
        $service = new OrderService;
        $express = $service->express($orderId);
        return $this->renderSuccess(compact('express'));
    }

    /**
     * 取消订单
     * @param int $orderId
     * @return Json
     * @throws BaseException
     */
    public function cancel(int $orderId): Json
    {
        $model = OrderModel::getDetail($orderId);
        if ($model->cancel()) {
            return $this->renderSuccess($model->getMessage());
        }
        return $this->renderError($model->getError() ?: '订单取消失败');
    }

    /**
     * 确认收货
     * @param int $orderId
     * @return Json
     * @throws BaseException
     */
    public function receipt(int $orderId): Json
    {
        $model = OrderModel::getDetail($orderId);
        if ($model->receipt()) {
            return $this->renderSuccess('确认收货成功');
        }
        return $this->renderError($model->getError());
    }

    /**
     * 获取当前用户待处理的订单数量
     * @return Json
     * @throws BaseException
     */
    public function todoCounts(): Json
    {
        $model = new OrderModel;
        $counts = $model->getTodoCounts();
        return $this->renderSuccess(compact('counts'));
    }

    /**
     * 获取自提核销二维码
     * @param int $orderId
     * @param string $channel 二维码渠道(小程序码、h5码)
     * @return Json
     * @throws BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function extractQrcode(int $orderId, string $channel = 'H5'): Json
    {
        // 订单详情
        $order = OrderModel::getDetail($orderId);
        // 判断是否为待核销订单
        if (!$order->checkExtractOrder($order)) {
            return $this->renderError($order->getError());
        }
        // 当前用户信息
        $userInfo = UserService::getCurrentLoginUser();
        // 创建二维码
        $Qrcode = new ExtractQRcode($userInfo, $orderId, $channel);
        return $this->renderSuccess(['qrcode' => $Qrcode->getImage()]);
    }
}