feat(system): 递归获取当前登录用户的所有子部门

- 修改了 getDeptList 方法,增加了对
This commit is contained in:
HuangLei 2025-06-19 10:51:51 +08:00
parent e15cf54d6a
commit 21aca2f55d
1 changed files with 36 additions and 3 deletions

View File

@ -5,6 +5,8 @@ import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
import cn.iocoder.yudao.framework.datapermission.core.util.DataPermissionUtils;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptListReqVO;
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
@ -18,6 +20,7 @@ import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import jakarta.annotation.Resource;
import java.util.*;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
@ -164,9 +167,39 @@ public class DeptServiceImpl implements DeptService {
@Override
public List<DeptDO> getDeptList(DeptListReqVO reqVO) {
List<DeptDO> list = deptMapper.selectList(reqVO);
list.sort(Comparator.comparing(DeptDO::getSort));
return list;
List<DeptDO> list = DataPermissionUtils.executeIgnore(() -> {
return deptMapper.selectList(reqVO);
});
Long loginUserDeptId = SecurityFrameworkUtils.getLoginUserDeptId();
//从当前登录的部门开始,递归获取子部门
// 从当前登录的部门开始, 递归获取所有子部门
List<DeptDO> allSubDepts = new ArrayList<>();
if (loginUserDeptId != null) {
DeptDO currentDept = deptMapper.selectById(loginUserDeptId);
if (currentDept != null) {
allSubDepts.add(currentDept);
collectAllSubDept(allSubDepts, currentDept.getId(), list);
}
}
// 合并所有子部门到结果列表中
allSubDepts.sort(Comparator.comparing(DeptDO::getSort));
return allSubDepts;
}
/**
* 递归收集所有子部门.
*
* @param allSubDepts 收集所有子部门的列表
* @param parentId 父部门ID
* @param allDepts 所有部门的列表
*/
private void collectAllSubDept(List<DeptDO> allSubDepts, Long parentId, List<DeptDO> allDepts) {
for (DeptDO dept : allDepts) {
if (parentId.equals(dept.getParentId())) {
allSubDepts.add(dept);
collectAllSubDept(allSubDepts, dept.getId(), allDepts); // 递归收集子部门
}
}
}
@Override