提交 2eb75f4c authored 作者: hzh's avatar hzh

品牌删除增加限制

上级 3851cee4
......@@ -22,6 +22,7 @@ public interface ErrorCodeConstants {
ErrorCode BRAND_NOT_EXISTS = new ErrorCode(1_008_002_000, "品牌不存在");
ErrorCode BRAND_DISABLED = new ErrorCode(1_008_002_001, "品牌已禁用");
ErrorCode BRAND_NAME_EXISTS = new ErrorCode(1_008_002_002, "品牌名称已存在");
ErrorCode BRAND_HAVE_BIND_SPU = new ErrorCode(1_008_001_005, "品牌下存在商品,无法删除");
// ========== 商品属性项 1-008-003-000 ==========
ErrorCode PROPERTY_NOT_EXISTS = new ErrorCode(1_008_003_000, "属性项不存在");
......
......@@ -124,6 +124,14 @@ public interface ProductSpuService {
*/
Long getSpuCountByCategoryId(Long categoryId);
/**
* 通过品牌 brandId 查询 SPU 个数
*
* @param brandId 品牌id
* @return SPU 数量
*/
Long getSpuCountByBrandId(Long brandId);
/**
* 校验商品是否有效。如下情况,视为无效:
......
......@@ -14,6 +14,8 @@ import org.dromara.mall.controller.product.admin.brand.vo.ProductBrandUpdateReqV
import org.dromara.mall.domain.product.ProductBrandDO;
import org.dromara.mall.mapper.product.ProductBrandMapper;
import org.dromara.mall.service.product.ProductBrandService;
import org.dromara.mall.service.product.ProductSpuService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
......@@ -36,6 +38,10 @@ public class ProductBrandServiceImpl implements ProductBrandService {
@Resource
private ProductBrandMapper productBrandMapper;
@Resource
@Lazy // 循环依赖,避免报错
private ProductSpuService productSpuService;
@Override
public Long createBrand(ProductBrandCreateReqVO createReqVO) {
// 校验
......@@ -62,6 +68,11 @@ public class ProductBrandServiceImpl implements ProductBrandService {
public void deleteBrand(Long id) {
// 校验存在
validateBrandExists(id);
// 校验品牌是否绑定了 SPU
Long spuCount = productSpuService.getSpuCountByBrandId(id);
if (spuCount > 0) {
throw exception(BRAND_HAVE_BIND_SPU);
}
// 删除
productBrandMapper.deleteById(id);
}
......
......@@ -273,4 +273,8 @@ public class ProductSpuServiceImpl implements ProductSpuService {
return productSpuMapper.selectCount(ProductSpuDO::getCategoryId, categoryId);
}
@Override
public Long getSpuCountByBrandId(Long brandId) {
return productSpuMapper.selectCount(ProductSpuDO::getBrandId, brandId);
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论