Browse Source

fix:商品状态

super-yimizi 2 months ago
parent
commit
720c7d4f62

+ 6 - 1
addons/shop/controller/Category.php

@@ -58,7 +58,12 @@ class Category extends Base
         $multiple = $this->request->get('multiple/d', 0);
         $multiple = $this->request->get('multiple/d', 0);
         $params = $filter;
         $params = $filter;
 
 
-        $goodsList = \addons\shop\model\Goods::where('category_id', 'in', \addons\shop\model\Category::getCategoryChildrenIds($category['id']))
+        $categoryIds = \addons\shop\model\Category::getCategoryChildrenIds($category['id']);
+        $goodsList = \addons\shop\model\Goods::where(function($query) use ($categoryIds) {
+            foreach ($categoryIds as $categoryId) {
+                $query->whereOr('category_ids', 'exp', "FIND_IN_SET('{$categoryId}', category_ids)");
+            }
+        })
             ->where(function ($query) use ($filter) {
             ->where(function ($query) use ($filter) {
                 //属性
                 //属性
                 $attributes = [];
                 $attributes = [];

+ 5 - 5
addons/shop/model/Category.php

@@ -240,9 +240,9 @@ class Category extends Model
      */
      */
     public static function getCategoryChildrenIds($ids, $withself = true)
     public static function getCategoryChildrenIds($ids, $withself = true)
     {
     {
-        $cacheName = 'shop-childrens-' . $ids . '-' . $withself;
-        $result = Cache::get($cacheName);
-        if ($result === false) {
+        // $cacheName = 'shop-childrens-' . $ids . '-' . $withself;
+        // $result = Cache::get($cacheName);
+        // if ($result === false) {
             $categoryList = Category::where('status', 'normal')
             $categoryList = Category::where('status', 'normal')
                 ->order('weigh desc,id desc')
                 ->order('weigh desc,id desc')
                 ->cache(true, null, 'shop')
                 ->cache(true, null, 'shop')
@@ -255,8 +255,8 @@ class Category extends Model
             foreach ($CategoryIds as $index => $CategoryId) {
             foreach ($CategoryIds as $index => $CategoryId) {
                 $result = array_merge($result, $tree->getChildrenIds($CategoryId, $withself));
                 $result = array_merge($result, $tree->getChildrenIds($CategoryId, $withself));
             }
             }
-            Cache::set($cacheName, $result);
-        }
+        //     Cache::set($cacheName, $result);
+        // }
         return $result;
         return $result;
     }
     }
 
 

+ 22 - 3
addons/shop/model/Goods.php

@@ -166,15 +166,26 @@ class Goods extends Model
 
 
         $goodsModel = self::with($with)->alias('a');
         $goodsModel = self::with($with)->alias('a');
         if ($category !== '') {
         if ($category !== '') {
+            $categoryIds = [];
             if ($type === 'son') {
             if ($type === 'son') {
                 $subQuery = Category::where('parent_id', 'in', $category)->field('id')->buildSql();
                 $subQuery = Category::where('parent_id', 'in', $category)->field('id')->buildSql();
                 //子级
                 //子级
-                $where['category_id'] = ['exp', Db::raw(' IN ' . '(' . $subQuery . ')')];
+                $categoryIds = Db::query($subQuery);
+                $categoryIds = array_column($categoryIds, 'id');
             } elseif ($type === 'sons') {
             } elseif ($type === 'sons') {
                 //所有子级
                 //所有子级
-                $where['category_id'] = ['in', Category::getCategoryChildrenIds($category)];
+                $categoryIds = Category::getCategoryChildrenIds($category);
             } else {
             } else {
-                $where['category_id'] = ['in', $category];
+                $categoryIds = is_array($category) ? $category : explode(',', $category);
+            }
+            
+            if (!empty($categoryIds)) {
+                $condition .= ($condition ? ' AND ' : '') . '(';
+                $conditionParts = [];
+                foreach ($categoryIds as $categoryId) {
+                    $conditionParts[] = "FIND_IN_SET('{$categoryId}', category_ids)";
+                }
+                $condition .= implode(' OR ', $conditionParts) . ')';
             }
             }
         }
         }
         //如果有设置标志,则拆分标志信息并构造condition条件
         //如果有设置标志,则拆分标志信息并构造condition条件
@@ -230,8 +241,16 @@ class Goods extends Model
 
 
     public function Category()
     public function Category()
     {
     {
+        // 由于现在使用category_ids字段,这里返回主分类(第一个分类)
         return $this->belongsTo('Category', 'category_id', 'id');
         return $this->belongsTo('Category', 'category_id', 'id');
     }
     }
+    
+    public function Categories()
+    {
+        // 新增:获取商品的所有分类
+        $categoryIds = explode(',', $this->category_ids ?: '');
+        return Category::where('id', 'in', array_filter($categoryIds))->select();
+    }
 
 
     public function Sku()
     public function Sku()
     {
     {

+ 10 - 4
application/api/controller/Goods.php

@@ -2,6 +2,7 @@
 
 
 namespace app\api\controller;
 namespace app\api\controller;
 
 
+use app\common\Enum\GoodsEnum;
 use app\common\model\Goods as GoodsModel;
 use app\common\model\Goods as GoodsModel;
 use app\common\model\Guarantee;
 use app\common\model\Guarantee;
 use app\common\model\Collect;
 use app\common\model\Collect;
@@ -65,7 +66,7 @@ class Goods extends Base
                     }
                     }
                 ])->order('createtime', 'desc')->limit(10);
                 ])->order('createtime', 'desc')->limit(10);
             }
             }
