소스 검색

花束绿植模块优化

shish 1 년 전
부모
커밋
1847149b64

+ 1 - 1
app-hd/controllers/CategoryController.php

@@ -14,7 +14,7 @@ class CategoryController extends BaseController
     public function actionGetAllCategory()
     {
         $where = ['mainId' => $this->mainId, 'delStatus' => 0];
-        $list = CategoryClass::getAllByCondition($where, null, '*');
+        $list = CategoryClass::getAllByCondition($where, 'inTurn desc', '*');
         util::success(['list' => $list]);
     }
 

+ 17 - 1
app-hd/controllers/GoodsController.php

@@ -95,7 +95,7 @@ class GoodsController extends BaseController
             $respond = GoodsClass::cgPlant($post, $this->shop);
             $transaction->commit();
             util::success($respond);
-        } catch (Exception $e) {
+        } catch (\Exception $e) {
             $transaction->rollBack();
             util::fail();
         }
@@ -136,6 +136,22 @@ class GoodsController extends BaseController
         util::success($data);
     }
 
+    public function actionGetGoodsInfoList()
+    {
+        $get = Yii::$app->request->get();
+        $cId = isset($get['cId']) ? $get['cId'] : 0;
+        $flowerNum = isset($get['flowerNum']) ? $get['flowerNum'] : 0;
+        if(empty($cId)){
+            util::fail('分类不能为空');
+        }
+        $where = ['cId' => $cId];
+        if(!empty($flowerNum)){
+            $params['flowerNum'] = $flowerNum;
+        }
+        $data = GoodsClass::getGoodsData($where,$params);
+        util::success($data);
+    }
+
     //获取商品详情 ssh 2019.12.7
     public function actionDetail()
     {

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

@@ -17,4 +17,27 @@ class GoodsCategoryClass extends BaseClass
 		self::deleteByCondition(['gId'=>$goodsId]);
 	}
 
+	//批量获取多个商品的分类ID ssh 2023
+	public static function getGoodsHasCategoryIdsBatch($goodsIds)
+	{
+		if (empty($goodsIds)) {
+			return [];
+		}
+		
+		// 一次性查询所有商品的分类关系
+		$categoryList = self::getAllByCondition(['gId' => ['in', $goodsIds]], null, 'gId,cId');
+		
+		// 按商品ID分组整理结果
+		$result = [];
+		foreach ($categoryList as $item) {
+			$goodsId = $item['gId'];
+			$categoryId = $item['cId'];
+			if (!isset($result[$goodsId])) {
+				$result[$goodsId] = [];
+			}
+			$result[$goodsId][] = $categoryId;
+		}
+		
+		return $result;
+	}
 }

+ 51 - 1
biz-hd/goods/classes/GoodsClass.php

@@ -19,6 +19,7 @@ use common\components\stringUtil;
 use common\components\util;
 use Yii;
 use bizHd\base\classes\BaseClass;
+use bizHd\goods\models\GoodsCategory;
 
 class GoodsClass extends BaseClass
 {
@@ -292,6 +293,49 @@ class GoodsClass extends BaseClass
         return $info;
     }
 
+    public static function getGoodsData($where,$params){
+        // 获取分页参数
+        $page = Yii::$app->request->get('page', 1);
+        $pageSize = Yii::$app->request->get('pageSize', 20);
+        
+        $flowerNum = $params['flowerNum'] ?? 0;
+        // 查询某分类下有库存的商品
+        $query = GoodsCategory::find()
+            ->where($where)
+            ->joinWith(['goods' => function($query) use ($flowerNum) {
+                $query->andWhere(['delStatus' => 0]);
+                $query->andWhere(['>', 'stock', 0]);
+                if(isset($flowerNum) && $flowerNum > 0){
+                    $query->andWhere(['flowerNum' => $flowerNum]); 
+                }
+            }]);    
+            
+        // 获取总数
+        $total = $query->count();
+        
+        // 分页查询
+        $goodsList = $query->offset(($page - 1) * $pageSize)
+            ->limit($pageSize)
+            ->all();
+        
+        // 整理结果,只要有库存的商品
+        $list = [];
+        foreach ($goodsList as $goodsCategory) {
+            if ($goodsCategory->goods) { // 只要有关联且有库存的商品
+                $list[] = $goodsCategory->goods->attributes;
+            }
+        }
+        if(!empty($list)){
+            $list = self::groupGoodsBaseInfo($list);
+        }
+        return [
+            'list' => $list,
+            'total' => $total,
+            'page' => $page,
+            'pageSize' => $pageSize
+        ];
+    }
+
     //组装商品基本信息 ssh 2019.12.2
     public static function groupGoodsBaseInfo($list, $getItem = false)
     {
@@ -300,6 +344,12 @@ class GoodsClass extends BaseClass
         $mainId = $currentData['mainId'] ?? 0;
         $rise = GoodsSettingClass::getRise($mainId);
         $sjHasCategory = CategoryClass::getCategory($mainId);
+        
+        // 收集所有商品ID
+        $goodsIds = array_column($list, 'id');
+        // 一次性获取所有商品的分类关系
+        $allGoodsCategories = GoodsCategoryClass::getGoodsHasCategoryIdsBatch($goodsIds);
+        
         foreach ($list as $key => $val) {
             $id = $val['id'];
             //大中小图片转化
@@ -319,7 +369,7 @@ class GoodsClass extends BaseClass
                 }
             }
             //商品的分类
