添加了子分类的查询方法

This commit is contained in:
puzvv
2025-12-20 20:40:29 +08:00
parent 2b8f2307b7
commit 957fa896b8
4 changed files with 34 additions and 6 deletions

View File

@@ -121,11 +121,20 @@ public class ProductController {
}
/**
* 查询所有商品分类(树形结构)
* 查询所有顶级商品分类(树形结构)
*/
@GetMapping("/category")
public ResponseEntity<List<ProductCategory>> getAllCategories() {
List<ProductCategory> categories = categoryService.getAllCategories();
List<ProductCategory> categories = categoryService.getAllTopCategories();
return ResponseEntity.success(categories);
}
/**
* 查询子级商品分类
*/
@GetMapping("/category/{id}")
public ResponseEntity<List<ProductCategory>> getChildCategories(@PathVariable Long id) {
List<ProductCategory> categories = categoryService.getChildCategories(id);
return ResponseEntity.success(categories);
}
}

View File

@@ -73,8 +73,18 @@ public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMappe
}
@Override
public List<ProductCategory> getAllCategories() {
public List<ProductCategory> getAllTopCategories() {
QueryWrapper<ProductCategory> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("is_deleted", 0)
.eq("level",1);
return categoryMapper.selectList(queryWrapper);
}
@Override
public List<ProductCategory> getChildCategories(Long id) {
QueryWrapper<ProductCategory> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("is_deleted", 0)
.eq("parent_id", id);
return categoryMapper.selectList(queryWrapper);
}
}

View File

@@ -29,5 +29,7 @@ public interface ProductCategoryService extends IService<ProductCategory> {
* 查询所有分类(树形结构)
* @return 分类列表
*/
List<ProductCategory> getAllCategories();
List<ProductCategory> getAllTopCategories();
List<ProductCategory> getChildCategories(Long id);
}

View File

@@ -9,10 +9,17 @@
FROM product p
LEFT JOIN product_category c ON p.category_id = c.id
WHERE p.is_deleted = 0
<if test="categoryId != null">AND p.category_id = #{categoryId}</if>
<if test="categoryId != null">
AND (
p.category_id = #{categoryId}
OR p.category_id IN (
SELECT id FROM product_category WHERE parent_id = #{categoryId} AND is_deleted = 0
)
)
</if>
<if test="name != null and name != ''">AND p.name LIKE CONCAT('%', #{name}, '%')</if>
<if test="status != null">AND p.status = #{status}</if>
ORDER BY p.sort ASC, p.create_time DESC
</select>
</mapper>
</mapper>