Commit 6269f050eb3a0159dcf2213a8ba7cbfa1e798b05

Authored by YunaiV
1 parent 7cbd447b

【代码优化】SYSTEM:调整部门下级的计算逻辑,基于 leaderUserId 直接过滤,替代原本 user 所在的 dept

yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/user/AdminUserApiImpl.java
@@ -12,10 +12,7 @@ import cn.iocoder.yudao.module.system.service.user.AdminUserService; @@ -12,10 +12,7 @@ import cn.iocoder.yudao.module.system.service.user.AdminUserService;
12 import jakarta.annotation.Resource; 12 import jakarta.annotation.Resource;
13 import org.springframework.stereotype.Service; 13 import org.springframework.stereotype.Service;
14 14
15 -import java.util.ArrayList;  
16 -import java.util.Collection;  
17 -import java.util.Collections;  
18 -import java.util.List; 15 +import java.util.*;
19 16
20 import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; 17 import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
21 18
@@ -41,21 +38,13 @@ public class AdminUserApiImpl implements AdminUserApi { @@ -41,21 +38,13 @@ public class AdminUserApiImpl implements AdminUserApi {
41 @Override 38 @Override
42 public List<AdminUserRespDTO> getUserListBySubordinate(Long id) { 39 public List<AdminUserRespDTO> getUserListBySubordinate(Long id) {
43 // 1.1 获取用户负责的部门 40 // 1.1 获取用户负责的部门
44 - AdminUserDO user = userService.getUser(id);  
45 - if (user == null) {  
46 - return Collections.emptyList();  
47 - }  
48 - ArrayList<Long> deptIds = new ArrayList<>();  
49 - DeptDO dept = deptService.getDept(user.getDeptId());  
50 - if (dept == null) {  
51 - return Collections.emptyList();  
52 - }  
53 - if (ObjUtil.notEqual(dept.getLeaderUserId(), id)) { // 校验为负责人 41 + List<DeptDO> depts = deptService.getDeptListByLeaderUserId(id);
  42 + if (CollUtil.isEmpty(depts)) {
54 return Collections.emptyList(); 43 return Collections.emptyList();
55 } 44 }
56 - deptIds.add(dept.getId());  
57 // 1.2 获取所有子部门 45 // 1.2 获取所有子部门
58 - List<DeptDO> childDeptList = deptService.getChildDeptList(dept.getId()); 46 + Set<Long> deptIds = convertSet(depts, DeptDO::getId);
  47 + List<DeptDO> childDeptList = deptService.getChildDeptList(deptIds);
59 if (CollUtil.isNotEmpty(childDeptList)) { 48 if (CollUtil.isNotEmpty(childDeptList)) {
60 deptIds.addAll(convertSet(childDeptList, DeptDO::getId)); 49 deptIds.addAll(convertSet(childDeptList, DeptDO::getId));
61 } 50 }
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/dept/DeptMapper.java
@@ -30,4 +30,8 @@ public interface DeptMapper extends BaseMapperX&lt;DeptDO&gt; { @@ -30,4 +30,8 @@ public interface DeptMapper extends BaseMapperX&lt;DeptDO&gt; {
30 return selectList(DeptDO::getParentId, parentIds); 30 return selectList(DeptDO::getParentId, parentIds);
31 } 31 }
32 32
  33 + default List<DeptDO> selectListByLeaderUserId(Long id) {
  34 + return selectList(DeptDO::getLeaderUserId, id);
  35 + }
  36 +
33 } 37 }
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/dept/DeptService.java
@@ -5,10 +5,7 @@ import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptListReqV @@ -5,10 +5,7 @@ import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptListReqV
5 import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptSaveReqVO; 5 import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptSaveReqVO;
6 import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO; 6 import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
7 7
8 -import java.util.Collection;  
9 -import java.util.List;  
10 -import java.util.Map;  
11 -import java.util.Set; 8 +import java.util.*;
12 9
13 /** 10 /**
14 * 部门 Service 接口 11 * 部门 Service 接口
@@ -80,7 +77,25 @@ public interface DeptService { @@ -80,7 +77,25 @@ public interface DeptService {
80 * @param id 部门编号 77 * @param id 部门编号
81 * @return 子部门列表 78 * @return 子部门列表
82 */ 79 */
83 - List<DeptDO> getChildDeptList(Long id); 80 + default List<DeptDO> getChildDeptList(Long id) {
  81 + return getChildDeptList(Collections.singleton(id));
  82 + }
  83 +
  84 + /**
  85 + * 获得指定部门的所有子部门
  86 + *
  87 + * @param ids 部门编号数组
  88 + * @return 子部门列表
  89 + */
  90 + List<DeptDO> getChildDeptList(Collection<Long> ids);
  91 +
  92 + /**
  93 + * 获得指定领导者的部门列表
  94 + *
  95 + * @param id 领导者编号
  96 + * @return 部门列表
  97 + */
  98 + List<DeptDO> getDeptListByLeaderUserId(Long id);
84 99
85 /** 100 /**
86 * 获得所有子部门,从缓存中 101 * 获得所有子部门,从缓存中
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/dept/DeptServiceImpl.java
@@ -170,10 +170,10 @@ public class DeptServiceImpl implements DeptService { @@ -170,10 +170,10 @@ public class DeptServiceImpl implements DeptService {
170 } 170 }
171 171
172 @Override 172 @Override
173 - public List<DeptDO> getChildDeptList(Long id) { 173 + public List<DeptDO> getChildDeptList(Collection<Long> ids) {
174 List<DeptDO> children = new LinkedList<>(); 174 List<DeptDO> children = new LinkedList<>();
175 // 遍历每一层 175 // 遍历每一层
176 - Collection<Long> parentIds = Collections.singleton(id); 176 + Collection<Long> parentIds = ids;
177 for (int i = 0; i < Short.MAX_VALUE; i++) { // 使用 Short.MAX_VALUE 避免 bug 场景下,存在死循环 177 for (int i = 0; i < Short.MAX_VALUE; i++) { // 使用 Short.MAX_VALUE 避免 bug 场景下,存在死循环
178 // 查询当前层,所有的子部门 178 // 查询当前层,所有的子部门
179 List<DeptDO> depts = deptMapper.selectListByParentId(parentIds); 179 List<DeptDO> depts = deptMapper.selectListByParentId(parentIds);
@@ -189,6 +189,11 @@ public class DeptServiceImpl implements DeptService { @@ -189,6 +189,11 @@ public class DeptServiceImpl implements DeptService {
189 } 189 }
190 190
191 @Override 191 @Override
  192 + public List<DeptDO> getDeptListByLeaderUserId(Long id) {
  193 + return deptMapper.selectListByLeaderUserId(id);
  194 + }
  195 +
  196 + @Override
192 @DataPermission(enable = false) // 禁用数据权限,避免建立不正确的缓存 197 @DataPermission(enable = false) // 禁用数据权限,避免建立不正确的缓存
193 @Cacheable(cacheNames = RedisKeyConstants.DEPT_CHILDREN_ID_LIST, key = "#id") 198 @Cacheable(cacheNames = RedisKeyConstants.DEPT_CHILDREN_ID_LIST, key = "#id")
194 public Set<Long> getChildDeptIdListFromCache(Long id) { 199 public Set<Long> getChildDeptIdListFromCache(Long id) {
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImpl.java
@@ -335,6 +335,7 @@ public class AdminUserServiceImpl implements AdminUserService { @@ -335,6 +335,7 @@ public class AdminUserServiceImpl implements AdminUserService {
335 335
336 /** 336 /**
337 * 获得部门条件:查询指定部门的子部门编号们,包括自身 337 * 获得部门条件:查询指定部门的子部门编号们,包括自身
  338 + *
338 * @param deptId 部门编号 339 * @param deptId 部门编号
339 * @return 部门编号集合 340 * @return 部门编号集合
340 */ 341 */