该段代码定义了一个queryOrganizationsTree方法,其目的是构建并返回一个组织架构树(OrganizationsTree)。这个树状结构反映了组织或部门的层级关系
以下是该函数的详细解释:
-
首先,通过调用
DepartmentInfoDao.querySetupBusinessGroupAll()方法,从数据库中获取所有的组织部门信息,这些信息被存储在departAll列表中。 -
接着,使用Java 8的流操作对
departAll进行处理。通过Collectors.groupingBy方法,以parentId为分组依据,将所有组织部门信息分组存储到一个masterMap中。这个映射表的键是父组织的ID,值是属于该父组织的所有子组织列表。 -
创建一个新的
OrganizationsTree对象,该对象代表组织结构树的一个节点。设置根节点ID和NAME,这通常代表整个组织的根节点。 -
调用
getOrganizationTree方法,传入根节点的ID和之前创建的masterMap,该方法会根据父子关系递归地构建出完整的组织结构树。返回的子节点列表被设置为根节点的子节点。 -
最后,返回构建好的组织结构树的根节点。
POJO:
//list数据库PO
public class DepartmentInfoPO {
/**
* 部门ID
*/
private Integer deptId;
/**
* 名称
*/
private String deptName;
/**
* 父节点ID
*/
private Integer parentId;
}
//tree结构bo
public class OrganizationsTree {
//ID
private Integer id;
//名称
private String label;
//下级部门
private List<OrganizationsTree> children;
public OrganizationsTree(Integer id, String label) {
this.id = id;
this.label = label;
}
}
实现方法:
/**
* 获取组织
* @return
*/
private OrganizationsTree queryOrganizationsTree() {
//查询数据库list
List<DepartmentInfoPO> departAll = departmentInfoDao.querySetupBusinessGroupAll();
// 分组
Map<Integer, List<DepartmentInfoPO>> masterMap = departAll.stream().collect(Collectors.groupingBy(DepartmentInfoPO::getParentId));
OrganizationsTree organizationsTree = new OrganizationsTree();
organizationsTree.setId("根节点ID");
organizationsTree.setLabel("根节点NAME");
List<OrganizationsTree> departmentInfoNext = getOrganizationTree(GroupConstants.BAI_DU_DEPT_ID, masterMap);
organizationsTree.setChildren(departmentInfoNext);
return organizationsTree;
}
/**
* 获取本部门的所有子部门信息列表
*/
public List<OrganizationsTree> getOrganizationTree(Integer dept, Map<Integer, List<DepartmentInfoPO>> masterMap) {
List<OrganizationsTree> list = new ArrayList<>();
// 获取本部门下的所有子部门
List<DepartmentInfoPO> departmentInfoList = masterMap.get(dept);
if (CollectionUtils.isNotEmpty(departmentInfoList)) {
for (DepartmentInfoPO departmentInfo : departmentInfoList) {
Integer deptId = departmentInfo.getDeptId();
OrganizationsTree organizationsTree = new OrganizationsTree();
organizationsTree.setId(deptId);
organizationsTree.setLabel(departmentInfo.getDeptName());
// 递归至末级部门
List<OrganizationsTree> infoNext = this.getOrganizationTree(deptId, masterMap);
if (CollectionUtils.isNotEmpty(infoNext)) {
organizationsTree.setChildren(infoNext);
}
list.add(organizationsTree);
}
}
return list;
}
3602

被折叠的 条评论
为什么被折叠?



