Parcourir la source

优化用户下单方法-进行中

sunxbiao il y a 1 an
Parent
commit
fdcc044d06
1 fichiers modifiés avec 83 ajouts et 56 suppressions
  1. 83 56
      app/common/repositories/store/order/StoreOrderRepository.php

+ 83 - 56
app/common/repositories/store/order/StoreOrderRepository.php

@@ -11590,19 +11590,30 @@ class StoreOrderRepository extends BaseRepository
11590
         /** @var ShippingTemplateRegionRepository $shippingTemplateRegionRepository */
11590
         /** @var ShippingTemplateRegionRepository $shippingTemplateRegionRepository */
11591
         $shippingTemplateRegionRepository = app()->make(ShippingTemplateRegionRepository::class);
11591
         $shippingTemplateRegionRepository = app()->make(ShippingTemplateRegionRepository::class);
11592
 
11592
 
11593
+        // 记录 用户 在指定商家的 优惠券
11594
+        $storeCouponUserEntityListByMerIdMap = [];
11595
+        // 当前优惠金额
11596
+        $couponPrice = 0.00;
11597
+        // 需要使用掉的优惠券
11598
+        $useCouponProduct = [];
11599
+
11600
+        // 商家列表 按 订单信息按 商家分组
11593
         $merchantMap = [];
11601
         $merchantMap = [];
11602
+        // 记录 每家商户 每类商品 在此笔订单中的金额(只记录普通商品)
11603
+        // 商品 => 此笔订单内此商品总价
11604
+        $productPriceByMerIdMap = [];
11605
+        // 订单所产生的养老金
11594
         $yanglaojin = 0.00;
11606
         $yanglaojin = 0.00;
11607
+        // 订单总金额 (只含普通商品)
11608
+        $validTotalPrice = 0.00;
11609
+        // 订单总金额 (包含 秒杀商品价格)
11595
         $totalPrice = 0.00;
11610
         $totalPrice = 0.00;
11611
+        // 订单总数量
11596
         $totalNum = 0;
11612
         $totalNum = 0;
11597
-        // 记录每类商品 在此笔订单中的金额
11598
-        // 商品 => 此笔订单内此商品总价
11599
-        $productPriceMap = [];
11600
-        $validTotalPrice = 0.00;
11601
-        $productCart = [];
11602
-        // $address ? ($noDeliver ? 'noDeliver' : 'finish') : 'noAddress'
11603
-        $status = 'noAddress';
11613
+        // 此笔订单产生的总 邮费
11604
         $postage = 0.00;
11614
         $postage = 0.00;
11605
 
11615
 
11616
+
11606
         // 循环购物车 对购物车内的商品 进行条件验证 以及根据不同的商品 计算邮费
11617
         // 循环购物车 对购物车内的商品 进行条件验证 以及根据不同的商品 计算邮费
11607
         foreach ($cartEntityList as $cartEntity) {
11618
         foreach ($cartEntityList as $cartEntity) {
11608
             // 验证商户
11619
             // 验证商户
@@ -11648,8 +11659,10 @@ class StoreOrderRepository extends BaseRepository
11648
 
11659
 
11649
             // 开始 计算 **************
11660
             // 开始 计算 **************
11650
 
11661
 
11651
-            // 计算养老金
11652
-            $yanglaojin = bcadd($yanglaojin, $productRepository->yanglaojin($cartEntity->getProductId()), 2);
11662
+            // ??? 暂时不理解这一步
11663
+            // type => '商品类型:1:普通商品。2:爆款商品。3:消费积分商品 。5:会员专享商品(城中大仓)6:每日免费领商品 7:商户特殊商品 8:共富会员商品 9:共富高级会员商品 10:高级会员商品 11村代理商品(废弃) 12镇代理商品(废弃) 13县代理商品(废弃)  14- 合伙人商品 15 股权商品 16合伙人+股权商品',
11664
+            // ProductType => 0.普通商品 1.秒杀商品
11665
+            $productEntity->setProductType($productEntity->getType());
11653
 
11666
 
11654
             // 验证是否是秒杀产品 如果是秒杀产品,并且设置了 大仓会员价 则将大仓会员价 设置为 商品价格
11667
             // 验证是否是秒杀产品 如果是秒杀产品,并且设置了 大仓会员价 则将大仓会员价 设置为 商品价格
11655
             if ($productEntity->getSeckill() == ProductEnum::SECKILL['Yes']['code'] && !is_null($productEntity->getDcPrice()) && $productEntity->getDcPrice() > 0.00) {
11668
             if ($productEntity->getSeckill() == ProductEnum::SECKILL['Yes']['code'] && !is_null($productEntity->getDcPrice()) && $productEntity->getDcPrice() > 0.00) {
@@ -11661,10 +11674,52 @@ class StoreOrderRepository extends BaseRepository
11661
                 }
11674
                 }
11662
             }
11675
             }
