feat(goods): 添加商品分类校验与层级管理功能- 在 ProductCategory 实体类中增加名称和图标的非空校验注解

-为控制器启用参数校验支持,确保请求数据合法性
- 实现分类保存和更新方法,自动设置分类层级并检查重复性
- 引入断言工具防止重复分类插入或更新
- 增加对父级分类不存在情况的默认值处理逻辑
This commit is contained in:
huk 2025-09-29 11:20:40 +08:00
parent 5b7c78e024
commit ee76a4cfda
4 changed files with 39 additions and 0 deletions

View File

@ -14,6 +14,7 @@ import org.dromara.common.core.domain.R;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@ -29,6 +30,7 @@ import java.util.List;
@RestController
@RequestMapping("/pms/product/category")
@RequiredArgsConstructor
@Validated
public class ProductCategoryController extends BaseController {
private final ProductCategoryService service;

View File

@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;
import org.apache.ibatis.type.JdbcType;
import org.dromara.common.core.domain.model.BaseAudit;
@ -30,6 +31,7 @@ public class ProductCategory extends BaseAudit {
@Schema(description = "NAME")
@Excel(name = "NAME")
@NotBlank(message = "名称不能为空")
private String name;
@Schema(description = "分类级别0->1级1->2级")
@ -46,6 +48,7 @@ public class ProductCategory extends BaseAudit {
@Schema(description = "图标")
@Excel(name = "图标")
@NotBlank(message = "图标不能为空")
private String icon;
@Schema(description = "删除标志0代表存在1代表删除")

View File

@ -8,5 +8,10 @@ import com.wzj.soopin.goods.domain.vo.ProductCategoryVO;
import java.util.List;
public interface ProductCategoryService extends IService<ProductCategory> {
boolean save(ProductCategory productCategory);
boolean updateById(ProductCategory productCategory);
List<ProductCategoryVO> tree(Wrapper<ProductCategory> wrapper);
}

View File

@ -1,6 +1,8 @@
package com.wzj.soopin.goods.service.impl;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzj.soopin.goods.convert.ProductCategoryConvert;
import com.wzj.soopin.goods.domain.entity.ProductCategory;
@ -8,6 +10,7 @@ import com.wzj.soopin.goods.domain.vo.ProductCategoryVO;
import com.wzj.soopin.goods.mapper.ProductCategoryMapper;
import com.wzj.soopin.goods.service.ProductCategoryService;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.exception.ServiceException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -28,6 +31,32 @@ public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMappe
private final ProductCategoryConvert convert;
@Override
public boolean save(ProductCategory productCategory) {
if (productCategory.getParentId() == null || productCategory.getParentId() == 0) {
productCategory.setLevel(1);
productCategory.setParentId(0L);
} else {
ProductCategory parent = this.getById(productCategory.getParentId());
productCategory.setLevel(parent.getLevel() + 1);
}
boolean exists = this.exists(Wrappers.lambdaQuery(ProductCategory.class)
.eq(ProductCategory::getName, productCategory.getName())
.eq(ProductCategory::getParentId, productCategory.getParentId()));
Assert.isFalse(exists, () -> ServiceException.of("分类已存在"));
return super.save(productCategory);
}
@Override
public boolean updateById(ProductCategory productCategory) {
boolean exists = this.exists(Wrappers.lambdaQuery(ProductCategory.class)
.ne(ProductCategory::getId, productCategory.getId())
.eq(ProductCategory::getName, productCategory.getName())
.eq(ProductCategory::getParentId, productCategory.getParentId()));
Assert.isFalse(exists, () -> ServiceException.of("分类已存在"));
return super.updateById(productCategory);
}
@Override
public List<ProductCategoryVO> tree(Wrapper<ProductCategory> wrapper) {
List<ProductCategory> list = this.list(wrapper);