|
@@ -1,5 +1,6 @@
|
|
|
package cn.iocoder.yudao.module.product.service.brand;
|
|
|
|
|
|
+import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
|
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
|
import cn.iocoder.yudao.module.product.controller.admin.brand.vo.ProductBrandCreateReqVO;
|
|
|
import cn.iocoder.yudao.module.product.controller.admin.brand.vo.ProductBrandListReqVO;
|
|
@@ -8,6 +9,7 @@ import cn.iocoder.yudao.module.product.controller.admin.brand.vo.ProductBrandUpd
|
|
|
import cn.iocoder.yudao.module.product.convert.brand.ProductBrandConvert;
|
|
|
import cn.iocoder.yudao.module.product.dal.dataobject.brand.ProductBrandDO;
|
|
|
import cn.iocoder.yudao.module.product.dal.mysql.brand.ProductBrandMapper;
|
|
|
+import com.google.common.annotations.VisibleForTesting;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
|
|
@@ -16,7 +18,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.BRAND_NOT_EXISTS;
|
|
|
+import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.*;
|
|
|
|
|
|
/**
|
|
|
* 品牌 Service 实现类
|
|
@@ -31,7 +33,10 @@ public class ProductBrandServiceImpl implements ProductBrandService {
|
|
|
private ProductBrandMapper brandMapper;
|
|
|
|
|
|
@Override
|
|
|
- public Long createProductBrand(ProductBrandCreateReqVO createReqVO) {
|
|
|
+ public Long createBrand(ProductBrandCreateReqVO createReqVO) {
|
|
|
+ // 校验
|
|
|
+ validateBrandNameUnique(null, createReqVO.getName());
|
|
|
+
|
|
|
// 插入
|
|
|
ProductBrandDO brand = ProductBrandConvert.INSTANCE.convert(createReqVO);
|
|
|
brandMapper.insert(brand);
|
|
@@ -43,6 +48,7 @@ public class ProductBrandServiceImpl implements ProductBrandService {
|
|
|
public void updateBrand(ProductBrandUpdateReqVO updateReqVO) {
|
|
|
// 校验存在
|
|
|
validateBrandExists(updateReqVO.getId());
|
|
|
+ validateBrandNameUnique(updateReqVO.getId(), updateReqVO.getName());
|
|
|
// 更新
|
|
|
ProductBrandDO updateObj = ProductBrandConvert.INSTANCE.convert(updateReqVO);
|
|
|
brandMapper.updateById(updateObj);
|
|
@@ -51,7 +57,7 @@ public class ProductBrandServiceImpl implements ProductBrandService {
|
|
|
@Override
|
|
|
public void deleteBrand(Long id) {
|
|
|
// 校验存在
|
|
|
- this.validateBrandExists(id);
|
|
|
+ validateBrandExists(id);
|
|
|
// 删除
|
|
|
brandMapper.deleteById(id);
|
|
|
}
|
|
@@ -62,6 +68,21 @@ public class ProductBrandServiceImpl implements ProductBrandService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @VisibleForTesting
|
|
|
+ public void validateBrandNameUnique(Long id, String name) {
|
|
|
+ ProductBrandDO brand = brandMapper.selectByName(name);
|
|
|
+ if (brand == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 如果 id 为空,说明不用比较是否为相同 id 的字典类型
|
|
|
+ if (id == null) {
|
|
|
+ throw exception(BRAND_NAME_EXISTS);
|
|
|
+ }
|
|
|
+ if (!brand.getId().equals(id)) {
|
|
|
+ throw exception(BRAND_NAME_EXISTS);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public ProductBrandDO getBrand(Long id) {
|
|
|
return brandMapper.selectById(id);
|
|
@@ -79,9 +100,13 @@ public class ProductBrandServiceImpl implements ProductBrandService {
|
|
|
|
|
|
@Override
|
|
|
public void validateProductBrand(Long id) {
|
|
|
- if(getBrand(id) == null){
|
|
|
+ ProductBrandDO brand = brandMapper.selectById(id);
|
|
|
+ if (brand == null) {
|
|
|
throw exception(BRAND_NOT_EXISTS);
|
|
|
}
|
|
|
+ if (brand.getStatus().equals(CommonStatusEnum.DISABLE.getStatus())) {
|
|
|
+ throw exception(BRAND_DISABLED);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|