『博客开发日记-后台』之更新部门接口的实现

本文最后更新于 2026年5月24日 下午

更新部门接口的实现


更新部门接口的需求

检查部门是否存在

校验部门名称是否重复(排除自己)

校验部门编号是否重复(排除自己)

检查是否将部门状态从正常改为禁用并统计该部门下的成员数量

如果部门内有成员则不允许禁用

添加父节点路径:根节点为 0,子节点为 父节点路径,父节点ID

顶级部门设置 treePath

如果当前部门是顶级部门直接赋值 0

如果不是顶级部门就先查父部门是否存在

再取父部门的路径再用","拼接出当前部门完整的层级路径


代码实现

DeptController

1
2
3
4
5
6
7
8
9
@PutMapping("/{id}")
@PreAuthorize("@ps.hasPermission('blog:dept:update')")
@SystemLog(businessName = "编辑部门接口")
@ApiOperation(value = "编辑部门接口", notes = "编辑部门", response = String.class)
@ApiImplicitParam(name = "id", value = "部门ID", dataType = "long", paramType = "path", required = true)
public ResponseResult updateDept(@PathVariable Long id, @Valid @RequestBody UpdateDeptDto updateDeptDto)
{
return sysDeptService.updateDept(id, updateDeptDto);
}

创建 UpdateDeptDto

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
/**
* 更新部门请求DTO
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(description = "更新部门请求对象")
public class AddDeptDto {

@NotBlank(message = "部门名称不能为空")
@ApiModelProperty(value = "部门名称", required = true, example = "技术部")
private String name;

@ApiModelProperty(value = "部门编码", example = "TECH")
private String code;

@NotNull(message = "上级部门不能为空")
@ApiModelProperty(value = "父部门ID(顶级部门可传'0')", required = true, example = "1")
private Long parentId;

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

@NotBlank(message = "状态不能为空")
@Pattern(regexp = "^[01]$", message = "状态只能为0或1")
@ApiModelProperty(value = "状态(0-正常,1-禁用)", required = true, example = "0")
private String status;
}


在 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
//更新部门
@Override
@Transactional
public ResponseResult updateDept(Long id, UpdateDeptDto updateDeptDto)
{
//检查部门是否存在
SysDept oldDept = sysDeptService.getById(id);
if (oldDept == null) {
return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "该部门不存在!");
}

//校验部门名称是否重复(排除自己)
LambdaQueryWrapper<SysDept> nameQueryWrapper = new LambdaQueryWrapper<>();
nameQueryWrapper.eq(SysDept::getName, updateDeptDto.getName())
.ne(SysDept::getId, id);
long nameCount = sysDeptService.count(nameQueryWrapper);
if (nameCount > 0) {
return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "部门名称已存在,请修改后重试!");
}

//校验部门编号是否重复(排除自己)
LambdaQueryWrapper<SysDept> codeQueryWrapper = new LambdaQueryWrapper<>();
codeQueryWrapper.eq(SysDept::getCode, updateDeptDto.getCode())
.ne(SysDept::getId, id);
long codeCount = sysDeptService.count(codeQueryWrapper);
if (codeCount > 0) {
return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "部门编号已存在,请修改后重试!");
}

//检查是否将部门状态从正常改为禁用
if (SystemConstants.STATUS_NORMAL.equals(oldDept.getStatus())
&& !SystemConstants.STATUS_NORMAL.equals(updateDeptDto.getStatus())) {
//统计该部门下的成员数量
LambdaQueryWrapper<SysUser> userQueryWrapper = new LambdaQueryWrapper<>();
userQueryWrapper.eq(SysUser::getId, id);
long userCount = sysUserService.count(userQueryWrapper);

if (userCount > 0) {
return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "该部门下存在成员,不可禁用!");
}
}

//获取 父节点ID路径
Long parentId = updateDeptDto.getParentId();
oldDept.setParentId(parentId);

//顶级部门设置 treePath
if (Objects.equals(parentId, 0L)) {
oldDept.setTreePath(SystemConstants.PARENT_NODE_PATH);//如果当前部门是顶级部门直接赋值 0
} else {
SysDept parentDept = sysDeptService.getById(parentId);//如果不是顶级部门就先查父部门是否存在
if (parentDept == null) {
return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "上级部门不存在!");
}
String parentTreePath = parentDept.getTreePath();//再取父部门的路径再用","拼接出当前部门完整的层级路径
if (parentTreePath == null || parentTreePath.isBlank()) {
parentTreePath = SystemConstants.PARENT_NODE_PATH;
}
oldDept.setTreePath(parentTreePath + "," + parentDept.getId());
}

//更新部门信息
SysDept sysDept = BeanCopyUtils.copyBean(updateDeptDto, SysDept.class);
sysDept.setId(id);
sysDeptService.updateById(sysDept);

return ResponseResult.okResult();
}



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

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


预告

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

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

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


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


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