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

use app\admin\model\LifeFarmLandOrder;
use app\admin\model\LifeFarmLand;
use app\admin\model\LifeFarmLandOrderDetail;

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

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

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

    /**
     * Notes: 下单
     * @param string $package_name
     * @param array $package_rules
     * @return int
     * User: YCP
     * Date: 2022/10/25
     */
    public static function insertOrder(array $params, int $user_id)
    {
        $land_id = explode(',',$params['order_goods_id']);
        $params['order_price'] = 0;
        foreach ($land_id as &$v) {
            $res = LifeFarmLand::where(['land_id'=>$v])->first();
            if(empty($res) || $res === false)
            {
                throw new \Exception('地块id'.$v.'信息不存在');
            }
            $params['order_price']+=$res['land_price'];
            $log = LifeFarmLandOrderDetail::where(['detail_goods_id'=>$v,'detail_pay_status'=>2])->where('detail_expire_time','>',time())->first();
            //var_dump($log);
            if($log){
                throw new \Exception($res['land_name'].'已售出');
            }
        }

        LifeFarmLandOrder::affairBegin();
        try {
            $params['order_user_id'] = $user_id;
            //$params['order_price'] = $res['land_price'];
            $params['order_dno'] = "YMXT".rand(99999,999999);
            $params['order_create_time'] = time();
            $result =  LifeFarmLandOrder::insertGetId($params);
            if (!empty($result)){
                $msg = '用户' . $user_id . '在：' . date("Y-m-d H:i:s", time()) . '下单-编号: ' . $result;
                plog('life-farm-land-order', '悦活-生态农场-一亩心田-下单', $msg);
                //写入订单详情
                foreach ($land_id as $value){
                    $land = LifeFarmLand::where(['land_id'=>$value])->first();
                    $item = [
                      'detail_order_id'     =>$result,
                      'detail_user_id'      =>$user_id,
                      'detail_goods_id'     =>$value,
                      'detail_goods_name'   =>$land['land_name'],
                      'detail_goods_price'  =>$land['land_price'],
                      'detail_create_time'  =>time(),
                      'detail_expire_time'  =>time()+365*24*60*60,//一年有效期
                    ];
                    LifeFarmLandOrderDetail::insert($item);
                }
                LifeFarmLandOrder::affairCommit();
                return $result;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            LifeFarmLandOrder::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)
    {
        LifeFarmLandOrder::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 = LifeFarmLandOrder::where($where)->update($data);
            if ($result !== false){
                LifeFarmLandOrder::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            LifeFarmLandOrder::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }
}
