From 7c835f1dd5a11506dec63a09890828c968efe272 Mon Sep 17 00:00:00 2001 From: puzvv <1@> Date: Thu, 18 Dec 2025 16:28:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=95=86=E5=93=81=E5=88=86=E7=B1=BB=E7=9A=84?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E3=80=81=E4=BF=AE=E6=94=B9=E3=80=81=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E5=8A=9F=E8=83=BD=E5=AE=8C=E6=88=90=EF=BC=8C=E7=8E=B0?= =?UTF-8?q?=E5=9C=A8=E8=83=BD=E5=A4=9F=E6=AD=A3=E7=A1=AE=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=88=86=E7=B1=BB=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Common/Constant/MessageConstant.java | 1 + .../Controllers/ProductController.java | 45 +++++++++++ .../Mapper/ProductCategoryMapper.java | 7 -- .../Impl/ProductCategoryServiceImpl.java | 80 +++++++++++++++++++ .../Service/Impl/ProductServiceImpl.java | 10 +++ .../Service/ProductCategoryService.java | 33 ++++++++ .../pojo/Dto/ProductCategoryDTO.java | 14 ++++ 7 files changed, 183 insertions(+), 7 deletions(-) create mode 100644 src/main/java/icu/sunway/ai_spring_example/Service/Impl/ProductCategoryServiceImpl.java create mode 100644 src/main/java/icu/sunway/ai_spring_example/Service/ProductCategoryService.java create mode 100644 src/main/java/icu/sunway/ai_spring_example/pojo/Dto/ProductCategoryDTO.java diff --git a/src/main/java/icu/sunway/ai_spring_example/Common/Constant/MessageConstant.java b/src/main/java/icu/sunway/ai_spring_example/Common/Constant/MessageConstant.java index db61dbe..8fded17 100644 --- a/src/main/java/icu/sunway/ai_spring_example/Common/Constant/MessageConstant.java +++ b/src/main/java/icu/sunway/ai_spring_example/Common/Constant/MessageConstant.java @@ -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 = "商品有子商品,不能删除"; } diff --git a/src/main/java/icu/sunway/ai_spring_example/Controllers/ProductController.java b/src/main/java/icu/sunway/ai_spring_example/Controllers/ProductController.java index 318c1e0..1951e5a 100644 --- a/src/main/java/icu/sunway/ai_spring_example/Controllers/ProductController.java +++ b/src/main/java/icu/sunway/ai_spring_example/Controllers/ProductController.java @@ -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> getAllCategories() { + List categories = categoryService.getAllCategories(); + return ResponseEntity.success(categories); + } } diff --git a/src/main/java/icu/sunway/ai_spring_example/Mapper/ProductCategoryMapper.java b/src/main/java/icu/sunway/ai_spring_example/Mapper/ProductCategoryMapper.java index ac19911..b3f4ee4 100644 --- a/src/main/java/icu/sunway/ai_spring_example/Mapper/ProductCategoryMapper.java +++ b/src/main/java/icu/sunway/ai_spring_example/Mapper/ProductCategoryMapper.java @@ -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 { - - /** - * 查询所有分类(树形结构) - * @return 分类列表 - */ - List selectAllCategories(); } \ No newline at end of file diff --git a/src/main/java/icu/sunway/ai_spring_example/Service/Impl/ProductCategoryServiceImpl.java b/src/main/java/icu/sunway/ai_spring_example/Service/Impl/ProductCategoryServiceImpl.java new file mode 100644 index 0000000..c940c4a --- /dev/null +++ b/src/main/java/icu/sunway/ai_spring_example/Service/Impl/ProductCategoryServiceImpl.java @@ -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 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 queryWrapper = new LambdaQueryWrapper<>(); + queryWrapper.eq(ProductCategory::getParentId, id); + List 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 getAllCategories() { + QueryWrapper queryWrapper = new QueryWrapper<>(); + return categoryMapper.selectList(queryWrapper); + } +} \ No newline at end of file diff --git a/src/main/java/icu/sunway/ai_spring_example/Service/Impl/ProductServiceImpl.java b/src/main/java/icu/sunway/ai_spring_example/Service/Impl/ProductServiceImpl.java index 612b3d5..9cd7741 100644 --- a/src/main/java/icu/sunway/ai_spring_example/Service/Impl/ProductServiceImpl.java +++ b/src/main/java/icu/sunway/ai_spring_example/Service/Impl/ProductServiceImpl.java @@ -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 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 impl .createTime(product.getCreateTime()) .updateTime(product.getUpdateTime()) .build(); + if (productVO == null) { throw new BusinessException(MessageConstant.PRODUCT_NOT_FOUND); } + QueryWrapper queryWrapperCategory = new QueryWrapper<>(); + queryWrapperCategory.eq("id", product.getCategoryId()); + ProductCategory productCategory = productCategoryMapper.selectOne(queryWrapperCategory); + productVO.setCategoryName(productCategory.getName()); // 查询规格列表 productVO.setSkuList(productSkuMapper.selectByProductId(id)); return productVO; diff --git a/src/main/java/icu/sunway/ai_spring_example/Service/ProductCategoryService.java b/src/main/java/icu/sunway/ai_spring_example/Service/ProductCategoryService.java new file mode 100644 index 0000000..b4eb671 --- /dev/null +++ b/src/main/java/icu/sunway/ai_spring_example/Service/ProductCategoryService.java @@ -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 { + + /** + * 添加商品分类 + * @param categoryDTO 分类信息 + */ + void addCategory(ProductCategoryDTO categoryDTO); + + /** + * 修改商品分类 + * @param categoryDTO 分类信息 + */ + void updateCategory(ProductCategoryDTO categoryDTO); + + /** + * 删除商品分类 + * @param id 分类ID + */ + void deleteCategory(Long id); + + /** + * 查询所有分类(树形结构) + * @return 分类列表 + */ + List getAllCategories(); +} \ No newline at end of file diff --git a/src/main/java/icu/sunway/ai_spring_example/pojo/Dto/ProductCategoryDTO.java b/src/main/java/icu/sunway/ai_spring_example/pojo/Dto/ProductCategoryDTO.java new file mode 100644 index 0000000..243492e --- /dev/null +++ b/src/main/java/icu/sunway/ai_spring_example/pojo/Dto/ProductCategoryDTO.java @@ -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; +}