Explorar el Código

1. 特价情况下的限购,且购买数量超限购时,限购数量的为特价,其余按原价
2. 订单取消的情况要清空限购缓存

shizhongqi hace 4 meses
padre
commit
70229a1bbd

+ 1 - 2
biz-ghs/order/classes/OrderClass.php

@@ -827,8 +827,7 @@ class OrderClass extends BaseClass
         $data['payStatus'] = self::PAY_STATUS_UN_PAY;
         $data['customId'] = !empty($data['customId']) ? $data['customId'] : 0;
         $live = $data['live'] ?? 1;
-        $currentShopId = $data['shopId'] ?? 0;
-        $shop = ShopClass::getLockById($currentShopId);
+        $shop = ShopClass::getLockById($shopId);
         if (empty($shop)) {
             util::fail('没有找到门店7');
         }

+ 2 - 4
biz-ghs/order/classes/OrderItemClass.php

@@ -371,17 +371,15 @@ class OrderItemClass extends BaseClass
             }
 
             $ratio = $p['ratio'] ?? 0;
-            $currentWeight = $p['weight'];
             $bigNum = $p['bigNum'] ?? 0;
             $smallNum = $p['smallNum'] ?? 0;
             $itemNum = ProductClass::mergeItemNum($bigNum, $smallNum, $ratio);
-            $w = bcmul($itemNum, $currentWeight, 2);
+            $w = bcmul($itemNum, $p['weight'], 2);
             $weight = bcadd($w, $weight, 2);
 
             $limitBuy = $p['limitBuy'];
