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

use app\admin\model\CinemaOrder;

class CinemaOrderServer
{
    /**
     * 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] =  CinemaOrder::getOrderList($page, $limit,$keywords);
        if (!empty($list)){
            foreach ($list as $k => $v){
                $list[$k]['order_create_time'] = date('Y-m-d H:i:s',$v['order_create_time']);
                $list[$k]['order_update_time'] = $v['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 orderInfo($order_id)
    {
        CinemaOrder::affairBegin();
        try {
            $where = [];
            $where['order_id'] = $order_id;
            $result = CinemaOrder::with(['user','detail'])->where($where)->first();
            if (!empty($result)){
                CinemaOrder::affairCommit();
                return $result;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            CinemaOrder::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)
    {
        CinemaOrder::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 = CinemaOrder::where($where)->update($data);
            if ($result !== false){
                CinemaOrder::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            CinemaOrder::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }
}
