『博客开发日记-后台』之获取部门列表接口的实现

本文最后更新于 2026年5月22日 晚上

获取部门列表接口的实现


获取部门列表接口的需求

根据关键字查询

查询状态正常的用户

根据状态查询

获取当前用户拥有的数据权限范围

查询状态正常的部门,按排序降序

查询所有符合条件的部门

如果不是超级管理员且查出来的部门不包含根节点则补上根节点,保证树能正常展开


代码实现

在 DeptController 中添加接口

1
2
3
4
5
6
7
8
@GetMapping
@PreAuthorize("@ps.hasPermission('sys:dept:query')")
@SystemLog(businessName = "部门树列表")
@ApiOperation(value = "部门树列表", notes = "获取部门树列表(不分页)", response = PageVo.class)
public ResponseResult getDeptList(@Valid DeptListDto deptListDto)
{
return sysDeptService.getDeptList(deptListDto);
}

创建 DeptListVo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* 部门Vo
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@ApiModel(description = "获取当前登录用户所属部门树列表")
public class DeptListVo
{
@ApiModelProperty(value = "部门id", example = "6")
private Long id;

@ApiModelProperty(value = "父ID", example = "1")
private Long parentId;

@ApiModelProperty(value = "部门名称", example = "总部")
private String name;

@ApiModelProperty(value = "排序", example = "5")
private Integer sort;

@ApiModelProperty(value = "状态(0-启用,1-禁用)", example = "0")
private String status;

@ApiModelProperty(value = "父节点ID路径", example = "1,2")
private String treePath;

@ApiModelProperty(value = "创建时间", example = "2024-01-01 12:00:00")
private Date createTime;

@TableField(exist = false)
@ApiModelProperty(value = "子部门")
private List<DeptListVo> children;
}

在 SysDeptServiceImpl 中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//获取部门列表(树结构)
@Override
public ResponseResult getDeptList(DeptListDto deptListDto)
{
//构建查询条件
LambdaQueryWrapper<SysDept> queryWrapper = new LambdaQueryWrapper<>();

//根据关键字查询
if (StringUtils.hasText(deptListDto.getKeywords())) {
queryWrapper.like(SysDept::getName, deptListDto.getKeywords());
}

//根据状态查询
if (StringUtils.hasText(deptListDto.getStatus())) {
queryWrapper.eq(SysDept::getStatus, deptListDto.getStatus());
}

//当前登录用户
LoginUser loginUser = SecurityUtils.getLoginUser();
Long currentUserId = loginUser.getSysUser().getId();

//获取当前用户拥有的数据权限范围
applyDeptDataScopeFilter(queryWrapper);

//按排序降序
queryWrapper.orderByAsc(SysDept::getSort);

//查询所有符合条件的部门
List<SysDept> sysDepts = list(queryWrapper);

//如果不是超级管理员且查出来的部门不包含根节点则补上根节点,保证树能正常展开
if (!Objects.equals(currentUserId, 1L)) {
sysDepts = addParentDepts(sysDepts);
}

//封装成vo
List<DeptListVo> deptListVos = BeanCopyUtils.copyBeanList(sysDepts, DeptListVo.class);

//构建菜单树
List<DeptListVo> deptListTree = builderDeptListTree(deptListVos, 0L);

return ResponseResult.okResult(deptListTree);
}

//获取当前用户拥有的数据权限范围
private void applyDeptDataScopeFilter(LambdaQueryWrapper<SysDept> queryWrapper)
{
LoginUser loginUser = SecurityUtils.getLoginUser();
Long currentUserId = loginUser.getSysUser().getId();

//超级管理员直接看全部;普通用户按数据权限限制
if (!Objects.equals(currentUserId, 1L)) {
List<SysRole> roles = sysRoleService.selectRolesByUserId(currentUserId);
Integer maxDataScope = roles.stream()
.map(SysRole::getDataScope)
.filter(Objects::nonNull)
.min(Integer::compareTo)
.orElse(4);

//判断用户权限大小
String deptId = loginUser.getSysUser().getDeptId();
//超级管理员可以看到所有是数据
if (!Objects.equals(maxDataScope, 1))
{
//可查看本部门及子部门
if (Objects.equals(maxDataScope, 2)) {
//如果当前用户本身绑定了部门就查出本部门以及所有子部门
if (deptId != null && !deptId.isBlank()) {
List<Long> deptIds = selectDeptAndChildrenIds(Long.valueOf(deptId));
queryWrapper.in(SysDept::getId, deptIds);
} else {
//如果当前用户没有部门信息则只能看自己创建的数据
queryWrapper.eq(SysDept::getCreateBy, currentUserId);
}
} else if (Objects.equals(maxDataScope, 3)) {
//只能查看本部门数据
if (deptId != null && !deptId.isBlank()) {
queryWrapper.eq(SysDept::getId, Long.valueOf(deptId));
} else {
//如果当前用户没有部门信息则只能看自己创建的数据
queryWrapper.eq(SysDept::getCreateBy, currentUserId);
}
} else if (Objects.equals(maxDataScope, 4)) {
//只能查看本人创建/拥有的数据
queryWrapper.eq(SysDept::getCreateBy, currentUserId);
} else if (Objects.equals(maxDataScope, 5)) {
//表示自定义部门权限
//先遍历用户拥有的角色 把所有自定义部门权限对应的部门都查出来
List<Long> deptIds = new ArrayList<>();
for (SysRole role : roles) {
if (Objects.equals(role.getDataScope(), 5)) {
deptIds.addAll(selectDeptAndChildrenIdsByRole(role.getId()));
}
}

//如果查到了对应部门就按部门进行过滤
if (!deptIds.isEmpty()) {
queryWrapper.in(SysDept::getId, deptIds);
} else {
//如果没有配置任何自定义部门权限则只能看自己创建的数据
queryWrapper.eq(SysDept::getCreateBy, currentUserId);
}
}
}
}
}

//创建DeptList的菜单树
private List<DeptListVo> builderDeptListTree(List<DeptListVo> deptListVos, Long parentId)
{
return deptListVos.stream()
.filter(deptListVo -> parentId.equals(deptListVo.getParentId()))
.map(menu -> menu.setChildren(getChildrenByDeptList(menu, deptListVos)))
.collect(Collectors.toList());
}

//获取传入参数的 子部门集合
private List<DeptListVo> getChildrenByDeptList(DeptListVo deptListVo, List<DeptListVo> deptListVos)
{
return deptListVos.stream()
.filter(child -> deptListVo.getId().equals(child.getParentId()))
.map(child -> child.setChildren(getChildrenByDeptList(child, deptListVos)))
.collect(Collectors.toList());
}

需要注意的是

为了避免重复代码过多

我将 获取当前用户拥有的数据权限范围 相关的代码提取出来成 applyDeptDataScopeFilter 方法方便调用了

并且在 AdminUserServiceImpl 中也进行了类似的操作




PS:该系列只做为作者学习开发项目做的笔记用

不一定符合读者来学习,仅供参考


预告

后续会记录博客的开发过程

每次学习会做一份笔记来进行发表

“一花一世界,一叶一菩提”


版权所有 © 2026 云梦泽
欢迎访问我的个人网站:https://hgt12.github.io/


『博客开发日记-后台』之获取部门列表接口的实现
http://example.com/2026/05/22/『博客开发日记-后台』之获取部门列表接口的实现/
作者
云梦泽
发布于
2026年5月22日
更新于
2026年5月22日
许可协议