feat(站内通知): 铃铛支持按类型大类筛选(系统异常/任务异常/配置异常/系统通知)

原来铃铛只能按关键字和日期筛,任务失败、密钥欠费、麦象异常全混在一起,
用户没法只看自己关心的那类。

后端 NotificationService 增加 scene→大类映射(未知 scene 归「系统通知」兜底),
列表接口新增 category 参数,落到 SQL 是 scene IN (...);「系统通知」作兜底类
还要纳入未登记的 scene,故表达为「= system 或 不在其它三类里」。列表项返回
category,前端不必各自维护一份 scene 映射。前后端两个通知接口同步加参数。

前端铃铛筛选区新增一行类型 chip(全部 + 四类),与关键字、日期一起参与
hasFilter 与重置;空分类不下发,避免后端查询落空。
This commit is contained in:
2026-09-16 10:32:11 +08:00
parent 7cbbc7c0ca
commit 672bb90f8d
11 changed files with 318 additions and 29 deletions
@@ -34,7 +34,8 @@ class AdminNotificationControllerTest {
when(adminAuthSupport.requireAdmin(request)).thenReturn(admin(24L));
controller.summary(request);
controller.page(request, 1L, 20L, false, "余额", LocalDate.of(2026, 9, 10), LocalDate.of(2026, 9, 13));
controller.page(request, 1L, 20L, false, "余额", NotificationService.CATEGORY_CONFIG_ERROR,
LocalDate.of(2026, 9, 10), LocalDate.of(2026, 9, 13));
verify(notificationService).summary(24L, NotificationService.AUDIENCE_ADMIN);
ArgumentCaptor<NotificationPageQuery> queryCaptor = ArgumentCaptor.forClass(NotificationPageQuery.class);
@@ -44,6 +45,7 @@ class AdminNotificationControllerTest {
assertThat(query.getPageSize()).isEqualTo(20L);
assertThat(query.getOnlyUnread()).isFalse();
assertThat(query.getKeyword()).isEqualTo("余额");
assertThat(query.getCategory()).isEqualTo(NotificationService.CATEGORY_CONFIG_ERROR);
assertThat(query.getStartDate()).isEqualTo(LocalDate.of(2026, 9, 10));
assertThat(query.getEndDate()).isEqualTo(LocalDate.of(2026, 9, 13));
}
@@ -33,7 +33,8 @@ class NotificationControllerTest {
when(adminAuthSupport.requireUser(request)).thenReturn(user(1095L));
controller.summary(request);
controller.page(request, 2L, 20L, true, "欠费", LocalDate.of(2026, 9, 1), LocalDate.of(2026, 9, 13));
controller.page(request, 2L, 20L, true, "欠费", NotificationService.CATEGORY_TASK_ERROR,
LocalDate.of(2026, 9, 1), LocalDate.of(2026, 9, 13));
verify(notificationService).summary(1095L, NotificationService.AUDIENCE_USER);
ArgumentCaptor<NotificationPageQuery> queryCaptor = ArgumentCaptor.forClass(NotificationPageQuery.class);
@@ -43,6 +44,7 @@ class NotificationControllerTest {
assertThat(query.getPageSize()).isEqualTo(20L);
assertThat(query.getOnlyUnread()).isTrue();
assertThat(query.getKeyword()).isEqualTo("欠费");
assertThat(query.getCategory()).isEqualTo(NotificationService.CATEGORY_TASK_ERROR);
assertThat(query.getStartDate()).isEqualTo(LocalDate.of(2026, 9, 1));
assertThat(query.getEndDate()).isEqualTo(LocalDate.of(2026, 9, 13));
}
@@ -133,7 +133,7 @@ class NotificationServiceTest {
when(mapper.selectList(any())).thenReturn(List.of(first, second));
NotificationPageVo page = service.page(7L, NotificationService.AUDIENCE_USER,
NotificationPageQuery.of(1L, 20L, false, null, null, null));
NotificationPageQuery.of(1L, 20L, false, null, null, null, null));
assertThat(page.getItems()).hasSize(2);
assertThat(page.getItems().get(0).getRead()).isTrue();
@@ -149,7 +149,7 @@ class NotificationServiceTest {
when(mapper.selectCount(any())).thenReturn(0L, 0L);
NotificationPageVo page = service.page(7L, NotificationService.AUDIENCE_ADMIN,
NotificationPageQuery.of(0L, 500L, true, null, null, null));
NotificationPageQuery.of(0L, 500L, true, null, null, null, null));
assertThat(page.getItems()).isEmpty();
assertThat(page.getPage()).isEqualTo(1L);
@@ -204,7 +204,7 @@ class NotificationServiceTest {
when(mapper.selectList(any())).thenReturn(List.of());
service.page(24L, NotificationService.AUDIENCE_ADMIN,
NotificationPageQuery.of(1L, 20L, true, null, null, null));
NotificationPageQuery.of(1L, 20L, true, null, null, null, null));
ArgumentCaptor<LambdaQueryWrapper<UserNotificationEntity>> countCaptor =
ArgumentCaptor.forClass(LambdaQueryWrapper.class);
@@ -224,7 +224,7 @@ class NotificationServiceTest {
when(mapper.selectList(any())).thenReturn(List.of());
service.page(24L, NotificationService.AUDIENCE_ADMIN, NotificationPageQuery.of(
1L, 20L, false, "余额不足", LocalDate.of(2026, 9, 10), LocalDate.of(2026, 9, 13)));
1L, 20L, false, "余额不足", null, LocalDate.of(2026, 9, 10), LocalDate.of(2026, 9, 13)));
ArgumentCaptor<LambdaQueryWrapper<UserNotificationEntity>> listCaptor =
ArgumentCaptor.forClass(LambdaQueryWrapper.class);
@@ -235,6 +235,87 @@ class NotificationServiceTest {
.contains("%余额不足%", LocalDateTime.of(2026, 9, 10, 0, 0), LocalDateTime.of(2026, 9, 14, 0, 0));
}
/** 类型大类映射:scene 归并成四类,未知 scene 兜底「系统通知」,保证筛选互斥且完备。 */
@Test
void categoryOfMapsScenesToUserVisibleGroups() {
assertThat(NotificationService.categoryOf(NotificationService.SCENE_TASK_FAILED))
.isEqualTo(NotificationService.CATEGORY_TASK_ERROR);
assertThat(NotificationService.categoryOf(NotificationService.SCENE_SERVICE_DOWN))
.isEqualTo(NotificationService.CATEGORY_SYSTEM_ERROR);
assertThat(NotificationService.categoryOf(NotificationService.SCENE_MAIXIANG_ANOMALY))
.isEqualTo(NotificationService.CATEGORY_SYSTEM_ERROR);
assertThat(NotificationService.categoryOf(NotificationService.SCENE_SECRET_BALANCE))
.isEqualTo(NotificationService.CATEGORY_CONFIG_ERROR);
assertThat(NotificationService.categoryOf(NotificationService.SCENE_SECRET_INVALID))
.isEqualTo(NotificationService.CATEGORY_CONFIG_ERROR);
assertThat(NotificationService.categoryOf(NotificationService.SCENE_SYSTEM))
.isEqualTo(NotificationService.CATEGORY_SYSTEM_NOTICE);
// 历史遗留/新增未登记的 scene 不能被筛丢
assertThat(NotificationService.categoryOf("legacy_scene"))
.isEqualTo(NotificationService.CATEGORY_SYSTEM_NOTICE);
assertThat(NotificationService.categoryOf(null))
.isEqualTo(NotificationService.CATEGORY_SYSTEM_NOTICE);
}
/** 类型筛选落到 SQL 上是 scene IN (...);空/未知分类不追加条件,避免前端传错值就查不到数据。 */
@Test
@SuppressWarnings("unchecked")
void pageAppliesCategoryAsSceneInFilter() {
when(mapper.selectCount(any())).thenReturn(1L, 0L);
when(mapper.selectList(any())).thenReturn(List.of());
service.page(24L, NotificationService.AUDIENCE_USER, NotificationPageQuery.of(
1L, 20L, false, null, NotificationService.CATEGORY_SYSTEM_ERROR, null, null));
ArgumentCaptor<LambdaQueryWrapper<UserNotificationEntity>> listCaptor =
ArgumentCaptor.forClass(LambdaQueryWrapper.class);
verify(mapper).selectList(listCaptor.capture());
LambdaQueryWrapper<UserNotificationEntity> wrapper = listCaptor.getValue();
assertThat(wrapper.getSqlSegment()).contains("scene");
assertThat(wrapper.getParamNameValuePairs().values())
.contains(NotificationService.SCENE_SERVICE_DOWN, NotificationService.SCENE_MAIXIANG_ANOMALY);
assertThat(NotificationService.scenesOfCategory(null)).isEmpty();
assertThat(NotificationService.scenesOfCategory("")).isEmpty();
assertThat(NotificationService.scenesOfCategory("not_a_category")).isEmpty();
}
/** 「系统通知」是兜底类:除 scene=system 外还要纳入未登记 scene,否则那些通知筛选时会消失。 */
@Test
@SuppressWarnings("unchecked")
void pageTreatsSystemNoticeCategoryAsCatchAll() {
when(mapper.selectCount(any())).thenReturn(1L, 0L);
when(mapper.selectList(any())).thenReturn(List.of());
service.page(24L, NotificationService.AUDIENCE_USER, NotificationPageQuery.of(
1L, 20L, false, null, NotificationService.CATEGORY_SYSTEM_NOTICE, null, null));
ArgumentCaptor<LambdaQueryWrapper<UserNotificationEntity>> listCaptor =
ArgumentCaptor.forClass(LambdaQueryWrapper.class);
verify(mapper).selectList(listCaptor.capture());
LambdaQueryWrapper<UserNotificationEntity> wrapper = listCaptor.getValue();
assertThat(wrapper.getSqlSegment()).contains("scene");
assertThat(wrapper.getParamNameValuePairs().values())
.contains(NotificationService.SCENE_SYSTEM, NotificationService.SCENE_TASK_FAILED);
}
/** 列表项必须带类型大类,前端据此展示与筛选,不必各自维护一份 scene 映射。 */
@Test
void pageItemsCarryCategory() {
UserNotificationEntity row = new UserNotificationEntity();
row.setId(9L);
row.setScene(NotificationService.SCENE_SECRET_BALANCE);
when(mapper.selectCount(any())).thenReturn(1L, 0L);
when(mapper.selectList(any())).thenReturn(List.of(row));
NotificationPageVo page = service.page(7L, NotificationService.AUDIENCE_USER,
NotificationPageQuery.of(1L, 20L, false, null, null, null, null));
assertThat(page.getItems()).hasSize(1);
assertThat(page.getItems().get(0).getCategory())
.isEqualTo(NotificationService.CATEGORY_CONFIG_ERROR);
}
@Test
@SuppressWarnings("unchecked")
void summaryAndUnreadCountScopeByReceiverAndAudience() {