Преглед изворни кода

批量组装商品基本信息 -- groupGoodsBaseInfo 的优化版

shizhongqi пре 1 година
родитељ
комит
8dabf241b9

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

@@ -355,7 +355,10 @@ class GoodsClass extends BaseClass
         
         //注意这里涉及商家节日涨价的问题,不要去取不同商家的商品,确有必要另外起方法!!!!!!!!!!
         $currentData = current($list);
-        $mainId = $currentData['mainId'] ?? 0;
+        $mainId = intval($currentData['mainId']);
+        if ($mainId <= 0) {
+            util::fail('mainId 出错');
+        }
         $rise = GoodsSettingClass::getRise($mainId);
         $sjHasCategory = CategoryClass::getCategory($mainId);
         
@@ -439,6 +442,119 @@ class GoodsClass extends BaseClass
         return $list;
     }
 
+    /**
+     * 批量组装商品基本信息 (优化版)
+     * @param array $list goods商品数组
+     * @param bool $getItem 是否获取"成品的花材"
+     * @param array $sortIds 按商品分类的inTurn 进行排序的商品id数组
+     * @return array
+     */
+    public static function groupGoodsBaseInfoBatch($list, $getItem = false, $sortIds = [])
+    {
+        if (empty($list)) {
+            return [];
+        }
+
+        //注意这里涉及商家节日涨价的问题,不要去取不同商家的商品,确有必要另外起方法!!!!!!!!!!
+        $currentData = current($list);
+        $mainId = intval($currentData['mainId']);
+        if ($mainId <= 0) {
+            // 如果列表为空或者第一个商品没有mainId,则直接返回,避免错误
+            return $list;
+        }
+
+        // 1. 一次性获取所有需要的外部数据
+        $rise = GoodsSettingClass::getRise($mainId);
+        $sjHasCategory = CategoryClass::getCategory($mainId);
+        $goodsIds = array_column($list, 'id');
+        $allGoodsCategories = GoodsCategoryClass::getGoodsHasCategoryIdsBatch($goodsIds);
+
+        $groupedItems = [];
+        if ($getItem && !empty($goodsIds)) {
+            $itemList = GoodsItemClass::getAllByCondition(['goodsId' => ['in', $goodsIds]]);
+            // 手动按 goodsId 分组
+            foreach ($itemList as $item) {
+                $groupedItems[$item['goodsId']][] = $item;
+            }
+        }
+
+        $arrNotEmpty = is_array($sortIds) && !empty($sortIds);
+
+        // 2. 循环组装数据
+        foreach ($list as &$val) {
+            $id = $val['id'];
+            //大中小图片转化
+            $val = business::formatGoodsImg($val);
+            //免费配送范围
+            $val['freeSendDist'] = 5;
+            $val['createDate'] = isset($val['createTime']) ? date("Y-m-d", strtotime($val['createTime'])) : '';
+
+            //节日涨价
+            if (!empty($rise)) {
+                $price = $val['price'];
+                if ($rise['riseType'] == 0) {
+                    //百分比
+                    $val['price'] = $price + sprintf("%.1f", ($price * $rise['riseAmount'] / 100));
+                } else {
+                    //金额
+                    $val['price'] = $price + $rise['riseAmount'];
+                }
+            }
+
+            //商品的分类
+            $categoryIds = $allGoodsCategories[$id] ?? [];
+            $val['categoryIdList'] = $categoryIds;
+            $belongCategory = [];
+            if (!empty($categoryIds)) {
+                foreach ($categoryIds as $currentId) {
+                    $belongCategory[] = $sjHasCategory[$currentId]['categoryName'] ?? '';
+                }
+            }
+            $val['belongCategory'] = $belongCategory;
+
+            $shortCover = $val['cover'] ?? '';
+            // 兼容 cover 保存的是数字(表示第几张图片)
+            if (intval($shortCover) > 0) {
+                $shopImg = isset($val['shortImgList']) ? $val['shortImgList'] : [];
+                $idx = intval($shortCover) - 1;
+                if (isset($shopImg[$idx])) {
+                    $shortCover = $shopImg[$idx];
+                }
+            }
+            $val['shortCover'] = $shortCover;
+            $val['cover'] = imgUtil::groupImg($shortCover);
+            $val['smallCover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
+            $val['bigCover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
+
+            if ($getItem) {
+                $currentItems = $groupedItems[$id] ?? [];
+                foreach ($currentItems as &$itemVal) {
+                    $itemShortCover = $itemVal['cover'] ?? '';
+                    $itemVal['shortCover'] = $itemShortCover;
+                    $itemVal['smallCover'] = imgUtil::groupImg($itemShortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
+                    $itemVal['bigCover'] = imgUtil::groupImg($itemShortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
+                }
+                $val['goodsItem'] = $currentItems;
+            }
+            //前端使用字段
+            $val['bigCount'] = 0;
+            if ($arrNotEmpty) {
+                $sortIndex = array_search($id, $sortIds);
+                $val['sortWeight'] = $sortIndex !== false ? $sortIndex : count($sortIds);
+            }
+        }
+        unset($val); // 销毁引用
+
+        if ($arrNotEmpty) {
+            usort($list, function($a, $b) {
+                return $a['sortWeight'] - $b['sortWeight'];
+            });
+        }
+
+        return $list;
+    }
+
+
     //新建商品 ssh 20220404
     public static function addGoods($data, $work = null)
     {

+ 0 - 5
biz-hd/goods/classes/GoodsSettingClass.php

@@ -2,14 +2,9 @@
 
 namespace bizHd\goods\classes;
 
-use bizHd\goods\services\GoodsCategoryService;
-use bizHd\goods\services\GoodsStyleService;
 use bizHd\saas\classes\FestClass;
 use bizHd\saas\classes\FestRiseClass;
-use common\components\business;
-use common\components\stringUtil;
 use common\components\util;
-use Yii;
 use bizHd\base\classes\BaseClass;
 
 class GoodsSettingClass extends BaseClass

+ 22 - 7
biz-hd/goods/services/GoodsService.php

@@ -164,19 +164,34 @@ class GoodsService extends BaseService
         // 获取数据
         $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);
+        //         }
+        //     }
+        // }
+
+
         // 整理数据
-        $goodsList = [];
+        $goodsToProcess = [];
         foreach ($list as $goodsCategory) {
             if ($goodsCategory->goods) {
-                $goodsData = $goodsCategory->goods->toArray();
-                // 使用现有的方法处理商品信息
-                $processedGoods = GoodsClass::groupGoodsBaseInfo([$goodsData]);
-                if (!empty($processedGoods)) {
-                    $goodsList[] = current($processedGoods);
-                }
+                $goodsToProcess[] = $goodsCategory->goods->toArray();
             }
         }
 
+        $goodsList = [];
+        if (!empty($goodsToProcess)) {
+            // 使用批量处理方法
+            $goodsList = GoodsClass::groupGoodsBaseInfoBatch($goodsToProcess);
+        }
+
         $totalPage = ceil($total / $pageSize);
         $moreData = $totalPage > $page ? 1 : 0;//是否还有更多数据
         return [