获取树
List<DepList> treeList = new ArrayList<>();
List<DepList> originalList = baseMapper.getDepList();
for (DepList d : originalList) {
if (d.getParentId() == 0 && ids.contains(d.getId())) {
treeList.add(getChildrenWithRole(d, originalList, ids));
}
}
递归
private DepList getChildrenWithRole(DepList dep, List<DepList> originalList, Set<Long> ids) {
for (DepList p : originalList) {
if (p.getParentId().equals(dep.getId()) && ids.contains(p.getId())) {
if (Objects.isNull(dep.getChildren())) {
dep.setChildren(new LinkedList<>());
}
dep.getChildren().add(getChildrenWithRole(p, originalList, ids));
}
}
return dep;
}
对子集遍历,插入人员信息
for (DepList dept : treeList) {
getTreeUserData(dept);
}
递归
private void getTreeUserData(DepList dept) {
List<DepList> deptUser = userDepartmentService.getDepListByDeptId(dept.getId());
List<DepList> t = dept.getChildren();
if (CollectionUtils.isNotEmpty(t)) {
for (DepList t1 : t) {
getTreeUserData(t1);
}
t.addAll(deptUser);
} else {
dept.setChildren(deptUser);
}
}