-            $categoryIds = GoodsCategoryService::getGoodsHasCategoryIds($id);
+            $categoryIds = $allGoodsCategories[$id] ?? [];
             $val['categoryIdList'] = $categoryIds;
             $belongCategory = [];
             if (!empty($categoryIds)) {

+ 87 - 13
biz-hd/goods/services/GoodsService.php

@@ -9,6 +9,7 @@ use bizHd\goods\classes\GoodsCategoryClass;
 use bizHd\goods\classes\GoodsClass;
 use bizHd\goods\classes\GoodsItemClass;
 use bizHd\goods\models\GoodsCategory;
+use bizHd\goods\models\Goods;
 use common\components\util;
 use Yii;
 use linslin\yii2\curl;
@@ -21,25 +22,21 @@ class GoodsService extends BaseService
     //后台列表 ssh 2019.12.7
     public static function getGoodsList($where)
     {
+        $page = Yii::$app->request->get('page', 1);
+        $pageSize = Yii::$app->request->get('pageSize', 20);
+
         if (isset($where['cId'])) {
-            $data = GoodsCategoryClass::getList('*', $where, 'inTurn DESC', 'goods');
-            $list = [];
-            if (isset($data['list']) && !empty($data['list'])) {
-                foreach ($data['list'] as $val) {
-                    if (isset($val['goods']) && !empty($val['goods'])) {
-                        $list[] = $val['goods'];
-                    }
-                }
-            }
+            // 使用新的分类商品查询方法
+            return self::getCategoryGoodsList($where, $page, $pageSize);
         } else {
             $data = GoodsClass::getList('*', $where, 'inTurn DESC,updateTime DESC');
             $list = isset($data['list']) && !empty($data['list']) ? $data['list'] : [];
-        }
-        if (empty($list)) {
+            if (empty($list)) {
+                return $data;
+            }
+            $data['list'] = GoodsClass::groupGoodsBaseInfo($list);
             return $data;
         }
-        $data['list'] = GoodsClass::groupGoodsBaseInfo($list);
-        return $data;
     }
 
     //获取商品完整详情,包括分类 ssh 2019.12.3
@@ -116,4 +113,81 @@ class GoodsService extends BaseService
         return true;
     }
 
+    /**
+     * 获取分类下的商品列表,支持分页和库存筛选
+     * @param array $where 查询条件
+     * @param int $page 页码
+     * @param int $pageSize 每页数量
+     * @return array
+     */
+    public static function getCategoryGoodsList($where = [], $page = 1, $pageSize = 20)
+    {
+        // 构建基础查询
+        $query = GoodsCategory::find()
+            ->joinWith(['goods' => function($query) use ($where) {
+                // 商品表的筛选条件
+                if (isset($where['mainId'])) {
+                    $query->andWhere(['goods.mainId' => $where['mainId']]);
+                }
+                if (isset($where['delStatus'])) {
+                    $query->andWhere(['goods.delStatus' => $where['delStatus']]);
+                }
+                if (isset($where['status'])) {
+                    $query->andWhere(['goods.status' => $where['status']]);
+                }
+                if (isset($where['name'])) {
+                    $query->andWhere(['like', 'goods.name', $where['name']]);
+                }
+                // 如果需要筛选有库存的商品
+                if (isset($where['hasStock']) && $where['hasStock'] == 1) {
+                    $query->andWhere(['>', 'goods.stock', 0]);
+                }
+            }]);
+
+        // 分类表的筛选条件
+        if (isset($where['cId'])) {
+            $query->andWhere(['goodsCategory.cId' => $where['cId']]);
+        }
+        if (isset($where['flower'])) {
+            $query->andWhere(['goodsCategory.flower' => $where['flower']]);
+        }
+        if (isset($where['flowerNum'])) {
+            $query->andWhere(['goodsCategory.flowerNum' => $where['flowerNum']]);
+        }
+
+        // 计算总数
+        $total = $query->count();
+
+        // 分页
+        $offset = ($page - 1) * $pageSize;
+        $query->offset($offset)->limit($pageSize);
+
+        // 排序
+        $query->orderBy(['goodsCategory.inTurn' => SORT_DESC]);
+
+        // 获取数据
+        $list = $query->all();
+
+        // 整理数据
+        $goodsList = [];
+        foreach ($list as $goodsCategory) {
+            if ($goodsCategory->goods) {
+                $goodsData = $goodsCategory->goods->toArray();
+                // 使用现有的方法处理商品信息
+                $processedGoods = GoodsClass::groupGoodsBaseInfo([$goodsData]);
+                if (!empty($processedGoods)) {
+                    $goodsList[] = current($processedGoods);
+                }
+            }
+        }
+
+        return [
+            'list' => $goodsList,
+            'total' => $total,
+            'page' => $page,
+            'pageSize' => $pageSize,
+            'totalPage' => ceil($total / $pageSize)
+        ];
+    }
+
 }