Преглед на файлове

1. 删除分类更新,实现以下逻辑:
1.在删除一个分类前,先检查该分类下是否有商品。
2.创建或获取默认分类:
- 检查当前店铺是否存在一个“默认分类”。
- 如果不存在,系统将自动创建一个名为“默认分类”且 default 值为 1 的新分类。
3.重新分配商品:
- 遍历要删除分类中的所有商品。
- 如果一个商品只属于当前这一个分类,系统会把它移动到“默认分类”中。
- 如果一个商品同时还属于其他分类,系统会直接将其从当前要删除的分类中移除。
4.完成所有商品的迁移或移除后,再安全地删除这个分类。

2.使用 模型Class 替换 模型Services

3.修复更新商品的bug: ['in', 'cId', $toDeleteCategoryIds] 的正确用法

shizhongqi преди 1 година
родител
ревизия
45f106983c

+ 2 - 1
app-ghs/controllers/CategoryController.php

@@ -12,6 +12,7 @@ use bizHd\goods\services\CategoryService;
 use bizHd\goods\services\GoodsCategoryService;
 use Yii;
 use common\components\util;
+use bizHd\goods\classes\CategoryClass;
 
 class CategoryController extends BaseController
 {
@@ -74,7 +75,7 @@ class CategoryController extends BaseController
         $categoryId = isset($get['categoryId']) ? $get['categoryId'] : 0;
         $category = CategoryService::getById($categoryId);
         CategoryService::valid($category, $this->mainId);
-        CategoryService::deleteCategory($categoryId);
+        CategoryClass::deleteCategory($categoryId);
         util::complete('删除成功');
     }
 

+ 30 - 8
app-hd/controllers/CategoryController.php

@@ -3,6 +3,7 @@
 namespace hd\controllers;
 
 use bizHd\goods\classes\CategoryClass;
+use bizHd\goods\classes\GoodsCategoryClass;
 use bizHd\goods\services\CategoryService;
 use bizHd\goods\services\GoodsCategoryService;
 use Yii;
