//module为根节点id
public List<ModuleChildOutVo> getModuleByProjectNo(String moduleId) {
//查询该根模块id下所有模块
LambdaQueryWrapper<Module> moduleLbq = new LambdaQueryWrapper<>();
//设置查询条件
moduleLbq.eq(Module::getId,moduleId);
//查询
List<Module> moduleList = this.list(moduleLbq);
//存放根节点
List<ModuleChildOutVo> rootModule = new ArrayList<>();
//循环模块集合把第一级过滤出来
moduleList.stream().forEach(module->{
if(module.getLevel().equals("0")){
ModuleChildOutVo moduleChildOutVo = new ModuleChildOutVo();
BeanUtils.copyProperties(module,moduleChildOutVo);
rootModule.add(moduleChildOutVo);
}
});
//为根模块设置子模块,getClild是递归调用的
for (ModuleChildOutVo module : rootModule) {
/* 获取根节点下的所有子节点 使用getChild方法*/
List<ModuleChildOutVo> childList = getChild(module.getId(), moduleList);
module.setModuleChilds(childList);//给根节点设置子节点
}
return rootModule;
}
/**
* 获取子模块
* @param id 父节点id
* @param allModule 所有模块列表
* @return 每个根节点下,所有子模块列表
*/
public List<ModuleChildOutVo> getChild(Integer id, List<Module> allModule){
//子模块
List<ModuleChildOutVo> childList = new ArrayList<ModuleChildOutVo>();
for (Module module : allModule) {
// 遍历所有节点,将所有模块的父id与传过来的根节点的id比较
//相等说明:为该根节点的子节点。
if(module.getParentId().equals(id+"")){
ModuleChildOutVo moduleChildOutVo = new ModuleChildOutVo();
BeanUtils.copyProperties(module,moduleChildOutVo);
childList.add(moduleChildOutVo);
}
}
//递归
for (ModuleChildOutVo nav : childList) {
nav.setModuleChilds(getChild(nav.getId(), allModule));
}
//Collections.sort(childList,order());//排序
//如果节点下没有子节点,返回一个空List(递归退出)
if(childList.size() == 0){
return new ArrayList<ModuleChildOutVo>();
}
return childList;
}
版权声明:本文为Myselfh原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。