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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
@Service("sysDeptService") public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements SysDeptService { @Autowired private SysRoleService sysRoleService;
@Autowired private SysRoleDeptService sysRoleDeptService;
@Override public ResponseResult<List<DeptOptionVo>> getDeptOptions() { LambdaQueryWrapper<SysDept> queryWrapper = new LambdaQueryWrapper<>();
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); } } } }
queryWrapper.eq(SysDept::getStatus, SystemConstants.STATUS_NORMAL) .orderByAsc(SysDept::getSort);
List<SysDept> sysDepts = list(queryWrapper);
if (!Objects.equals(currentUserId, 1L)) { sysDepts = addParentDepts(sysDepts); }
List<DeptOptionVo> deptOptionVos = BeanCopyUtils.copyBeanList(sysDepts, DeptOptionVo.class);
List<DeptOptionVo> deptOptionTree = builderDeptOptionsTree(deptOptionVos, 0L);
return ResponseResult.okResult(deptOptionTree); }
@Override public List<Long> selectDeptAndChildrenIds(Long deptId) { List<Long> deptIds = new ArrayList<>(); collectDeptAndChildrenIds(deptId, deptIds); return deptIds; }
private List<SysDept> addParentDepts(List<SysDept> sysDepts) { List<SysDept> allDepts = new ArrayList<>(sysDepts); for (SysDept dept : sysDepts) { collectParentDepts(dept.getParentId(), allDepts); } return allDepts.stream().distinct().collect(Collectors.toList()); }
private void collectParentDepts(Long parentId, List<SysDept> allDepts) { if (parentId == null || parentId == 0L) { return; } SysDept parent = getById(parentId); if (parent != null && allDepts.stream().noneMatch(item -> Objects.equals(item.getId(), parent.getId()))) { allDepts.add(parent); collectParentDepts(parent.getParentId(), allDepts); } }
private List<Long> selectDeptAndChildrenIdsByRole(Long roleId) { LambdaQueryWrapper<SysRoleDept> wrapper = new LambdaQueryWrapper<>(); wrapper.eq(SysRoleDept::getRoleId, roleId) .select(SysRoleDept::getDeptId); List<SysRoleDept> roleDepts = sysRoleDeptService.list(wrapper); List<Long> deptIds = new ArrayList<>(); for (SysRoleDept roleDept : roleDepts) { deptIds.addAll(selectDeptAndChildrenIds(roleDept.getDeptId())); } return deptIds; }
private void collectDeptAndChildrenIds(Long deptId, List<Long> deptIds) { deptIds.add(deptId); LambdaQueryWrapper<SysDept> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(SysDept::getParentId, deptId) .eq(SysDept::getStatus, SystemConstants.STATUS_NORMAL); List<SysDept> children = list(queryWrapper); for (SysDept child : children) { collectDeptAndChildrenIds(child.getId(), deptIds); } }
|