1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| @Override public ResponseResult getDeptList(DeptListDto deptListDto) { LambdaQueryWrapper<SysDept> queryWrapper = new LambdaQueryWrapper<>();
if (StringUtils.hasText(deptListDto.getKeywords())) { queryWrapper.like(SysDept::getName, deptListDto.getKeywords()); }
if (StringUtils.hasText(deptListDto.getStatus())) { queryWrapper.eq(SysDept::getStatus, deptListDto.getStatus()); }
LoginUser loginUser = SecurityUtils.getLoginUser(); Long currentUserId = loginUser.getSysUser().getId();
applyDeptDataScopeFilter(queryWrapper);
queryWrapper.orderByAsc(SysDept::getSort);
List<SysDept> sysDepts = list(queryWrapper);
if (!Objects.equals(currentUserId, 1L)) { sysDepts = addParentDepts(sysDepts); }
List<DeptListVo> deptListVos = BeanCopyUtils.copyBeanList(sysDepts, DeptListVo.class);
List<DeptListVo> deptListTree = builderDeptListTree(deptListVos, 0L);
return ResponseResult.okResult(deptListTree); }
private void applyDeptDataScopeFilter(LambdaQueryWrapper<SysDept> queryWrapper) { LoginUser loginUser = SecurityUtils.getLoginUser(); Long currentUserId = loginUser.getSysUser().getId();
if (!Objects.equals(currentUserId, 1L)) { List<SysRole> roles = sysRoleService.selectRolesByUserId(currentUserId); Integer maxDataScope = roles.stream() .map(SysRole::getDataScope) .filter(Objects::nonNull) .min(Integer::compareTo) .orElse(4);
String deptId = loginUser.getSysUser().getDeptId(); if (!Objects.equals(maxDataScope, 1)) { if (Objects.equals(maxDataScope, 2)) { if (deptId != null && !deptId.isBlank()) { List<Long> deptIds = selectDeptAndChildrenIds(Long.valueOf(deptId)); queryWrapper.in(SysDept::getId, deptIds); } else { queryWrapper.eq(SysDept::getCreateBy, currentUserId); } } else if (Objects.equals(maxDataScope, 3)) { if (deptId != null && !deptId.isBlank()) { queryWrapper.eq(SysDept::getId, Long.valueOf(deptId)); } else { queryWrapper.eq(SysDept::getCreateBy, currentUserId); } } else if (Objects.equals(maxDataScope, 4)) { queryWrapper.eq(SysDept::getCreateBy, currentUserId); } else if (Objects.equals(maxDataScope, 5)) { List<Long> deptIds = new ArrayList<>(); for (SysRole role : roles) { if (Objects.equals(role.getDataScope(), 5)) { deptIds.addAll(selectDeptAndChildrenIdsByRole(role.getId())); } }
if (!deptIds.isEmpty()) { queryWrapper.in(SysDept::getId, deptIds); } else { queryWrapper.eq(SysDept::getCreateBy, currentUserId); } } } } }
private List<DeptListVo> builderDeptListTree(List<DeptListVo> deptListVos, Long parentId) { return deptListVos.stream() .filter(deptListVo -> parentId.equals(deptListVo.getParentId())) .map(menu -> menu.setChildren(getChildrenByDeptList(menu, deptListVos))) .collect(Collectors.toList()); }
private List<DeptListVo> getChildrenByDeptList(DeptListVo deptListVo, List<DeptListVo> deptListVos) { return deptListVos.stream() .filter(child -> deptListVo.getId().equals(child.getParentId())) .map(child -> child.setChildren(getChildrenByDeptList(child, deptListVos))) .collect(Collectors.toList()); }
|