From d9d473dcf00dea0377855c43ec1cf1244398cc5b Mon Sep 17 00:00:00 2001 From: jeromesoar Date: Thu, 12 May 2022 17:09:18 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20TODO=20=E5=88=86=E7=B1=BB=E5=90=88?= =?UTF-8?q?=E7=90=86=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../product/enums/ErrorCodeConstants.java | 2 ++ .../dal/mysql/category/CategoryMapper.java | 3 ++ .../service/category/CategoryServiceImpl.java | 31 ++++++++++++------- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/yudao-module-mall/yudao-module-product-api/src/main/java/cn/iocoder/yudao/module/product/enums/ErrorCodeConstants.java b/yudao-module-mall/yudao-module-product-api/src/main/java/cn/iocoder/yudao/module/product/enums/ErrorCodeConstants.java index e355358b5..4ab84d5bb 100644 --- a/yudao-module-mall/yudao-module-product-api/src/main/java/cn/iocoder/yudao/module/product/enums/ErrorCodeConstants.java +++ b/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 { // ========== 商品分类相关 1008001000============ ErrorCode CATEGORY_NOT_EXISTS = new ErrorCode(1008001000, "商品分类不存在"); + ErrorCode CATEGORY_PARENT_NOT_EXISTS = new ErrorCode(1008001001, "父分类不存在"); + ErrorCode CATEGORY_EXISTS_CHILDREN = new ErrorCode(1008001002, "存在子分类,无法删除"); // ========== 品牌相关编号 1008002000 ========== ErrorCode BRAND_NOT_EXISTS = new ErrorCode(1008002000, "品牌不存在"); diff --git a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/mysql/category/CategoryMapper.java b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/mysql/category/CategoryMapper.java index 109f1ff54..a6ea75b2e 100644 --- a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/mysql/category/CategoryMapper.java +++ b/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 { .orderByDesc(CategoryDO::getId)); } + default Long selectCountByParentId(Long parentId) { + return selectCount(CategoryDO::getParentId, parentId); + } } diff --git a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/category/CategoryServiceImpl.java b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/category/CategoryServiceImpl.java index 4f9ed2c24..97a0af529 100644 --- a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/category/CategoryServiceImpl.java +++ b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/category/CategoryServiceImpl.java @@ -1,5 +1,7 @@ package cn.iocoder.yudao.module.product.service.category; +import cn.iocoder.yudao.framework.common.exception.ErrorCode; +import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.module.product.controller.admin.category.vo.*; import cn.iocoder.yudao.module.product.convert.category.CategoryConvert; @@ -13,7 +15,7 @@ import java.util.Collection; import java.util.List; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; -import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.CATEGORY_NOT_EXISTS; +import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.*; /** * 商品分类 Service 实现类 @@ -29,7 +31,8 @@ public class CategoryServiceImpl implements CategoryService { @Override public Long createCategory(CategoryCreateReqVO createReqVO) { - // TODO JeromeSoar:校验父分类 + // 校验父分类存在 + this.validateCategoryExists(createReqVO.getParentId(), CATEGORY_PARENT_NOT_EXISTS); // 插入 CategoryDO category = CategoryConvert.INSTANCE.convert(createReqVO); categoryMapper.insert(category); @@ -39,9 +42,10 @@ public class CategoryServiceImpl implements CategoryService { @Override public void updateCategory(CategoryUpdateReqVO updateReqVO) { - // TODO JeromeSoar:校验父分类 - // 校验存在 - this.validateCategoryExists(updateReqVO.getId()); + // 校验父分类存在 + this.validateCategoryExists(updateReqVO.getParentId(), CATEGORY_PARENT_NOT_EXISTS); + // 校验分类是否存在 + this.validateCategoryExists(updateReqVO.getId(), CATEGORY_NOT_EXISTS); // 更新 CategoryDO updateObj = CategoryConvert.INSTANCE.convert(updateReqVO); categoryMapper.updateById(updateObj); @@ -49,18 +53,23 @@ public class CategoryServiceImpl implements CategoryService { @Override public void deleteCategory(Long id) { - // TODO JeromeSoar:校验删除的商品分类是否存在 // TODO 芋艿 补充只有不存在商品才可以删除 - // 校验存在 - this.validateCategoryExists(id); + // 校验分类是否存在 + CategoryDO categoryDO = this.validateCategoryExists(id, CATEGORY_NOT_EXISTS); + // 校验是否还有子分类 + if (categoryMapper.selectCountByParentId(categoryDO.getParentId()) > 0) { + throw ServiceExceptionUtil.exception(CATEGORY_EXISTS_CHILDREN); + } // 删除 categoryMapper.deleteById(id); } - private void validateCategoryExists(Long id) { - if (categoryMapper.selectById(id) == null) { - throw exception(CATEGORY_NOT_EXISTS); + private CategoryDO validateCategoryExists(Long id, ErrorCode errorCode) { + CategoryDO categoryDO = categoryMapper.selectById(id); + if (categoryDO == null) { + throw exception(errorCode); } + return categoryDO; } @Override