当前位置:网站首页>递归处理组织的几种情况
递归处理组织的几种情况
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 -边栏推荐
- 腾讯云免费SSL证书扩展文件含义
- [IELTS reading] Wang Xiwei reading P2 (reading fill in the blank)
- RK3568开发板评测篇(二):开发环境搭建
- Nc17059 queue Q
- leetcode-849:到最近的人的最大距离
- AttributeError: ‘tuple‘ object has no attribute ‘layer‘问题解决
- Deep analysis of data storage in memory
- 【AutoSAR 八 OS】
- [Luogu p4320] road meets (round square tree)
- Basic use of shell script
猜你喜欢
【AutoSAR 二 AppL概述】
The "2022 China Digital Office Market Research Report" can be downloaded to explain the 176.8 billion yuan market in detail
kubernetes资源对象介绍及常用命令(五)-(NFS&PV&PVC)
Detailed explanation of pod life cycle
How SQLSEVER removes data with duplicate IDS
为什么网站打开速度慢?
The difference between tail -f, tail -f and tail
基于ARM RK3568的红外热成像体温检测系统
One of the reasons why setinterval timer does not take effect in ie: the callback is the arrow function
Lu Zhe, chief scientist of Shiping information: building data and personnel centered security capabilities
随机推荐
【Pulsar文档】概念和架构/Concepts and Architecture
Logback configuration file
Meaning of Tencent cloud free SSL certificate extension file
cordova-plugin-device获取设备信息插件导致华为审核不通过
[daily training] 871 Minimum refueling times
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
Test shift right: Elk practice of online quality monitoring
【AutoSAR 十 IO架构】
Use Jenkins II job
数组与集合性能比较
Sentry developer contribution Guide - configure pycharm
【雅思阅读】王希伟阅读P2(阅读填空)
Some introduction and precautions about XML
Extension of flutter
瑞萨RZ/G2L ARM开发板存储读写速度与网络实测
Rust ownership (very important)
机器学习:numpy版本线性回归预测波士顿房价
如何系统学习机器学习
1.11 - bus
Lex & yacc & bison & flex configuration problems