更新
This commit is contained in:
parent
8f2f872467
commit
d7a461cc83
@ -8,6 +8,7 @@ import com.ruoyi.cms.core.NewTreeSelect;
|
||||
import com.ruoyi.cms.domain.CmsCategory;
|
||||
import com.ruoyi.cms.domain.CmsContent;
|
||||
import com.ruoyi.cms.domain.vo.CMSCategoryVo;
|
||||
import com.ruoyi.cms.domain.vo.TreeVo;
|
||||
import com.ruoyi.cms.query.CmsCategoryQuery;
|
||||
import com.ruoyi.cms.query.CmsContentQuery;
|
||||
import com.ruoyi.common.core.domain.TreeSelect;
|
||||
@ -137,4 +138,6 @@ public interface ICmsCategoryService extends IService<CmsCategory>
|
||||
* 获取某个栏目的子栏目
|
||||
*/
|
||||
List<CmsCategory> getCategoryIdByParentId(Long id);
|
||||
|
||||
List<TreeVo> getLeavesCategoryTree();
|
||||
}
|
||||
|
@ -1,8 +1,6 @@
|
||||
package com.ruoyi.cms.service.impl;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
@ -14,6 +12,7 @@ import com.github.pagehelper.PageInfo;
|
||||
import com.ruoyi.cms.core.NewTreeSelect;
|
||||
import com.ruoyi.cms.domain.CmsContent;
|
||||
import com.ruoyi.cms.domain.vo.CMSCategoryVo;
|
||||
import com.ruoyi.cms.domain.vo.TreeVo;
|
||||
import com.ruoyi.cms.mapper.CmsContentMapper;
|
||||
import com.ruoyi.cms.query.CmsCategoryQuery;
|
||||
import com.ruoyi.cms.query.CmsContentQuery;
|
||||
@ -300,4 +299,78 @@ public class CmsCategoryServiceImpl extends ServiceImpl<CmsCategoryMapper, CmsCa
|
||||
public List<CmsCategory> getCategoryIdByParentId(Long id) {
|
||||
return baseMapper.selectList(new QueryWrapper<CmsCategory>().eq("parent_id", id).eq("is_disable", 0).eq("del_flag", 0).orderByAsc("category_sort"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TreeVo> getLeavesCategoryTree() {
|
||||
List<CmsCategory> cmsCategories = baseMapper.selectList(new QueryWrapper<CmsCategory>());
|
||||
List<CmsCategory> cmsCategories1 = buildCategoryTree(cmsCategories);
|
||||
List<TreeVo> treeVos = new ArrayList<>();
|
||||
for (CmsCategory cmsCategory : cmsCategories1) {
|
||||
TreeVo treeVo = new TreeVo();
|
||||
treeVo.setLabel(cmsCategory.getCategoryName());
|
||||
treeVo.setValue(cmsCategory.getId().toString());
|
||||
List<TreeVo> children = new ArrayList<>();
|
||||
cmsCategory.getChildren().forEach(cmsCategory1 -> {
|
||||
TreeVo treeVo1 = new TreeVo();
|
||||
treeVo1.setLabel(cmsCategory1.getCategoryName());
|
||||
treeVo1.setValue(cmsCategory1.getId().toString());
|
||||
children.add(treeVo1);
|
||||
});
|
||||
treeVo.setChildren(children);
|
||||
treeVos.add(treeVo);
|
||||
}
|
||||
return treeVos;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将栏目列表转换为树形结构
|
||||
*
|
||||
* @param categoryList 栏目列表
|
||||
* @return 树形结构的栏目列表
|
||||
*/
|
||||
public List<CmsCategory> buildCategoryTree(List<CmsCategory> categoryList) {
|
||||
// 创建一个Map,用于存储栏目ID和栏目对象的映射关系
|
||||
Map<Long, CmsCategory> categoryMap = new HashMap<>();
|
||||
|
||||
// 创建一个列表,用于存储根栏目(即没有父栏目的栏目)
|
||||
List<CmsCategory> rootCategories = new ArrayList<>();
|
||||
|
||||
// 第一次遍历:将所有栏目放入Map中,并找到根栏目
|
||||
for (CmsCategory category : categoryList) {
|
||||
categoryMap.put(category.getId(), category);
|
||||
if (category.getParentId() == null || category.getParentId() == 0) {
|
||||
rootCategories.add(category);
|
||||
}
|
||||
}
|
||||
|
||||
// 第二次遍历:将子栏目放入对应的父栏目的children列表中
|
||||
for (CmsCategory category : categoryList) {
|
||||
if (category.getParentId() != null && category.getParentId() != 0) {
|
||||
CmsCategory parentCategory = categoryMap.get(category.getParentId());
|
||||
if (parentCategory != null) {
|
||||
parentCategory.getChildren().add(category);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return rootCategories;
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归打印树形结构的栏目
|
||||
*
|
||||
* @param categories 栏目列表
|
||||
* @param level 当前层级
|
||||
*/
|
||||
private static void printCategoryTree(List<CmsCategory> categories, int level) {
|
||||
for (CmsCategory category : categories) {
|
||||
for (int i = 0; i < level; i++) {
|
||||
System.out.print("--");
|
||||
}
|
||||
System.out.println(category.getCategoryName());
|
||||
if (!category.getChildren().isEmpty()) {
|
||||
printCategoryTree(category.getChildren(), level + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user