当前位置:网站首页>递归处理组织的几种情况
递归处理组织的几种情况
2022-07-03 00:11:00 【泊船夜雪,他年可期】
递归处理组织的几种情况
前言
近期在工作中的一点小总结,在此进行记录。
递归封装树形结构组织
/**
* 树形结构封装
*
* @param root
* @param all
* @return
*/
private List<OrgVO> getChildren(OrgVO root, List<OrgVO> all) {
return all.stream().filter(orgVO ->
root.getId().equals(orgVO.getParentId())).map(orgVO -> {
orgVO.setChildrenEntity(getChildren(orgVO, all));
return orgVO;
}).sorted(Comparator.comparingInt(c -> (c.getSort() == null ? 0 : c.getSort()))).collect(Collectors.toList());
}
递归获取子孙机构
/**
* 递归获取子孙机构
* @param orgCode
* @return
*/
private List<Org> getAllChildren(String orgCode){
List<Org> allChildren = new ArrayList<>();
List<Org> orgListByParent = OrgService.getOrgListByParent(orgCode);
// 这里判断 size 大于 1 的原因是 getOrgListByParent() 方法如果没有获取到子孙机构则返回当前机构信息
if (CollectionUtils.isNotEmpty(orgListByParent) && orgListByParent.size() > 1){
allChildren.addAll(orgListByParent);
for (Org org : orgListByParent) {
List<Org> children = getAllChildren(organization.getOrgCode());
allChildren.addAll(children);
}
}
return allChildren;
}
递归累加子孙机构人数
/**
* 递归统计当前+子孙部门人数,并返回当前部门及子级
* @param root
* @param data
* @return
*/
private List<UserDTO> getUserNum(UserDTO root, List<UserDTO> data){
Integer userCount = root.getUserCount();
data.forEach(dto -> {
if (root.getOrgCode().equals(dto.getParentCode())){
root.setUserCount(userCount+dto.getUserCount());
List<UserDTO> collect = data.stream().filter(dto1 -> dto1.getParentCode().equals(dto.getOrgCode())).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(collect)){
getUserNum(dto,collect);
}
}
});
List<UserDTO> collect = data.stream().filter(userDTO -> userDTO.getParentCode().equals(root.getOrgCode())).collect(Collectors.toList());
root.userCount(collect.stream().map(UserDTO::getUserCount).mapToInt(Integer::intValue).sum()+userCount);
collect.add(0,root);
return collect;
}
总结
递归统计人数该方法可能会出现异常,使用中需注意。
- END -边栏推荐
- 图解网络:什么是虚拟路由器冗余协议 VRRP?
- Lu Zhe, chief scientist of Shiping information: building data and personnel centered security capabilities
- antv x6节点拖拽到画布上后的回调事件(踩大坑记录)
- University of Toronto: Anthony coach | the conditions of deep reinforcement learning can induce dynamic risk measurement
- Solution to the problem of abnormal display of PDF exported Chinese documents of confluence
- 机器学习:numpy版本线性回归预测波士顿房价
- Hdu3507 (slope DP entry)
- 文件操作IO-Part2
- The "2022 China Digital Office Market Research Report" can be downloaded to explain the 176.8 billion yuan market in detail
- Test shift right: Elk practice of online quality monitoring
猜你喜欢
University of Oslo: Li Meng | deep reinforcement learning based on swing transformer
Illustrated network: what is virtual router redundancy protocol VRRP?
tail -f 、tail -F、tailf的区别
Win10 多种方式解决无法安装.Net3.5的问题
使用jenkins之二Job
Nacos+openfeign error reporting solution
antv x6节点拖拽到画布上后的回调事件(踩大坑记录)
Leetcode-849: maximum distance to the nearest person
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
Arduino开发之按键检测与正弦信号输出
随机推荐
【Pulsar文档】概念和架构/Concepts and Architecture
2022 list of manufacturers of Chinese 3D vision enterprises (guided positioning and sorting scenes)
Why is the website slow to open?
Kubernetes resource object introduction and common commands (V) - (NFS & PV & PVC)
瑞萨电子RZ/G2L开发板上手评测
如何系统学习机器学习
Centos7 one click compilation to build MySQL script
测试右移:线上质量监控 ELK 实战
Vulkan-性能及精细化
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
Test shift right: Elk practice of online quality monitoring
[Luogu p4320] road meets (round square tree)
数组与集合性能比较
【案例分享】让新时代教育发展与“数”俱进
leetcode-849:到最近的人的最大距离
Solution to the problem of abnormal display of PDF exported Chinese documents of confluence
leetcode-2280:表示一个折线图的最少线段数
Leetcode-934: the shortest Bridge
[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)
【AutoSAR 五 方法论】