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


use app\admin\model\MedicalCareOrder;
use app\admin\model\MedicalCareOrderProduct;
use app\admin\model\PackageOrderDetail;
use app\admin\model\SystemAdmin;
use app\admin\model\SystemMenu;
use app\admin\model\SystemRole;

class MedicalCareOrderServer
{
    /**
     * Notes:获取订单列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: QJF
     * Date: 2022/9/17
     */
    public static function getProductOrderList(int $page, int $limit, string $keywords)
    {
        [$list, $count] =  MedicalCareOrder::getOrderList($page, $limit,$keywords);

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

    /**
     * Notes:获取订单详情
     * @param int $order_id
     * @return int
     * User: QJF
     * Date: 2022/9/17
     */
    public static function productOrderInfo($order_id)
    {
        SystemRole::affairBegin();
        try {
            $where = [];
            $where['order_id'] = $order_id;
            $result = MedicalCareOrder::with(['user','Shop'])->where($where)->first();
            //订单详情的列表
            $result['detail_list'] = MedicalCareOrderProduct::with(['product','user'])
                ->where(['order_product_order_id'=>$order_id])->get();

            if (!empty($result)){
                SystemRole::affairCommit();
                return $result;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            SystemRole::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)
    {
        MedicalCareOrder::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'] = date("Y-m-d H:i:s",time());
            }else{
                $data['order_pay_time'] = "";
            }
            $result = MedicalCareOrder::where($where)->update($data);
            if ($result !== false){
                $user_id = MedicalCareOrder::where($where)->value('order_user_id');
                events('createUserNews', ['log_type' => 0, 'log_uid' => $user_id, 'log_content' => '您的订单支付成功了！']);
                MedicalCareOrder::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            MedicalCareOrder::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }
}
