소스 검색

app-mall 新增接口 app-mall/order/limit-buy-info:零售商的限购花材信息

shizhongqi 2 달 전
부모
커밋
4e5847c7e2
2개의 변경된 파일107개의 추가작업 그리고 0개의 파일을 삭제
  1. 25 0
      app-mall/controllers/OrderController.php
  2. 82 0
      biz-hd/product/classes/ProductClass.php

+ 25 - 0
app-mall/controllers/OrderController.php

@@ -162,6 +162,31 @@ class OrderController extends BaseController
         util::success($respond);
     }
 
+    // 零售商的限购花材信息(与批发端 PurchaseController::actionLimitBuyInfo 对应,按花店客户与 hd_limit_buy Redis)
+    public function actionLimitBuyInfo()
+    {
+        $post = Yii::$app->request->post();
+        $list = $post['list'] ?? [];
+        if (empty($list) || !is_array($list)) {
+            util::fail('请选择花材');
+        }
+
+        $hd = $this->hd;
+        if (empty($hd)) {
+            util::fail('没有找到花店');
+        }
+        if ($hd->shopId != $this->shopId) {
+            util::fail('不是你的花店');
+        }
+        $hdCustomId = $hd->customId ?? 0;
+        if (empty($hdCustomId)) {
+            util::fail('没有找到客户');
+        }
+
+        $respond = \bizHd\product\classes\ProductClass::getLimitBuyInfoByList($list, $hdCustomId);
+        util::success($respond);
+    }
+
     //购买花材 ssh 20220511
     public function actionBuyItem()
     {

+ 82 - 0
biz-hd/product/classes/ProductClass.php

@@ -479,6 +479,88 @@ class ProductClass extends BaseClass
         }
     }
 
+    /**
+     * 获取零售客户当前清单中各花材的限购情况(Redis 键为 hd_limit_buy:,与批发商 limit_buy_ 区分)
+     * @param array $list 花材列表,项含 id 及 bigCount/smallCount 或 bigNum/smallNum
+     * @param int|string $customId 零售端 xhCustom.id(花店客户)
+     */
+    public static function getLimitBuyInfoByList($list, $customId)
+    {
+        if (empty($list) || !is_array($list)) {
+            return [];
+        }
+
+        $buyNumMap = [];
+        $ids = [];
+        foreach ($list as $item) {
+            $productId = $item['id'] ?? 0;
+            if (empty($productId)) {
+                continue;
+            }
+
+            $productId = intval($productId);
+            $bigNum = $item['bigCount'] ?? ($item['bigNum'] ?? 0);
+            $smallNum = $item['smallCount'] ?? ($item['smallNum'] ?? 0);
+            if (isset($buyNumMap[$productId]) == false) {
+                $buyNumMap[$productId] = [
+                    'bigNum' => 0,
+                    'smallNum' => 0,
+                ];
+            }
+            $buyNumMap[$productId]['bigNum'] = bcadd($buyNumMap[$productId]['bigNum'], $bigNum, 2);
+            $buyNumMap[$productId]['smallNum'] = bcadd($buyNumMap[$productId]['smallNum'], $smallNum, 2);
+            $ids[] = $productId;
+        }
+
+        $ids = array_values(array_unique($ids));
+        if (empty($ids)) {
+            return [];
+        }
+
+        $productData = \bizGhs\product\classes\ProductClass::getProductByIds($ids);
+        $productData = array_column($productData, null, 'id');
+        $respond = [];
+        foreach ($ids as $productId) {
+            $product = $productData[$productId] ?? [];
+            if (empty($product)) {
+                continue;
+            }
+
+            $limitBuy = $product['limitBuy'] ?? 0;
+            $isLimit = $limitBuy > 0 ? true : false;
+            $hasBuyNum = 0;
+            if ($isLimit) {
+                $limitKey = self::LIMIT_BUY_KEY . $productId;
+                $hasBuyNum = Yii::$app->redis->executeCommand('HGET', [$limitKey, $customId]);
+                $hasBuyNum = $hasBuyNum == null ? 0 : $hasBuyNum;
+            }
+
+            $buyNum = $buyNumMap[$productId] ?? [];
+            $ratio = $product['ratio'] ?? 1;
+            $currentBuyNum = \bizGhs\product\classes\ProductClass::mergeItemNum($buyNum['bigNum'] ?? 0, $buyNum['smallNum'] ?? 0, $ratio);
+            $totalBuyNum = bcadd($hasBuyNum, $currentBuyNum, 2);
+            $specialPrice = (
+                ($product['hjDiscountPrice'] ?? 0) > 0
+                && ($product['discountPrice'] ?? 0) > 0
+                && ($product['skDiscountPrice'] ?? 0) > 0
+            ) ? true : false;
+
+            $respond[] = [
+                'id' => $productId,
+                'name' => $product['name'] ?? '',
+                'isLimit' => $isLimit,
+                'limitBuy' => (float)$limitBuy,
+                'specialPrice' => $specialPrice,
+                'hasBuyNum' => $isLimit ? (float)$hasBuyNum : 0,
+                'currentBuyNum' => (float)$currentBuyNum,
+                'reachLimitBuyNum' => ($isLimit && $totalBuyNum > $limitBuy) ? true : false,
+                'exceedNum' => $isLimit ? (float)bcsub($hasBuyNum, $limitBuy, 2) : 0,
+            ];
+        }
+
+        return $respond;
+    }
+
     //取消限购的缓存
     public static function cancelLimitBuy($productId)
     {