mirror of
				https://github.com/YunaiV/ruoyi-vue-pro.git
				synced 2025-10-31 18:49:06 +08:00 
			
		
		
		
	【代码优化】SYSTEM:调整部门下级的计算逻辑,基于 leaderUserId 直接过滤,替代原本 user 所在的 dept
This commit is contained in:
		| @ -12,10 +12,7 @@ import cn.iocoder.yudao.module.system.service.user.AdminUserService; | |||||||
| import jakarta.annotation.Resource; | import jakarta.annotation.Resource; | ||||||
| import org.springframework.stereotype.Service; | import org.springframework.stereotype.Service; | ||||||
|  |  | ||||||
| import java.util.ArrayList; | import java.util.*; | ||||||
| import java.util.Collection; |  | ||||||
| import java.util.Collections; |  | ||||||
| import java.util.List; |  | ||||||
|  |  | ||||||
| import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; | ||||||
|  |  | ||||||
| @ -41,21 +38,13 @@ public class AdminUserApiImpl implements AdminUserApi { | |||||||
|     @Override |     @Override | ||||||
|     public List<AdminUserRespDTO> getUserListBySubordinate(Long id) { |     public List<AdminUserRespDTO> getUserListBySubordinate(Long id) { | ||||||
|         // 1.1 获取用户负责的部门 |         // 1.1 获取用户负责的部门 | ||||||
|         AdminUserDO user = userService.getUser(id); |         List<DeptDO> depts = deptService.getDeptListByLeaderUserId(id); | ||||||
|         if (user == null) { |         if (CollUtil.isEmpty(depts)) { | ||||||
|             return Collections.emptyList(); |             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)) { // 校验为负责人 |  | ||||||
|             return Collections.emptyList(); |  | ||||||
|         } |  | ||||||
|         deptIds.add(dept.getId()); |  | ||||||
|         // 1.2 获取所有子部门 |         // 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)) { |         if (CollUtil.isNotEmpty(childDeptList)) { | ||||||
|             deptIds.addAll(convertSet(childDeptList, DeptDO::getId)); |             deptIds.addAll(convertSet(childDeptList, DeptDO::getId)); | ||||||
|         } |         } | ||||||
|  | |||||||
| @ -30,4 +30,8 @@ public interface DeptMapper extends BaseMapperX<DeptDO> { | |||||||
|         return selectList(DeptDO::getParentId, parentIds); |         return selectList(DeptDO::getParentId, parentIds); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     default List<DeptDO> selectListByLeaderUserId(Long id) { | ||||||
|  |         return selectList(DeptDO::getLeaderUserId, id); | ||||||
|  |     } | ||||||
|  |  | ||||||
| } | } | ||||||
|  | |||||||
| @ -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.controller.admin.dept.vo.dept.DeptSaveReqVO; | ||||||
| import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO; | import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO; | ||||||
|  |  | ||||||
| import java.util.Collection; | import java.util.*; | ||||||
| import java.util.List; |  | ||||||
| import java.util.Map; |  | ||||||
| import java.util.Set; |  | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * 部门 Service 接口 |  * 部门 Service 接口 | ||||||
| @ -80,7 +77,25 @@ public interface DeptService { | |||||||
|      * @param id 部门编号 |      * @param id 部门编号 | ||||||
|      * @return 子部门列表 |      * @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); | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * 获得所有子部门,从缓存中 |      * 获得所有子部门,从缓存中 | ||||||
|  | |||||||
| @ -170,10 +170,10 @@ public class DeptServiceImpl implements DeptService { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public List<DeptDO> getChildDeptList(Long id) { |     public List<DeptDO> getChildDeptList(Collection<Long> ids) { | ||||||
|         List<DeptDO> children = new LinkedList<>(); |         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 场景下,存在死循环 |         for (int i = 0; i < Short.MAX_VALUE; i++) { // 使用 Short.MAX_VALUE 避免 bug 场景下,存在死循环 | ||||||
|             // 查询当前层,所有的子部门 |             // 查询当前层,所有的子部门 | ||||||
|             List<DeptDO> depts = deptMapper.selectListByParentId(parentIds); |             List<DeptDO> depts = deptMapper.selectListByParentId(parentIds); | ||||||
| @ -188,6 +188,11 @@ public class DeptServiceImpl implements DeptService { | |||||||
|         return children; |         return children; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public List<DeptDO> getDeptListByLeaderUserId(Long id) { | ||||||
|  |         return deptMapper.selectListByLeaderUserId(id); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     @DataPermission(enable = false) // 禁用数据权限,避免建立不正确的缓存 |     @DataPermission(enable = false) // 禁用数据权限,避免建立不正确的缓存 | ||||||
|     @Cacheable(cacheNames = RedisKeyConstants.DEPT_CHILDREN_ID_LIST, key = "#id") |     @Cacheable(cacheNames = RedisKeyConstants.DEPT_CHILDREN_ID_LIST, key = "#id") | ||||||
|  | |||||||
| @ -335,6 +335,7 @@ public class AdminUserServiceImpl implements AdminUserService { | |||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * 获得部门条件:查询指定部门的子部门编号们,包括自身 |      * 获得部门条件:查询指定部门的子部门编号们,包括自身 | ||||||
|  |      * | ||||||
|      * @param deptId 部门编号 |      * @param deptId 部门编号 | ||||||
|      * @return 部门编号集合 |      * @return 部门编号集合 | ||||||
|      */ |      */ | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 YunaiV
					YunaiV