<?php

namespace app\admin\controller\order;

use app\admin\validate\coupon\CouponValidate;
use app\admin\validate\device\DeviceValidate;
use app\admin\validate\order\OrderValidate;
use app\controller\Curd;
use app\model\Coupon;
use app\model\Device;
use app\model\Order;
use app\model\OrderSheet;
use app\model\Supplier;
use app\model\SysSerial;
use support\exception\BusinessException;
use support\Request;
use support\Response;

class CompleteController extends Curd
{
    public function __construct()
    {
        $this->model = new Order();
        $this->validate = true;
        $this->validateClass = new OrderValidate();
    }

    /**
     * @Desc 列表
     * @Author Gorden
     * @Date 2024/3/28 15:01
     *
     * @param Request $request
     * @return Response
     * @throws \support\exception\BusinessException
     */
    public function select(Request $request): Response
    {
        [$where, $format, $limit, $field, $order] = $this->selectInput($request);
        $order = $request->get('order', 'desc');
        $field = $field ?? 'order_addtimes';
        $where['order_status_system'] = 'CONFIRM';
        $where['order_is_complete'] = ['in', ['Y', 'R']];
        $query = $this->doSelect($where, $field, $order);
        return $this->doFormat($query, $format, $limit);
    }

    /**
     * @Desc 订单商品详情
     * @Author Gorden
     * @Date 2024/3/29 8:50
     *
     * @param Request $request
     * @return Response
     */
    public function sheet(Request $request)
    {
        $orderId = $request->get('order_id');
        $orderSheet = OrderSheet::with([
            'member' => function ($query) {
                $query->select('member_id', 'member_mobile');
            },
            'goods' => function ($query) {
                $query->select('goods_id', 'goods_name', 'goods_cover', 'goods_market_price', 'goods_sales_price', 'join_goods_supplier_id');
            },
            'memberInfo',
            'cert'
        ])->where('join_sheet_order_id', $orderId)
            ->get()
            ->toArray();
        foreach ($orderSheet as &$item) {
            $item['goods']['goods_cover'] = getenv('STORAGE_DOMAIN') . $item['goods']['goods_cover'];
            $item['goods']['supplier_name'] = '';
            if (!empty($item['goods']['join_goods_supplier_id'])) {
                $supplier = Supplier::where('supplier_id',$item['goods']['join_goods_supplier_id'])->value('supplier_name');
                if ($supplier){
                    $item['goods']['supplier_name'] = $supplier;
                }
            }
        }

        $order = Order::where('order_id', $orderId)->first();
        $data = [
            'order' => $order,
            'sheet' => $orderSheet
        ];

        return json_success('', $data);
    }

    protected function updateInput(Request $request): array
    {
        $primary_key = $this->model->getKeyName();
        $id = $request->post($primary_key);
        $data = $this->inputFilter($request->post());
        $data['coupon_begindate'] = strtotime($data['coupon_begindate']);
        $data['coupon_enddate'] = strtotime($data['coupon_enddate']);
        $model = $this->model->find($id);
        if (!$model) {
            throw new BusinessException('记录不存在', 2);
        }
        unset($data[$primary_key]);
        return [$id, $data];
    }
}