『博客开发日记-后台』之删除字典项接口的实现

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

删除字典项接口的实现


删除字典项接口的需求

检查是否有选择到要删除的字典项

检查选择的字典项是否存在

如果有不存在的字典项,就找出对应的字典项id


代码实现

在 DeptController 中添加接口

1
2
3
4
5
6
7
8
9
@DeleteMapping("/{dictCode}/items/{ids}")
@PreAuthorize("@ps.hasPermission('blog:dict:delete')")
@SystemLog(businessName = "删除字典项(逻辑删除)")
@ApiOperation(value = "删除字典项接口(批量逻辑删除)", notes = "删除字典项,支持批量删除", response = String.class)
@ApiImplicitParam(name = "ids", value = "字典项id", dataType = "Long", paramType = "path", required = true)
public ResponseResult deleteDictItem(@PathVariable Long[] ids)
{
return sysDictItemService.deleteDictItem(ids);
}

在 SysDictItemServiceImpl 中

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
//删除字典项
@Override
public ResponseResult deleteDictItem(Long[] ids)
{
//检查是否有选择到要删除的字典项
if (ids == null || ids.length == 0) {
return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "请选择要删除的字典项");
}

//获取字典项id列表
List<Long> idList = List.of(ids);

//检查选择的字典项是否存在
List<SysDictItem> dictItemsToDelete = sysDictItemService.listByIds(idList);
if (dictItemsToDelete.size() != ids.length){
//如果有不存在的字典项,就找出对应的字典项id
Set<Long> existIds = dictItemsToDelete.stream()
.map(SysDictItem::getId)
.collect(Collectors.toSet());

List<Long> notExistIds = idList.stream()
.filter(id -> !existIds.contains(id))
.toList();

return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "删除失败!!原因:以下字典项 id 不存在:" + notExistIds);
}

//删除字典项
sysDictItemService.removeByIds(idList);

return ResponseResult.okResult();
}

需要注意的是

为了避免重复代码过多

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

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




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

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


预告

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

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

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


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


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