From 6269f050eb3a0159dcf2213a8ba7cbfa1e798b05 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Wed, 2 Oct 2024 10:57:57 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E3=80=91SYSTEM=EF=BC=9A=E8=B0=83=E6=95=B4=E9=83=A8=E9=97=A8?= =?UTF-8?q?=E4=B8=8B=E7=BA=A7=E7=9A=84=E8=AE=A1=E7=AE=97=E9=80=BB=E8=BE=91?= =?UTF-8?q?=EF=BC=8C=E5=9F=BA=E4=BA=8E=20leaderUserId=20=E7=9B=B4=E6=8E=A5?= =?UTF-8?q?=E8=BF=87=E6=BB=A4=EF=BC=8C=E6=9B=BF=E4=BB=A3=E5=8E=9F=E6=9C=AC?= =?UTF-8?q?=20user=20=E6=89=80=E5=9C=A8=E7=9A=84=20dept?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/api/user/AdminUserApiImpl.java | 21 ++++------------ .../system/dal/mysql/dept/DeptMapper.java | 4 +++ .../system/service/dept/DeptService.java | 25 +++++++++++++++---- .../system/service/dept/DeptServiceImpl.java | 9 +++++-- .../service/user/AdminUserServiceImpl.java | 1 + 5 files changed, 37 insertions(+), 23 deletions(-) diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/user/AdminUserApiImpl.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/user/AdminUserApiImpl.java index 0c60c2e35..6af7bcd42 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/user/AdminUserApiImpl.java +++ b/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 getUserListBySubordinate(Long id) { // 1.1 获取用户负责的部门 - AdminUserDO user = userService.getUser(id); - if (user == null) { + List depts = deptService.getDeptListByLeaderUserId(id); + if (CollUtil.isEmpty(depts)) { return Collections.emptyList(); } - ArrayList 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 获取所有子部门 - List childDeptList = deptService.getChildDeptList(dept.getId()); + Set deptIds = convertSet(depts, DeptDO::getId); + List childDeptList = deptService.getChildDeptList(deptIds); if (CollUtil.isNotEmpty(childDeptList)) { deptIds.addAll(convertSet(childDeptList, DeptDO::getId)); } diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/dept/DeptMapper.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/dept/DeptMapper.java index cc4f334e6..a09fcf7d7 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/dept/DeptMapper.java +++ b/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 { return selectList(DeptDO::getParentId, parentIds); } + default List selectListByLeaderUserId(Long id) { + return selectList(DeptDO::getLeaderUserId, id); + } + } diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/dept/DeptService.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/dept/DeptService.java index 11cb5f42e..a0b765e59 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/dept/DeptService.java +++ b/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 getChildDeptList(Long id); + default List getChildDeptList(Long id) { + return getChildDeptList(Collections.singleton(id)); + } + + /** + * 获得指定部门的所有子部门 + * + * @param ids 部门编号数组 + * @return 子部门列表 + */ + List getChildDeptList(Collection ids); + + /** + * 获得指定领导者的部门列表 + * + * @param id 领导者编号 + * @return 部门列表 + */ + List getDeptListByLeaderUserId(Long id); /** * 获得所有子部门,从缓存中 diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/dept/DeptServiceImpl.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/dept/DeptServiceImpl.java index fcfc0adc5..c0d7b0eff 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/dept/DeptServiceImpl.java +++ b/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 getChildDeptList(Long id) { + public List getChildDeptList(Collection ids) { List children = new LinkedList<>(); // 遍历每一层 - Collection parentIds = Collections.singleton(id); + Collection parentIds = ids; for (int i = 0; i < Short.MAX_VALUE; i++) { // 使用 Short.MAX_VALUE 避免 bug 场景下,存在死循环 // 查询当前层,所有的子部门 List depts = deptMapper.selectListByParentId(parentIds); @@ -188,6 +188,11 @@ public class DeptServiceImpl implements DeptService { return children; } + @Override + public List getDeptListByLeaderUserId(Long id) { + return deptMapper.selectListByLeaderUserId(id); + } + @Override @DataPermission(enable = false) // 禁用数据权限,避免建立不正确的缓存 @Cacheable(cacheNames = RedisKeyConstants.DEPT_CHILDREN_ID_LIST, key = "#id") diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImpl.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImpl.java index cb9a73ff7..1beefc7c1 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImpl.java +++ b/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 部门编号集合 */