1. 优化角色部门权限分配时的部门加载效果

This commit is contained in:
chenbowen
2025-09-22 18:01:59 +08:00
parent 386d47fde1
commit 9bcf365767
4 changed files with 61 additions and 8 deletions

View File

@@ -111,7 +111,7 @@ public class DeptController {
@GetMapping("/top-level-list")
@Operation(summary = "获取顶级部门列表", description = "用于懒加载,返回没有父部门的顶级部门")
@Operation(summary = "获取当前用户可访问的顶级部门列表", description = "用于懒加载,返回当前用户所属部门的最顶层祖先部门,如果用户没有关联任何部门则返回空列表")
@PreAuthorize("@ss.hasPermission('system:dept:query')")
public CommonResult<List<DeptRespVO>> getTopLevelDeptList() {
List<DeptDO> list = deptService.getTopLevelDeptList();

View File

@@ -129,10 +129,11 @@ public interface DeptService {
Set<CompanyDeptInfo> getCompanyDeptInfoListByUserId(Long userId);
/**
* 获取顶级部门列表
* 用于懒加载,返回没有父部门的顶级部门
* 获取当前用户可访问的顶级部门列表
* 用于懒加载,返回当前用户所属部门的最顶层祖先部门
* 如果用户没有关联任何部门则返回空列表
*
* @return 顶级部门列表
* @return 当前用户可访问的顶级部门列表
*/
List<DeptDO> getTopLevelDeptList();

View File

@@ -3,6 +3,7 @@ package com.zt.plat.module.system.service.dept;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.annotations.VisibleForTesting;
import com.zt.plat.framework.common.enums.CommonStatusEnum;
import com.zt.plat.framework.common.pojo.CompanyDeptInfo;
import com.zt.plat.framework.common.util.object.BeanUtils;
@@ -15,7 +16,6 @@ import com.zt.plat.module.system.dal.mysql.dept.DeptMapper;
import com.zt.plat.module.system.dal.mysql.userdept.UserDeptMapper;
import com.zt.plat.module.system.dal.redis.RedisKeyConstants;
import com.zt.plat.module.system.enums.dept.DeptSourceEnum;
import com.google.common.annotations.VisibleForTesting;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
@@ -392,8 +392,60 @@ public class DeptServiceImpl implements DeptService {
@Override
public List<DeptDO> getTopLevelDeptList() {
// 获取顶级部门没有父部门的部门parentId为null或0
return deptMapper.selectList(DeptDO.PARENT_ID_ROOT, CommonStatusEnum.ENABLE.getStatus());
// 获取当前用户所属的部门列表
Set<Long> deptIds = userDeptMapper.selectValidListByUserIds(singleton(getLoginUserId()))
.stream()
.map(UserDeptDO::getDeptId)
.collect(Collectors.toSet());
if (CollUtil.isEmpty(deptIds)) {
// 如果用户没有关联任何部门,返回空列表
return Collections.emptyList();
}
// 获取用户所属部门的最顶层祖先部门
Set<Long> topLevelDeptIds = new HashSet<>();
for (Long deptId : deptIds) {
DeptDO dept = getDept(deptId);
if (dept != null && CommonStatusEnum.ENABLE.getStatus().equals(dept.getStatus())) {
// 找到该部门的最顶层祖先
DeptDO topLevelDept = findTopLevelAncestor(dept);
if (topLevelDept != null) {
topLevelDeptIds.add(topLevelDept.getId());
}
}
}
// 根据顶层部门ID获取部门详情
return topLevelDeptIds.stream()
.map(this::getDept)
.filter(Objects::nonNull)
.filter(dept -> CommonStatusEnum.ENABLE.getStatus().equals(dept.getStatus()))
.distinct()
.collect(Collectors.toList());
}
/**
* 找到部门的最顶层祖先
*
* @param dept 部门
* @return 最顶层祖先部门
*/
private DeptDO findTopLevelAncestor(DeptDO dept) {
if (dept == null) {
return null;
}
DeptDO current = dept;
while (current.getParentId() != null && !DeptDO.PARENT_ID_ROOT.equals(current.getParentId())) {
DeptDO parent = getDept(current.getParentId());
if (parent == null || !CommonStatusEnum.ENABLE.getStatus().equals(parent.getStatus())) {
break;
}
current = parent;
}
return current;
}
@Override