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

use app\admin\model\TravelOrder;
use app\admin\model\TravelLine;

class OrderServer
{
    /**
     * Notes:获取订单列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: YCP
     * Date: 2022/10/21
     */
    public static function getOrderList(int $page, int $limit, string $keywords)
    {
        [$list, $count] =  TravelOrder::getOrderList($page, $limit,$keywords);

        return compact('list', 'page', 'limit', 'count');
    }

    /**
     * Notes:获取订单详情
     * @param int $order_id
     * @return int
     * User: YCP
     * Date: 2022/10/21
     */
    public static function orderInfo(int $order_id)
    {
        TravelOrder::affairBegin();
        try {
            $where = [];
            $where['order_id'] = $order_id;
            $result = TravelOrder::with(['Line'])->where($where)->first();
            if (!empty($result)){
                TravelOrder::affairCommit();
                return $result;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            TravelOrder::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)
    {
        TravelOrder::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 = TravelOrder::where($where)->update($data);
            if ($result !== false){
                TravelOrder::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            TravelOrder::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 updateOrderStatus($order_id, $order_status, $order_reject_reason)
    {
        TravelOrder::affairBegin();
        try {
            $where = [];
            $where['order_id'] = $order_id;
            $data = [];
            $data['order_status'] = $order_status;
            if($data['order_status'] == 10){
                $data['order_reject_reason'] = $order_reject_reason;
            }

            $result = TravelOrder::where($where)->update($data);
            if ($result !== false){
                TravelOrder::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            TravelOrder::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    
}
