From 3d73403e988ea76ffe5083b530a163e62ed39e1d Mon Sep 17 00:00:00 2001
From: xiaoyong931011 <15274802129@163.com>
Date: Mon, 27 Sep 2021 16:03:19 +0800
Subject: [PATCH] 20210927

---
 src/main/java/cc/mrbird/febs/mall/service/impl/AdminMallGoodsCategoryService.java  |   33 ++++++++++++++++
 src/main/resources/mapper/modules/MallGoodsCategoryMapper.xml                      |    6 +++
 src/main/resources/mapper/modules/MallGoodsMapper.xml                              |    4 ++
 src/main/java/cc/mrbird/febs/mall/mapper/MallGoodsCategoryMapper.java              |    2 +
 src/main/java/cc/mrbird/febs/mall/controller/AdminMallGoodsCategoryController.java |   17 ++++++--
 src/main/java/cc/mrbird/febs/mall/mapper/MallGoodsMapper.java                      |    3 +
 src/main/resources/templates/febs/views/modules/product/categoryList.html          |   12 ++++++
 src/main/java/cc/mrbird/febs/mall/service/IAdminMallGoodsCategoryService.java      |    2 +
 8 files changed, 75 insertions(+), 4 deletions(-)

diff --git a/src/main/java/cc/mrbird/febs/mall/controller/AdminMallGoodsCategoryController.java b/src/main/java/cc/mrbird/febs/mall/controller/AdminMallGoodsCategoryController.java
index 6c52ae7..92acc71 100644
--- a/src/main/java/cc/mrbird/febs/mall/controller/AdminMallGoodsCategoryController.java
+++ b/src/main/java/cc/mrbird/febs/mall/controller/AdminMallGoodsCategoryController.java
@@ -11,12 +11,10 @@
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.validation.annotation.Validated;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
 import javax.validation.Valid;
+import javax.validation.constraints.NotNull;
 import java.util.List;
 import java.util.Map;
 
@@ -72,4 +70,15 @@
     public FebsResponse updateCategory(@Valid MallGoodsCategory mallGoodsCategory) {
         return goodsCategoryService.updateCategory(mallGoodsCategory);
     }
+
+    /**
+     * 商品分类-删除
+     */
+    @GetMapping("delCategary/{id}")
+    @ControllerEndpoint(operation = " 商品分类-删除", exceptionMessage = "操作失败")
+    public FebsResponse delCategary(@NotNull(message = "{required}") @PathVariable Long id) {
+        return goodsCategoryService.delCategary(id);
+    }
+
+
 }
diff --git a/src/main/java/cc/mrbird/febs/mall/mapper/MallGoodsCategoryMapper.java b/src/main/java/cc/mrbird/febs/mall/mapper/MallGoodsCategoryMapper.java
index 1e2da56..012470b 100644
--- a/src/main/java/cc/mrbird/febs/mall/mapper/MallGoodsCategoryMapper.java
+++ b/src/main/java/cc/mrbird/febs/mall/mapper/MallGoodsCategoryMapper.java
@@ -28,4 +28,6 @@
     AdminMallGoodsCategoryVo getMallGoodsCategoryInfoById(@Param("id")long id);
 
     List<AdminMallGoodsCategoryTreeVo> getParentCategorys();
+
+    List<MallGoodsCategory> selectChildCategaryById(@Param("id")Long id);
 }
diff --git a/src/main/java/cc/mrbird/febs/mall/mapper/MallGoodsMapper.java b/src/main/java/cc/mrbird/febs/mall/mapper/MallGoodsMapper.java
index 9e11a7f..1ed6192 100644
--- a/src/main/java/cc/mrbird/febs/mall/mapper/MallGoodsMapper.java
+++ b/src/main/java/cc/mrbird/febs/mall/mapper/MallGoodsMapper.java
@@ -11,6 +11,7 @@
 import org.apache.ibatis.annotations.Param;
 
 import java.math.BigDecimal;