11663
 
11676
 
11664
-            // ??? 暂时不理解这一步
11665
-            // type => '商品类型:1:普通商品。2:爆款商品。3:消费积分商品 。5:会员专享商品(城中大仓)6:每日免费领商品 7:商户特殊商品 8:共富会员商品 9:共富高级会员商品 10:高级会员商品 11村代理商品(废弃) 12镇代理商品(废弃) 13县代理商品(废弃)  14- 合伙人商品 15 股权商品 16合伙人+股权商品',
11666
-            // ProductType => 0.普通商品 1.秒杀商品
11667
-            $productEntity->setProductType($productEntity->getType());
11677
+            // 计算优惠券 ******************
11678
+            // 商品券 优先于 店铺券
11679
+            // 商品券(平台优惠券)
11680
+            if (!isset($storeCouponUserEntityListByMerIdMap[0])) {
11681
+                $storeCouponUserEntityListByMerIdMap[0] = $storeCouponUserRepository->getValidStoreCouponUserEntityListByMerIdAndUid(0, $cartEntity->getUid());
11682
+            }
11683
+            // 店铺券(满减券)
11684
+            if (!isset($storeCouponUserEntityListByMerIdMap[$merchantEntity->getMerId()])) {
11685
+                // 查询 当前用户 在当前 店铺的 优惠券
11686
+                $storeCouponUserEntityListByMerIdMap[$merchantEntity->getMerId()] = $storeCouponUserRepository->getValidStoreCouponUserEntityListByMerIdAndUid($merchantEntity->getMerId(), $cartEntity->getUid());
11687
+            }
11688
+
11689
+            if (!empty($storeCouponUserEntityListByMerIdMap[0]) || !empty($storeCouponUserEntityListByMerIdMap[$merchantEntity->getMerId()])) {
11690
+                foreach ([0, $merchantEntity->getMerId()] as $merId) {
11691
+                    foreach ($storeCouponUserEntityListByMerIdMap[$merId] as $storeCouponUserEntity) {
11692
+                        // 如果不能明确当前优惠券的 获取方式 ,则忽略当前的优惠券的有效性
11693
+                        if (empty($storeCouponUserEntity->getType())) {
11694
+                            continue;
11695
+                        }
11696
+                        // 优惠卷关联商品 辅助表
11697
+                        $storeCouponProductEntityList = $storeCouponProductRepository->getStoreCouponProductEntityListByCouponId($storeCouponUserEntity->getCouponId());
11698
+                        if (!empty($storeCouponProductEntityList)) {
11699
+                            // 商品券
11700
+                            foreach ($storeCouponProductEntityList as $storeCouponProductEntity) {
11701
+                                // 如果当前 优惠券 所关联的 商品 是否在当前购买的 该商家的 商品列表中,(没有则说明此条关联记录 无用) 并且当前 该商家内 该商品没有已使用的优惠券
11702
+                                if (in_array($storeCouponProductEntity->getProductId(), array_keys($productPriceByMerIdMap[$merchantEntity->getMerId()])) && !isset($useCouponProduct[$storeCouponProductEntity->getProductId()][$merchantEntity->getMerId()])) {
11703
+                                    // 如果 当前该商家 该商品 的价格 超过了 “最低消费多少金额可用优惠券” 的金额 则记录该优惠金额,并记录该优惠券ID 后续设置为已使用
11704
+                                    if ($productPriceByMerIdMap[$merchantEntity->getMerId()][$storeCouponProductEntity->getProductId()] >= $storeCouponUserEntity->getUseMinPrice()) {
11705
+                                        $couponPrice = bcadd($couponPrice, $storeCouponUserEntity->getCouponPrice(), 2);
11706
+                                        // 一个商家的 一种商品 只能使用一次优惠券
11707
+                                        $useCouponProduct[$storeCouponProductEntity->getProductId()][$merchantEntity->getMerId()] = $storeCouponUserEntity->getCouponUserId();
11708
+                                        break;
11709
+                                    }
11710
+                                }
11711
+                            }
11712
+                        } else {
11713
+                            // 店铺券(平台券-满减券)
11714
+                        }
11715
+
11716
+                    }
11717
+                }
11718
+
11719
+            }
11720
+
11721
+            // 计算养老金 TODO
11722
+            $yanglaojin = bcadd($yanglaojin, $productRepository->yanglaojin($cartEntity->getProductId()), 2);
11668
 
