Explorar o código

超出限购情况的价格计算--先前的不对,要加上已购买的数量来计算

shizhongqi hai 4 meses
pai
achega
a77f563276

+ 5 - 2
app-mall/controllers/OrderController.php

@@ -307,8 +307,11 @@ class OrderController extends BaseController
                 if (empty($product)) {
                     util::fail('有商品信息没有找到');
                 }
-                $price = \bizGhs\product\classes\ProductClass::getFinalPrice($product, $level, $priceMap, $addPriceMap);
-                $num = $element['num'] ?? 0;
+                $num = $element['num'] ?? 0; // 此次购买数量
+                $limitKey = \bizHd\product\classes\ProductClass::LIMIT_BUY_KEY . $productId;
+                $hasBuyNum = Yii::$app->redis->executeCommand('HGET', [$limitKey, $hdCustomId]);
+                $hasBuyNum = $num + ($hasBuyNum == null ? 0 : $hasBuyNum); // 已购买总数量
+                $price = \bizGhs\product\classes\ProductClass::getFinalPrice($product, $level, $priceMap, $addPriceMap, 0, $num, $hasBuyNum);
                 $currentTotal = bcmul($price, $num, 2);
                 $modifyPrice = bcadd($modifyPrice, $currentTotal, 2);
                 $productList[$eleKey]['unitPrice'] = $price;

+ 20 - 7
biz-ghs/product/classes/ProductClass.php

@@ -767,9 +767,19 @@ class ProductClass extends BaseClass
         return $list;
     }
 
-    //输出价格 ssh 20211009
-    //$params 原来是 $addPriceMap,现在 $addPriceMap 值没用了,把它改成 $params ,用作传别的参数
-    public static function getFinalPrice($product, $level, $priceMap, $params, $changePrice = 0, $buyNum = 0)
+    /**
+     * 输出价格 ssh 20211009
+     * @param $product 花材信息
+     * @param $level 等级
+     * @param $priceMap 各个等级对应的price addPrice字段
+     * @param $params 原来是 $addPriceMap,现在 $addPriceMap 值没用了,把它改成 $params ,用作传别的参数
+     * @param int $changePrice 开单改价
+     * @param int $buyNum 当前购买数量
+     * @param int $totalBuyNum 已购买总数量
+     * @return int|string|null
+     * @throws \Exception
+     */
+    public static function getFinalPrice($product, $level, $priceMap, $params, $changePrice = 0, $buyNum = 0, $totalBuyNum = 0)
     {
         //开单改价优先级最高
         if ($changePrice > 0) {
@@ -808,10 +818,13 @@ class ProductClass extends BaseClass
             }
 
             //超出限购情况的价格计算
-            if ($limitBuy > 0 && $buyNum > $limitBuy) {
-                $allPrice = $price * $limitBuy;
-                $allPrice = $allPrice + (($buyNum - $limitBuy) * $oldPrice);
-                $price = bcdiv($allPrice, $buyNum, 2);
+            if ($limitBuy > 0 && $totalBuyNum > $limitBuy) {
+                $overBuyNum = $totalBuyNum - $limitBuy;
+                $overBuyPrice = bcmul($overBuyNum, $oldPrice, 2); // 超出限购部分的价格
+
+                $leftBuyNum = $buyNum - $overBuyNum;
+                $leftBuyPrice = bcmul($leftBuyNum, $price, 2); // 未超出限购部分的价格
+                $price = bcdiv($overBuyPrice + $leftBuyPrice, $buyNum, 2); // 最终价格
             }
         }
 

+ 5 - 1
biz-hd/purchase/classes/PurchaseClass.php

@@ -821,7 +821,11 @@ class PurchaseClass extends BaseClass
             $stock = $hdProductData[$hdProductId]['stock'] ?? 0;
             $stockFormat = ProductClass::formatStock($stock, $ratio);
 
+            $limitKey = ProductClass::LIMIT_BUY_KEY . $hdProductId;
+            $hasNum = Yii::$app->redis->executeCommand('HGET', [$limitKey, $customId]);
+            $hasBuyNum = $hasNum ?: 0;
             $itemNum = ProductClass::mergeItemNum($product['bigNum'], $product['smallNum'], $ratio);
+            $hasBuyNum = bcadd($itemNum, $hasBuyNum, 2);
 
             $weight = $hdProductData[$hdProductId]['weight'] ?? 0;
             $currentWeight = bcmul($weight, $itemNum, 2);
@@ -840,7 +844,7 @@ class PurchaseClass extends BaseClass
                     $price = 0.1;
                 } else {
                     //用批发店花材的价格
-                    $price = ProductClass::getFinalPrice($currentGhsProduct, $giveLevel, $priceMap, $addPriceMap, $userPrice, $itemNum);
+                    $price = ProductClass::getFinalPrice($currentGhsProduct, $giveLevel, $priceMap, $addPriceMap, $userPrice, $itemNum, $hasBuyNum);
                 }
                 $thisPrice = $price;