商品分类的添加、修改、删除功能完成,现在能够正确获取分类名称

This commit is contained in:
puzvv
2025-12-18 16:28:32 +08:00
parent d179c9d96b
commit 7c835f1dd5
7 changed files with 183 additions and 7 deletions

View File

@@ -18,4 +18,5 @@ public class MessageConstant {
public static final String ORDER_STATUS_ERROR = "订单状态错误";
public static final String ORDER_NOT_FOUND = "订单不存在";
public static final String PRODUCT_NOT_FOUND = "商品不存在";
public static final String PRODUCT_HAS_CHILDREN = "商品有子商品,不能删除";
}

View File

@@ -2,8 +2,11 @@ package icu.sunway.ai_spring_example.Controllers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import icu.sunway.ai_spring_example.Common.Response.ResponseEntity;
import icu.sunway.ai_spring_example.Service.ProductCategoryService;
import icu.sunway.ai_spring_example.Service.ProductService;
import icu.sunway.ai_spring_example.pojo.Dto.ProductCategoryDTO;
import icu.sunway.ai_spring_example.pojo.Dto.ProductDTO;
import icu.sunway.ai_spring_example.pojo.Entity.ProductCategory;
import icu.sunway.ai_spring_example.pojo.Vo.ProductListVO;
import icu.sunway.ai_spring_example.pojo.Vo.ProductVO;
import lombok.extern.slf4j.Slf4j;
@@ -11,6 +14,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@@ -21,6 +25,9 @@ public class ProductController {
@Autowired
private ProductService productService;
@Autowired
private ProductCategoryService categoryService;
/**
* 新增商品
*/
@@ -83,4 +90,42 @@ public class ProductController {
ProductVO productVO = productService.getProductDetail(id);
return ResponseEntity.success(productVO);
}
/**
* 添加商品分类
*/
@PostMapping("/category")
public ResponseEntity addCategory(@RequestBody ProductCategoryDTO categoryDTO) {
log.info("新增商品分类: {}", categoryDTO);
categoryService.addCategory(categoryDTO);
return ResponseEntity.success("分类新增成功");
}
/**
* 修改商品分类
*/
@PutMapping("/category")
public ResponseEntity updateCategory(@RequestBody ProductCategoryDTO categoryDTO) {
log.info("修改商品分类: {}", categoryDTO);
categoryService.updateCategory(categoryDTO);
return ResponseEntity.success("分类修改成功");
}
/**
* 删除商品分类
*/
@DeleteMapping("/category/{id}")
public ResponseEntity deleteCategory(@PathVariable Long id) {
log.info("删除商品分类: id={}", id);
categoryService.deleteCategory(id);
return ResponseEntity.success("分类删除成功");
}
/**
* 查询所有商品分类(树形结构)
*/
@GetMapping("/category")
public ResponseEntity<List<ProductCategory>> getAllCategories() {
List<ProductCategory> categories = categoryService.getAllCategories();
return ResponseEntity.success(categories);
}
}

View File

@@ -4,14 +4,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import icu.sunway.ai_spring_example.pojo.Entity.ProductCategory;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface ProductCategoryMapper extends BaseMapper<ProductCategory> {
/**
* 查询所有分类(树形结构)
* @return 分类列表
*/
List<ProductCategory> selectAllCategories();
}

View File

