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

use app\admin\model\LifeFarmGoods;
use app\admin\model\LifeFarmOrder;
use app\admin\model\LifeFarmOrderDetail;
use app\admin\model\LifeFoodOrderDetail;
use app\admin\model\MedicalCareOrder;
use app\admin\model\MedicalCareOrderProduct;
use app\admin\model\SystemRole;

class FarmOrderServer
{
    /**
     * Notes:获取订单列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: QJF
     * Date: 2022/11/04
     */
    public static function getOrderList(int $page, int $limit, string $keywords)
    {
        [$list, $count] =  LifeFarmOrder::getOrderList($page, $limit,$keywords);
        return compact('list', 'page', 'limit', 'count');
    }

    /**
     * Notes:订单详情
     * @return \support\Response
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * User: QJF
     * Date: 2022/9/27
     */
    public static function orderInfo($order_id)
    {
        LifeFarmOrder::affairBegin();
        try {
            $where = [];
            $where['order_id'] = $order_id;
            $result = LifeFarmOrder::with(['User','Shop'])->where($where)->first();
            //订单详情的列表1
            $result['detail_list'] = LifeFarmOrderDetail::where(['detail_order_id'=>$order_id])->get();
            if (!empty($result)){
                LifeFarmOrder::affairCommit();
                return $result;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            LifeFarmOrder::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }
    /**
     * Notes:订单发货
     * User: QJF
     * Date: 2022/11/4
     */
    public static function orderDelivery($param,$admin_id)
    {
        LifeFarmOrder::affairBegin();
        try {
            $where = [];
            $where['order_id'] = $param['order_id'];
            $data['order_deliver_id'] = 1;
            $data['order_number'] = $param['order_number'];//物流单号
            $data['order_company'] = $param['courier_company'];//物流公司
            $data['order_address_nickname'] = $param['order_address_nickname'];//收货人姓名
            $data['order_address_mobile'] = $param['order_address_mobile'];//收货人手机号
            $data['order_address'] = $param['order_address'];//收货人地址
            $data['order_remke'] = $param['order_remke'];//收货人备注
            $data['order_deliver_time'] = time();
            $data['order_update_time'] = time();
            $result = LifeFarmOrder::where($where)->update($data);
            if (!empty($result)){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '订单发货-编号: ' . $param['order_id'];
                plog('life-farmorder-delivery', '农场-绿色蔬菜订单发货', $msg);
                LifeFarmOrder::affairCommit();
                return true;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            LifeFarmOrder::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)
    {
        LifeFarmOrder::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 = LifeFarmOrder::where($where)->update($data);
            if ($result !== false){
                LifeFarmOrder::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            LifeFarmOrder::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }
}