11723
 
11669
             // 计算单价
11724
             // 计算单价
11670
             $price = bcmul($cartEntity->getCartNum(), $productAttrValueEntity->getPrice(), 2);
11725
             $price = bcmul($cartEntity->getCartNum(), $productAttrValueEntity->getPrice(), 2);
@@ -11675,13 +11730,10 @@ class StoreOrderRepository extends BaseRepository
11675
 
11730
 
11676
             // 如果是 普通商品 product_type 类型 0=普通产品
11731
             // 如果是 普通商品 product_type 类型 0=普通产品
11677
             if ($cartEntity->getProductType() == CartEnum::PRODUCT_TYPE['Ordinary']['code']) {
11732
             if ($cartEntity->getProductType() == CartEnum::PRODUCT_TYPE['Ordinary']['code']) {
11678
-                $productPriceMap[$cartEntity->getProductId()] = bcadd($productPriceMap[$cartEntity->getProductId()] ?? 0, $price, 2);
11733
+                $productPriceByMerIdMap[$cartEntity->getMerId()][$cartEntity->getProductId()] = bcadd($productPriceByMerIdMap[$cartEntity->getMerId()][$cartEntity->getProductId()] ?? 0, $price, 2);
11679
                 $validTotalPrice = bcadd($validTotalPrice, $price, 2);
11734
                 $validTotalPrice = bcadd($validTotalPrice, $price, 2);
11680
             }
11735
             }
11681
 
11736
 
11682
-            $productCart[$cartEntity->getProductId()][] = $cartEntity->getCartId();
11683
-
11684
-
11685
             // 计算运费 获取该商品的运费模板 运费表
11737
             // 计算运费 获取该商品的运费模板 运费表
11686
             // TODO  注意 这个地址好像有问题,我再 客户端->收货地址管理中 (rrx_user_address)增加的廊坊市 对应的城市编号是(code_list)315 且city_id 字段的值是 2,而我再后台设置 可配送地址时 ,廊坊市对应的编码是 58247
11738
             // TODO  注意 这个地址好像有问题,我再 客户端->收货地址管理中 (rrx_user_address)增加的廊坊市 对应的城市编号是(code_list)315 且city_id 字段的值是 2,而我再后台设置 可配送地址时 ,廊坊市对应的编码是 58247
11687
             // 判断商品是否是 供应链(微唯宝)商品
11739
             // 判断商品是否是 供应链(微唯宝)商品
@@ -11755,41 +11807,20 @@ class StoreOrderRepository extends BaseRepository
11755
                 }
11807
                 }
11756
             }
11808
             }
11757
 
11809
 
11758
-            // // 将商户 存贮
11759
-            // $merchantEntityArr = $merchantEntity->toArray();
11760
-            //
11761
-            // // 这块有问题???? 这个优惠券查出来的意义是什么,而且这种组合方式也有问题 一个商户假如有多个商品
11762
-            // // 获取 当前消费者 有没有 从该商家发型的优惠券里 进行领取
11763
-            // $couponCountByMerIdUid = $storeCouponRepository->validMerCouponExists($cartEntity->getMerId(), $cartEntity->getUid());
11764
-            // if ($couponCountByMerIdUid > 0) {
11765
-            //     $merchantEntityArr['hasCoupon'] = $couponCountByMerIdUid;
11766
-            // }
11767
-            //
11768
-            // if (($list[$cartEntity->getMerId()]['hasCoupon'] ?? 0) == 0) {
11769
-            //     // 获取该商品 所绑定的优惠券
11770
-            //     $couponIdListByProductId = $storeCouponProductRepository->productByCouponId($cartEntity->getProductId());
11771
-            //     if (!empty($couponIdListByProductId)) {
11772
-            //         $list[$cartEntity->getMerId()]['hasCoupon'] = $storeCouponRepository->validProductCouponExists([$cartEntity->getProductId()], $cartEntity->getUid());
11773
-            //     }
11774
-            // }
11775
-            //
11776
-            // $list[$cartEntity->getMerId()] = $merchantEntityArr;
11777
-            // $list[$cartEntity->getMerId()]['hasCoupon'] = 0;
11778
-            // // 应该还有商品信息等数据
11779
-
11780
-
11781
             $merchantMap[$cartEntity->getMerId()]['merchantEntity'] = $merchantEntity;
11810
             $merchantMap[$cartEntity->getMerId()]['merchantEntity'] = $merchantEntity;
