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

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

获取我的通知列表接口的实现


获取我的通知列表接口的需求

先校验登录状态,未登录无法查看我的通知

标题模糊搜索

按发布时间倒序

回填发布人名称

查询并回填阅读状态

未读优先,已读在后;同状态下按发布时间倒序


代码实现

在 NoticeController 中添加接口

1
2
3
4
5
6
7
8
9
10
11
@GetMapping("/my")
@SystemLog(businessName = "获取我的通知列表接口")
@ApiOperation(value = "获取我的通知列表接口", notes = "分页查询我的通知列表", response = PageVo.class)
@ApiImplicitParams({
@ApiImplicitParam(name = "pageNum", value = "页号", dataType = "int", paramType = "query"),
@ApiImplicitParam(name = "pageSize", value = "每页数量", dataType = "int", paramType = "query")
})
public ResponseResult<PageVo> getMyNoticeList(Integer pageNum, Integer pageSize, @Valid NoticeListDto NoticeListDto)
{
return sysNoticeService.getMyNoticeList(pageNum, pageSize, NoticeListDto);
}

创建 MyNoticeListVo

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
/**
* 我的通知Vo
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@ApiModel(description = "我的通知列表响应对象")
public class MyNoticeListVo
{
@ApiModelProperty(value = "通知id", example = "6")
private Long id;

@ApiModelProperty(value = "通知标题", example = "这里是标题")
private String title;

@ApiModelProperty(value = "通知内容", example = "这里是内容")
private String content;

@ApiModelProperty(value = "通知类型(关联字典编码:notice_type)", example = "系统维护")
private Integer type;

@ApiModelProperty(value = "通知等级(字典code:notice_level)", example = "M")
private String level;

@ApiModelProperty(value = "状态(0: 未发布, 1: 已发布, 2: 已撤回)", example = "0")
private String status;

@TableField(exist = false)
@ApiModelProperty(value = "读取状态(0: 未读, 1: 已读)", example = "0")
private String isRead;

@ApiModelProperty(value = "发布人名字", example = "云梦泽")
private String publisherName;

@TableField(fill = FieldFill.INSERT_UPDATE)
@ApiModelProperty(value = "发布时间", example = "2024-01-01 12:00:00")
private Date publishTime;

}

在 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
 //获取我的通知列表
@Override
public ResponseResult<PageVo> getMyNoticeList(Integer pageNum, Integer pageSize, NoticeListDto noticeListDto)
{
//先校验登录状态,未登录无法查看我的通知
Long userId = SecurityUtils.getUserId();
if (userId == null) {
return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
}

//构建查询条件
LambdaQueryWrapper<SysNotice> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(SysNotice::getStatus, SystemConstants.NOTICE_STATUS_PUBLISHED)
.eq(SysNotice::getDelFlag, SystemConstants.STATUS_NORMAL)
.and(wrapper -> wrapper.eq(SysNotice::getTargetType, SystemConstants.TARGET_TYPE_IS_ALL)
.or()
.apply("find_in_set({0}, target_user_ids)", userId));

//标题模糊搜索
if (noticeListDto != null && StringUtils.hasText(noticeListDto.getTitle())) {
queryWrapper.like(SysNotice::getTitle, noticeListDto.getTitle());
}

//按发布时间倒序
queryWrapper.orderByDesc(SysNotice::getPublishTime);

//分页查询
Page<SysNotice> page = new Page<>(pageNum, pageSize);
this.page(page, queryWrapper);

List<SysNotice> noticeList = page.getRecords();
List<MyNoticeListVo> myNoticeListVos = BeanCopyUtils.copyBeanList(noticeList, MyNoticeListVo.class);

//回填发布人名称
List<Long> publisherIds = noticeList.stream()
.map(SysNotice::getPublisherId)
.filter(Objects::nonNull)
.distinct()
.collect(Collectors.toList());

Map<Long, String> publisherNameMap = new HashMap<>();
if (!publisherIds.isEmpty()) {
List<SysUser> publisherList = sysUserService.listByIds(publisherIds);
publisherNameMap = publisherList.stream()
.collect(Collectors.toMap(SysUser::getId,
user -> StringUtils.hasText(user.getNickname()) ? user.getNickname() : user.getUsername(),
(oldVal, newVal) -> oldVal));
}

for (int i = 0; i < noticeList.size(); i++) {
SysNotice notice = noticeList.get(i);
MyNoticeListVo myNoticeListVo = myNoticeListVos.get(i);
myNoticeListVo.setPublisherName(publisherNameMap.get(notice.getPublisherId()));
}

//默认都设置为未读
for (MyNoticeListVo myNoticeListVo : myNoticeListVos) {
myNoticeListVo.setIsRead(SystemConstants.NOTICE_NOT_READ);
}

//查询并回填阅读状态
if (!noticeList.isEmpty()) {
List<Long> noticeIds = noticeList.stream().map(SysNotice::getId).collect(Collectors.toList());
List<SysNoticeUser> noticeUserList = sysNoticeUserService.list(new LambdaQueryWrapper<SysNoticeUser>()
.eq(SysNoticeUser::getUserId, userId)
.in(SysNoticeUser::getNoticeId, noticeIds));

Map<Long, String> noticeReadMap = noticeUserList.stream()
.collect(Collectors.toMap(SysNoticeUser::getNoticeId, SysNoticeUser::getIsRead, (oldVal, newVal) -> oldVal));

for (MyNoticeListVo myNoticeListVo : myNoticeListVos) {
myNoticeListVo.setIsRead(noticeReadMap.getOrDefault(myNoticeListVo.getId(), SystemConstants.NOTICE_NOT_READ));
}
}

//未读优先,已读在后;同状态下按发布时间倒序
myNoticeListVos.sort(Comparator.comparing((MyNoticeListVo vo) -> SystemConstants.NOTICE_IS_READ.equals(vo.getIsRead()))
.thenComparing(MyNoticeListVo::getPublishTime, Comparator.nullsLast(Comparator.reverseOrder())));

PageVo pageVo = new PageVo(myNoticeListVos, page.getTotal());
return ResponseResult.okResult(pageVo);
}




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

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


预告

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

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

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


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


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