sorry 5 лет назад
Родитель
Сommit
f25df93a11

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

@@ -67,7 +67,13 @@ class CategoryController extends BaseController
         $categoryId = isset($get['categoryId']) ? $get['categoryId'] : 0;
         $category = CategoryService::getById($categoryId);
         CategoryService::valid($category, $this->sjId);
-        CategoryService::deleteCategory($categoryId);
+        if ($category['goodsNum'] > 0) {
+            util::fail('删除失败,该分类已被商品使用');
+        }
+        $count = CategoryService::deleteCategory($categoryId);
+        if (!$count) {
+            util::fail('删除失败');
+        }
         util::complete('删除成功');
     }
 

+ 5 - 3
biz-hd/goods/classes/CategoryClass.php

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

+ 36 - 0
biz-hd/goods/classes/GoodsClass.php

@@ -137,6 +137,12 @@ class GoodsClass extends BaseClass
             $addCategory[] = ['gId' => $id, 'goodsName' => $goodsName, 'cId' => $categoryId, 'cName' => $cName, 'merchantId' => $data['merchantId'], 'py' => $py];
         }
         GoodsCategoryClass::batchAdd($addCategory);
+        // 分类商品数+1
+        CategoryClass::counters([
+            'goodsNum' => 1
+        ], [
+            'id' => $categoryIdList
+        ]);
         //处理款式
         if (!empty($goodsStyleList) && $data['goodsStyle'] == 1) {
             $addStyle = [];
@@ -169,12 +175,26 @@ class GoodsClass extends BaseClass
         }
         //先删除再添加分类
         GoodsCategoryClass::delGoodsCategory($id);
+        // 当前商品的分类
+        $goodsHasCategoryIds = GoodsCategoryService::getGoodsHasCategoryIds($id);
+        // 分类商品数-1
+        CategoryClass::counters([
+            'goodsNum' => -1
+        ], [
+            'id' => $goodsHasCategoryIds
+        ]);
         $addCategory = [];
         foreach ($categoryIdList as $categoryId) {
             $cName = $categoryData[$categoryId]['categoryName'];
             $addCategory[] = ['gId' => $id, 'goodsName' => $data['goodsName'], 'py' => $py, 'cId' => $categoryId, 'cName' => $cName, 'merchantId' => $data['merchantId']];
         }
         GoodsCategoryClass::batchAdd($addCategory);
+        // 分类商品数+1
+        CategoryClass::counters([
+            'goodsNum' => -1
+        ], [
+            'id' => $categoryIdList
+        ]);
 
         //处理款式
         if (!empty($goodsStyleList) && $data['goodsStyle'] == 1) {
@@ -189,6 +209,22 @@ class GoodsClass extends BaseClass
         self::updateById($id, $data);
     }
 
+
+    // 删除商品 ruidian 2021.5.7
+    public static function deleteGoods($id)
+    {
+        // 当前商品的分类
+        $goodsHasCategoryIds = GoodsCategoryService::getGoodsHasCategoryIds($id);
+        // 分类商品数-1
+        CategoryClass::counters([
+            'goodsNum' => -1
+        ], [
+            'id' => $goodsHasCategoryIds
+        ]);
+        GoodsClass::updateById($id, ['delStatus' => 1]);
+        GoodsCategoryClass::updateByCondition(['gId' => $id], ['delStatus' => 1]);
+    }
+
     //产品上下架 ssh 2019.12.8
     public static function changeStatus($id, $status)
     {

+ 1 - 3
biz-hd/goods/services/GoodsService.php

@@ -49,9 +49,7 @@ class GoodsService extends BaseService
     //删除商品 ssh 2019.12.7
     public static function deleteGoods($goods)
     {
-        $id = $goods['id'];
-        GoodsClass::updateById($id, ['delStatus' => 1]);
-        GoodsCategoryClass::updateByCondition(['gId' => $id], ['delStatus' => 1]);
+        GoodsClass::deleteGoods($goods['id']);
     }
 
     //添加商品 ssh 2019.12.7