-            $currentNum = floatval($itemNum);
             if ($limitBuy > 0) {
-                ProductClass::handleLimitBuy($p, $customId, $currentNum);
+                ProductClass::handleLimitBuy($p, $customId, floatval($itemNum));
             }
 
             if ($stockMayChange == true) {

+ 28 - 14
biz-ghs/product/classes/ProductClass.php

@@ -38,7 +38,7 @@ class ProductClass extends BaseClass
     use OrderTrait;
 
     public static $baseFile = '\bizGhs\product\models\Product';
-    const LIMIT_BUY_ROLLBACK_SNAPSHOT_KEY = 'limitBuyRollbackSnapshot'; //记录限购旧值快照缓存键
+    
     const PRICE_LABEL_AUTO = 1; //自动调价
     const PRICE_LABEL_CHANGE = 2; //改价
     const LOCK_STOCK = 'lock_stock';//修改库存锁
@@ -46,6 +46,10 @@ class ProductClass extends BaseClass
     const LOCK_PRICE = 'lock_price'; //改价锁
     const LOCK_CHECK_STOCK = 'lock_check_stock'; //判断库存
 
+    const LIMIT_BUY_KEY = 'limit_buy_';
+    const CLEAR_MARK_KEY = 'limit_buy_clear_at:';
+    const LIMIT_BUY_ROLLBACK_SNAPSHOT_KEY = 'limitBuyRollbackSnapshot'; //记录限购旧值快照缓存键
+
     public static function getNotShowFrontHideMainIds()
     {
         if (getenv('YII_ENV', 'local') == 'production') {
@@ -668,7 +672,7 @@ class ProductClass extends BaseClass
 
             // 限购缓存
             if ($v['limitBuy'] > 0) {
-                $limitKey = 'limit_buy_' . $v['id'];
+                $limitKey = self::LIMIT_BUY_KEY . $v['id'];
                 $has = Yii::$app->redis->executeCommand('HEXISTS', [$limitKey, 0]);
                 if (!empty($has)) {
                     // 获取缓存超时时间
@@ -786,10 +790,13 @@ class ProductClass extends BaseClass
             $price = $product[$currentPriceField] ?? 0;
         }
 
+        //特价与限购的综合处理
         $discountPrice = $product['discountPrice'] ?? 0;
         $hjDiscountPrice = $product['hjDiscountPrice'] ?? 0;
         $skDiscountPrice = $product['skDiscountPrice'] ?? 0;
+        $limitBuy = $product['limitBuy'];
         if ($discountPrice > 0) {
+            $oldPrice = $price;
             if ($level == 0) {
                 $price = $skDiscountPrice;
             }
@@ -799,7 +806,16 @@ class ProductClass extends BaseClass
             if ($level == 2) {
                 $price = $hjDiscountPrice;
             }
+
+            //超出限购情况的价格计算
+            if ($limitBuy > 0 && $buyNum > $limitBuy) {
+                $allPrice = $price * $limitBuy;
+                $allPrice = $allPrice + (($buyNum - $limitBuy) * $oldPrice);
+                $price = bcdiv($allPrice, $buyNum, 2);
+            }
         }
+
+        //满多少数量,再优惠多少钱
         $reachNum = $product['reachNum'] ?? 0;
         $reachNumDiscount = $product['reachNumDiscount'] ?? 0;
         if ($reachNum > 0) {
@@ -1476,7 +1492,6 @@ class ProductClass extends BaseClass
                         $totalReachDiscount = bcadd($totalReachDiscount, $currentReachDiscount, 2);
                     }
                 }
-
             } else {
                 $itemPrice = $v['unitPrice'] ?? 0;
                 $thisPrice = $itemPrice;
@@ -1492,8 +1507,6 @@ class ProductClass extends BaseClass
             $tmp['productId'] = $ghsProductId;
             $tmp['price'] = $price;
 
-            $totalPrice = bcadd($totalPrice, $price, 2);
-
             $tmp['name'] = $currentGhsProduct['name'] ?? '';
             $tmp['smallUnit'] = $currentGhsProduct['smallUnit'] ?? '支';
             $tmp['bigUnit'] = $currentGhsProduct['bigUnit'] ?? '扎';
@@ -1525,6 +1538,7 @@ class ProductClass extends BaseClass
             $tmp['discountPrice'] = $currentGhsProduct['discountPrice'];
             $tmp['skDiscountPrice'] = $currentGhsProduct['skDiscountPrice'];
 
+            $totalPrice = bcadd($totalPrice, $price, 2);
             $totalBigNum += $bigNum;
             $totalSmallNum += $smallNum;
 
@@ -2646,8 +2660,8 @@ class ProductClass extends BaseClass
     // 创建限购缓存(有过期时间)
     public static function createLimitBuyCache($productId, $seconds = 0)
     {
-        $limitKey = 'limit_buy_' . $productId;
-        $clearMarkKey = 'limit_buy_clear_at:' . $productId;
+        $limitKey = self::LIMIT_BUY_KEY . $productId;
+        $clearMarkKey = self::CLEAR_MARK_KEY . $productId;
 
         $has = Yii::$app->redis->executeCommand('HEXISTS', [$limitKey, 0]);
         if (!empty($has)) {
@@ -2749,7 +2763,7 @@ class ProductClass extends BaseClass
             if ($productId <= 0 || $customId <= 0) {
                 continue;
             }
-            $limitKey = 'limit_buy_' . $productId;
+            $limitKey = self::LIMIT_BUY_KEY . $productId;
             $hasOldValue = intval($snapshot['hasOldValue'] ?? 0);
             if ($hasOldValue == 1) {
                 $oldValue = $snapshot['oldValue'] ?? '0';
@@ -2771,7 +2785,7 @@ class ProductClass extends BaseClass
         }
 
         $productId = $product['productId'];
-        $limitKey = 'limit_buy_' . $productId;
+        $limitKey = self::LIMIT_BUY_KEY . $productId;
         $has = Yii::$app->redis->executeCommand('HEXISTS', [$limitKey, $customId]);
         if (!empty($has)) {
             $hasNum = Yii::$app->redis->executeCommand('HGET', [$limitKey, $customId]);
@@ -2803,7 +2817,7 @@ class ProductClass extends BaseClass
     //取消限购的缓存 ssh 20240605
     public static function cancelLimitBuy($productId)
     {
-        $limitKey = 'limit_buy_' . $productId;
+        $limitKey = self::LIMIT_BUY_KEY . $productId;
         $arr = Yii::$app->redis->executeCommand('HKEYS', [$limitKey]);
         if (!empty($arr)) {
             foreach ($arr as $field) {
@@ -2822,7 +2836,7 @@ class ProductClass extends BaseClass
             return false;
         }
 
-        $clearMarkKey = 'limit_buy_clear_at:' . $productId;
+        $clearMarkKey = self::CLEAR_MARK_KEY . $productId;
         $currentClearAt = intval(Yii::$app->redis->executeCommand('GET', [$clearMarkKey]));
         return $currentClearAt > 0 && $currentClearAt == $clearAt;
     }
@@ -2834,7 +2848,7 @@ class ProductClass extends BaseClass
         if ($productId <= 0) {
             return false;
         }
-        $clearMarkKey = 'limit_buy_clear_at:' . $productId;
+        $clearMarkKey = self::CLEAR_MARK_KEY . $productId;
         Yii::$app->redis->executeCommand('DEL', [$clearMarkKey]);
         return true;
     }
@@ -2842,7 +2856,7 @@ class ProductClass extends BaseClass
     //$num -1 表示减全部,大于-1表示减值
     public static function baseClearLimitBuy($productId, $customId, $num = -1)
     {
-        $limitKey = 'limit_buy_' . $productId;
+        $limitKey = self::LIMIT_BUY_KEY . $productId;
         if ($num <= -1) {
             //清掉全部
             Yii::$app->redis->executeCommand('HDEL', [$limitKey, $customId]);
@@ -2862,7 +2876,7 @@ class ProductClass extends BaseClass
     {
         $productId = $product->id;
         $limitBuy = $product->limitBuy ?? 0;
-        $limitKey = 'limit_buy_' . $productId;
+        $limitKey = self::LIMIT_BUY_KEY . $productId;
         $arr = Yii::$app->redis->executeCommand('HKEYS', [$limitKey]);
         $customList = [];
         if (!empty($arr)) {

+ 28 - 0
biz-hd/purchase/services/PurchaseService.php

@@ -11,7 +11,9 @@ use biz\stat\classes\StatCgGhsClass;
 use biz\stat\classes\StatOutClass;
 use bizGhs\custom\classes\CustomClass;
 use bizGhs\order\classes\OrderClass;
+use bizGhs\order\classes\OrderItemClass;
 use bizGhs\order\services\OrderService;
+use bizGhs\product\classes\ProductClass;
 use bizHd\base\services\BaseService;
 use bizHd\cg\classes\CgRefundClass;
 use bizHd\purchase\classes\PurchaseClass;
@@ -196,6 +198,7 @@ class PurchaseService extends BaseService
         $id = $purchase->id;
         $purchase = PurchaseClass::cancel($id);
         $saleId = $purchase->saleId;
+        
         //销售单失效
         OrderClass::cancel($saleId);
         $couponId = $purchase->hbId;
@@ -203,6 +206,31 @@ class PurchaseService extends BaseService
             $coupon = ShopCouponClass::getLockById($couponId);
             ShopCouponClass::rollback($coupon);
         }
+
+        //清空限购缓存
+        $order = OrderClass::getById($saleId, true);
+        if (!empty($order)) {
+            $book = $order->book ?? 0;
+            $customId = $order->customId ?? 0;
+            $orderSn = $order->orderSn;
+            if ($book != 1 && !empty($customId)) {
+                $orderItemList = OrderItemClass::getOrderItem($orderSn);
+                if (!empty($orderItemList)) {
+                    foreach ($orderItemList as $item) {
+                        if ($item['limitBuy'] <= 0) {
+                            continue;
+                        }
+
+                        $productId = $item['productId'] ?? 0;
+                        $num = floor($item['num'] ?? 0);
+                        if (!empty($productId) && $num > 0) {
+                            ProductClass::baseClearLimitBuy($productId, $customId, $num);
+                        }
+                    }
+                }
+            }
+        }
+
         return $purchase;
     }