Quellcode durchsuchen

feat(mall-category): 支持商品列表多维筛选

- 新增商城场景列表接口,并在商品列表响应中回填场景 ID 与名称

- 支持按多分类、场景和价格区间筛选商品列表

API: app-mall/category/use-case-list 新增场景列表接口;category/goods-list 新增 categoryIds、useCaseIds、minPrice、maxPrice 筛选参数并增加 useCaseIdList、useCaseNames 响应字段
shizhongqi vor 1 Monat
Ursprung
Commit
e19a48001e

+ 91 - 1
app-mall/controllers/CategoryController.php

@@ -3,6 +3,8 @@
 namespace mall\controllers;
 
 use bizHd\custom\classes\CustomClass;
+use bizHd\goods\classes\GoodsUseCaseClass;
+use bizHd\goods\classes\UseCaseClass;
 use bizMall\goods\services\CategoryService;
 use Yii;
 use common\components\util;
@@ -10,7 +12,7 @@ use common\components\util;
 class CategoryController extends BaseController
 {
 
-    public $guestAccess = ['list']; // 'goods-list' ---- 为什么不用认证
+    public $guestAccess = ['list', 'use-case-list']; // 'goods-list' ---- 为什么不用认证
 
     //获取分类 ssh 2019.12.2
     public function actionList()
@@ -62,7 +64,95 @@ class CategoryController extends BaseController
         $params = [];
         //调用花店端的方法,后面再分离 (为了代码维护可以不分离,免得一处改了,另一处没修改到。--- 所以分离好后,又还原了)
         $data = \bizHd\goods\classes\GoodsCategoryClass::getGoodsList($get, $shop, $custom, $params);
+        $data['list'] = $this->appendUseCaseInfo($data['list'] ?? []);
         util::success($data);
     }
 
+    /**
+     * 场景列表(商城筛选弹窗用)
+     * 游客可访问,未登录返回空数组
+     */
+    public function actionUseCaseList()
+    {
+        $user = $this->user;
+        if (empty($user)) {
+            util::success([]);
+        }
+        $mainId = intval($this->mainId ?? 0);
+        if ($mainId <= 0) {
+            util::success([]);
+        }
+        $list = UseCaseClass::getAllByCondition(
+            ['mainId' => $mainId, 'status' => 1, 'delStatus' => 0],
+            'inTurn DESC',
+            'id,useCaseName'
+        );
+        util::success($list ?: []);
+    }
+
+    /**
+     * 批量为商品列表补充场景 ID 与名称,供列表页标签展示
+     *
+     * @param array $list
+     * @return array
+     */
+    private function appendUseCaseInfo($list)
+    {
+        if (empty($list)) {
+            return [];
+        }
+        $goodsIds = array_column($list, 'id');
+        if (empty($goodsIds)) {
+            return $list;
+        }
+
+        $relations = GoodsUseCaseClass::getAllByCondition(
+            ['goodsId' => ['in', $goodsIds]],
+            null,
+            'goodsId,useCaseId'
+        );
+        $useCaseIds = array_unique(array_column($relations ?: [], 'useCaseId'));
+        $nameMap = [];
+        if (!empty($useCaseIds)) {
+            $useCases = UseCaseClass::getAllByCondition(
+                [
+                    'id' => ['in', $useCaseIds],
+                    'mainId' => intval($this->mainId ?? 0),
+                    'delStatus' => 0,
+                ],
+                null,
+                'id,useCaseName'
+            );
+            foreach ($useCases ?: [] as $uc) {
+                $nameMap[intval($uc['id'])] = $uc['useCaseName'] ?? '';
+            }
+        }
+
+        $goodsUseCaseMap = [];
+        foreach ($relations ?: [] as $rel) {
+            $gid = intval($rel['goodsId']);
+            $uid = intval($rel['useCaseId']);
+            if (!isset($goodsUseCaseMap[$gid])) {
+                $goodsUseCaseMap[$gid] = [];
+            }
+            if (!isset($nameMap[$uid])) {
+                continue;
+            }
+            $goodsUseCaseMap[$gid][] = [
+                'id' => $uid,
+                'name' => $nameMap[$uid],
+            ];
+        }
+
+        foreach ($list as &$item) {
+            $gid = intval($item['id'] ?? 0);
+            $cases = $goodsUseCaseMap[$gid] ?? [];
+            $item['useCaseIdList'] = array_column($cases, 'id');
+            $item['useCaseNames'] = array_values(array_filter(array_column($cases, 'name')));
+        }
+        unset($item);
+
+        return $list;
+    }
+
 }

+ 28 - 0
biz-hd/goods/classes/GoodsCategoryClass.php

@@ -39,6 +39,13 @@ class GoodsCategoryClass extends BaseClass
             }
             $query->andWhere(['xhGoodsCategory.cId' => $cId]);
         }
+        // 多分类筛选(逗号分隔),与单个 categoryId 并存
+        if (!empty($askInfo['categoryIds'])) {
+            $categoryIds = array_filter(array_map('intval', explode(',', strval($askInfo['categoryIds']))));
+            if (!empty($categoryIds)) {
+                $query->andWhere(['xhGoodsCategory.cId' => $categoryIds]);
+            }
+        }
         if (!empty($askInfo['searchText'])) {
             if($askInfo['searchType'] == 1){
                 $query->andWhere(['xhGoods.sn' => $askInfo['searchText']]);
@@ -89,6 +96,27 @@ class GoodsCategoryClass extends BaseClass
             } elseif ($requestType == 'album') {
                 util::fail('相册已经不存在');
             }
+
+            // 场景筛选(逗号分隔的 useCaseId)
+            if (!empty($askInfo['useCaseIds'])) {
+                $useCaseIds = array_filter(array_map('intval', explode(',', strval($askInfo['useCaseIds']))));
+                if (!empty($useCaseIds)) {
+                    $query->andWhere([
+                        'xhGoods.id' => (new \yii\db\Query())
+                            ->select('goodsId')
+                            ->from('xhGoodsUseCaseRelation')
+                            ->where(['useCaseId' => $useCaseIds]),
+                    ]);
+                }
+            }
+
+            // 价格区间筛选(按商品基础价 xhGoods.price)
+            if (isset($askInfo['minPrice']) && $askInfo['minPrice'] !== '' && is_numeric($askInfo['minPrice'])) {
+                $query->andWhere(['>=', 'xhGoods.price', floatval($askInfo['minPrice'])]);
+            }
+            if (isset($askInfo['maxPrice']) && $askInfo['maxPrice'] !== '' && is_numeric($askInfo['maxPrice'])) {
+                $query->andWhere(['<=', 'xhGoods.price', floatval($askInfo['maxPrice'])]);
+            }
         }]);