From 957fa896b8dd9f484e360e3a026fb55e46975902 Mon Sep 17 00:00:00 2001 From: puzvv <1@> Date: Sat, 20 Dec 2025 20:40:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E5=AD=90=E5=88=86?= =?UTF-8?q?=E7=B1=BB=E7=9A=84=E6=9F=A5=E8=AF=A2=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/ProductController.java | 13 +++++++++++-- .../Service/Impl/ProductCategoryServiceImpl.java | 12 +++++++++++- .../Service/ProductCategoryService.java | 4 +++- src/main/resources/mapper/ProductMapper.xml | 11 +++++++++-- 4 files changed, 34 insertions(+), 6 deletions(-) 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 1951e5a..c9199a1 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 @@ -121,11 +121,20 @@ public class ProductController { } /** - * 查询所有商品分类(树形结构) + * 查询所有顶级商品分类(树形结构) */ @GetMapping("/category") public ResponseEntity> getAllCategories() { - List categories = categoryService.getAllCategories(); + List categories = categoryService.getAllTopCategories(); + return ResponseEntity.success(categories); + } + + /** + * 查询子级商品分类 + */ + @GetMapping("/category/{id}") + public ResponseEntity> getChildCategories(@PathVariable Long id) { + List categories = categoryService.getChildCategories(id); return ResponseEntity.success(categories); } } 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 index c940c4a..dd18c2c 100644 --- 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 @@ -73,8 +73,18 @@ public class ProductCategoryServiceImpl extends ServiceImpl getAllCategories() { + public List getAllTopCategories() { QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("is_deleted", 0) + .eq("level",1); + return categoryMapper.selectList(queryWrapper); + } + + @Override + public List getChildCategories(Long id) { + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("is_deleted", 0) + .eq("parent_id", id); return categoryMapper.selectList(queryWrapper); } } \ No newline at end of file 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 index b4eb671..5bd15c8 100644 --- a/src/main/java/icu/sunway/ai_spring_example/Service/ProductCategoryService.java +++ b/src/main/java/icu/sunway/ai_spring_example/Service/ProductCategoryService.java @@ -29,5 +29,7 @@ public interface ProductCategoryService extends IService { * 查询所有分类(树形结构) * @return 分类列表 */ - List getAllCategories(); + List getAllTopCategories(); + + List getChildCategories(Long id); } \ No newline at end of file diff --git a/src/main/resources/mapper/ProductMapper.xml b/src/main/resources/mapper/ProductMapper.xml index 4496ecf..3ebba8f 100644 --- a/src/main/resources/mapper/ProductMapper.xml +++ b/src/main/resources/mapper/ProductMapper.xml @@ -9,10 +9,17 @@ FROM product p LEFT JOIN product_category c ON p.category_id = c.id WHERE p.is_deleted = 0 - AND p.category_id = #{categoryId} + + AND ( + p.category_id = #{categoryId} + OR p.category_id IN ( + SELECT id FROM product_category WHERE parent_id = #{categoryId} AND is_deleted = 0 + ) + ) + AND p.name LIKE CONCAT('%', #{name}, '%') AND p.status = #{status} ORDER BY p.sort ASC, p.create_time DESC - \ No newline at end of file +