Commit d9d473dcf00dea0377855c43ec1cf1244398cc5b

Authored by jeromesoar
1 parent ff307a4c

fix: TODO 分类合理性

yudao-module-mall/yudao-module-product-api/src/main/java/cn/iocoder/yudao/module/product/enums/ErrorCodeConstants.java
@@ -11,6 +11,8 @@ public interface ErrorCodeConstants { @@ -11,6 +11,8 @@ public interface ErrorCodeConstants {
11 11
12 // ========== 商品分类相关 1008001000============ 12 // ========== 商品分类相关 1008001000============
13 ErrorCode CATEGORY_NOT_EXISTS = new ErrorCode(1008001000, "商品分类不存在"); 13 ErrorCode CATEGORY_NOT_EXISTS = new ErrorCode(1008001000, "商品分类不存在");
  14 + ErrorCode CATEGORY_PARENT_NOT_EXISTS = new ErrorCode(1008001001, "父分类不存在");
  15 + ErrorCode CATEGORY_EXISTS_CHILDREN = new ErrorCode(1008001002, "存在子分类,无法删除");
14 16
15 // ========== 品牌相关编号 1008002000 ========== 17 // ========== 品牌相关编号 1008002000 ==========
16 ErrorCode BRAND_NOT_EXISTS = new ErrorCode(1008002000, "品牌不存在"); 18 ErrorCode BRAND_NOT_EXISTS = new ErrorCode(1008002000, "品牌不存在");
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/mysql/category/CategoryMapper.java
@@ -34,4 +34,7 @@ public interface CategoryMapper extends BaseMapperX<CategoryDO> { @@ -34,4 +34,7 @@ public interface CategoryMapper extends BaseMapperX<CategoryDO> {
34 .orderByDesc(CategoryDO::getId)); 34 .orderByDesc(CategoryDO::getId));
35 } 35 }
36 36
  37 + default Long selectCountByParentId(Long parentId) {
  38 + return selectCount(CategoryDO::getParentId, parentId);
  39 + }
37 } 40 }
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/category/CategoryServiceImpl.java
1 package cn.iocoder.yudao.module.product.service.category; 1 package cn.iocoder.yudao.module.product.service.category;
2 2
  3 +import cn.iocoder.yudao.framework.common.exception.ErrorCode;
  4 +import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
3 import cn.iocoder.yudao.framework.common.pojo.PageResult; 5 import cn.iocoder.yudao.framework.common.pojo.PageResult;
4 import cn.iocoder.yudao.module.product.controller.admin.category.vo.*; 6 import cn.iocoder.yudao.module.product.controller.admin.category.vo.*;
5 import cn.iocoder.yudao.module.product.convert.category.CategoryConvert; 7 import cn.iocoder.yudao.module.product.convert.category.CategoryConvert;
@@ -13,7 +15,7 @@ import java.util.Collection; @@ -13,7 +15,7 @@ import java.util.Collection;
13 import java.util.List; 15 import java.util.List;
14 16
15 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; 17 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
16 -import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.CATEGORY_NOT_EXISTS; 18 +import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.*;
17 19
18 /** 20 /**
19 * 商品分类 Service 实现类 21 * 商品分类 Service 实现类
@@ -29,7 +31,8 @@ public class CategoryServiceImpl implements CategoryService { @@ -29,7 +31,8 @@ public class CategoryServiceImpl implements CategoryService {
29 31
30 @Override 32 @Override
31 public Long createCategory(CategoryCreateReqVO createReqVO) { 33 public Long createCategory(CategoryCreateReqVO createReqVO) {
32 - // TODO JeromeSoar:校验父分类 34 + // 校验父分类存在
  35 + this.validateCategoryExists(createReqVO.getParentId(), CATEGORY_PARENT_NOT_EXISTS);
33 // 插入 36 // 插入
34 CategoryDO category = CategoryConvert.INSTANCE.convert(createReqVO); 37 CategoryDO category = CategoryConvert.INSTANCE.convert(createReqVO);
35 categoryMapper.insert(category); 38 categoryMapper.insert(category);
@@ -39,9 +42,10 @@ public class CategoryServiceImpl implements CategoryService { @@ -39,9 +42,10 @@ public class CategoryServiceImpl implements CategoryService {
39 42
40 @Override 43 @Override
41 public void updateCategory(CategoryUpdateReqVO updateReqVO) { 44 public void updateCategory(CategoryUpdateReqVO updateReqVO) {
42 - // TODO JeromeSoar:校验父分类  
43 - // 校验存在  
44 - this.validateCategoryExists(updateReqVO.getId()); 45 + // 校验父分类存在
  46 + this.validateCategoryExists(updateReqVO.getParentId(), CATEGORY_PARENT_NOT_EXISTS);
  47 + // 校验分类是否存在
  48 + this.validateCategoryExists(updateReqVO.getId(), CATEGORY_NOT_EXISTS);
45 // 更新 49 // 更新
46 CategoryDO updateObj = CategoryConvert.INSTANCE.convert(updateReqVO); 50 CategoryDO updateObj = CategoryConvert.INSTANCE.convert(updateReqVO);
47 categoryMapper.updateById(updateObj); 51 categoryMapper.updateById(updateObj);
@@ -49,18 +53,23 @@ public class CategoryServiceImpl implements CategoryService { @@ -49,18 +53,23 @@ public class CategoryServiceImpl implements CategoryService {
49 53
50 @Override 54 @Override
51 public void deleteCategory(Long id) { 55 public void deleteCategory(Long id) {
52 - // TODO JeromeSoar:校验删除的商品分类是否存在  
53 // TODO 芋艿 补充只有不存在商品才可以删除 56 // TODO 芋艿 补充只有不存在商品才可以删除
54 - // 校验存在  
55 - this.validateCategoryExists(id); 57 + // 校验分类是否存在
  58 + CategoryDO categoryDO = this.validateCategoryExists(id, CATEGORY_NOT_EXISTS);
  59 + // 校验是否还有子分类
  60 + if (categoryMapper.selectCountByParentId(categoryDO.getParentId()) > 0) {
  61 + throw ServiceExceptionUtil.exception(CATEGORY_EXISTS_CHILDREN);
  62 + }
56 // 删除 63 // 删除
57 categoryMapper.deleteById(id); 64 categoryMapper.deleteById(id);
58 } 65 }
59 66
60 - private void validateCategoryExists(Long id) {  
61 - if (categoryMapper.selectById(id) == null) {  
62 - throw exception(CATEGORY_NOT_EXISTS); 67 + private CategoryDO validateCategoryExists(Long id, ErrorCode errorCode) {
  68 + CategoryDO categoryDO = categoryMapper.selectById(id);
  69 + if (categoryDO == null) {
  70 + throw exception(errorCode);
63 } 71 }
  72 + return categoryDO;
64 } 73 }
65 74
66 @Override 75 @Override