@Override
public List<DeptDTO> findAllTree() {
List<Dept> deptList = findAll();
List<DeptDTO> rootList = deptList.stream().filter(dept -> dept.getPid().equals(0)).map(dept -> DeptDTO.adapt(dept)).collect(Collectors.toList());
format2tree(rootList, deptList);
return rootList;
}
/**
* 树状格式化
* @param rootList 最顶级列表
* @param allList 所有列表
*/
private void format2tree(List<DeptDTO> rootList, List<Dept> allList) {
for (DeptDTO deptDTO : rootList) {
List<DeptDTO> nextList = new ArrayList<>();
for (Dept item : allList) {
if (deptDTO.getId().equals(item.getPid())) {
nextList.add(DeptDTO.adapt(item));
}
}
if (!CollectionUtils.isEmpty(nextList)) {
deptDTO.setChildren(nextList);
format2tree(nextList, allList);
}
}
}