『博客开发日记-后台』之发布通知接口的实现

本文最后更新于 2026年5月30日 上午

发布通知接口的实现


发布通知接口的需求

检查通知是否存在

已发布的通知无需重复发布

更新通知状态并写入发布时间

指定用户公告需要先校验目标用户是否为空

发布通知的同时,删除该通知之前的用户阅读记录,因为可能是重新发布

添加新的用户阅读记录

收集需要插入数据的用户列表

批量插入数据

需要注意的是

这里涉及到同步插入数据

如果1万以内的用户基数同步插入时的压力一般不会很大

如果是1万以上的用户基数就要考虑分批插入或者是异步生成了


代码实现

在 NoticeController 中添加接口

1
2
3
4
5
6
7
8
9
@PutMapping("/{id}/publish")
@PreAuthorize("@ps.hasPermission('sys:notice:publish')")
@SystemLog(businessName = "发布通知")
@ApiOperation(value = "发布通知接口", notes = "发布通知", response = String.class)
@ApiImplicitParam(name = "id", value = "通知ID", dataType = "long", paramType = "path", required = true)
public ResponseResult publishNotice(@PathVariable Long id)
{
return sysNoticeService.publishNotice(id);
}

给发布通知接口放行


在 NoticeServiceImpl 中

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
//发布通知
@Override
@Transactional
public ResponseResult publishNotice(Long id)
{
//检查通知是否存在
SysNotice oldNotice = sysNoticeService.getById(id);
if (oldNotice == null) {
return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "该通知不存在!");
}

//已发布的通知无需重复发布
if (SystemConstants.NOTICE_STATUS_PUBLISHED.equals(oldNotice.getStatus())) {
return ResponseResult.okResult();
}

//指定用户公告需要先校验目标用户是否为空
if (SystemConstants.TARGET_TYPE_IS_SPECIFIED.equals(oldNotice.getTargetType())
&& !StringUtils.hasText(oldNotice.getTargetUserIds())) {
return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "推送指定用户不能为空");
}

//更新通知状态并写入发布时间
SysNotice notice = new SysNotice();
notice.setId(id);
notice.setStatus(SystemConstants.NOTICE_STATUS_PUBLISHED);
notice.setPublishTime(new Date());
notice.setPublisherId(SecurityUtils.getUserId());

boolean updated = sysNoticeService.updateById(notice);
if (!updated) {
return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "发布通知失败");
}

//发布通知的同时,删除该通知之前的用户阅读记录,因为可能是重新发布
sysNoticeUserService.remove(new LambdaQueryWrapper<SysNoticeUser>()
.eq(SysNoticeUser::getNoticeId, id));

//添加新的用户阅读记录
List<Long> targetUserIdList = null;
if (SystemConstants.TARGET_TYPE_IS_SPECIFIED.equals(oldNotice.getTargetType())) {
targetUserIdList = Arrays.stream(oldNotice.getTargetUserIds().split(","))
.map(Long::valueOf)
.toList();
}

//收集需要插入数据的用户列表
List<SysUser> targetUserList = sysUserService.list(new LambdaQueryWrapper<SysUser>()
.in(SystemConstants.TARGET_TYPE_IS_SPECIFIED.equals(oldNotice.getTargetType()), SysUser::getId, targetUserIdList));

//批量插入数据
List<SysNoticeUser> noticeUserList = targetUserList.stream().map(user -> {
SysNoticeUser noticeUser = new SysNoticeUser();
noticeUser.setNoticeId(id);
noticeUser.setUserId(user.getId());
noticeUser.setIsRead(SystemConstants.NOTICE_NOT_READ);
return noticeUser;
}).toList();

if (!noticeUserList.isEmpty()) {
sysNoticeUserService.saveBatch(noticeUserList);
}

return ResponseResult.okResult();
}




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

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


预告

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

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

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


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


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