# Conflicts:
#	dl_admin/ruoyi-admin/src/main/java/com/ruoyi/busi/service/impl/BusiCategoryServiceImpl.java
This commit is contained in:
Vinjor 2025-06-30 16:01:36 +08:00
commit 045ead8de4

View File

@ -47,10 +47,10 @@ public class BusiCategoryServiceImpl extends ServiceImpl<BusiCategoryMapper, Bus
if(StringUtils.isNotEmpty(category.getCatgType())){
lambdaQueryWrapper.eq(BusiCategory::getCatgType,category.getCatgType());
}
lambdaQueryWrapper.orderByDesc(BusiCategory::getSort);
if (!StringUtils.isEmpty(category.getCatgName())) {
lambdaQueryWrapper.like(BusiCategory::getCatgName, category.getCatgName());
}
lambdaQueryWrapper.orderByDesc(BusiCategory::getSort);
List<BusiCategory> list = list(lambdaQueryWrapper);
return buildCategoryTree(list);
}
@ -78,17 +78,91 @@ public class BusiCategoryServiceImpl extends ServiceImpl<BusiCategoryMapper, Bus
item.setProdsJunior(idToProdsJunior.get(item.getId()));
}
});
List<BusiCategory> saveResult = computeProdsAll(categoryList);
updateBatchById(saveResult);
}
/**
* 统计每个节点的递归产品数并返回更新后的列表
*
* @param categoryList List<BusiCategory>
* @return java.util.List<com.ruoyi.busi.domain.BusiCategory>
* @author PQZ
* @date 15:07 2025/6/30
**/
private List<BusiCategory> computeProdsAll(List<BusiCategory> categoryList) {
if (categoryList == null || categoryList.isEmpty()) {
return categoryList;
}
// 构建id到节点的映射便于查找
Map<String, BusiCategory> idToCategory = new HashMap<>();
for (BusiCategory category : categoryList) {
idToCategory.put(category.getId(), category);
}
// 构建树结构找出根节点
BusiCategory root = null;
for (BusiCategory category : categoryList) {
if (category.getParentId() == null) {
root = category;
}
}
// 递归统计
calculateProdsAll(root, categoryList);
return categoryList;
}
/**
* 递归计算子节点数据
*
* @param node 根节点
* @param categories 全部数据
* @return java.lang.Integer
* @author PQZ
* @date 15:24 2025/6/30
**/
private static Integer calculateProdsAll(BusiCategory node, List<BusiCategory> categories) {
if (node == null) {
return 0;
}
Integer sum = 0;
for (BusiCategory child : getChildren(node, categories)) {
sum += calculateProdsAll(child, categories);
}
Integer currentProdsJunior = (node.getProdsJunior() != null) ? node.getProdsJunior() : 0;
node.setProdsAll(sum + currentProdsJunior);
;
return node.getProdsAll();
}
/**
* 获取某节点的子节点
*
* @param parent 父节点
* @param categories 所有栏目
* @return java.util.List<com.ruoyi.busi.domain.BusiCategory>
* @author PQZ
* @date 15:24 2025/6/30
**/
private static List<BusiCategory> getChildren(BusiCategory parent, List<BusiCategory> categories) {
List<BusiCategory> children = new ArrayList<>();
for (BusiCategory cat : categories) {
if (parent.getId().equals(cat.getParentId())) {
children.add(cat);
}
}
return children;
}
/**
* 生成树结构
* @author PQZ
* @date 14:11 2025/6/23
*
* @param list 网站栏目列表
* @return java.util.List<com.ruoyi.busi.vo.BusiCategoryVO>
* @author PQZ
* @date 14:11 2025/6/23
**/
private List<BusiCategoryVO> buildCategoryTree(List<BusiCategory> list) {
// Map存放id到VO的映射