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

use app\admin\model\LifePackageOrder;

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

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

    /**
     * Notes:获取订单详情
     * @param int $order_id
     * @return int
     * User: YCP
     * Date: 2022/10/19
     */
    public static function orderInfo($order_id)
    {
        LifePackageOrder::affairBegin();
        try {
            $where = [];
            $where['order_id'] = $order_id;
            $result = LifePackageOrder::with(['Shop','User','Package','PackageGoods'])->where($where)->first();
            if (!empty($result)){
                LifePackageOrder::affairCommit();
                return $result;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            LifePackageOrder::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:修改订单完成状态
     * @param string $order_id
     * @param int $order_finish
     * @return int
     * User: YCP
     * Date: 2022/10/19
     */
    public static function updateFinish($order_id, $order_finish)
    {
        LifePackageOrder::affairBegin();
        try {
            $where = [];
            $where['order_id'] = $order_id;
            $data = [];
            $data['order_finish'] = $order_finish;
            if($order_finish == 1){
                $data['order_finish_time'] = time();
            }else{
                $data['order_finish_time'] = "";
            }
            $result = LifePackageOrder::where($where)->update($data);
            if ($result !== false){
                LifePackageOrder::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            LifePackageOrder::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)
    {
        LifePackageOrder::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 = LifePackageOrder::where($where)->update($data);
            if ($result !== false){
                LifePackageOrder::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            LifePackageOrder::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }
}
