Răsfoiți Sursa

1.解决花束商品排序问题 2.花束商品编辑接口中,对分类数据的优化处理

shizhongqi 1 an în urmă
părinte
comite
65bab9436f
2 a modificat fișierele cu 65 adăugiri și 38 ștergeri
  1. 1 1
      app-hd/controllers/GoodsController.php
  2. 64 37
      biz-hd/goods/classes/GoodsClass.php

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

@@ -181,7 +181,7 @@ class GoodsController extends BaseController
         
         $data = $form->attributes;
         $id = $data['id'];
-        $info = GoodsService::getById($id);
+        $info = GoodsClass::getById($id);
         GoodsService::valid($info, $this->mainId);
         $data['sjId'] = $this->sjId;
         $data['mainId'] = $this->mainId;

+ 64 - 37
biz-hd/goods/classes/GoodsClass.php

@@ -346,6 +346,7 @@ class GoodsClass extends BaseClass
     public static function groupGoodsBaseInfo($list, $getItem = false, $sortIds= [])
     {
         $newList = [];
+        $arrNotEmpty = is_array($sortIds) && !empty($sortIds);
         
         //注意这里涉及商家节日涨价的问题,不要去取不同商家的商品,确有必要另外起方法!!!!!!!!!!
         $currentData = current($list);
@@ -413,20 +414,21 @@ class GoodsClass extends BaseClass
             }
             //前端使用字段
             $val['bigCount'] = 0;
-            if (is_array($sortIds) && !empty($sortIds)) {
+            if ($arrNotEmpty) {
                 $sortIndex = array_search($id, $sortIds);
-                $newList[$sortIndex] = $val;
-                // $val['sortWeight'] = $sortIndex !== false ? $sortIndex : count($sortIds);
-            } else {
-                $list[$key] = $val;
+                // $newList[$sortIndex] = $val;
+                $val['sortWeight'] = $sortIndex !== false ? $sortIndex : count($sortIds);
             }
+
+            $list[$key] = $val; 
         }
 
-        if (is_array($sortIds) && !empty($sortIds)) {
-            // usort($list, function($a, $b) {
-            //     return ($a['sortWeight'] ?? PHP_INT_MAX) - ($b['sortWeight'] ?? PHP_INT_MAX);
-            // });
-            $list = $newList;
+        if ($arrNotEmpty) {
+            usort($list, function($a, $b) {
+                return $a['sortWeight'] - $b['sortWeight'];
+            });
+            //return $newList;
+            // return array_values($newList);
         }
 
         return $list;
@@ -567,11 +569,11 @@ class GoodsClass extends BaseClass
         return $info;
     }
 
-    //更新商品 ssh 2019.12.7
-    public static function updateGoods($info, $data, $staff)
+    //更新商品
+    public static function updateGoods($goods, $data, $staff)
     {
-        $id = $info['id'] ?? 0;
-        $preSetItemNum = $info['setItemNum'] ?? 0;
+        $id = $goods['id'] ?? 0;
+        $preSetItemNum = $goods['setItemNum'] ?? 0;
 
         $categoryIdList = isset($data['categoryIdList']) && !empty($data['categoryIdList']) ? $data['categoryIdList'] : [];
         unset($data['categoryIdList']);
@@ -594,30 +596,55 @@ class GoodsClass extends BaseClass
         if (!empty($diff)) {
             util::fail('不能使用别人的分类');
         }
-        //先删除再添加分类
+        //精确处理分类变更:分别处理删除、新增、修改三种情况
         $goodsHasCategoryIds = GoodsCategoryService::getGoodsHasCategoryIds($id);
-        CategoryClass::counters(['goodsNum' => -1], ['id' => $goodsHasCategoryIds]);
-        GoodsCategoryClass::delGoodsCategory($id);
-        $addCategory = [];
-        foreach ($categoryIdList as $categoryId) {
-            $cName = $categoryData[$categoryId]['categoryName'];
-            $addCategory[] = [
-                'gId' => $id,
-                'name' => $name,
-                'py' => $py,
-                'cId' => $categoryId,
-                'cName' => $cName,
-                'sjId' => $sjId,
-                'flower' => $flower,
-                'mainId' => $mainId,
-                'status' => $status,
-                'delStatus' => $delStatus,
-                'setItemNum' => $setItemNum,
-                'flowerNum' => $flowerNum,
-            ];
+        
+        // 1. 找出需要删除的分类:原有分类中不在新分类列表中的
+        $toDeleteCategoryIds = array_diff($goodsHasCategoryIds, $categoryIdList);
+        if (!empty($toDeleteCategoryIds)) {
+            GoodsCategoryClass::deleteByCondition(['gId' => $id, 'cId' => ['in', $toDeleteCategoryIds]]);
+            CategoryClass::counters(['goodsNum' => -1], ['id' => $toDeleteCategoryIds]);
         }
-        GoodsCategoryClass::batchAdd($addCategory);
-        CategoryClass::counters(['goodsNum' => 1], ['id' => $categoryIdList]);
+        
+        // 2. 找出需要新增的分类:新分类列表中不在原有分类中的
+        $toAddCategoryIds = array_diff($categoryIdList, $goodsHasCategoryIds);
+        if (!empty($toAddCategoryIds)) {
+            $addCategory = [];
+            foreach ($toAddCategoryIds as $categoryId) {
+                $cName = $categoryData[$categoryId]['categoryName'];
+                $addCategory[] = [
+                    'gId' => $id,
+                    'name' => $name,
+                    'py' => $py,
+                    'cId' => $categoryId,
+                    'cName' => $cName,
+                    'sjId' => $sjId,
+                    'flower' => $flower,
+                    'mainId' => $mainId,
+                    'status' => $status,
+                    'setItemNum' => $setItemNum,
+                ];
+            }
+            GoodsCategoryClass::batchAdd($addCategory);
+            CategoryClass::counters(['goodsNum' => 1], ['id' => $toAddCategoryIds]);
+        }
+        
+        // 3. 处理需要修改的分类:既在原有分类中也在新分类列表中的
+        $toUpdateCategoryIds = array_intersect($goodsHasCategoryIds, $categoryIdList);
+        if (!empty($toUpdateCategoryIds)) {
+            foreach ($toUpdateCategoryIds as $categoryId) {
+                $cName = $categoryData[$categoryId]['categoryName'];
+                $updateData = [
+                    'name' => $name,
+                    'py' => $py,
+                    'cName' => $cName,
+                    'status' => $status,
+                    'setItemNum' => $setItemNum,
+                ];
+                GoodsCategoryClass::updateByCondition(['gId' => $id, 'cId' => $categoryId], $updateData);
+            }
+        }
+        
         // 兼容 cover 参数是数字
         if (intval($data['cover']) > 0) {
             // 如果是大于0的整数,说明是图片序号
@@ -629,7 +656,7 @@ class GoodsClass extends BaseClass
 
         //库存可以修改
         if (isset($data['stock']) && is_numeric($data['stock'])) {
-            $preStock = $info['stock'] ?? 0;
+            $preStock = $goods['stock'] ?? 0;
             $newStock = $data['stock'];
             if ($newStock != $preStock) {
                 if ($staff->super != 1) {