@@ -0,0 +1,80 @@
package icu.sunway.ai_spring_example.Service.Impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import icu.sunway.ai_spring_example.Common.Constant.MessageConstant;
import icu.sunway.ai_spring_example.Mapper.ProductCategoryMapper;
import icu.sunway.ai_spring_example.Service.ProductCategoryService;
import icu.sunway.ai_spring_example.pojo.Dto.ProductCategoryDTO;
import icu.sunway.ai_spring_example.pojo.Entity.ProductCategory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;
@Service
@Slf4j
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements ProductCategoryService {
@Autowired
private ProductCategoryMapper categoryMapper;
@Override
@Transactional
public void addCategory(ProductCategoryDTO categoryDTO) {
ProductCategory category = new ProductCategory();
BeanUtils.copyProperties(categoryDTO, category);
// 设置默认值如果DTO中未传递
if (category.getParentId() == null) {
category.setParentId(0L); // 默认为一级分类
}
if (category.getSort() == null) {
category.setSort(0);
}
if (category.getIsDeleted() == null) {
category.setIsDeleted(0);
}
categoryMapper.insert(category);
log.info("新增商品分类: {}", category);
}
@Override
@Transactional
public void updateCategory(ProductCategoryDTO categoryDTO) {
// 验证分类是否存在
ProductCategory existingCategory = categoryMapper.selectById(categoryDTO.getId());
if (existingCategory == null) {
throw new RuntimeException("商品分类不存在");
}
// 复制更新字段
BeanUtils.copyProperties(categoryDTO, existingCategory);
existingCategory.setUpdateTime(LocalDateTime.now()); // 更新时间
categoryMapper.updateById(existingCategory);
log.info("修改商品分类: {}", existingCategory);
}
@Override
@Transactional
public void deleteCategory(Long id) {
// 检查是否有子分类
LambdaQueryWrapper<ProductCategory> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(ProductCategory::getParentId, id);
List<ProductCategory> children = categoryMapper.selectList(queryWrapper);
if (!children.isEmpty()) {
throw new RuntimeException(MessageConstant.PRODUCT_HAS_CHILDREN);
}
// 逻辑删除字段isDeleted设置为1
categoryMapper.deleteById(id);
log.info("删除商品分类: id={}", id);
}
@Override
public List<ProductCategory> getAllCategories() {
QueryWrapper<ProductCategory> queryWrapper = new QueryWrapper<>();
return categoryMapper.selectList(queryWrapper);
}
}

View File

@@ -7,12 +7,14 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import icu.sunway.ai_spring_example.Common.Exception.BusinessException;
import icu.sunway.ai_spring_example.Common.Constant.MessageConstant;
import icu.sunway.ai_spring_example.Mapper.ProductCategoryMapper;
import icu.sunway.ai_spring_example.Mapper.ProductMapper;
import icu.sunway.ai_spring_example.Mapper.ProductSkuMapper;
import icu.sunway.ai_spring_example.Service.ProductService;
import icu.sunway.ai_spring_example.pojo.Dto.ProductDTO;
import icu.sunway.ai_spring_example.pojo.Dto.ProductSkuDTO;
import icu.sunway.ai_spring_example.pojo.Entity.Product;
import icu.sunway.ai_spring_example.pojo.Entity.ProductCategory;
import icu.sunway.ai_spring_example.pojo.Entity.ProductSku;
import icu.sunway.ai_spring_example.pojo.Vo.ProductListVO;
import icu.sunway.ai_spring_example.pojo.Vo.ProductVO;
@@ -35,6 +37,9 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> impl
@Autowired
private ProductSkuMapper productSkuMapper;
@Autowired
private ProductCategoryMapper productCategoryMapper;
@Override
@Transactional
public void saveProduct(ProductDTO productDTO) {
@@ -116,9 +121,14 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> impl
.createTime(product.getCreateTime())
.updateTime(product.getUpdateTime())
.build();
if (productVO == null) {
throw new BusinessException(MessageConstant.PRODUCT_NOT_FOUND);
}
QueryWrapper<ProductCategory> queryWrapperCategory = new QueryWrapper<>();
queryWrapperCategory.eq("id", product.getCategoryId());
ProductCategory productCategory = productCategoryMapper.selectOne(queryWrapperCategory);
productVO.setCategoryName(productCategory.getName());
// 查询规格列表
productVO.setSkuList(productSkuMapper.selectByProductId(id));
return productVO;

View File

@@ -0,0 +1,33 @@
package icu.sunway.ai_spring_example.Service;
import com.baomidou.mybatisplus.extension.service.IService;
import icu.sunway.ai_spring_example.pojo.Dto.ProductCategoryDTO;
import icu.sunway.ai_spring_example.pojo.Entity.ProductCategory;
import java.util.List;
public interface ProductCategoryService extends IService<ProductCategory> {
/**
* 添加商品分类
* @param categoryDTO 分类信息
*/
void addCategory(ProductCategoryDTO categoryDTO);
/**
* 修改商品分类
* @param categoryDTO 分类信息
*/
void updateCategory(ProductCategoryDTO categoryDTO);
/**
* 删除商品分类
* @param id 分类ID
*/
void deleteCategory(Long id);
/**
* 查询所有分类(树形结构)
* @return 分类列表
*/
List<ProductCategory> getAllCategories();
}

View File

@@ -0,0 +1,14 @@
package icu.sunway.ai_spring_example.pojo.Dto;
import lombok.Data;
@Data
public class ProductCategoryDTO {
private Long id;
private Long parentId;
private String name;
private Integer status;
private Integer sort;
private String icon;
private Integer level;
}