refactor: 优化通知公告相关代码

This commit is contained in:
2025-06-08 12:01:19 +08:00
parent d3389dbe17
commit 4989161319
7 changed files with 47 additions and 26 deletions

View File

@@ -33,7 +33,12 @@ public enum NoticeMethodEnum implements BaseEnum<Integer> {
/** /**
* 系统消息 * 系统消息
*/ */
SYSTEM_MESSAGE(1, "系统消息"),; SYSTEM_MESSAGE(1, "系统消息"),
/**
* 登录弹窗
*/
POPUP(2, "登录弹窗"),;
private final Integer value; private final Integer value;
private final String description; private final String description;

View File

@@ -45,12 +45,13 @@ public interface NoticeMapper extends BaseMapper<NoticeDO> {
IPage<NoticeResp> selectNoticePage(@Param("page") Page<NoticeDO> page, @Param("query") NoticeQuery query); IPage<NoticeResp> selectNoticePage(@Param("page") Page<NoticeDO> page, @Param("query") NoticeQuery query);
/** /**
* 查询未读公告数量 * 查询未读公告 ID 列表
* *
* @param userId 用户 ID * @param noticeMethod 通知方式
* @return 未读公告数量 * @param userId 用户 ID
* @return 未读公告 ID 列表
*/ */
Long selectUnreadCountByUserId(@Param("userId") Long userId); List<Long> selectUnreadIdsByUserId(@Param("noticeMethod") Integer noticeMethod, @Param("userId") Long userId);
/** /**
* 查询仪表盘公告列表 * 查询仪表盘公告列表

View File

@@ -24,15 +24,15 @@ import java.io.Serial;
import java.io.Serializable; import java.io.Serializable;
/** /**
* 未读公告响应参数 * 未读公告数量响应参数
* *
* @author Charles7c * @author Charles7c
* @since 2025/5/20 22:00 * @since 2025/5/22 22:15
*/ */
@Data @Data
@NoArgsConstructor @NoArgsConstructor
@Schema(description = "未读公告响应参数") @Schema(description = "未读公告数量响应参数")
public class NoticeUnreadResp implements Serializable { public class NoticeUnreadCountResp implements Serializable {
@Serial @Serial
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@@ -41,9 +41,9 @@ public class NoticeUnreadResp implements Serializable {
* 未读公告数量 * 未读公告数量
*/ */
@Schema(description = "未读公告数量", example = "1") @Schema(description = "未读公告数量", example = "1")
private Long total; private Integer total;
public NoticeUnreadResp(Long total) { public NoticeUnreadCountResp(Integer total) {
this.total = total; this.total = total;
} }
} }

View File

@@ -16,13 +16,13 @@
package top.continew.admin.system.service; package top.continew.admin.system.service;
import top.continew.admin.system.enums.NoticeMethodEnum;
import top.continew.admin.system.model.entity.NoticeDO; import top.continew.admin.system.model.entity.NoticeDO;
import top.continew.admin.system.model.query.NoticeQuery; import top.continew.admin.system.model.query.NoticeQuery;
import top.continew.admin.system.model.req.NoticeReq; import top.continew.admin.system.model.req.NoticeReq;
import top.continew.admin.system.model.resp.dashboard.DashboardNoticeResp; import top.continew.admin.system.model.resp.dashboard.DashboardNoticeResp;
import top.continew.admin.system.model.resp.notice.NoticeDetailResp; import top.continew.admin.system.model.resp.notice.NoticeDetailResp;
import top.continew.admin.system.model.resp.notice.NoticeResp; import top.continew.admin.system.model.resp.notice.NoticeResp;
import top.continew.admin.system.model.resp.notice.NoticeUnreadResp;
import top.continew.starter.data.mp.service.IService; import top.continew.starter.data.mp.service.IService;
import top.continew.starter.extension.crud.service.BaseService; import top.continew.starter.extension.crud.service.BaseService;
@@ -44,12 +44,13 @@ public interface NoticeService extends BaseService<NoticeResp, NoticeDetailResp,
void publish(NoticeDO notice); void publish(NoticeDO notice);
/** /**
* 查询未读公告数量 * 查询未读公告 ID 列表
* *
* @param method 通知方式
* @param userId 用户 ID * @param userId 用户 ID
* @return 未读公告响应参数 * @return 未读公告 ID 响应参数
*/ */
NoticeUnreadResp countUnreadByUserId(Long userId); List<Long> listUnreadIdsByUserId(NoticeMethodEnum method, Long userId);
/** /**
* 阅读公告 * 阅读公告

View File

@@ -31,7 +31,6 @@ import top.continew.admin.system.model.req.NoticeReq;
import top.continew.admin.system.model.resp.dashboard.DashboardNoticeResp; import top.continew.admin.system.model.resp.dashboard.DashboardNoticeResp;
import top.continew.admin.system.model.resp.notice.NoticeDetailResp; import top.continew.admin.system.model.resp.notice.NoticeDetailResp;
import top.continew.admin.system.model.resp.notice.NoticeResp; import top.continew.admin.system.model.resp.notice.NoticeResp;
import top.continew.admin.system.model.resp.notice.NoticeUnreadResp;
import top.continew.admin.system.service.MessageService; import top.continew.admin.system.service.MessageService;
import top.continew.admin.system.service.NoticeLogService; import top.continew.admin.system.service.NoticeLogService;
import top.continew.admin.system.service.NoticeService; import top.continew.admin.system.service.NoticeService;
@@ -169,8 +168,8 @@ public class NoticeServiceImpl extends BaseServiceImpl<NoticeMapper, NoticeDO, N
} }
@Override @Override
public NoticeUnreadResp countUnreadByUserId(Long userId) { public List<Long> listUnreadIdsByUserId(NoticeMethodEnum method, Long userId) {
return new NoticeUnreadResp(baseMapper.selectUnreadCountByUserId(userId)); return baseMapper.selectUnreadIdsByUserId(method != null ? method.getValue() : null, userId);
} }
@Override @Override

View File

@@ -24,7 +24,7 @@
LEFT JOIN sys_notice_log AS t2 ON t2.notice_id = t1.id LEFT JOIN sys_notice_log AS t2 ON t2.notice_id = t1.id
<where> <where>
<if test="query.userId != null"> <if test="query.userId != null">
(t1.notice_scope = 1 OR (t1.notice_scope = 2 AND JSON_EXTRACT(t1.notice_users, "$[0]") = CAST(#{query.userId} AS CHAR))) (t1.notice_scope = 1 OR (t1.notice_scope = 2 AND JSON_CONTAINS(t1.notice_users, CONCAT('"', #{userId}, '"'))))
</if> </if>
<if test="query.title != null and query.title != ''"> <if test="query.title != null and query.title != ''">
AND t1.title LIKE CONCAT('%', #{query.title}, '%') AND t1.title LIKE CONCAT('%', #{query.title}, '%')
@@ -41,12 +41,15 @@
</if> </if>
</select> </select>
<select id="selectUnreadCountByUserId" resultType="java.lang.Long"> <select id="selectUnreadIdsByUserId" resultType="java.lang.Long">
SELECT SELECT
COUNT(1) t1.id
FROM sys_notice AS t1 FROM sys_notice AS t1
LEFT JOIN sys_notice_log AS t2 ON t2.notice_id = t1.id LEFT JOIN sys_notice_log AS t2 ON t2.notice_id = t1.id
WHERE (t1.notice_scope = 1 OR (t1.notice_scope = 2 AND JSON_CONTAINS(t1.notice_users, CONCAT('"', #{userId}, '"')))) WHERE (t1.notice_scope = 1 OR (t1.notice_scope = 2 AND JSON_CONTAINS(t1.notice_users, CONCAT('"', #{userId}, '"'))))
<if test="noticeMethod != null">
AND JSON_CONTAINS(t1.notice_methods, CAST(#{noticeMethod} AS CHAR))
</if>
AND t2.read_time IS NULL AND t2.read_time IS NULL
</select> </select>

View File

@@ -24,6 +24,7 @@ import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import top.continew.admin.common.context.UserContextHolder; import top.continew.admin.common.context.UserContextHolder;
import top.continew.admin.system.enums.NoticeMethodEnum;
import top.continew.admin.system.enums.NoticeScopeEnum; import top.continew.admin.system.enums.NoticeScopeEnum;
import top.continew.admin.system.model.query.MessageQuery; import top.continew.admin.system.model.query.MessageQuery;
import top.continew.admin.system.model.query.NoticeQuery; import top.continew.admin.system.model.query.NoticeQuery;
@@ -31,7 +32,7 @@ import top.continew.admin.system.model.resp.message.MessageResp;
import top.continew.admin.system.model.resp.message.MessageUnreadResp; import top.continew.admin.system.model.resp.message.MessageUnreadResp;
import top.continew.admin.system.model.resp.notice.NoticeDetailResp; import top.continew.admin.system.model.resp.notice.NoticeDetailResp;
import top.continew.admin.system.model.resp.notice.NoticeResp; import top.continew.admin.system.model.resp.notice.NoticeResp;
import top.continew.admin.system.model.resp.notice.NoticeUnreadResp; import top.continew.admin.system.model.resp.notice.NoticeUnreadCountResp;
import top.continew.admin.system.service.MessageService; import top.continew.admin.system.service.MessageService;
import top.continew.admin.system.service.NoticeService; import top.continew.admin.system.service.NoticeService;
import top.continew.starter.core.validation.CheckUtils; import top.continew.starter.core.validation.CheckUtils;
@@ -41,6 +42,8 @@ import top.continew.starter.extension.crud.model.resp.BasePageResp;
import top.continew.starter.extension.crud.model.resp.PageResp; import top.continew.starter.extension.crud.model.resp.PageResp;
import top.continew.starter.log.annotation.Log; import top.continew.starter.log.annotation.Log;
import java.util.List;
/** /**
* 个人消息 API * 个人消息 API
* *
@@ -93,8 +96,17 @@ public class UserMessageController {
@Log(ignore = true) @Log(ignore = true)
@Operation(summary = "查询未读公告数量", description = "查询当前用户的未读公告数量") @Operation(summary = "查询未读公告数量", description = "查询当前用户的未读公告数量")
@GetMapping("/notice/unread") @GetMapping("/notice/unread")
public NoticeUnreadResp countUnreadNotice() { public NoticeUnreadCountResp countUnreadNotice() {
return noticeService.countUnreadByUserId(UserContextHolder.getUserId()); List<Long> list = noticeService.listUnreadIdsByUserId(null, UserContextHolder.getUserId());
return new NoticeUnreadCountResp(list.size());
}
@Log(ignore = true)
@Operation(summary = "查询未读公告", description = "查询当前用户的未读公告")
@Parameter(name = "method", description = "通知方式", example = "LOGIN_POPUP", in = ParameterIn.PATH)
@GetMapping("/notice/unread/{method}")
public List<Long> listUnreadNotice(@PathVariable String method) {
return noticeService.listUnreadIdsByUserId(NoticeMethodEnum.valueOf(method), UserContextHolder.getUserId());
} }
@Operation(summary = "分页查询公告列表", description = "分页查询公告列表") @Operation(summary = "分页查询公告列表", description = "分页查询公告列表")