<?php
namespace app\admin\server\life;

use app\admin\model\GoodsOrder;

class GoodsOrderServer
{
    /**
     * Notes:获取订单列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: ZQ
     * Date: 2022/10/13
     */
    public static function getOrderList(int $page, int $limit, string $keywords,$order_type_model)
    {
        [$list, $count] =  GoodsOrder::getOrderList($page, $limit,$keywords,$order_type_model);
        return compact('list', 'page', 'limit', 'count');
    }

    /**
     * Notes:获取订单详情
     * @param int $order_id
     * @return int
     * User: ZQ
     * Date: 2022/10/13
     */
    public static function orderInfo($order_id)
    {
        $where = [];
        $where['order_id'] = $order_id;
        $result = GoodsOrder::with(['user','detail'])->where($where)
            ->leftJoin('merchant_shop','shop_id','=','life_market_order.order_shop_id')
            ->first(['life_market_order.*','merchant_shop.shop_name']);
        if (!empty($result)){
            return $result;
        }else{
            return false;
        }
    }

    /**
     * Notes:发货
     * User: YCP
     * Date: 2022/11/9
     */
    public static function orderDelivery($params,$admin_id)
    {
        GoodsOrder::affairBegin();
        try {
            $where = [];
            $where['order_id'] = $params['order_id'];
            $data['order_express_no'] = $params['order_express_no'];//快递单号
            $data['order_express_company'] = $params['order_express_company'];//物流公司
            $data['order_status'] = $params['order_status']; // 1待发货2已发货，待收货3已收货待评价4已完成5退款中6已退款7待使用8已使用9待预约10退款驳回
            if($data['order_status'] == 2){
                $data['order_deliver_time'] = time();
            }
            if($data['order_status'] == 10){
                $data['order_reject_reason'] = $params['order_reject_reason'];
            }
            
            $result = GoodsOrder::where($where)->update($data);
            if (!empty($result)){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '订单发货-编号: ' . $params['order_id'];
                plog('life-order-delivery', '超市订单发货', $msg);
                GoodsOrder::affairCommit();
                return true;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            GoodsOrder::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:修改订单支付状态
     * @param string $order_id
     * @param int $order_finish
     * @return int
     * User: YCP
     * Date: 2022/11/28
     */
    public static function updatePayStatus($order_id, $order_pay_status)
    {
        GoodsOrder::affairBegin();
        try {
            $where = [];
            $where['order_id'] = $order_id;
            $data = [];
            $data['order_pay_status'] = $order_pay_status;
            if($order_pay_status == 1){
                $data['order_pay_time'] = time();
            }else{
                $data['order_pay_time'] = "";
            }
            $result = GoodsOrder::where($where)->update($data);
            if ($result !== false){
                $user_id = GoodsOrder::where($where)->value('order_user_id');
                events('createUserNews', ['log_type' => 0, 'log_uid' => $user_id, 'log_content' => '您的订单支付成功了！']);
                GoodsOrder::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            GoodsOrder::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }
}
