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

use app\admin\model\RaiseGoodsOrder;

class RaiseOldGoodsOrderServer
{
    /**
     * 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)
    {
        [$list, $count] =  RaiseGoodsOrder::getOrderList($page, $limit,$keywords);
        if (!empty($list)){
            foreach ($list as $k => $v){
                $list[$k]['order_subscribe_time'] = date('Y-m-d H:i:s',$v['order_subscribe_time']);
                $list[$k]['order_create_time'] = date('Y-m-d H:i:s',$v['order_create_time']);
                if (!empty($v['order_update_time'])){
                    $list[$k]['order_update_time'] = date('Y-m-d H:i:s',$v['order_update_time']);
                }
            }
        }
        return compact('list', 'page', 'limit', 'count');
    }

    /**
     * Notes:获取订单详情
     * @param int $order_id
     * @return int
     * User: ZQ
     * Date: 2022/10/13
     */
    public static function getOrderInfo($order_id)
    {
        $where = [];
        $where['order_id'] = $order_id;
        $result = RaiseGoodsOrder::where($where)->with(['user'])->first();
        if (!empty($result)){
            $result['order_subscribe_time'] = date('Y-m-d H:i:s',$result['order_subscribe_time']);
            $result['order_create_time'] = date('Y-m-d H:i:s',$result['order_create_time']);
            if (!empty($result['order_update_time'])){
                $result['order_update_time'] = date('Y-m-d H:i:s',$result['order_update_time']);
            }

        }
        if (!empty($result)){
            return $result;
        }else{
            return false;
        }
    }

    /**
     * Notes:订单发货
     * @param int $order_id
     * @return int
     * User: ZQ
     * Date: 2022/10/13
     */
    public static function orderUpdate($order_id,$order_address,$order_address_mobile,$order_address_nickname,$order_remke,$order_express,$order_express_name)
    {
        RaiseGoodsOrder::affairBegin();
        try {
            $where = [];
            $where['order_id'] = $order_id;
            $data = [];
            $data['order_address'] = $order_address;
            $data['order_address_mobile'] = $order_address_mobile;
            $data['order_address_nickname'] = $order_address_nickname;
            $data['order_remke'] = $order_remke;
            $data['order_express'] = $order_express;
            $data['order_express_name'] = $order_express_name;
            $data['order_status'] = 1;
            $data['order_deliver_id'] = 1;
            $result = RaiseGoodsOrder::where($where)->update($data);
            if (!empty($result)){
                RaiseGoodsOrder::affairCommit();
                return $result;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            RaiseGoodsOrder::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)
    {
        RaiseGoodsOrder::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 = RaiseGoodsOrder::where($where)->update($data);
            if ($result !== false){
                RaiseGoodsOrder::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            RaiseGoodsOrder::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }
}