+import java.util.List;
 import java.util.Map;
 
 public interface MallGoodsMapper extends BaseMapper<MallGoods> {
@@ -28,4 +29,6 @@
     AdminMailGoodsDetailVo selectMallGoodsInfoById(@Param("id")long id);
 
     Map<String, BigDecimal> selectGoodsStockAndVolume(@Param("id") Long id);
+
+    List<MallGoods> selectMallGoodsByCategaryId(@Param("categaryId")Long id);
 }
diff --git a/src/main/java/cc/mrbird/febs/mall/service/IAdminMallGoodsCategoryService.java b/src/main/java/cc/mrbird/febs/mall/service/IAdminMallGoodsCategoryService.java
index eb4fefe..d6b356f 100644
--- a/src/main/java/cc/mrbird/febs/mall/service/IAdminMallGoodsCategoryService.java
+++ b/src/main/java/cc/mrbird/febs/mall/service/IAdminMallGoodsCategoryService.java
@@ -24,4 +24,6 @@
     List<AdminMallGoodsCategoryTreeVo> getParentCategorys();
 
     FebsResponse updateCategory(MallGoodsCategory mallGoodsCategory);
+
+    FebsResponse delCategary(Long id);
 }
diff --git a/src/main/java/cc/mrbird/febs/mall/service/impl/AdminMallGoodsCategoryService.java b/src/main/java/cc/mrbird/febs/mall/service/impl/AdminMallGoodsCategoryService.java
index 4a57128..32e64a7 100644
--- a/src/main/java/cc/mrbird/febs/mall/service/impl/AdminMallGoodsCategoryService.java
+++ b/src/main/java/cc/mrbird/febs/mall/service/impl/AdminMallGoodsCategoryService.java
@@ -2,8 +2,10 @@
 
 import cc.mrbird.febs.common.entity.FebsResponse;
 import cc.mrbird.febs.common.entity.QueryRequest;
+import cc.mrbird.febs.mall.entity.MallGoods;
 import cc.mrbird.febs.mall.entity.MallGoodsCategory;
 import cc.mrbird.febs.mall.mapper.MallGoodsCategoryMapper;
+import cc.mrbird.febs.mall.mapper.MallGoodsMapper;
 import cc.mrbird.febs.mall.service.IAdminMallGoodsCategoryService;
 import cc.mrbird.febs.mall.vo.AdminMallGoodsCategoryTreeVo;
 import cc.mrbird.febs.mall.vo.AdminMallGoodsCategoryVo;
@@ -25,6 +27,8 @@
 public class AdminMallGoodsCategoryService extends ServiceImpl<MallGoodsCategoryMapper, MallGoodsCategory> implements IAdminMallGoodsCategoryService {
 
     private final MallGoodsCategoryMapper mallGoodsCategoryMapper;
+
+    private final MallGoodsMapper mallGoodsMapper;
 
     @Override
     public IPage<MallGoodsCategory> getCategoryList(MallGoodsCategory mallGoodsCategory, QueryRequest request) {
@@ -122,4 +126,33 @@
         return new FebsResponse().success();
     }
 
+    @Override
+    public FebsResponse delCategary(Long id) {
+        MallGoodsCategory mallGoodsCategory = mallGoodsCategoryMapper.selectById(id);
+        if(ObjectUtil.isEmpty(mallGoodsCategory)){
+            return new FebsResponse().fail().message("系统繁忙,请刷新页面重试");
+        }
+        List<MallGoodsCategory> childCategarys = mallGoodsCategoryMapper.selectChildCategaryById(id);
+        if(CollUtil.isNotEmpty(childCategarys)){
+            for(MallGoodsCategory childCategary : childCategarys){
+                Long childCategaryId = childCategary.getId();
+                List<MallGoods> mallChildGoods = mallGoodsMapper.selectMallGoodsByCategaryId(childCategaryId);
+                if(CollUtil.isNotEmpty(mallChildGoods)){
+                    return new FebsResponse().fail().message("该分类下的子类【"+childCategary.getName()+"】还有商品,请先删除商品或者修改商品分类");
+                }
+            }
+        }
+
+        if(CollUtil.isNotEmpty(childCategarys)){
+            return new FebsResponse().fail().message("该分类下还有子类,请先删除子类");
+        }
+
+        List<MallGoods> mallGoods = mallGoodsMapper.selectMallGoodsByCategaryId(id);
+        if(CollUtil.isNotEmpty(mallGoods)){
+            return new FebsResponse().fail().message("该分类下还有商品,请先删除商品或者修改商品分类");
+        }
+        mallGoodsCategoryMapper.deleteById(mallGoodsCategory);
+        return new FebsResponse().success();
+    }
+
 }