11782
-            $merchantMap[$cartEntity->getMerId()]['productIdListByCart'][] = $cartEntity->getProductId();
11783
         }
11811
         }
11784
 
11812
 
11813
+        // 当前优惠金额
11814
+        $couponPrice = 0.00;
11815
+        // 需要使用掉的优惠券
11816
+        $useCouponProduct = [];
11785
 
11817
 
11786
         foreach ($merchantMap as $merId => $item) {
11818
         foreach ($merchantMap as $merId => $item) {
11787
             // 当前商户的实体信息
11819
             // 当前商户的实体信息
11788
             /** @var MerchantEntity $merchantEntity */
11820
             /** @var MerchantEntity $merchantEntity */
11789
             $merchantEntity = $item['merchantEntity'];
11821
             $merchantEntity = $item['merchantEntity'];
11790
-            // 当前订单中 从当前商户中选择的商品列表
11791
-            $productIdListByCart = $item['productIdListByCart'];
11792
 
11822
 
11823
+            // 店铺券
11793
             // 查询 当前用户 在当前 店铺的 优惠券
11824
             // 查询 当前用户 在当前 店铺的 优惠券
11794
             $storeCouponUserEntityList = $storeCouponUserRepository->getValidStoreCouponUserEntityListByMerIdAndUid($merchantEntity->getMerId(), $userEntity->getUid());
11825
             $storeCouponUserEntityList = $storeCouponUserRepository->getValidStoreCouponUserEntityListByMerIdAndUid($merchantEntity->getMerId(), $userEntity->getUid());
11795
             foreach ($storeCouponUserEntityList as $storeCouponUserEntity) {
11826
             foreach ($storeCouponUserEntityList as $storeCouponUserEntity) {
@@ -11801,22 +11832,18 @@ class StoreOrderRepository extends BaseRepository
11801
                 $storeCouponProductEntityList = $storeCouponProductRepository->getStoreCouponProductEntityListByCouponId($storeCouponUserEntity->getCouponId());
11832
                 $storeCouponProductEntityList = $storeCouponProductRepository->getStoreCouponProductEntityListByCouponId($storeCouponUserEntity->getCouponId());
11802
 
11833
 
11803
                 foreach ($storeCouponProductEntityList as $storeCouponProductEntity) {
11834
                 foreach ($storeCouponProductEntityList as $storeCouponProductEntity) {
11804
-                    // 如果当前 优惠券 所关联的 此 商品 不在当前购买的商品列表中,则说明此条关联记录 无用
11805
-                    if (!in_array($storeCouponProductEntity->getProductId(), $productIdListByCart)) {
11806
-                        continue;
11835
+                    // 如果当前 优惠券 所关联的 商品 是否在当前购买的 该商家的 商品列表中,(没有则说明此条关联记录 无用) 并且当前 该商家内 该商品没有已使用的优惠券
11836
+                    if (in_array($storeCouponProductEntity->getProductId(), array_keys($productPriceByMerIdMap[$merchantEntity->getMerId()])) && !isset($useCouponProduct[$merchantEntity->getMerId()][$storeCouponProductEntity->getProductId()])) {
11837
+                        // 如果 当前该商家 该商品 的价格 超过了 “最低消费多少金额可用优惠券” 的金额 则记录该优惠金额,并记录该优惠券ID 后续设置为已使用
11838
+                        if ($productPriceByMerIdMap[$merchantEntity->getMerId()][$storeCouponProductEntity->getProductId()] >= $storeCouponUserEntity->getUseMinPrice()) {
11839
+                            $couponPrice = bcadd($couponPrice, $storeCouponUserEntity->getCouponPrice(), 2);
11840
+                            // 一个商家的 一种商品 只能使用一次优惠券
11841
+                            $useCouponProduct[$merchantEntity->getMerId()][$storeCouponProductEntity->getProductId()] = $storeCouponUserEntity->getCouponUserId();
11842
+                            break;
11843
+                        }
11807
                     }
11844
                     }
11808
-
11809
                 }
11845
                 }
11810
-
11811
-                // 获取 当前消费者 有没有 从该商家发型的优惠券里 进行领取
11812
-                $couponCountByMerIdUid = $storeCouponRepository->validMerCouponExists($merchantEntity->getMerId(), $userEntity->getUid());
11813
-                if ($couponCountByMerIdUid > 0) {
11814
-
11815
-                }
11816
-
11817
-
11818
             }
11846
             }
11819
-
11820
         }
11847
         }
11821
 
11848
 
11822
 
11849