mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-07-25 00:15:06 +08:00
mall:优化商品的分类、品牌的代码
This commit is contained in:
@ -19,6 +19,8 @@ public interface ErrorCodeConstants {
|
||||
|
||||
// ========== 商品品牌相关编号 1008002000 ==========
|
||||
ErrorCode BRAND_NOT_EXISTS = new ErrorCode(1008002000, "品牌不存在");
|
||||
ErrorCode BRAND_DISABLED = new ErrorCode(1008002001, "品牌不存在");
|
||||
ErrorCode BRAND_NAME_EXISTS = new ErrorCode(1008002002, "品牌名称已存在");
|
||||
|
||||
// ========== 商品规格名称 1008003000 ==========
|
||||
ErrorCode PROPERTY_NOT_EXISTS = new ErrorCode(1008003000, "规格名称不存在");
|
||||
|
@ -33,7 +33,7 @@ public class ProductBrandController {
|
||||
@ApiOperation("创建品牌")
|
||||
@PreAuthorize("@ss.hasPermission('product:brand:create')")
|
||||
public CommonResult<Long> createBrand(@Valid @RequestBody ProductBrandCreateReqVO createReqVO) {
|
||||
return success(brandService.createProductBrand(createReqVO));
|
||||
return success(brandService.createBrand(createReqVO));
|
||||
}
|
||||
|
||||
@PutMapping("/update")
|
||||
@ -73,7 +73,7 @@ public class ProductBrandController {
|
||||
@GetMapping("/list")
|
||||
@ApiOperation("获得品牌列表")
|
||||
@PreAuthorize("@ss.hasPermission('product:brand:query')")
|
||||
public CommonResult<List<ProductBrandRespVO>> getProductCategoryList(@Valid ProductBrandListReqVO listVO) {
|
||||
public CommonResult<List<ProductBrandRespVO>> getBrandList(@Valid ProductBrandListReqVO listVO) {
|
||||
List<ProductBrandDO> list = brandService.getBrandList(listVO);
|
||||
list.sort(Comparator.comparing(ProductBrandDO::getSort));
|
||||
return success(ProductBrandConvert.INSTANCE.convertList(list));
|
||||
|
@ -20,7 +20,8 @@ public class ProductBrandBaseVO {
|
||||
@NotNull(message = "品牌图片不能为空")
|
||||
private String picUrl;
|
||||
|
||||
@ApiModelProperty(value = "品牌排序", example = "1")
|
||||
@ApiModelProperty(value = "品牌排序", required = true, example = "1")
|
||||
@NotNull(message = "品牌排序不能为空")
|
||||
private Integer sort;
|
||||
|
||||
@ApiModelProperty(value = "品牌描述", example = "描述")
|
||||
|
@ -21,7 +21,7 @@ public class ProductBrandPageReqVO extends PageParam {
|
||||
@ApiModelProperty(value = "品牌名称", example = "芋道")
|
||||
private String name;
|
||||
|
||||
@ApiModelProperty(value = "状态", example = "0")
|
||||
@ApiModelProperty(value = "状态", example = "0", notes = "参考 CommonStatusEnum 枚举")
|
||||
private Integer status;
|
||||
|
||||
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
|
||||
|
@ -34,14 +34,14 @@ public class ProductCategoryController {
|
||||
@PostMapping("/create")
|
||||
@ApiOperation("创建商品分类")
|
||||
@PreAuthorize("@ss.hasPermission('product:category:create')")
|
||||
public CommonResult<Long> createProductCategory(@Valid @RequestBody ProductCategoryCreateReqVO createReqVO) {
|
||||
public CommonResult<Long> createCategory(@Valid @RequestBody ProductCategoryCreateReqVO createReqVO) {
|
||||
return success(categoryService.createCategory(createReqVO));
|
||||
}
|
||||
|
||||
@PutMapping("/update")
|
||||
@ApiOperation("更新商品分类")
|
||||
@PreAuthorize("@ss.hasPermission('product:category:update')")
|
||||
public CommonResult<Boolean> updateProductCategory(@Valid @RequestBody ProductCategoryUpdateReqVO updateReqVO) {
|
||||
public CommonResult<Boolean> updateCategory(@Valid @RequestBody ProductCategoryUpdateReqVO updateReqVO) {
|
||||
categoryService.updateCategory(updateReqVO);
|
||||
return success(true);
|
||||
}
|
||||
@ -50,7 +50,7 @@ public class ProductCategoryController {
|
||||
@ApiOperation("删除商品分类")
|
||||
@ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
|
||||
@PreAuthorize("@ss.hasPermission('product:category:delete')")
|
||||
public CommonResult<Boolean> deleteProductCategory(@RequestParam("id") Long id) {
|
||||
public CommonResult<Boolean> deleteCategory(@RequestParam("id") Long id) {
|
||||
categoryService.deleteCategory(id);
|
||||
return success(true);
|
||||
}
|
||||
@ -59,7 +59,7 @@ public class ProductCategoryController {
|
||||
@ApiOperation("获得商品分类")
|
||||
@ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
|
||||
@PreAuthorize("@ss.hasPermission('product:category:query')")
|
||||
public CommonResult<ProductCategoryRespVO> getProductCategory(@RequestParam("id") Long id) {
|
||||
public CommonResult<ProductCategoryRespVO> getCategory(@RequestParam("id") Long id) {
|
||||
ProductCategoryDO category = categoryService.getCategory(id);
|
||||
return success(ProductCategoryConvert.INSTANCE.convert(category));
|
||||
}
|
||||
@ -67,7 +67,7 @@ public class ProductCategoryController {
|
||||
@GetMapping("/list")
|
||||
@ApiOperation("获得商品分类列表")
|
||||
@PreAuthorize("@ss.hasPermission('product:category:query')")
|
||||
public CommonResult<List<ProductCategoryRespVO>> getProductCategoryList(@Valid ProductCategoryListReqVO treeListReqVO) {
|
||||
public CommonResult<List<ProductCategoryRespVO>> getCategoryList(@Valid ProductCategoryListReqVO treeListReqVO) {
|
||||
List<ProductCategoryDO> list = categoryService.getEnableCategoryList(treeListReqVO);
|
||||
list.sort(Comparator.comparing(ProductCategoryDO::getSort));
|
||||
return success(ProductCategoryConvert.INSTANCE.convertList(list));
|
||||
|
@ -27,4 +27,8 @@ public interface ProductBrandMapper extends BaseMapperX<ProductBrandDO> {
|
||||
.likeIfPresent(ProductBrandDO::getName, reqVO.getName()));
|
||||
}
|
||||
|
||||
default ProductBrandDO selectByName(String name) {
|
||||
return selectOne(ProductBrandDO::getName, name);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ public interface ProductBrandService {
|
||||
* @param createReqVO 创建信息
|
||||
* @return 编号
|
||||
*/
|
||||
Long createProductBrand(@Valid ProductBrandCreateReqVO createReqVO);
|
||||
Long createBrand(@Valid ProductBrandCreateReqVO createReqVO);
|
||||
|
||||
/**
|
||||
* 更新品牌
|
||||
|
@ -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
|
||||
|
@ -63,7 +63,7 @@ public class ProductCategoryServiceImpl implements ProductCategoryService {
|
||||
if (productCategoryMapper.selectCountByParentId(id) > 0) {
|
||||
throw exception(CATEGORY_EXISTS_CHILDREN);
|
||||
}
|
||||
|
||||
// TODO 芋艿 补充只有不存在商品才可以删除
|
||||
// 删除
|
||||
productCategoryMapper.deleteById(id);
|
||||
}
|
||||
@ -94,8 +94,8 @@ public class ProductCategoryServiceImpl implements ProductCategoryService {
|
||||
@Override
|
||||
public void validateCategoryLevel(Long id) {
|
||||
Integer level = getProductCategoryLevel(id, 1);
|
||||
if(level < 3){
|
||||
throw exception(CATEGORY_LEVEL_ERROR);
|
||||
if (level < 3){
|
||||
throw exception(CATEGORY_LEVEL_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -43,7 +43,7 @@ public class ProductBrandServiceImplTest extends BaseDbUnitTest {
|
||||
ProductBrandCreateReqVO reqVO = randomPojo(ProductBrandCreateReqVO.class);
|
||||
|
||||
// 调用
|
||||
Long brandId = brandService.createProductBrand(reqVO);
|
||||
Long brandId = brandService.createBrand(reqVO);
|
||||
// 断言
|
||||
assertNotNull(brandId);
|
||||
// 校验记录的属性是否正确
|
||||
|
Reference in New Issue
Block a user