Browse Source

fix:商品

super-yimizi 2 months ago
parent
commit
1bd0dad5b9
1 changed files with 11 additions and 8 deletions
  1. 11 8
      application/api/controller/Goods.php

+ 11 - 8
application/api/controller/Goods.php

@@ -165,15 +165,18 @@ class Goods extends Base
             }
             //分类
             if (isset($param['category_id']) && !empty($param['category_id'])) {
-                $categoryIds = \app\common\model\Category::getCategoryChildrenIds($param['category_id']);
-                $conditions = [];
-                foreach ($categoryIds as $categoryId) {
-                    $conditions[] = "FIND_IN_SET('{$categoryId}', `category_ids`)";
-                }
-                if (!empty($conditions)) {
-                    $query->whereRaw('(' . implode(' OR ', $conditions) . ')');
-                }
+                $categoryIds = [];
+                // 获取子集
+                $categoryChildIds = \app\common\model\Category::getCategoryChildrenIds($param['category_id']);
+                $categoryIds = array_merge($categoryChildIds, [$param['category_id']]);
+                $query->where(function ($query) use ($categoryIds) {
+                    // 所有子分类使用 find_in_set or 匹配,亲测速度并不慢
+                    foreach ($categoryIds as $key => $category_id) {
+                        $query->whereOrRaw("find_in_set($category_id, category_ids)");
+                    }
+                });                
             }
+ 
             //属性
             if (isset($param['attributes']) && !empty($param['attributes'])) {
                 $query->where('id', 'IN', \app\common\model\GoodsAttr::getGoodsIds($param['attributes']));