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

use app\admin\model\HealthyOrder;

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

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

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

    /**
     * Notes:发货
     * User: YCP
     * Date: 2022/11/9
     */
    public static function orderDelivery($params,$admin_id)
    {
        HealthyOrder::affairBegin();
        try {
            $where = [];
            $where['order_id'] = $params['order_id'];
            $data['order_express_no'] = $params['order_express_no'];//快递单号
            $data['order_express_company'] = $params['order_express_company'];//物流公司
            $data['order_status'] = $params['order_status']; //0待发货   1已发货，待收货  2已收货待评价  3已完成  4部分退款 5全部退款
            $data['order_delivery'] = time();
            $result = HealthyOrder::where($where)->update($data);
            if (!empty($result)){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '订单发货-编号: ' . $params['order_id'];
                plog('healthy-healthyorder-delivery', '健康订单发货', $msg);
                HealthyOrder::affairCommit();
                return true;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            HealthyOrder::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)
    {
        HealthyOrder::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 = HealthyOrder::where($where)->update($data);
            if ($result !== false){
                HealthyOrder::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            HealthyOrder::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

}
