public List<SysDept> buildDeptTree(List<SysDept> depts){
//存放构建好的部门树
ArrayList<SysDept> deptTrees=new ArrayList<>();
ArrayList<SysDept> parents= new ArrayList<>();
//寻找顶级部门树
for (SysDept dept : depts) {
SysDept deptDto=buildParent(dept,depts);
if (!parents.contains(deptDto)){
parents.add(deptDto);
}
}
//构建部门数
for (SysDept parent : parents) {
SysDept tree=bulidTree(parent,depts);
deptTrees.add(tree);
}
return deptTrees;
}
//寻找子部门构建树
public SysDept bulidTree(SysDept parent,List<SysDept> depts){
ArrayList<SysDept> childrens= new ArrayList<>();
for (SysDept dept : depts) {
if (parent.getDeptId().equals(dept.getParentId())){
childrens.add(bulidTree(dept,depts));
}
}
parent.setChildren(childrens);
return parent;
}
//寻找父部门
public SysDept buildParent(SysDept sysDept,List<SysDept> depts){
SysDept parent=sysDept;
for (SysDept dept : depts) {
//如果存在递归往上找父部门
if (sysDept.getParentId().equals(dept.getDeptId())){
parent=buildParent(dept,depts);
}
}
return parent;
}
通过递归的方式查询部门树(传递list集合)
于 2023-11-13 09:12:59 首次发布