『博客开发日记』之获取通知详情接口的实现

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

获取通知详情接口的实现


获取通知详情接口的需求

检查id是否为空

查询通知基础信息

未登录用户只能查看全体通知;登录用户可查看全体通知和发给自己的指定通知

回填发布人名称

回填指定接收人的信息

登录用户查看详情后更新阅读状态


代码实现

在 NoticeController 中添加接口

1
2
3
4
5
6
7
8
9
@GetMapping("/notice/{id}")
@SystemLog(businessName = "获取通知详情接口")
@ApiOperation(value = "获取通知详情接口", notes = "获取通知的详细信息", response = PageVo.class)
@ApiImplicitParam(name = "id", value = "通知id", dataType = "long", paramType = "path")
public ResponseResult<BlogNoticeDetailVo> getNoticeDetail(@PathVariable Long id)
{
return noticeService.getNoticeDetail(id);
}


创建 BlogNoticeDetailVo

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
/**
* 前台通知详情Vo
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@ApiModel(description = "前台通知详情响应对象")
public class BlogNoticeDetailVo
{
@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;

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

@ApiModelProperty(value = "目标类型(1: 全体, 2: 指定)", example = "1")
private String targetType;

@TableField(exist = false)
@ApiModelProperty(value = "目标用户ID集合(多个使用英文逗号分割)", example = "1,2,3")
private String targetUserIds;

@TableField(exist = false)
@ApiModelProperty(value = "目标人名集合(多个使用英文逗号,分割)", example = "['张三','李四']")
private List<String> targetName;

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

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

添加常量

1
2
3
4
    /**
* 通知已读
*/
public static final String NOTICE_IS_READ = "1";

在 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
//获取通知详情
@Override
public ResponseResult<BlogNoticeDetailVo> getNoticeDetail(Long id)
{
//检查id是否为空
if (id == null) {
return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "通知id不能为空");
}

Long userId = SecurityUtils.getUserId();

//查询通知基础信息
LambdaQueryWrapper<SysNotice> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(SysNotice::getId, id)
.eq(SysNotice::getStatus, SystemConstants.NOTICE_STATUS_PUBLISHED)
.eq(SysNotice::getDelFlag, SystemConstants.STATUS_NORMAL);

//未登录用户只能查看全体通知;登录用户可查看全体通知和发给自己的指定通知
if (userId == null) {
queryWrapper.eq(SysNotice::getTargetType, SystemConstants.TARGET_TYPE_IS_ALL);
} else {
queryWrapper.and(wrapper -> wrapper
.eq(SysNotice::getTargetType, SystemConstants.TARGET_TYPE_IS_ALL)
.or(orWrapper -> orWrapper
.eq(SysNotice::getTargetType, SystemConstants.TARGET_TYPE_IS_SPECIFIED)
.apply("find_in_set({0}, target_user_ids)", userId)));
}

SysNotice notice = this.getOne(queryWrapper);
if (notice == null) {
return ResponseResult.errorResult(AppHttpCodeEnum.SELECT_ERROR, "通知不存在或无权限查看");
}

BlogNoticeDetailVo noticeDetailVo = BeanCopyUtils.copyBean(notice, BlogNoticeDetailVo.class);

//回填发布人名称
if (notice.getPublisherId() != null) {
SysUser publisher = sysUserService.getById(notice.getPublisherId());
if (publisher != null) {
noticeDetailVo.setPublisherName(StringUtils.hasText(publisher.getNickname()) ? publisher.getNickname() : publisher.getUsername());
}
}

//回填指定接收人的信息
if (SystemConstants.TARGET_TYPE_IS_SPECIFIED.equals(notice.getTargetType()) && StringUtils.hasText(notice.getTargetUserIds())) {
List<Long> targetUserIds = Arrays.stream(notice.getTargetUserIds().split(","))
.map(String::trim)
.filter(StringUtils::hasText)
.map(Long::valueOf)
.distinct()
.collect(Collectors.toList());

if (!targetUserIds.isEmpty()) {
List<SysUser> targetUserList = sysUserService.listByIds(targetUserIds);
List<String> targetNames = targetUserList.stream()
.map(user -> StringUtils.hasText(user.getNickname()) ? user.getNickname() : user.getUsername())
.collect(Collectors.toList());
noticeDetailVo.setTargetName(targetNames);
}
noticeDetailVo.setTargetUserIds(notice.getTargetUserIds());
}

//登录用户查看详情后更新阅读状态
if (userId != null) {
SysNoticeUser noticeUser = sysNoticeUserService.getOne(new LambdaQueryWrapper<SysNoticeUser>()
.eq(SysNoticeUser::getUserId, userId)
.eq(SysNoticeUser::getNoticeId, notice.getId())
.last("limit 1"));
if (noticeUser != null && StringUtils.hasText(noticeUser.getIsRead())) {
noticeDetailVo.setIsRead(noticeUser.getIsRead());
}
}

return ResponseResult.okResult(noticeDetailVo);
}





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

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


预告

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

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

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


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


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