-        ])->where('status', '<>', 'hidden')->where('id', $id)->find();
+        ])->whereIn('status',[GoodsEnum::STATUS_ON_SALE,GoodsEnum::STATUS_SOLD_OUT])->where('id', $id)->find();
 
 
         if (!$row) {
         if (!$row) {
             $this->error('未找到该商品');
             $this->error('未找到该商品');
@@ -134,7 +135,7 @@ class Goods extends Base
         // }
         // }
         // $row->coupon = $couponList;
         // $row->coupon = $couponList;
 
 
-        $row->visible(explode(',', 'id,title,subtitle,category_id,price,marketprice,sales,views,
+        $row->visible(explode(',', 'id,title,subtitle,category_ids,price,marketprice,sales,views,
         image,content,images,sku_spec,sku,comment,is_collect,guarantee,attributes,favor_rate,coupon'));
         image,content,images,sku_spec,sku,comment,is_collect,guarantee,attributes,favor_rate,coupon'));
         $row = $row->toArray();
         $row = $row->toArray();
         $row['content'] = \app\common\library\Service::formatTplToUniapp($row['content']);
         $row['content'] = \app\common\library\Service::formatTplToUniapp($row['content']);
@@ -150,7 +151,7 @@ class Goods extends Base
         $orderway = $this->request->param('orderway', 'desc');
         $orderway = $this->request->param('orderway', 'desc');
 
 
         $list = GoodsModel::where(function ($query) use ($param) {
         $list = GoodsModel::where(function ($query) use ($param) {
-            $query->where('status', 'normal');
+            $query->where('status',GoodsEnum::STATUS_ON_SALE);
             //关键词
             //关键词
             if (isset($param['keyword']) && !empty($param['keyword'])) {
             if (isset($param['keyword']) && !empty($param['keyword'])) {
                 $query->where('title|keywords', 'like', '%' . $param['keyword'] . '%');
                 $query->where('title|keywords', 'like', '%' . $param['keyword'] . '%');
@@ -163,7 +164,12 @@ class Goods extends Base
             }
             }
             //分类
             //分类
             if (isset($param['category_id']) && !empty($param['category_id'])) {
             if (isset($param['category_id']) && !empty($param['category_id'])) {
-                $query->where('category_id', 'IN', \addons\shop\model\Category::getCategoryChildrenIds($param['category_id']));
+                $categoryIds = \addons\shop\model\Category::getCategoryChildrenIds($param['category_id']);
+                $query->where(function($q) use ($categoryIds) {
+                    foreach ($categoryIds as $categoryId) {
+                        $q->whereOr('category_ids', 'exp', "FIND_IN_SET('{$categoryId}', category_ids)");
+                    }
+                });
             }
             }
             //属性
             //属性
             if (isset($param['attributes']) && !empty($param['attributes'])) {
             if (isset($param['attributes']) && !empty($param['attributes'])) {

+ 22 - 3
application/common/model/Goods.php

@@ -166,15 +166,26 @@ class Goods extends Model
 
 
         $goodsModel = self::with($with)->alias('a');
         $goodsModel = self::with($with)->alias('a');
         if ($category !== '') {
         if ($category !== '') {
+            $categoryIds = [];
             if ($type === 'son') {
             if ($type === 'son') {
                 $subQuery = Category::where('parent_id', 'in', $category)->field('id')->buildSql();
                 $subQuery = Category::where('parent_id', 'in', $category)->field('id')->buildSql();
                 //子级
                 //子级
-                $where['category_id'] = ['exp', Db::raw(' IN ' . '(' . $subQuery . ')')];
+                $categoryIds = Db::query($subQuery);
+                $categoryIds = array_column($categoryIds, 'id');
             } elseif ($type === 'sons') {
             } elseif ($type === 'sons') {
                 //所有子级
                 //所有子级
-                $where['category_id'] = ['in', Category::getCategoryChildrenIds($category)];
+                $categoryIds = Category::getCategoryChildrenIds($category);
             } else {
             } else {
-                $where['category_id'] = ['in', $category];
+                $categoryIds = is_array($category) ? $category : explode(',', $category);
+            }
+            
+            if (!empty($categoryIds)) {
+                $condition .= ($condition ? ' AND ' : '') . '(';
+                $conditionParts = [];
+                foreach ($categoryIds as $categoryId) {
+                    $conditionParts[] = "FIND_IN_SET('{$categoryId}', category_ids)";
+                }
+                $condition .= implode(' OR ', $conditionParts) . ')';
             }
             }
         }
         }
         //如果有设置标志,则拆分标志信息并构造condition条件
         //如果有设置标志,则拆分标志信息并构造condition条件
@@ -230,8 +241,16 @@ class Goods extends Model
 
 
     public function Category()
     public function Category()
     {
     {
+        // 由于现在使用category_ids字段,这里返回主分类(第一个分类)
         return $this->belongsTo('Category', 'category_id', 'id');
         return $this->belongsTo('Category', 'category_id', 'id');
     }
     }
+    
+    public function Categories()
+    {
+        // 新增:获取商品的所有分类
+        $categoryIds = explode(',', $this->category_ids ?: '');
+        return Category::where('id', 'in', array_filter($categoryIds))->select();
+    }
 
 
     public function Sku()
     public function Sku()
     {
     {