WIN-2308041133\Administrator 2 ماه پیش
والد
کامیت
3202fbe952
1فایلهای تغییر یافته به همراه195 افزوده شده و 190 حذف شده
  1. 195 190
      app/services/order/StoreOrderCreateServices.php

+ 195 - 190
app/services/order/StoreOrderCreateServices.php

@@ -143,219 +143,224 @@ class StoreOrderCreateServices extends BaseServices
      * @email 442384644@qq.com
      * @email 442384644@qq.com
      * @date 2023/03/01
      * @date 2023/03/01
      */
      */
-    public function createOrder($uid, $key, $userInfo, $addressId, $payType, $useIntegral = false, $couponId = 0, $mark = '', $combinationId = 0, $pinkId = 0, $seckillId = 0, $bargainId = 0, $shippingType = 1, $real_name = '', $phone = '', $storeId = 0, $news = false, $advanceId = 0, $customForm = [], $invoice_id = 0, $is_gift = 0, $gift_mark = '',$lb_spread_uid=0)
+    public function createOrder($uid, $key, $userInfo, $addressId, $payType, $useIntegral = false, $couponId = 0, $mark = '', $combinationId = 0, $pinkId = 0, $seckillId = 0, $bargainId = 0, $shippingType = 1, $real_name = '', $phone = '', $storeId = 0, $news = false, $advanceId = 0, $customForm = [], $invoice_id = 0, $is_gift = 0, $gift_mark = '', $lb_spread_uid = 0)
     {
     {
         try {
         try {
 
 
-        }
-        /** @var StoreOrderServices $orderService */
-        $storeOrderServices = app()->make(StoreOrderServices::class);
-        $bargainServices = app()->make(StoreBargainServices::class);
-        $cartGroup = $storeOrderServices->getCacheOrderInfo($uid, $key);
-        if (!$cartGroup) {
-            throw new ApiException(410208);
-        }
-        //下单前砍价验证
-        if ($bargainId) {
-            $bargainServices->checkBargainUser((int)$bargainId, $uid);
-        }
 
 
-        if ($pinkId) {
-            $pinkId = (int)$pinkId;
-            /** @var StorePinkServices $pinkServices */
-            $pinkServices = app()->make(StorePinkServices::class);
-            if ($pinkServices->isPink($pinkId, $uid))
-                throw new ApiStatusException('ORDER_EXIST', 410210, ['orderId' => $storeOrderServices->getStoreIdPink($pinkId, $uid)]);
-            if ($storeOrderServices->getIsOrderPink($pinkId, $uid))
-                throw new ApiStatusException('ORDER_EXIST', 410211, ['orderId' => $storeOrderServices->getStoreIdPink($pinkId, $uid)]);
-        }
-        $virtual_type = $cartGroup['cartInfo'][0]['productInfo']['virtual_type'] ?? 0;
+            /** @var StoreOrderServices $orderService */
+            $storeOrderServices = app()->make(StoreOrderServices::class);
+            $bargainServices = app()->make(StoreBargainServices::class);
+            $cartGroup = $storeOrderServices->getCacheOrderInfo($uid, $key);
+            if (!$cartGroup) {
+                throw new ApiException(410208);
+            }
+            //下单前砍价验证
+            if ($bargainId) {
+                $bargainServices->checkBargainUser((int)$bargainId, $uid);
+            }
 
 
-        //下单前发票验证
-        if ($invoice_id) {
-            app()->make(UserInvoiceServices::class)->checkInvoice((int)$invoice_id, $uid);
-        }
+            if ($pinkId) {
+                $pinkId = (int)$pinkId;
+                /** @var StorePinkServices $pinkServices */
+                $pinkServices = app()->make(StorePinkServices::class);
+                if ($pinkServices->isPink($pinkId, $uid))
+                    throw new ApiStatusException('ORDER_EXIST', 410210, ['orderId' => $storeOrderServices->getStoreIdPink($pinkId, $uid)]);
+                if ($storeOrderServices->getIsOrderPink($pinkId, $uid))
+                    throw new ApiStatusException('ORDER_EXIST', 410211, ['orderId' => $storeOrderServices->getStoreIdPink($pinkId, $uid)]);
+            }
+            $virtual_type = $cartGroup['cartInfo'][0]['productInfo']['virtual_type'] ?? 0;
 
 
-        /** @var StoreOrderComputedServices $computedServices */
-        $computedServices = app()->make(StoreOrderComputedServices::class);
-        $priceData = $computedServices->computedOrder($uid, $userInfo, $cartGroup, $addressId, $payType, $useIntegral, $couponId, true, $shippingType, $is_gift);
-        /** @var WechatUserServices $wechatServices */
-        $wechatServices = app()->make(WechatUserServices::class);
-        /** @var UserAddressServices $addressServices */
-        $addressServices = app()->make(UserAddressServices::class);
-        if ($is_gift == 0) {
-            if ($shippingType == 1 && $virtual_type == 0) {
-                if (!$addressId) {
-                    throw new ApiException(410045);
+            //下单前发票验证
+            if ($invoice_id) {
+                app()->make(UserInvoiceServices::class)->checkInvoice((int)$invoice_id, $uid);
+            }
+
+            /** @var StoreOrderComputedServices $computedServices */
+            $computedServices = app()->make(StoreOrderComputedServices::class);
+            $priceData = $computedServices->computedOrder($uid, $userInfo, $cartGroup, $addressId, $payType, $useIntegral, $couponId, true, $shippingType, $is_gift);
+            /** @var WechatUserServices $wechatServices */
+            $wechatServices = app()->make(WechatUserServices::class);
+            /** @var UserAddressServices $addressServices */
+            $addressServices = app()->make(UserAddressServices::class);
+            if ($is_gift == 0) {
+                if ($shippingType == 1 && $virtual_type == 0) {
+                    if (!$addressId) {
+                        throw new ApiException(410045);
+                    }
+                    if (!$addressInfo = $addressServices->getOne(['uid' => $uid, 'id' => $addressId, 'is_del' => 0]))
+                        throw new ApiException(410046);
+                    $addressInfo = $addressInfo->toArray();
+                } else {
+                    if ((!$real_name || !$phone) && $virtual_type == 0) {
+                        throw new ApiException(410245);
+                    }
+                    $addressInfo['real_name'] = $real_name;
+                    $addressInfo['phone'] = $phone;
+                    $addressInfo['province'] = '';
+                    $addressInfo['city'] = '';
+                    $addressInfo['district'] = '';
+                    $addressInfo['detail'] = '';
                 }
                 }
-                if (!$addressInfo = $addressServices->getOne(['uid' => $uid, 'id' => $addressId, 'is_del' => 0]))
-                    throw new ApiException(410046);
-                $addressInfo = $addressInfo->toArray();
             } else {
             } else {
-                if ((!$real_name || !$phone) && $virtual_type == 0) {
-                    throw new ApiException(410245);
-                }
-                $addressInfo['real_name'] = $real_name;
-                $addressInfo['phone'] = $phone;
+                $addressInfo['real_name'] = '';
+                $addressInfo['phone'] = '';
                 $addressInfo['province'] = '';
                 $addressInfo['province'] = '';
                 $addressInfo['city'] = '';
                 $addressInfo['city'] = '';
                 $addressInfo['district'] = '';
                 $addressInfo['district'] = '';
                 $addressInfo['detail'] = '';
                 $addressInfo['detail'] = '';
             }
             }
-        } else {
-            $addressInfo['real_name'] = '';
-            $addressInfo['phone'] = '';
-            $addressInfo['province'] = '';
-            $addressInfo['city'] = '';
-            $addressInfo['district'] = '';
-            $addressInfo['detail'] = '';
-        }
 
 
-        $cartInfo = $cartGroup['cartInfo'];
-        $priceGroup = $cartGroup['priceGroup'];
-        $cartIds = [];
-        $totalNum = 0;
-        $gainIntegral = 0;
-        $is_lb = 0;
-        foreach ($cartInfo as $cart) {
-            $cartIds[] = $cart['id'];
-            $totalNum += $cart['cart_num'];
-            if (!$seckillId) $seckillId = $cart['seckill_id'];
-            if (!$bargainId) $bargainId = $cart['bargain_id'];
-            if (!$combinationId) $combinationId = $cart['combination_id'];
-            if (!$advanceId) $advanceId = $cart['advance_id'];
-            $cartInfoGainIntegral = isset($cart['productInfo']['give_integral']) ? bcmul((string)$cart['cart_num'], (string)$cart['productInfo']['give_integral'], 0) : 0;
-            $gainIntegral = bcadd((string)$gainIntegral, (string)$cartInfoGainIntegral, 0);
+            $cartInfo = $cartGroup['cartInfo'];
+            $priceGroup = $cartGroup['priceGroup'];
+            $cartIds = [];
+            $totalNum = 0;
+            $gainIntegral = 0;
+            $is_lb = 0;
+            foreach ($cartInfo as $cart) {
+                $cartIds[] = $cart['id'];
+                $totalNum += $cart['cart_num'];
+                if (!$seckillId) $seckillId = $cart['seckill_id'];
+                if (!$bargainId) $bargainId = $cart['bargain_id'];
+                if (!$combinationId) $combinationId = $cart['combination_id'];
+                if (!$advanceId) $advanceId = $cart['advance_id'];
+                $cartInfoGainIntegral = isset($cart['productInfo']['give_integral']) ? bcmul((string)$cart['cart_num'], (string)$cart['productInfo']['give_integral'], 0) : 0;
+                $gainIntegral = bcadd((string)$gainIntegral, (string)$cartInfoGainIntegral, 0);
 //            if ($cart['productAttr']['product']['is_lb']==1){
 //            if ($cart['productAttr']['product']['is_lb']==1){
 //                $is_lb = 1;
 //                $is_lb = 1;
 //            }
 //            }
-            @file_put_contents('quanju.txt', json_encode($cart)."-购物车\r\n", 8);
+                @file_put_contents('quanju.txt', json_encode($cart) . "-购物车\r\n", 8);
 
 
-        }
-        if (count($cartInfo) == 1 && isset($cartInfo[0]['productInfo']['presale']) && $cartInfo[0]['productInfo']['presale'] == 1) {
-            $advance_id = $cartInfo[0]['product_id'];
-        } else {
-            $advance_id = 0;
-        }
-        $deduction = $seckillId || $bargainId || $combinationId;
-        if ($deduction) {
-            $couponId = 0;
-            $gainIntegral = 0;
-            $useIntegral = false;
-        }
-        //$shipping_type = 1 快递发货 $shipping_type = 2 门店自提
-        $storeSelfMention = sys_config('store_self_mention') ?? 0;
-        if (!$storeSelfMention) $shippingType = 1;
-        if ($is_gift == 1) $shippingType = 0;
-
-        $orderInfo = [
-            'uid' => $uid,
-            'order_id' => $this->getNewOrderId('cp'),
-            'real_name' => $addressInfo['real_name'],
-            'user_phone' => $addressInfo['phone'],
-            'user_address' => $addressInfo['province'] . ' ' . $addressInfo['city'] . ' ' . $addressInfo['district'] . ' ' . $addressInfo['detail'],
-            'cart_id' => $cartIds,
-            'total_num' => $totalNum,
-            'total_price' => $priceGroup['totalPrice'],
-            'total_postage' => $shippingType == 1 ? $priceGroup['storePostage'] : 0,
-            'coupon_id' => $couponId,
-            'coupon_price' => $priceData['coupon_price'],
-            'pay_price' => $priceData['pay_price'],
-            'pay_postage' => $priceData['pay_postage'],
-            'deduction_price' => $priceData['deduction_price'],
-            'gift_price' => $priceData['gift_price'],
-            'paid' => 0,
-            'pay_type' => $payType,
-            'use_integral' => $priceData['usedIntegral'],
-            'gain_integral' => $gainIntegral,
-            'mark' => htmlspecialchars($mark),
-            'combination_id' => $combinationId,
-            'pink_id' => $pinkId,
-            'seckill_id' => $seckillId,
-            'bargain_id' => $bargainId,
-            'advance_id' => $advance_id,
-            'cost' => $priceGroup['costPrice'],
-            'add_time' => time(),
-            'unique' => $key,
-            'shipping_type' => $shippingType,
-            'channel_type' => $userInfo['user_type'],
-            'province' => strval($userInfo['user_type'] == 'wechat' || $userInfo['user_type'] == 'routine' ? $wechatServices->value(['uid' => $uid, 'user_type' => $userInfo['user_type']], 'province') : ''),
-            'spread_uid' => 0,
-            'spread_two_uid' => 0,
-            'virtual_type' => $virtual_type,
-            'is_gift' => $is_gift,
-            'gift_mark' => $gift_mark,
-            'pay_uid' => $uid,
-            'custom_form' => json_encode($customForm),
-            'division_id' => $userInfo['division_id'],
-            'agent_id' => $userInfo['agent_id'],
-            'staff_id' => $userInfo['staff_id'],
-            'li_spread_uid' =>$lb_spread_uid,
-            'is_lb' => $is_lb
-        ];
-
-        if ($shippingType == 2) {
-            $orderInfo['verify_code'] = $this->getStoreCode();
-            /** @var SystemStoreServices $storeServices */
-            $storeServices = app()->make(SystemStoreServices::class);
-            $orderInfo['store_id'] = $storeServices->getStoreDispose($storeId, 'id');
-            if (!$orderInfo['store_id']) {
-                throw new ApiException(410247);
             }
             }
-        }
-        /** @var StoreOrderCartInfoServices $cartServices */
-        $cartServices = app()->make(StoreOrderCartInfoServices::class);
-        $priceData['coupon_id'] = $couponId;
-        $order = $this->transaction(function () use ($cartIds, $orderInfo, $cartInfo, $key, $userInfo, $useIntegral, $priceData, $combinationId, $seckillId, $bargainId, $cartServices, $uid, $addressId, $advanceId) {
-            //创建订单
-            $order = $this->dao->save($orderInfo);
-            if (!$order) {
-                throw new ApiException(410200);
+            if (count($cartInfo) == 1 && isset($cartInfo[0]['productInfo']['presale']) && $cartInfo[0]['productInfo']['presale'] == 1) {
+                $advance_id = $cartInfo[0]['product_id'];
+            } else {
+                $advance_id = 0;
             }
             }
-            //记录自提人电话和姓名
-            /** @var UserServices $userService */
-            $userService = app()->make(UserServices::class);
-            $realName = $userService->value(['uid' => $uid], 'real_name');
-            if ($realName == '') $userService->update(['uid' => $uid], ['real_name' => $orderInfo['real_name'], 'record_phone' => $orderInfo['user_phone']]);
-            //积分抵扣
-            if ($priceData['usedIntegral'] > 0) {
-                $this->deductIntegral($userInfo, $useIntegral, $priceData, (int)$userInfo['uid'], $order['id']);
+            $deduction = $seckillId || $bargainId || $combinationId;
+            if ($deduction) {
+                $couponId = 0;
+                $gainIntegral = 0;
+                $useIntegral = false;
+            }
+            //$shipping_type = 1 快递发货 $shipping_type = 2 门店自提
+            $storeSelfMention = sys_config('store_self_mention') ?? 0;
+            if (!$storeSelfMention) $shippingType = 1;
+            if ($is_gift == 1) $shippingType = 0;
+
+            $orderInfo = [
+                'uid' => $uid,
+                'order_id' => $this->getNewOrderId('cp'),
+                'real_name' => $addressInfo['real_name'],
+                'user_phone' => $addressInfo['phone'],
+                'user_address' => $addressInfo['province'] . ' ' . $addressInfo['city'] . ' ' . $addressInfo['district'] . ' ' . $addressInfo['detail'],
+                'cart_id' => $cartIds,
+                'total_num' => $totalNum,
+                'total_price' => $priceGroup['totalPrice'],
+                'total_postage' => $shippingType == 1 ? $priceGroup['storePostage'] : 0,
+                'coupon_id' => $couponId,
+                'coupon_price' => $priceData['coupon_price'],
+                'pay_price' => $priceData['pay_price'],
+                'pay_postage' => $priceData['pay_postage'],
+                'deduction_price' => $priceData['deduction_price'],
+                'gift_price' => $priceData['gift_price'],
+                'paid' => 0,
+                'pay_type' => $payType,
+                'use_integral' => $priceData['usedIntegral'],
+                'gain_integral' => $gainIntegral,
+                'mark' => htmlspecialchars($mark),
+                'combination_id' => $combinationId,
+                'pink_id' => $pinkId,
+                'seckill_id' => $seckillId,
+                'bargain_id' => $bargainId,
+                'advance_id' => $advance_id,
+                'cost' => $priceGroup['costPrice'],
+                'add_time' => time(),
+                'unique' => $key,
+                'shipping_type' => $shippingType,
+                'channel_type' => $userInfo['user_type'],
+                'province' => strval($userInfo['user_type'] == 'wechat' || $userInfo['user_type'] == 'routine' ? $wechatServices->value(['uid' => $uid, 'user_type' => $userInfo['user_type']], 'province') : ''),
+                'spread_uid' => 0,
+                'spread_two_uid' => 0,
+                'virtual_type' => $virtual_type,
+                'is_gift' => $is_gift,
+                'gift_mark' => $gift_mark,
+                'pay_uid' => $uid,
+                'custom_form' => json_encode($customForm),
+                'division_id' => $userInfo['division_id'],
+                'agent_id' => $userInfo['agent_id'],
+                'staff_id' => $userInfo['staff_id'],
+                'li_spread_uid' => $lb_spread_uid,
+                'is_lb' => $is_lb
+            ];
+
+            if ($shippingType == 2) {
+                $orderInfo['verify_code'] = $this->getStoreCode();
+                /** @var SystemStoreServices $storeServices */
+                $storeServices = app()->make(SystemStoreServices::class);
+                $orderInfo['store_id'] = $storeServices->getStoreDispose($storeId, 'id');
+                if (!$orderInfo['store_id']) {
+                    throw new ApiException(410247);
+                }
+            }
+            /** @var StoreOrderCartInfoServices $cartServices */
+            $cartServices = app()->make(StoreOrderCartInfoServices::class);
+            $priceData['coupon_id'] = $couponId;
+            $order = $this->transaction(function () use ($cartIds, $orderInfo, $cartInfo, $key, $userInfo, $useIntegral, $priceData, $combinationId, $seckillId, $bargainId, $cartServices, $uid, $addressId, $advanceId) {
+                //创建订单
+                $order = $this->dao->save($orderInfo);
+                if (!$order) {
+                    throw new ApiException(410200);
+                }
+                //记录自提人电话和姓名
+                /** @var UserServices $userService */
+                $userService = app()->make(UserServices::class);
+                $realName = $userService->value(['uid' => $uid], 'real_name');
+                if ($realName == '') $userService->update(['uid' => $uid], ['real_name' => $orderInfo['real_name'], 'record_phone' => $orderInfo['user_phone']]);
+                //积分抵扣
+                if ($priceData['usedIntegral'] > 0) {
+                    $this->deductIntegral($userInfo, $useIntegral, $priceData, (int)$userInfo['uid'], $order['id']);
+                }
+                //扣库存
+                $this->decGoodsStock($cartInfo, $combinationId, $seckillId, $bargainId, $advanceId);
+                //保存购物车商品信息
+                $cartServices->setCartInfo($order['id'], $uid, $cartInfo);
+                return $order;
+            });
+
+            //创建开票数据
+            if ($invoice_id) {
+                app()->make(StoreOrderInvoiceServices::class)->makeUp($uid, $order['order_id'], (int)$invoice_id);
             }
             }
-            //扣库存
-            $this->decGoodsStock($cartInfo, $combinationId, $seckillId, $bargainId, $advanceId);
-            //保存购物车商品信息
-            $cartServices->setCartInfo($order['id'], $uid, $cartInfo);
-            return $order;
-        });
 
 
-        //创建开票数据
-        if ($invoice_id) {
-            app()->make(StoreOrderInvoiceServices::class)->makeUp($uid, $order['order_id'], (int)$invoice_id);
-        }
+            // 订单创建成功后置事件
+            event('OrderCreateAfterListener', [$order, compact('cartInfo', 'priceData', 'addressId', 'cartIds', 'news'), $uid, $key, $combinationId, $seckillId, $bargainId]);
+            // 推送订单
+            event('OutPushListener', ['order_create_push', ['order_id' => (int)$order['id']]]);
+
+            //自定义事件-订单创建事件
+            event('CustomEventListener', ['order_create', [
+                'uid' => $uid,
+                'id' => (int)$order['id'],
+                'order_id' => $order['order_id'],
+                'real_name' => $order['real_name'],
+                'user_phone' => $order['user_phone'],
+                'user_address' => $order['user_address'],
+                'total_num' => $order['total_num'],
+                'pay_price' => $order['pay_price'],
+                'pay_postage' => $order['pay_postage'],
+                'deduction_price' => $order['deduction_price'],
+                'coupon_price' => $order['coupon_price'],
+                'store_name' => app()->make(StoreOrderCartInfoServices::class)->getCarIdByProductTitle((int)$order['id']),
+                'add_time' => date('Y-m-d H:i:s', $order['add_time']),
+            ]]);
 
 
-        // 订单创建成功后置事件
-        event('OrderCreateAfterListener', [$order, compact('cartInfo', 'priceData', 'addressId', 'cartIds', 'news'), $uid, $key, $combinationId, $seckillId, $bargainId]);
-        // 推送订单
-        event('OutPushListener', ['order_create_push', ['order_id' => (int)$order['id']]]);
-
-        //自定义事件-订单创建事件
-        event('CustomEventListener', ['order_create', [
-            'uid' => $uid,
-            'id' => (int)$order['id'],
-            'order_id' => $order['order_id'],
-            'real_name' => $order['real_name'],
-            'user_phone' => $order['user_phone'],
-            'user_address' => $order['user_address'],
-            'total_num' => $order['total_num'],
-            'pay_price' => $order['pay_price'],
-            'pay_postage' => $order['pay_postage'],
-            'deduction_price' => $order['deduction_price'],
-            'coupon_price' => $order['coupon_price'],
-            'store_name' => app()->make(StoreOrderCartInfoServices::class)->getCarIdByProductTitle((int)$order['id']),
-            'add_time' => date('Y-m-d H:i:s', $order['add_time']),
-        ]]);
-
-        return $order;
+            return $order;
+        } catch (\Exception $e) {
+            @file_put_contents('quanju4.txt', $e->getMessage() . "-创建订单报错内容\r\n", 8);
+            @file_put_contents('quanju4.txt', $e->getFile() . "-文件\r\n", 8);
+            @file_put_contents('quanju4.txt', $e->getLine() . "-位置\r\n", 8);
+        }
     }
     }