<?php

namespace app\api\logic\order;

use app\common\enum\OrderEnum;
use app\common\enum\PayEnum;
use app\common\enum\UserShipEnum;
use app\common\enum\YesNoEnum;
use app\common\logic\BaseLogic;
use app\common\logic\CapitalFlowLogic;
use app\common\logic\StoreFinanceFlowLogic;
use app\common\model\dict\DictData;
use app\common\model\dict\DictType;
use app\common\model\order\Cart;
use app\common\model\store_branch_product\StoreBranchProduct;
use app\common\model\store_order\StoreOrder;
use app\common\model\store_order_cart_info\StoreOrderCartInfo;
use app\common\model\store_product\StoreProduct;
use app\common\model\store_product_attr_value\StoreProductAttrValue;
use app\common\model\store_product_unit\StoreProductUnit;
use app\common\model\system_store\SystemStore;
use app\common\model\system_store\SystemStoreStaff;
use app\common\model\user\User;
use app\common\model\user\UserAddress;
use app\common\model\user\UserShip;
use app\common\model\user_spread_log\UserSpreadLog;
use Picqer\Barcode\BarcodeGeneratorJPG;
use Picqer\Barcode\BarcodeGeneratorPNG;
use support\exception\BusinessException;
use support\Log;
use taoser\exception\ValidateException;
use think\db\Raw;
use think\Exception;
use think\facade\Db;
use Workerman\Events\Select;
use Yansongda\Pay\Event\PayEnd;

/**
 * 订单逻辑
 * Class OrderLogic
 * @package app\api\logic\order
 */
class OrderLogic extends BaseLogic
{
    public static $total_price;
    public static $pay_price;
    public static $cost;
    public static $profit;
    public static $activity_price;

    /**
     * @notes 获取购物车商品信息
     * @param $params
     * @return array|bool
     */
    static public function cartIdByOrderInfo($cartId, $addressId, $user = null, $params = [])
    {

        $where = ['is_pay' => 0];
        $cart_select = Cart::whereIn('id', $cartId)->where($where)->field('id,product_id,cart_num')->select()->toArray();
        if (empty($cart_select)) {
            self::setError('购物车为空');
            return false;
        }
        try {
            self::$total_price = 0;
            self::$pay_price = 0;
            self::$cost = 0; //成本
            self::$profit = 0; //利润
            self::$activity_price = 0; //活动减少
            /** 计算价格 */
            
            foreach ($cart_select as $k => $v) {
                $find = StoreBranchProduct::where(['product_id' => $v['product_id'], 'store_id' => $params['store_id']])->field('id branch_product_id,store_name,image,unit,price,vip_price,cost,product_id')->find();
                if (!$find) {
                    continue;
                }
                unset($cart_select[$k]['id']);

                $cart_select[$k]['price'] = $find['price'];
                $cart_select[$k]['total_price'] = bcmul($v['cart_num'], $find['price'], 2); //订单总价
                $cart_select[$k]['deduction_price'] =self::$activity_price;//抵扣金额

                $onePrice = bcsub($find['price'], $find['cost'], 2);
                
                //利润
                $cart_select[$k]['profit'] = bcmul($v['cart_num'], $onePrice, 2); //利润
                $cart_select[$k]['cost'] = bcmul($v['cart_num'], $find['cost'], 2) ?? 0; //成本
                $cart_select[$k]['pay_price'] = bcmul($v['cart_num'], $find['price'], 2); //订单支付金额
                $cart_select[$k]['product_id'] = $find['product_id'];
                $cart_select[$k]['old_cart_id'] = $v['id'];
                $cart_select[$k]['cart_num'] = $v['cart_num'];
                $cart_select[$k]['verify_code'] = $params['verify_code'] ?? '';
                
                $cartInfo = $cart_select[$k];
                $cartInfo['name'] = $find['store_name'];
                $cartInfo['image'] = $find['image'];

                $cartInfo['vip_price'] = 0; //$cart_select[$k]['total'] - $vipPrice ?? 0;
                $cart_select[$k]['cart_info'] = json_encode($cartInfo);
                $cart_select[$k]['branch_product_id'] = $find['branch_product_id'];
                //理论上每笔都是拆分了
                $cart_select[$k]['name'] = $find['store_name'];
                $cart_select[$k]['imgs'] = $find['image'];
                $cart_select[$k]['store_id'] = $params['store_id'] ?? 0;
                $cart_select[$k]['unit_name'] = StoreProductUnit::where(['id' => $find['unit']])->value('name');
                self::$total_price = bcadd(self::$total_price, $cart_select[$k]['total_price'], 2);
                self::$pay_price = bcadd(self::$pay_price, $cart_select[$k]['pay_price'], 2);
                self::$cost = bcadd(self::$cost, $cart_select[$k]['cost'], 2);
                self::$profit = bcadd(self::$profit, $cart_select[$k]['profit'], 2);
            }
            $pay_price =self::$pay_price;// bcsub(self::$pay_price, self::$activity_price, 2); //减去活动优惠金额
            $vipPrice = 0;
            //成本价  收益
            $order = [
                'create_time' => time(),
                'order_id' => $params['order_id'] ?? getNewOrderId('PF'),
                'total_price' => self::$total_price, //总价
                'cost' => self::$cost, //成本价1-
                'profit' => self::$profit, //利润
                'pay_price' => $pay_price, //后期可能有降价抵扣
                'vip_price' => $vipPrice,
                'total_num' => count($cart_select), //总数
                'pay_type' => $params['pay_type'] ?? 0,
                'reservation_time' => $params['reservation_time'] ?? null,
                'cart_id' => implode(',', $cartId),
                'store_id' => $params['store_id'] ?? 0,
                'shipping_type' => $params['shipping_type'] ?? 2, //配送方式 1=快递 ,2=门店自提
                'activity' => '减免',
                'activity_price' => self::$activity_price,
                'activities' =>0,
                'deduction_price' => self::$activity_price

            ];
            $order['default_delivery'] = 0;
            if ($params['store_id']) {
                $order['default_delivery'] = SystemStore::where('id', $params['store_id'])->value('is_send');
            }
        } catch (\Exception $e) {
            self::setError($e->getMessage());
            return false;
        }
        return ['order' => $order, 'cart_list' => $cart_select];
    }