@@ -75,12 +76,38 @@ class CategoryController extends BaseController
     {
         $get = Yii::$app->request->get();
         $categoryId = isset($get['categoryId']) ? $get['categoryId'] : 0;
-        $category = CategoryService::getById($categoryId);
+        $category = CategoryClass::getById($categoryId);
         CategoryService::valid($category, $this->mainId);
+
         if ($category['goodsNum'] > 0) {
-            util::fail('分类下还有商品');
+            $goodsCateObjs = GoodsCategoryClass::getAllByCondition(['mainId'=>$this->mainId, 'cId'=>$categoryId]);
+
+            //检查是否有默认分类,没有默认分类创建一个
+            $defaultCategory = CategoryClass::getByCondition(['mainId'=>$this->mainId, 'default'=>1]);
+            if ($defaultCategory == null){
+                $data = [
+                    'mainId' => $this->mainId,
+                    'sjId' => $this->sjId,
+                    'categoryName' => '默认分类',
+                    'default' => 1
+                ];
+                $defaultCategory = CategoryClass::addCategory($data);
+            }
+            $defaultCategoryId = $defaultCategory['id'];
+            foreach ($goodsCateObjs as $gcObj) {
+                // 先判断商品是否也在其它分类下
+                $goodsOtherCateCount = GoodsCategoryClass::getCount(['mainId'=>$this->mainId, 'gId'=>$gcObj['gId'], 'delStatus'=>0]);
+                if ($goodsOtherCateCount == 1) {
+                    // 如果商品仅在此分类下,则归到默认分类
+                    GoodsCategoryClass::updateById($gcObj['id'], ['cId' => $defaultCategoryId, 'delStatus' => 0]);
+                } else {
+                    // 如果商品还在其它分类下,则直接删除此商品的当前分类
+                    GoodsCategoryClass::delGoodsCategory($this->mainId, $categoryId, $gcObj['gId']);
+                }
+            }
         }
-        $count = CategoryService::deleteCategory($categoryId);
+
+        $count = CategoryClass::deleteCategory($categoryId);
         if (!$count) {
             util::fail('删除失败');
         }
@@ -101,11 +128,6 @@ class CategoryController extends BaseController
         if (is_numeric($flower)) {
             $where['flower'] = $flower;
         }
-        $where['delStatus'] = $get['delStatus'] ?? 0;
-        $status = $get['status'] ?? '';
-        if (is_numeric($status)) {
-            $where['status'] = $status;
-        }
         $flowerNum = $get['flowerNum'] ?? 0;
         if (!empty($flowerNum)) {
             $where['flowerNum'] = $flowerNum;

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

@@ -114,7 +114,7 @@ class KindController extends BaseController
         if ($category['goodsNum'] > 0) {
             util::fail('品类下还有商品');
         }
-        $count = CategoryService::deleteCategory($categoryId);
+        $count = \bizHd\goods\classes\CategoryClass::deleteCategory($categoryId);
         if (!$count) {
             util::fail('删除失败');
         }

+ 1 - 2
biz-hd/goods/classes/CategoryClass.php

@@ -79,7 +79,6 @@ class CategoryClass extends BaseClass
     //删除分类 ssh 2020.3.3
     public static function deleteCategory($id)
     {
-        return self::updateByCondition(['id' => $id, 'goodsNum' => 0], ['delStatus' => 1]);
-        return true;
+        return self::updateByCondition(['id' => $id], ['goodsNum'=>0, 'delStatus'=>1]);
     }
 }

+ 3 - 3
biz-hd/goods/classes/GoodsCategoryClass.php

@@ -11,10 +11,10 @@ class GoodsCategoryClass extends BaseClass
 
 	public static $baseFile = '\bizHd\goods\models\GoodsCategory';
 
-	//删除商品的分类 ssh 2019.12.7
-	public static function delGoodsCategory($goodsId)
+	//删除商品的分类
+	public static function delGoodsCategory($mainId, $categoryId, $goodsId)
 	{
-		self::deleteByCondition(['gId'=>$goodsId]);
+		self::updateByCondition(['mainId'=>$mainId, 'cId'=>$categoryId, 'gId'=>$goodsId], ['delStatus' => 1]);
 	}
 
 	//批量获取多个商品的分类ID ssh 2023

+ 4 - 2
biz-hd/goods/classes/GoodsClass.php

@@ -423,7 +423,7 @@ class GoodsClass extends BaseClass
             $val['bigCover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
 
             if ($getItem) {
-                $currentItems = $groupedItems[$id];
+                $currentItems = isset($groupedItems[$id]) ? $groupedItems[$id] : [];
                 foreach ($currentItems as &$itemVal) {
                     $shortCover = $itemVal['cover'] ?? '';
                     $itemVal['shortCover'] = $shortCover;
@@ -622,7 +622,9 @@ class GoodsClass extends BaseClass
         // 1. 找出需要删除的分类:原有分类中不在新分类列表中的
         $toDeleteCategoryIds = array_diff($goodsHasCategoryIds, $categoryIdList);
         if (!empty($toDeleteCategoryIds)) {
-            GoodsCategoryClass::deleteByCondition(['gId' => $id, 'cId' => ['in', $toDeleteCategoryIds]]);
+            //GoodsCategoryClass::deleteByCondition(['gId' => $id, 'cId' => ['in', $toDeleteCategoryIds]]); //不支持,改用 ActiveRecord 的 deleteAll 方法
+            GoodsCategoryClass::getModel()::deleteAll(['and', ['gId' => $id], ['in', 'cId', $toDeleteCategoryIds]]); //先通过 getModel 获取 AR 实例,再调用 deleteAll 方法
+            //\bizHd\goods\models\GoodsCategory::deleteAll(['and', ['gId' => $id], ['in', 'cId', $toDeleteCategoryIds]]); //直接调用 deleteAll 方法
             CategoryClass::counters(['goodsNum' => -1], ['id' => $toDeleteCategoryIds]);
         }
         

+ 0 - 6
biz-hd/goods/services/CategoryService.php

@@ -4,7 +4,6 @@ namespace bizHd\goods\services;
 
 use bizHd\base\services\BaseService;
 use bizHd\goods\classes\CategoryClass;
-use bizHd\picText\classes\PicTextClass;
 use bizHd\goods\classes\GoodsCategoryClass;
 use common\components\imgUtil;
 use common\components\util;
@@ -118,11 +117,6 @@ class CategoryService extends BaseService
         GoodsCategoryClass::updateByCondition(['cId' => $categoryId], ['cName' => $post['categoryName']]);
     }
 
-    public static function deleteCategory($id)
-    {
-        return CategoryClass::deleteCategory($id);
-    }
-
     //获取开单页所有的分类
     public static function getCategoryClassAll($where)
     {

+ 1 - 1
biz-hd/goods/services/GoodsCategoryService.php

@@ -14,7 +14,7 @@ class GoodsCategoryService extends BaseService
     //查询一个分类下的商品,有分页 ssh 2019.12.2
     public static function getGoodsList($where)
     {
-        unset($where['delStatus']);//这个后面改进
+        $where['delStatus'] = 0; // 排除已删除
         $data = GoodsCategoryClass::getList('*', $where, 'inTurn DESC');
         $list = isset($data['list']) ? $data['list'] : [];
         if (empty($list)) {

+ 6 - 1
common/base/models/Base.php

@@ -169,7 +169,12 @@ class Base extends ActiveRecord
     }
 
     /**
-     *根据条件查出一条记录
+     * 根据条件查出一条记录
+     * @param $condition
+     * @param bool $returnObject
+     * @param bool $order
+     * @param string $field
+     * @return array|ActiveRecord|null
      */
     public function getByCondition($condition, $returnObject = false, $order = false, $field = '*')
     {