//module为根节点id
public List getModuleByProjectNo(String moduleId) {
//查询该根模块id下所有模块
LambdaQueryWrapper moduleLbq = new LambdaQueryWrapper<>();
//设置查询条件
moduleLbq.eq(Module::getId,moduleId);
//查询
List moduleList = this.list(moduleLbq);
//存放根节点
List 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) {
List childList = getChild(module.getId(), moduleList);
module.setModuleChilds(childList);//给根节点设置子节点
}
return rootModule;
}
public List getChild(Integer id, List allModule){
//子模块
List childList = new ArrayList();
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();
}
return childList;
}