当前位置:网站首页>递归处理组织的几种情况
递归处理组织的几种情况
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 -边栏推荐
- Array and collection performance comparison
- University of Toronto: Anthony coach | the conditions of deep reinforcement learning can induce dynamic risk measurement
- Linux软件:如何安装Redis服务
- 文件操作IO-Part2
- Automated defect analysis in electronic microscopic images
- 瑞萨RZ/G2L 处理器简介|框架图|功耗|原理图及硬件设计指南
- node_ Modules cannot be deleted
- leetcode-1964:找出到每个位置为止最长的有效障碍赛跑路线
- Unity learns from spaceshooter to record the difference between fixedupdate and update in unity for the second time
- 奥斯陆大学:Li Meng | 基于Swin-Transformer的深度强化学习
猜你喜欢
基于ARM RK3568的红外热成像体温检测系统
[shutter] image component (load network pictures | load static pictures | load local pictures | path | provider plug-in)
【AutoSAR 五 方法论】
图解网络:什么是虚拟路由器冗余协议 VRRP?
Rust ownership (very important)
【AutoSAR 四 BSW概述】
1.12 - Instructions
[AUTOSAR II appl overview]
[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)
ftrace工具的介绍及使用
随机推荐
Teach you JDBC hand in hand -- structure separation
全志A40i/T3如何通过SPI转CAN
Why is the website slow to open?
[pulsar document] concepts and architecture
【雅思阅读】王希伟阅读P2(阅读填空)
【AutoSAR 九 C/S原理架构】
解决ReactNative使用webView存在缓存问题
Leetcode-849: maximum distance to the nearest person
Arduino开发之按键检测与正弦信号输出
微信小程序获取某个元素的信息(高、宽等),并将px转换为rpx。
飞凌搭载TI AM62x的ARM核心板/开发板首发上市,亮相Embedded World 2022
瑞萨RZ/G2L ARM开发板存储读写速度与网络实测
【AutoSAR 十三 NVM】
Attributeerror: 'tuple' object has no attribute 'layer' problem solving
[IELTS reading] Wang Xiwei reading P2 (reading fill in the blank)
2022上半年值得被看见的10条文案,每一句都能带给你力量!
Array and collection performance comparison
Centos7 one click compilation to build MySQL script
【luogu P4320】道路相遇(圆方树)
How SQLSEVER removes data with duplicate IDS