WIN-2308041133\Administrator 2 周之前
父节点
当前提交
5bd28ba3e7

+ 42 - 1
app/services/activity/coupon/StoreCouponUserServices.php

@@ -16,6 +16,7 @@ use app\services\BaseServices;
 use app\dao\activity\coupon\StoreCouponUserDao;
 use app\services\product\product\StoreCategoryServices;
 use app\services\product\product\StoreProductCateServices;
+use app\services\product\product\StoreProductServices;
 use crmeb\utils\Arr;
 
 /**
@@ -122,7 +123,6 @@ class StoreCouponUserServices extends BaseServices
      * 下单页面显示可用优惠券
      * @param $uid
      * @param $cartGroup
-     * @param $price
      * @return array
      */
     public function getUsableCouponList(int $uid, array $cartGroup)
@@ -131,7 +131,48 @@ class StoreCouponUserServices extends BaseServices
         $result = [];
         if ($userCoupons) {
             $cartInfo = $cartGroup['valid'];
+
+            // 获取购物车中所有商品的 is_repeat、is_free 字段
+            $productIds = array_unique(array_column($cartInfo, 'product_id'));
+            $hasRepeat = false;
+            $hasFree = false;
+            if ($productIds) {
+                /** @var StoreProductServices $storeProductServices */
+                $storeProductServices = app()->make(StoreProductServices::class);
+                $products = $storeProductServices->getColumn([['id', 'in', $productIds]], 'is_repeat,is_free', 'id');
+                foreach ($products as $product) {
+                    if (!empty($product['is_repeat']) && $product['is_repeat'] == 1) {
+                        $hasRepeat = true;
+                    }
+                    if (!empty($product['is_free']) && $product['is_free'] == 1) {
+                        $hasFree = true;
+                    }
+                }
+            }
+
+            // 从系统配置获取特殊优惠券 id
+            $repeatVoucherId = (int)sys_config('repeat_voucher');
+            $freeVoucherId   = (int)sys_config('free_voucher');
+
             foreach ($userCoupons as $coupon) {
+                $couponId = (int)$coupon['cid'];
+
+                // is_repeat 为 1,则只显示 repeat_voucher 对应的优惠券,其他都跳过
+                if ($hasRepeat && $repeatVoucherId && $couponId !== $repeatVoucherId) {
+                    continue;
+                }
+                // is_free 为 1,则只显示 free_voucher 对应的优惠券,其他都跳过
+                if ($hasFree && $freeVoucherId && $couponId !== $freeVoucherId) {
+                    continue;
+                }
+                // is_repeat 和 is_free 都不为 1,则隐藏 repeat_voucher 和 free_voucher 优惠券
+                if (!$hasRepeat && $repeatVoucherId && $couponId === $repeatVoucherId) {
+                    continue;
+                }
+                if (!$hasFree && $freeVoucherId && $couponId === $freeVoucherId) {
+                    continue;
+                }
+
                 $price = 0;
                 $count = 0;
                 switch ($coupon['applicable_type']) {

+ 10 - 0
app/services/order/StoreCartServices.php

@@ -12,7 +12,9 @@ declare (strict_types=1);
 
 namespace app\services\order;
 
+use app\model\agent\AgentLevel;
 use app\services\activity\advance\StoreAdvanceServices;
+use app\services\agent\AgentLevelServices;
 use app\services\BaseServices;
 use app\dao\order\StoreCartDao;
 use app\services\activity\coupon\StoreCouponIssueServices;
@@ -531,6 +533,10 @@ class StoreCartServices extends BaseServices
                 /** @var SystemUserLevelServices $systemLevel */
                 $systemLevel = app()->make(SystemUserLevelServices::class);
                 $discount = $systemLevel->value(['id' => $userInfo['level'], 'is_del' => 0, 'is_show' => 1], 'discount') ?: 100;
+            }else{
+                /** @var AgentLevelServices $agentLevel */
+                $agentLevel = app()->make(AgentLevelServices::class);
+                $discount = $agentLevel->value(['id' => $userInfo['agent_level'], 'is_del' => 0], 'discount') ?: 100;
             }
             foreach ($cartList as &$item) {
                 $productInfo = $item['productInfo'];
@@ -584,6 +590,10 @@ class StoreCartServices extends BaseServices
                 /** @var SystemUserLevelServices $systemLevel */
                 $systemLevel = app()->make(SystemUserLevelServices::class);
                 $discount = $systemLevel->value(['id' => $userInfo['level'], 'is_del' => 0, 'is_show' => 1], 'discount') ?: 100;
+            }else{
+                /** @var AgentLevelServices $agentLevel */
+                $agentLevel = app()->make(AgentLevelServices::class);
+                $discount = $agentLevel->value(['id' => $userInfo['agent_level'], 'is_del' => 0], 'discount') ?: 100;
             }
         }
 

+ 24 - 0
app/services/order/StoreOrderComputedServices.php

@@ -15,6 +15,7 @@ use app\services\BaseServices;
 use app\dao\order\StoreOrderDao;
 use app\services\pay\PayServices;
 use app\services\product\product\StoreCategoryServices;
+use app\services\product\product\StoreProductServices;
 use app\services\user\member\MemberCardServices;
 use app\services\user\UserBillServices;
 use app\services\user\UserServices;
@@ -111,6 +112,29 @@ class StoreOrderComputedServices extends BaseServices
         $bargainId = $this->paramData['bargainId'] ?? 0;
         $isActivity = $combinationId || $seckillId || $bargainId;
         if (!$isActivity) {
+            // 当使用了优惠券时,检查购物车中是否存在复购商品(is_repeat=1)
+            // 若存在,则取消会员折扣(优惠券与会员折扣互斥),重新按原价计算总价
+            if ($couponId > 0) {
+                $productIds = array_unique(array_column($cartInfo, 'product_id'));
+                /** @var StoreProductServices $productServices */
+                $productServices = app()->make(StoreProductServices::class);
+                $hasRepeat = $productServices->getCount([['id', 'in', $productIds], ['is_repeat', '=', 1]]) > 0;
+                if ($hasRepeat) {
+                    // 将所有有会员折扣的商品还原为原价,重新汇总总价
+                    $newTotalPrice = '0';
+                    foreach ($cartInfo as &$cartItem) {
+                        if (isset($cartItem['price_type']) && in_array($cartItem['price_type'], ['level', 'member'])) {
+                            $cartItem['truePrice'] = $cartItem['sum_price'];
+                            $cartItem['vip_truePrice'] = 0;
+                            $cartItem['price_type'] = 'normal';
+                        }
+                        $newTotalPrice = bcadd($newTotalPrice, bcmul((string)$cartItem['truePrice'], (string)$cartItem['cart_num'], 4), 2);
+                    }
+                    unset($cartItem);
+                    $payPrice = (float)$newTotalPrice;
+                    $priceGroup['totalPrice'] = $newTotalPrice;
+                }
+            }
             //使用优惠劵
             [$payPrice, $couponPrice] = $this->useCouponId($couponId, $uid, $cartInfo, $payPrice, $isCreate);
             //使用积分

+ 5 - 0
app/services/pc/CartServices.php

@@ -13,6 +13,7 @@ declare (strict_types = 1);
 namespace app\services\pc;
 
 
+use app\services\agent\AgentLevelServices;
 use app\services\BaseServices;
 use app\services\order\StoreCartServices;
 use app\services\product\product\StoreProductServices;
@@ -46,6 +47,10 @@ class CartServices extends BaseServices
             /** @var SystemUserLevelServices $systemLevel */
             $systemLevel = app()->make(SystemUserLevelServices::class);
             $discount = $systemLevel->value(['id' => $userInfo['level'], 'is_del' => 0, 'is_show' => 1], 'discount') ?: 100;
+        }else{
+            /** @var AgentLevelServices $agentLevel */
+            $agentLevel = app()->make(AgentLevelServices::class);
+            $discount = $agentLevel->value(['id' => $userInfo['agent_level'], 'is_del' => 0], 'discount') ?: 100;
         }
         $valid = $invalid = [];
         foreach ($list as &$item) {