浏览代码

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

YunaiV 8 月之前
父节点
当前提交
6269f050eb

+ 5 - 16
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;
 import jakarta.annotation.Resource;
 import org.springframework.stereotype.Service;
 
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
+import java.util.*;
 
 import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
 
@@ -41,21 +38,13 @@ public class AdminUserApiImpl implements AdminUserApi {
     @Override
     public List<AdminUserRespDTO> getUserListBySubordinate(Long id) {
         // 1.1 获取用户负责的部门
-        AdminUserDO user = userService.getUser(id);
-        if (user == null) {
-            return Collections.emptyList();
-        }
-        ArrayList<Long> deptIds = new ArrayList<>();
-        DeptDO dept = deptService.getDept(user.getDeptId());
-        if (dept == null) {
-            return Collections.emptyList();
-        }
-        if (ObjUtil.notEqual(dept.getLeaderUserId(), id)) { // 校验为负责人
+        List<DeptDO> depts = deptService.getDeptListByLeaderUserId(id);
+        if (CollUtil.isEmpty(depts)) {
             return Collections.emptyList();
         }
-        deptIds.add(dept.getId());
         // 1.2 获取所有子部门
-        List<DeptDO> childDeptList = deptService.getChildDeptList(dept.getId());
+        Set<Long> deptIds = convertSet(depts, DeptDO::getId);
+        List<DeptDO> childDeptList = deptService.getChildDeptList(deptIds);
         if (CollUtil.isNotEmpty(childDeptList)) {
             deptIds.addAll(convertSet(childDeptList, DeptDO::getId));
         }

+ 4 - 0
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<DeptDO> {
         return selectList(DeptDO::getParentId, parentIds);
     }
 
+    default List<DeptDO> selectListByLeaderUserId(Long id) {
+        return selectList(DeptDO::getLeaderUserId, id);
+    }
+
 }

+ 20 - 5
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
 import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptSaveReqVO;
 import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
 
-import java.util.Collection;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
 
 /**
  * 部门 Service 接口
@@ -80,7 +77,25 @@ public interface DeptService {
      * @param id 部门编号
      * @return 子部门列表
      */
-    List<DeptDO> getChildDeptList(Long id);
+    default List<DeptDO> getChildDeptList(Long id) {
+        return getChildDeptList(Collections.singleton(id));
+    }
+
+    /**
+     * 获得指定部门的所有子部门
+     *
+     * @param ids 部门编号数组
+     * @return 子部门列表
+     */
+    List<DeptDO> getChildDeptList(Collection<Long> ids);
+
+    /**
+     * 获得指定领导者的部门列表
+     *
+     * @param id 领导者编号
+     * @return 部门列表
+     */
+    List<DeptDO> getDeptListByLeaderUserId(Long id);
 
     /**
      * 获得所有子部门,从缓存中

+ 7 - 2
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 {
     }
 
     @Override
-    public List<DeptDO> getChildDeptList(Long id) {
+    public List<DeptDO> getChildDeptList(Collection<Long> ids) {
         List<DeptDO> children = new LinkedList<>();
         // 遍历每一层
-        Collection<Long> parentIds = Collections.singleton(id);
+        Collection<Long> parentIds = ids;
         for (int i = 0; i < Short.MAX_VALUE; i++) { // 使用 Short.MAX_VALUE 避免 bug 场景下,存在死循环
             // 查询当前层,所有的子部门
             List<DeptDO> depts = deptMapper.selectListByParentId(parentIds);
@@ -188,6 +188,11 @@ public class DeptServiceImpl implements DeptService {
         return children;
     }
 
+    @Override
+    public List<DeptDO> getDeptListByLeaderUserId(Long id) {
+        return deptMapper.selectListByLeaderUserId(id);
+    }
+
     @Override
     @DataPermission(enable = false) // 禁用数据权限,避免建立不正确的缓存
     @Cacheable(cacheNames = RedisKeyConstants.DEPT_CHILDREN_ID_LIST, key = "#id")

+ 1 - 0
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 {
 
     /**
      * 获得部门条件:查询指定部门的子部门编号们,包括自身
+     *
      * @param deptId 部门编号
      * @return 部门编号集合
      */