diff --git a/src/main/resources/mapper/modules/MallGoodsCategoryMapper.xml b/src/main/resources/mapper/modules/MallGoodsCategoryMapper.xml
index ff946f0..7d707cd 100644
--- a/src/main/resources/mapper/modules/MallGoodsCategoryMapper.xml
+++ b/src/main/resources/mapper/modules/MallGoodsCategoryMapper.xml
@@ -66,4 +66,10 @@
     <select id="getParentCategorys" resultType="cc.mrbird.febs.mall.vo.AdminMallGoodsCategoryTreeVo">
         SELECT m.id parentId,m.name name  FROM mall_goods_category m where m.parent_id = 0
     </select>
+
+
+    <select id="selectChildCategaryById" resultType="cc.mrbird.febs.mall.entity.MallGoodsCategory">
+        select * from mall_goods_category
+        where parent_id = #{id}
+    </select>
 </mapper>
\ No newline at end of file
diff --git a/src/main/resources/mapper/modules/MallGoodsMapper.xml b/src/main/resources/mapper/modules/MallGoodsMapper.xml
index 1ecb522..334f799 100644
--- a/src/main/resources/mapper/modules/MallGoodsMapper.xml
+++ b/src/main/resources/mapper/modules/MallGoodsMapper.xml
@@ -152,4 +152,8 @@
         from mall_goods_sku
         where goods_id=#{id}
     </select>
+
+    <select id="selectMallGoodsByCategaryId" resultType="cc.mrbird.febs.mall.entity.MallGoods">
+        select * from mall_goods a where a.category_id = #{categaryId}
+    </select>
 </mapper>
\ No newline at end of file
diff --git a/src/main/resources/templates/febs/views/modules/product/categoryList.html b/src/main/resources/templates/febs/views/modules/product/categoryList.html
index 77ed76e..3bb27d3 100644
--- a/src/main/resources/templates/febs/views/modules/product/categoryList.html
+++ b/src/main/resources/templates/febs/views/modules/product/categoryList.html
@@ -88,6 +88,11 @@
                     }
                 });
             }
+            if (layEvent === 'delCategary') {
+                febs.modal.confirm('删除', '确认删除?', function () {
+                    delCategary(data.id);
+                });
+            }
             if (layEvent === 'seeImges') {
                 var t = $view.find('#seeImges'+data.id+'');
                 //页面层
@@ -105,6 +110,12 @@
             }
         });
 
+        function delCategary(id) {
+            febs.get(ctx + 'admin/goodsCategory/delCategary/' + id, null, function () {
+                febs.alert.success('操作成功');
+                $query.click();
+            });
+        }
 
         // 查询按钮
         $query.on('click', function () {
@@ -146,6 +157,7 @@
                     {title: '操作',
                         templet: function (d) {
                             return '<button class="layui-btn layui-btn-normal layui-btn-xs" lay-event="see" shiro:hasPermission="user:update">编辑</button>'
+                            +'<button class="layui-btn layui-btn-normal layui-btn-xs" lay-event="delCategary" shiro:hasPermission="user:update">删除</button>'
                         },minWidth: 300,align:'center'}
                 ]]
             });

--
Gitblit v1.9.1