feat(notice): 新增公告优先级和未读状态功能,优化公告展示逻辑

This commit is contained in:
2025-12-30 22:49:03 +08:00
parent 76c324b0df
commit 88a4e58130
21 changed files with 1520 additions and 13 deletions

View File

@@ -61,22 +61,44 @@ public class SysNoticeController extends BaseController {
/**
* 获取当前用户的通知列表(公开接口)
* 通知类型:通常 noticeType = '1' 代表通知
* 通知类型:通常 noticeType = '1' 代表通知noticeType = '2' 代表公告
* 返回已发布且状态正常的所有公告和通知,并标注已读状态
* 按优先级排序,高优先级在前
*/
@GetMapping("/public/notice")
public AjaxResult getUserNotices() {
// 获取当前用户信息
LoginUser loginUser = getLoginUser();
SysUser currentUser = loginUser.getUser();
// 查询状态正常0且已发布1的通知
// 查询已发布且状态正常的所有公告和通知
SysNotice notice = new SysNotice();
notice.setStatus("0");
notice.setPublishStatus("1");
// 通知类型设置为 '1'(通知)
notice.setNoticeType("1");
List<SysNotice> list = noticeService.selectNoticeList(notice);
// 按优先级排序1高 2中 3低相同优先级按创建时间降序
list.sort((a, b) -> {
String priorityA = a.getPriority() != null ? a.getPriority() : "3";
String priorityB = b.getPriority() != null ? b.getPriority() : "3";
int priorityCompare = priorityA.compareTo(priorityB);
if (priorityCompare != 0) {
return priorityCompare;
}
// 相同优先级,按创建时间降序
return b.getCreateTime().compareTo(a.getCreateTime());
});
// 获取用户已读的公告/通知ID列表
List<Long> readIds = noticeReadService.selectReadNoticeIdsByUserId(currentUser.getUserId());
// 为每个公告/通知添加已读状态
for (SysNotice item : list) {
boolean isRead = readIds.contains(item.getNoticeId());
item.setIsRead(isRead);
}
return success(list);
}
@@ -143,6 +165,10 @@ public class SysNoticeController extends BaseController {
if (notice.getPublishStatus() == null || notice.getPublishStatus().isEmpty()) {
notice.setPublishStatus("0");
}
// 设置默认优先级为中2
if (notice.getPriority() == null || notice.getPriority().isEmpty()) {
notice.setPriority("2");
}
return toAjax(noticeService.insertNotice(notice));
}

View File

@@ -34,6 +34,12 @@ public class SysNotice extends BaseEntity {
/** 发布状态0未发布 1已发布 */
private String publishStatus;
/** 优先级1高 2中 3低 */
private String priority;
/** 是否已读(前端展示用,不映射到数据库) */
private Boolean isRead;
public Long getNoticeId() {
return noticeId;
}
@@ -85,6 +91,22 @@ public class SysNotice extends BaseEntity {
this.publishStatus = publishStatus;
}
public String getPriority() {
return priority;
}
public void setPriority(String priority) {
this.priority = priority;
}
public Boolean getIsRead() {
return isRead;
}
public void setIsRead(Boolean isRead) {
this.isRead = isRead;
}
@Override
public String toString() {
return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).append("noticeId", getNoticeId())

View File

@@ -11,6 +11,7 @@
<result property="noticeContent" column="notice_content"/>
<result property="status" column="status"/>
<result property="publishStatus" column="publish_status"/>
<result property="priority" column="priority"/>
<result property="createBy" column="create_by"/>
<result property="createTime" column="create_time"/>
<result property="updateBy" column="update_by"/>
@@ -25,6 +26,7 @@
convert_from(notice_content, 'UTF8') as notice_content,
status,
publish_status,
priority,
create_by,
create_time,
update_by,
@@ -67,6 +69,7 @@
<if test="noticeType != null and noticeType != '' ">notice_type,</if>
<if test="noticeContent != null and noticeContent != '' ">notice_content,</if>
<if test="status != null and status != '' ">status,</if>
<if test="priority != null and priority != '' ">priority,</if>
<if test="remark != null and remark != ''">remark,</if>
<if test="createBy != null and createBy != ''">create_by,</if>
create_time
@@ -76,6 +79,7 @@
<if test="noticeType != null and noticeType != ''">#{noticeType},</if>
<if test="noticeContent != null and noticeContent != ''">cast(#{noticeContent} as bytea),</if>
<if test="status != null and status != ''">#{status},</if>
<if test="priority != null and priority != ''">#{priority},</if>
<if test="remark != null and remark != ''">#{remark},</if>
<if test="createBy != null and createBy != ''">#{createBy},</if>
now()
@@ -90,6 +94,7 @@
<if test="noticeContent != null">notice_content = cast(#{noticeContent} as bytea),</if>
<if test="status != null and status != ''">status = #{status},</if>
<if test="publishStatus != null and publishStatus != ''">publish_status = #{publishStatus},</if>
<if test="priority != null and priority != ''">priority = #{priority},</if>
<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
update_time = now()
</set>

View File

@@ -6,7 +6,7 @@ spring:
druid:
# 主库数据源
master:
url: jdbc:postgresql://192.168.110.252:15432/postgresql?currentSchema=hisdev&characterEncoding=UTF-8&client_encoding=UTF-8
url: jdbc:postgresql://47.116.196.11:15432/postgresql?currentSchema=hisdev&characterEncoding=UTF-8&client_encoding=UTF-8
username: postgresql
password: Jchl1528
# 从库数据源
@@ -64,9 +64,9 @@ spring:
# redis 配置
redis:
# 地址
host: 192.168.110.252
host: 47.116.196.11
# 端口默认为6379
port: 6379
port: 26379
# 数据库索引
database: 1
# 密码

View File

@@ -0,0 +1,10 @@
-- 为 sys_notice 表添加优先级字段
-- 执行前请先备份数据库
ALTER TABLE sys_notice ADD COLUMN priority VARCHAR(10) DEFAULT '2';
-- 添加注释
COMMENT ON COLUMN sys_notice.priority IS '优先级1高 2中 3低';
-- 为现有数据设置默认优先级
UPDATE sys_notice SET priority = '2' WHERE priority IS NULL;