JAVA全系列 教程
3762个小节阅读:7088.5k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
在商品服务模块编写商品类型Mapper
xxxxxxxxxx
public interface ProductTypeMapper extends BaseMapper<ProductType> {
}
在商品服务模块编写商品类型服务实现类
xxxxxxxxxx
@DubboService
public class ProductTypeServiceImpl implements ProductTypeService {
@Autowired
private ProductTypeMapper productTypeMapper;
@Override
public void add(ProductType productType) {
// 查询父类型
ProductType productTypeParent = productTypeMapper.selectById(productType.getParentId());
if (productTypeParent == null){ // 如果没有父类型,则为1级类型
productType.setLevel(1);
}else if(productTypeParent.getLevel() < 3){ // 如果父类型级别<3,则级别为父级别+1
productType.setLevel(productTypeParent.getLevel()+1);
}else if(productTypeParent.getLevel() >= 3){ // 如果父类型级别>=3,则不能添加子类型
throw new BusException(CodeEnum.INSERT_PRODUCT_TYPE_ERROR);
}
productTypeMapper.insert(productType);
}
@Override
public void update(ProductType productType) {
// 查询父类型
ProductType productTypeParent = productTypeMapper.selectById(productType.getParentId());
if (productTypeParent == null){ // 如果没有父类型,则为1级类型
productType.setLevel(1);
}else if(productTypeParent.getLevel() < 3){ // 如果父类型级别<3,则级别为父级别+1
productType.setLevel(productTypeParent.getLevel()+1);
}else if(productTypeParent.getLevel() >= 3){ // 如果父类型级别>=3,则不能添加子类型
throw new BusException(CodeEnum.INSERT_PRODUCT_TYPE_ERROR);
}
productTypeMapper.updateById(productType);
}
@Override
public void delete(Long id) {
// 查询该类型的子类型
QueryWrapper<ProductType> queryWrapper = new QueryWrapper();
queryWrapper.eq("parentId",id);
List<ProductType> productTypes = productTypeMapper.selectList(queryWrapper);
// 如果该类型有子类型,删除失败
if (productTypes != null && productTypes.size() > 0){
throw new BusException(CodeEnum.DELETE_PRODUCT_TYPE_ERROR);
}
productTypeMapper.deleteById(id);
}
@Override
public ProductType findById(Long id) {
return productTypeMapper.selectById(id);
}
@Override
public Page<ProductType> search(ProductType productType,int page, int size) {
QueryWrapper<ProductType> queryWrapper = new QueryWrapper();
if (productType != null){
// 类型名不为空时
if (StringUtils.hasText(productType.getName())){
queryWrapper.like("name",productType.getName());
}
// 上级类型id不为空
if (productType.getParentId()!= null){
queryWrapper.eq("parentId",productType.getParentId());
}
}
return productTypeMapper.selectPage(new Page(page,size),queryWrapper);
}
@Override
public List<ProductType> findProductType(ProductType productType) {
QueryWrapper<ProductType> queryWrapper = new QueryWrapper();
if (productType != null){
// 类型名不为空时
if (StringUtils.hasText(productType.getName())){
queryWrapper.like("name",productType.getName());
}
// 上级类型id不为空
if (productType.getParentId()!= null){
queryWrapper.eq("parentId",productType.getParentId());
}
}
List<ProductType> productTypes = productTypeMapper.selectList(queryWrapper);
return productTypes;
}
}