    /**
     * 创建新订单
     * @return Object|bool|array
     */
    static public function createOrder($cartId, $addressId, $user = null, $params = [])
    {
        $order_id = getNewOrderId('PF');
        $code = rand(1,10).'-'.substr($order_id, -5);
        $verify_code = createCode($code);
        $params['order_id'] = $order_id;
        $params['verify_code'] =$verify_code;
        $orderInfo = self::cartIdByOrderInfo($cartId, $addressId, $user, $params);
        if (!$orderInfo) {
            return false;
        }
        $_order = $orderInfo['order'];
        $_order['uid'] = request()->userId;
        $user = User::where('id', \request()->userId)->find();
        $_order['is_vip'] = $user['user_ship'];
        $_order['spread_uid'] = UserSpreadLog::where('uid',request()->userId)->value('old_spread_uid')??0;//预留分享关系
        $_order['real_name'] = $user['real_name'];
        $_order['mobile'] = $user['mobile'];
        $_order['pay_type'] = $orderInfo['order']['pay_type'];
        $_order['verify_code'] = $verify_code;
        $_order['reservation_time'] = null;
        if (isset($params['reservation_time']) && $params['reservation_time']) {
            $_order['reservation_time'] = $params['reservation_time'];
            $_order['reservation'] = YesNoEnum::YES;
        }
        if ($addressId > 0) {
            $address = UserAddress::where(['id' => $addressId, 'uid' => Request()->userId])->find();
            if ($address) {
                $_order['real_name'] = $address['real_name'];
                $_order['user_phone'] = $address['phone'];
                $_order['user_address'] = $address['detail'];
            }
        }
        if ($params['shipping_type'] == 2) {
            $_order['status'] = 1;
        }
        //生成核销码
        $generator = new BarcodeGeneratorPNG();
        $barcode = $generator->getBarcode($verify_code, $generator::TYPE_CODE_128);
        $findPath = '/image/barcode/'.time().'.png';
        $savePath = 'public'.$findPath;
        file_put_contents($savePath, $barcode);
        $_order['verify_img'] = $findPath;
        Db::startTrans();
        try {
            $order = StoreOrder::create($_order);
            $goods_list = $orderInfo['cart_list'];
            $updateData = [];
            foreach ($goods_list as $k => $v) {
                $goods_list[$k]['oid'] = $order->id;
                $goods_list[$k]['uid'] = request()->userId;
                $goods_list[$k]['cart_id'] = implode(',', $cartId);
                $goods_list[$k]['delivery_id'] = $params['store_id']; //商家id
                $StoreBranchProduct = StoreBranchProduct::where('id',$v['branch_product_id'])->find();
                $updateData[] = [
                    'id' => $v['branch_product_id'],
                    'stock' => $StoreBranchProduct['stock']-$v['cart_num'],
                    'sales' => ['inc', $v['cart_num']]
                ];
                if($StoreBranchProduct['stock']-$v['cart_num']<=0){
                    Db::name('store_product_cate')->where(['cate_id'=>$StoreBranchProduct['cate_id'],'store_id'=>$params['store_id']])->update(['count'=>0]);
                }
            }
            (new StoreOrderCartInfo())->saveAll($goods_list);
            (new StoreBranchProduct())->saveAll($updateData);
            $where = ['is_pay' => 0];
            Cart::whereIn('id', $cartId)->where($where)->update(['is_pay' => 1]);
            Db::commit();
            return $order;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 获取订单号
     * @param $type
     * @return string
     * @author likeadmin
     * @date 2021/7/28 17:05
     */
    static public function getNewOrderId($type)
    {
        list($msec, $sec) = explode(' ', microtime());
        $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
        $orderId = $type . $msectime . mt_rand(10000, max(intval($msec * 10000) + 10000, 98369));
        return $orderId;
    }


    /**
     * @notes 订单支付成功
     * @param $order 订单
     * @param $CallbackData 回调数据
     * @date 2021/7/8 00:40
     */
    static function paySuccess($order, $CallbackData = [])
    {
        Db::startTrans();
        try {
            $order->money = $CallbackData['money'];
            $order->paid = 1;
            $order->save();
            Log::info('支付成功');
            // 提交事务
            Db::commit();
            return true;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            Log::error('支付失败' . $e->getMessage() . '。like:' . $e->getLine());
            self::setError('支付失败' . $e->getMessage());
            return false;
        }
    }

    /**
     * 获取用户常用购买记录
     */
    public static function frequentlyPurchase($params)
    {
        try {
            $goods_id = StoreOrderCartInfo::where('uid', Request()->userId)->page($params['page_no'])->limit(50)->column('product_id');
            if (!$goods_id) {
                return [];
            }
            $goods_arr = array_unique($goods_id);
            $select = StoreBranchProduct::where('product_id', 'in', $goods_arr)->with('unitName')->field('id,store_name,price,image,unit')->select();
            return $select->toArray();
        } catch (\Exception $e) {
            self::setError($e->getMessage());
            return false;
        }
    }

    public static function purchaseAgain($order_id)
    {
        $arr = StoreOrderCartInfo::where('oid', $order_id)->field('product_id,cart_num,staff_id')->select();
        $data = [];
        foreach ($arr as $k => $v) {
            $data[$k]['product_id'] = $v['product_id'];
            $unique = StoreProductAttrValue::where('product_id', $v['product_id'])->value('v');
            $data[$k]['product_attr_unique'] = $unique;
            $data[$k]['cart_num'] = $v['cart_num'];
            $data[$k]['type'] = '';
            $data[$k]['uid'] = Request()->userId;
            $store_id = StoreProduct::where('id', $v['product_id'])->value('store_id');
            $data[$k]['store_id'] = $store_id;
            $data[$k]['staff_id'] = $v['staff_id'];
            $data[$k]['combination_id'] = 0;
            $data[$k]['seckill_id'] = 0;
            $data[$k]['bargain_id'] = 0;
            $data[$k]['discount_id'] = 0;
            $data[$k]['status'] = 1;
            $data[$k]['staff_id'] = 0;
            $data[$k]['is_new'] = 0;
            $data[$k]['is_del'] = 0;
            $data[$k]['is_pay'] = 0;
        }
        if ($data) {
            (new Cart())->saveAll($data);
            return true;
        }
        return false;
    }


    public static function detail($params,$url=''): array
    {
        $find = StoreOrder::where($params)->findOrEmpty()->toArray();
        if ($find) {

            $find['goods_list'] = StoreOrderCartInfo::where('oid', $find['id'])
                ->field('product_id,cart_num nums,store_id')->select()->each(function ($item) use ($find) {
                    $find = StoreBranchProduct::where('product_id', $item['product_id'])->where('store_id', $find['store_id'])->find();
                    $item['store_name'] = $find['store_name'];
                    $item['image'] = $find['image'];
                    $item['price'] = $find['price'];
                    $item['unit_name'] = StoreProductUnit::where('id', $find['unit'])->value('name') ?? '';
                    $item['msg'] = '预计48小时发货';
                    return $item;
                });

            $store = SystemStore::where('id', $find['store_id'])->field('id,name,phone,address,detailed_address')->find();
            $find['store_info'] = $store;

            if($find['verify_img']){
                $find['verify_img'] = $url.$find['verify_img'];
            }
        }
        return $find;
    }

    //取消售后
    public static function cancelSell($where)
    {
        return StoreOrder::where($where)->update(
            [
                'refund_status' => OrderEnum::CANCEL_SALE,
                'status' => OrderEnum::CANCEL_ORDER,
                'refund_cancle_time' => date('Y-m-d H:i:s', time())
            ]
        );
    }


    //核销

    /**
     * @param $params
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author: codeliu
     * @Time: 2024/6/3  22:42
     */
    public static function writeOff($params): bool
    {
        $data = StoreOrder::with('store')->where([
            'verify_code' => $params['verify_code']
        ])->find();
        if (empty($data)) {
            return false;
        }
        Db::startTrans();
        try {
            StoreOrder::update([
                'verify_code'=>$params['verify_code'].'-1',
                'status' => OrderEnum::RECEIVED_GOODS,
                'is_writeoff' => OrderEnum::IS_OK,
                'update_time' => time(),
            ], ['id' => $data['id']]);
            (new StoreOrderCartInfo())->update([
                'verify_code'=>$params['verify_code'].'-1',
                'writeoff_time' => time(),
                'is_writeoff' => YesNoEnum::YES,
                'update_time' => time(),
            ], ['oid' => $data['id']]);
            $financeFlow = (new StoreFinanceFlowLogic)->getStoreOrder($data['id'], $data['store_id']);
            if (!empty($financeFlow)) {
                $capitalFlowLogic = new CapitalFlowLogic($data->store, 'store');
                $capitalFlowLogic->storeIncome('store_order_income', 'order', $data['id'], $financeFlow['number']);
            }
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    public static function write_count($info, $params)
    {
        $store_id = SystemStoreStaff::where('phone', $info['mobile'])->value('store_id');
        if (empty($store_id)) {
            throw new \Exception('该用户未绑定店铺请查看');
        }
        $query = StoreOrderCartInfo::alias('o')
            ->leftJoin('store_branch_product p', 'p.id = o.product_id')
            ->leftJoin('store_order s', 's.id = o.oid')
            ->field('o.oid,p.store_name,s.order_id')
            ->where('o.store_id', $store_id);

        if (isset($params['name']) && $params['name']) {
            if ($params['name'] && preg_match('/[\x{4e00}-\x{9fff}]+/u', $params['name']) == 1) {
                $query->where('p.store_name', 'like', '%' . $params['name'] . '%');
            } else {
                $query->where('s.order_id', $params['name']);
            }
        }
        $product = $query->select();
        if (empty($product)) {
            return [
                'no_send' => 0,
                'send' => 0
            ];
        }
        $oids = array_column($product->toArray(), 'oid');
        $uniqueOids = array_unique($oids);
        $no_send = StoreOrder::whereIn('id', $uniqueOids)
            ->where('status', 1)->count();
        $send = StoreOrder::whereIn('id', $uniqueOids)
            ->where('status', 2)->count();
        return [
            'no_send' => $no_send,
            'send' => $send
        ];
    }

    public static function getOne($code)
    {
        return StoreOrder::with(['store'])->where('verify_code', $code)
            ->select()->each(function ($item) {
                $item['goods_list'] = StoreOrderCartInfo::where('oid', $item['id'])->with('goodsName')->field('product_id,cart_num,verify_code,is_writeoff,writeoff_time')->limit(3)->select();
                $item['goods_count'] = count(explode(',', $item['cart_id']));
                return $item; //返回处理后的数据。
            })
            ->toArray();
    }

    public static function write_list($info, $status, $params)
    {

        $store_id = SystemStoreStaff::where('phone', $info['mobile'])->value('store_id');
        if (empty($store_id)) {
            throw new \Exception('该用户未绑定店铺请查看');
        }

        //先查商品相似
        $query = StoreOrderCartInfo::alias('o')
            ->leftJoin('store_branch_product p', 'p.id = o.product_id')
            ->leftJoin('store_order s', 's.id = o.oid')
            ->field('o.oid,p.store_name,s.order_id')
            ->where('o.store_id', $store_id);

        if (isset($params['name']) && $params['name']) {
            if ($params['name'] && preg_match('/[\x{4e00}-\x{9fff}]+/u', $params['name']) == 1) {
                $query->where('p.store_name', 'like', '%' . $params['name'] . '%');
            } else {
                $query->where('s.order_id', $params['name']);
            }
        }
        $product = $query->select();
        if (empty($product)) {
            return [
                'list' => [],
                'count' => 0
            ];
        }
        $oids = array_column($product->toArray(), 'oid');
        $uniqueOids = array_unique($oids);
        $query = StoreOrder::with(['store'])
            ->whereIn('id', $uniqueOids)
            ->where('status', $status);
        $count = $query->count();
        $list = $query
            ->page($params['page_no'], $params['page_size'])
            ->order(['id' => 'desc'])
            ->select()
            ->each(function ($item) {
                $item['goods_list'] = StoreOrderCartInfo::where('oid', $item['id'])->with('goodsName')->field('product_id,cart_num,verify_code,is_writeoff,writeoff_time')->limit(3)->select();
                $item['goods_count'] = count(explode(',', $item['cart_id']));
            })
            ->toArray();
        return [
            'list' => $list,
            'count' => $count
        ];
    }

    public static function dealRefund($uid, $params)
    {
        //todo 单子不是完成的不允许退款
        //单笔不拆单子直接修改状态
        $order = StoreOrder::where('id', $params['id'])->withTrashed()->findOrEmpty();
        $params['refund_num'] = 1; //todo 拿实际数量
        if (count($params['old_cart_id']) == 1) {
            $order->refund_status = OrderEnum::REFUND_STATUS_YES;
            $order->status = OrderEnum::ALREADY_REFUND;
            $order->refund_reason_wap_explain = $params['refund_message'] ?? '';
            $order->mark = $params['mark'] ?? '';
            $order->refund_num = $params['refund_num'] ?? 1;
            $refund_price_cart = StoreOrderCartInfo::where('oid', $params['id'])
                ->field('id,oid,cart_info')
                ->find()->toArray();
            $vipPrice = $order['vip_price'];
            $price = $refund_price_cart['cart_info']['price'] * $params['refund_num'];
            $onePrice = 0;
            bcscale(2);
            if ($vipPrice) {
                //每单的vip价格
                $onePrice = bcdiv($vipPrice, $refund_price_cart['cart_info']['cart_num']);
            }
            if ($price > $onePrice) {
                $price = bcsub($price, $onePrice);
            }
            $order->refund_price = $price;
            $order->refund_type = $params['refund_type'];
            $order->save();
        } else {
            // 多单的情况 拆主订单为新的2单 修改新的2单的核销码 修改cart_info的核销码 和订单id 退款直接退一单的钱
            $order->delete_time = time();
            $order->save();
            Db::startTrans();
            try {
                $order = $order->toArray();
                $cart_info = StoreOrderCartInfo::where('oid', $params['id'])
                    ->whereNotIn('old_cart_id', $params['old_cart_id'])
                    ->select()->toArray();
                if ($cart_info) {
                    $leftOrder = self::dealCreateLeftOrder($order, $cart_info, $params);
                    self::dealChangeCartInfo($leftOrder);
                }
                $refundOrder = self::dealCreateRefundOrder($order, $params);
                self::dealChangeCartInfo($refundOrder);
                //                d($leftOrder,$refundOrder);
                Db::commit();
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                throw new BusinessException($e->getMessage());
            }
        }

        return 1;
    }


    public static function dealChangeCartInfo($leftOrder)
    {
        $code = $leftOrder->verify_code;
        $new_oid = $leftOrder->id;
        $old_id = $leftOrder->pid;
        $car_id = explode(',', $leftOrder->cart_id);
        return StoreOrderCartInfo::where('oid', $old_id)
            ->whereIn('old_cart_id', $car_id)
            ->update([
                'oid' => $new_oid,
                'verify_code' => $code
            ]);
    }


    public static function dealCreateLeftOrder($order, $cart_info, $params)
    {
        //查出不在这个退货中的数据
        $order['order_id'] = $order['order_id'] . '-2';
        $order['pid'] = $order['id'];
        unset($order['id']);
        $allOldCartIds = [];
        $totalTotal = 0;
        $totalPrice = 0;
        $totalVipPrice = 0;
        foreach ($cart_info as $value) {
            if (isset($value['old_cart_id'])) {
                $allOldCartIds[] = $value['old_cart_id'];
            }
            if (isset($value['cart_info'])) {
                if (isset($value['cart_info']['total'])) {
                    $totalTotal += floatval($value['cart_info']['total']);
                }
                if (isset($value['cart_info']['price'])) {
                    $totalPrice += floatval($value['cart_info']['price']);
                }
                if (isset($value['cart_info']['vip_price'])) {
                    $totalVipPrice += floatval($value['cart_info']['vip_price']);
                }
            }
        }
        $order['cart_id'] = implode(',', $allOldCartIds);
        $order['total_num'] = count($cart_info);
        $order['refund_type'] = $params['refund_type'];
        $order['total_price'] = number_format($totalTotal, 2);
        $order['pay_price'] = number_format($totalPrice, 2);
        $order['vip_price'] = number_format($totalVipPrice, 2);
        $order['verify_code'] = verificationCode();
        $order['create_time'] = time();
        $order['update_time'] = null;
        $order['delete_time'] = null;
        return StoreOrder::create($order);
    }


    public static function dealCreateRefundOrder($order, $params)
    {
        $order['order_id'] = $order['order_id'] . '-1';
        $order['pid'] = $order['id'];
        unset($order['id']);
        $order['cart_id'] = implode(',', $params['old_cart_id']);

        $order['refund_status'] = OrderEnum::REFUND_STATUS_YES;
        $order['status'] = OrderEnum::ALREADY_REFUND;
        $order['refund_num'] = $params['refund_num']; //按数量整单退剩余的
        $order['refund_reason_wap_explain'] = $params['refund_message'] ?? '';
        $order['mark'] = $params['mark'] ?? '';
        $order['total_num'] = count($params['old_cart_id']);
        $refund_price_cart = StoreOrderCartInfo::whereIn('old_cart_id', $params['old_cart_id'])
            ->field('id,oid,cart_info')
            ->select()->toArray();
        $totalTotals = array_column(array_column($refund_price_cart, 'cart_info'), 'total');
        $totalTotal = array_reduce($totalTotals, function ($carry, $item) {
            return $carry + $item;
        }, 0);
        $totalPrices = array_column(array_column($refund_price_cart, 'cart_info'), 'price');
        $totalPrice = array_reduce($totalPrices, function ($carry, $item) {
            return $carry + $item;
        }, 0);

        $totalVipPrices = array_column(array_column($refund_price_cart, 'cart_info'), 'vip_price');
        $totalVipPrices = array_reduce($totalVipPrices, function ($carry, $item) {
            return $carry + $item;
        }, 0);

        $order['total_price'] = number_format($totalTotal, 2);
        $order['pay_price'] = number_format($totalPrice, 2);
        $order['vip_price'] = number_format($totalVipPrices, 2);
        $order['refund_price'] = number_format($totalPrice, 2);
        $order['verify_code'] = verificationCode();
        $order['refund_reason_time'] = time();
        $order['create_time'] = time() + 1;
        $order['update_time'] = null;
        $order['delete_time'] = null;
        return StoreOrder::create($order);
    }


    public static function dealVip($pay_price)
    {
        $check = DictType::where('type', 'activities')->find();
        if (isset($check) && $check['status'] == 1) {
            //                $discountRate = '0.99';//首单逻辑
            $discountRate = $check['remark'];
            $discountRate = bcdiv($discountRate, '100', 2);
            $pay_price = bcdiv(bcmul($pay_price, $discountRate, 4), '1', 2);
        } else {
            $userVip = User::where('id', \request()->userId)->value('user_ship');
            if ($userVip) {
                switch ($userVip) {
                    case UserShipEnum::VIP1:
                        $discountRate = UserShip::where('id', UserShipEnum::VIP1)->value('discount');
                        break;
                    case UserShipEnum::VIP2:
                        $discountRate = UserShip::where('id', UserShipEnum::VIP2)->value('discount');
                        break;
                    case UserShipEnum::VIP3:
                        $discountRate = UserShip::where('id', UserShipEnum::VIP3)->value('discount');
                        break;
                    case UserShipEnum::VIP4:
                        $discountRate = UserShip::where('id', UserShipEnum::VIP4)->value('discount');
                        break;
                    case UserShipEnum::VIP5:
                        $discountRate = UserShip::where('id', UserShipEnum::VIP5)->value('discount');
                        break;
                    default:
                        $discountRate = 1;
                }
                $discountRate = bcdiv($discountRate, '100', 2);
                $pay_price = bcdiv(bcmul($pay_price, $discountRate, 4), '1', 2);
            }
        }
        return $pay_price;
    }
}