『博客开发日记-后台』之获取部门下拉选项接口的实现

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

获取部门下拉选项接口的实现


获取部门下拉选项接口的需求

按排序字段降序排列

查询状态正常的部门

要根据用户当前所拥有的权限来查询部门列表

如果当前用户无权查看部门,则不能显示出来


代码实现

创建 DeptOptionVo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@ApiModel(description = "部门选项")
public class DeptOptionVo {
@ApiModelProperty(value = "部门ID", example = "1")
@JSONField(name = "value")//转换成 value 再响应给前端
private Long id;

@ApiModelProperty(value = "父部门ID", example = "0")
private Long parentId;

@ApiModelProperty(value = "部门名称", example = "总公司")
@JSONField(name = "label")//转换成 label 再响应给前端
private String name;

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

创建 DeptController

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
/**
* 部门接口
*/
@RestController
@RequestMapping("/depts")
@Api(tags = "部门", description = "部门接口")
public class DeptController
{
@Autowired
private SysDeptService sysDeptService;







@GetMapping("/options")
@SystemLog(businessName = "部门下拉选项")
@ApiOperation(value = "部门下拉选项", notes = "获取部门下拉数据", response = DeptOptionVo.class, responseContainer = "List")
public ResponseResult getDeptOptions()
{
return sysDeptService.getDeptOptions();
}


}

在 SysRoleServiceImpl 中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//查询di为1的用户所拥有的角色列表
@Override
public List<SysRole> selectRolesByUserId(Long id)
{
if (id == 1L) {
SysRole sysRole = new SysRole();
sysRole.setId(1L);
sysRole.setRoleCode("admin");
sysRole.setName("系统超级管理员");
sysRole.setDataScope(1);
return List.of(sysRole);
}
return getBaseMapper().selectRolesByUserId(id);
}

相对应的在 SysRoleMapper.xml 也要写上对应的sql联表查询

1
2
3
4
5
6
7
8
9
10
11
12
13

<select id="selectRolesByUserId" resultType="com.mengze.domain.entity.SysRole">
SELECT r.id,
r.role_code,
r.name,
r.data_scope
FROM sys_user_role ur
LEFT JOIN sys_role r ON ur.role_id = r.id
WHERE ur.user_id = #{userId}
AND r.`status` = 0
AND r.del_flag = 0
</select>


在 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* 部门表(SysDept)表服务实现类
*
* @author makejava
* @since 2026-05-15 13:06:17
*/
@Service("sysDeptService")
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements SysDeptService
{
@Autowired
private SysRoleService sysRoleService;

@Autowired
private SysRoleDeptService sysRoleDeptService;

//获取部门下拉选项(树结构)
@Override
public ResponseResult<List<DeptOptionVo>> getDeptOptions()
{
//构建查询条件
LambdaQueryWrapper<SysDept> queryWrapper = new LambdaQueryWrapper<>();

//当前登录用户
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);
}
}
}
}

//查询状态正常的部门,按排序降序
queryWrapper.eq(SysDept::getStatus, SystemConstants.STATUS_NORMAL)
.orderByAsc(SysDept::getSort);

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

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

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

//构建菜单树
List<DeptOptionVo> deptOptionTree = builderDeptOptionsTree(deptOptionVos, 0L);

return ResponseResult.okResult(deptOptionTree);
}

@Override
public List<Long> selectDeptAndChildrenIds(Long deptId)
{
List<Long> deptIds = new ArrayList<>();
collectDeptAndChildrenIds(deptId, deptIds);
return deptIds;
}

//添加父部门
private List<SysDept> addParentDepts(List<SysDept> sysDepts)
{
List<SysDept> allDepts = new ArrayList<>(sysDepts);
for (SysDept dept : sysDepts) {
collectParentDepts(dept.getParentId(), allDepts);
}
return allDepts.stream().distinct().collect(Collectors.toList());
}

//收集父部门
private void collectParentDepts(Long parentId, List<SysDept> allDepts)
{
if (parentId == null || parentId == 0L) {
return;
}
SysDept parent = getById(parentId);
if (parent != null && allDepts.stream().noneMatch(item -> Objects.equals(item.getId(), parent.getId()))) {
allDepts.add(parent);
collectParentDepts(parent.getParentId(), allDepts);
}
}

//根据当前角色查询部门id
private List<Long> selectDeptAndChildrenIdsByRole(Long roleId)
{
LambdaQueryWrapper<SysRoleDept> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(SysRoleDept::getRoleId, roleId)
.select(SysRoleDept::getDeptId);
List<SysRoleDept> roleDepts = sysRoleDeptService.list(wrapper);
List<Long> deptIds = new ArrayList<>();
for (SysRoleDept roleDept : roleDepts) {
deptIds.addAll(selectDeptAndChildrenIds(roleDept.getDeptId()));
}
return deptIds;
}

//查询部门与子部门id
private void collectDeptAndChildrenIds(Long deptId, List<Long> deptIds)
{
deptIds.add(deptId);
LambdaQueryWrapper<SysDept> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(SysDept::getParentId, deptId)
.eq(SysDept::getStatus, SystemConstants.STATUS_NORMAL);
List<SysDept> children = list(queryWrapper);
for (SysDept child : children) {
collectDeptAndChildrenIds(child.getId(), deptIds);
}
}



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

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


预告

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

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

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


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


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