Browse Source

fix(task): 晚提醒默认开启(拆早晚开关,早提醒收件人不变)

Xiaogang Liao 3 hours ago
parent
commit
b6ed552e5b

+ 21 - 1
cfc-backend/src/main/java/com/etotem/cfc/task/TaskDailyReminderScheduledTask.java

@@ -173,7 +173,7 @@ public class TaskDailyReminderScheduledTask {
             for (Map.Entry<Long, List<Task>> entry : byUser.entrySet()) {
             for (Map.Entry<Long, List<Task>> entry : byUser.entrySet()) {
                 Long userId = entry.getKey();
                 Long userId = entry.getKey();
                 List<Task> userTasks = entry.getValue();
                 List<Task> userTasks = entry.getValue();
-                if (!isDailyEnabled(userId)) {
+                if (!isDailyEnabledEvening(userId)) {
                     continue;
                     continue;
                 }
                 }
                 if (alreadySentToday(userId, TYPE_EVENING, dayStart)) {
                 if (alreadySentToday(userId, TYPE_EVENING, dayStart)) {
@@ -337,6 +337,26 @@ public class TaskDailyReminderScheduledTask {
         return map;
         return map;
     }
     }
 
 
+    /**
+     * 晚间鼓励开关(2026-11-29):默认开启——无配置记录 / daily_enabled 为空 → 开启(1);
+     * 仅显式 0 → 关闭。与早提醒的 isDailyEnabled 分离,保证早提醒收件人不扩大。
+     */
+    private boolean isDailyEnabledEvening(Long userId) {
+        if (taskReminderConfigService == null) {
+            return true; // 单测未注入配置服务 → 不拦截
+        }
+        try {
+            TaskReminderConfig config = taskReminderConfigService.loadAllConfigs().get(userId);
+            if (config == null || config.getDailyEnabled() == null) {
+                return true;
+            }
+            return !Integer.valueOf(0).equals(config.getDailyEnabled());
+        } catch (Exception e) {
+            log.warn("日提醒-晚:配置读取失败 userId={}, error={}", userId, e.getMessage());
+            return true; // 晚间默认开,读配置失败也不阻断
+        }
+    }
+
     /** 日提醒开关:仅当配置服务可用时按 daily_enabled=1 过滤(单测未注入 → 不拦截)。 */
     /** 日提醒开关:仅当配置服务可用时按 daily_enabled=1 过滤(单测未注入 → 不拦截)。 */
     private boolean isDailyEnabled(Long userId) {
     private boolean isDailyEnabled(Long userId) {
         if (taskReminderConfigService == null) {
         if (taskReminderConfigService == null) {

+ 68 - 0
cfc-backend/src/test/java/com/etotem/cfc/unit/TaskReminderDailyScheduledTaskContractTest.java

@@ -308,6 +308,74 @@ class TaskReminderDailyScheduledTaskContractTest {
                 any(), anyLong(), any(), any());
                 any(), anyLong(), any(), any());
     }
     }
 
 
+    // ==================== 晚默认开 / 早不变 拆开关(2026-11-29 增强) ====================
+
+    /** 装配一个带 wechat channel + 指定 config service 的 host */
+    @SuppressWarnings("unchecked")
+    private Object hostWithConfigAndChannel(TaskReminderConfigService cfgService,
+                                            com.etotem.cfc.service.notification.NotificationChannel wechat) {
+        Object host = contractHost();
+        inject(host, "taskReminderConfigService", cfgService);
+        inject(host, "userMapper", mock(com.etotem.cfc.mapper.UserMapper.class));
+        inject(host, "notificationChannels", new ArrayList<>(
+                java.util.Collections.singletonList(wechat)));
+        return host;
+    }
+
+    @Test
+    @DisplayName("晚默认开-1:无配置记录用户(configService 返回空map)→ 晚提醒仍发送")
+    @SuppressWarnings("unchecked")
+    void eveningDefaultOn_noConfig() throws Exception {
+        TaskReminderConfigService cfg = mock(TaskReminderConfigService.class);
+        when(cfg.loadAllConfigs()).thenReturn(new java.util.HashMap<>());
+        com.etotem.cfc.service.notification.NotificationChannel wechat =
+                mock(com.etotem.cfc.service.notification.NotificationChannel.class);
+        when(wechat.getChannelType()).thenReturn("wechat");
+        when(wechat.isEnabled()).thenReturn(true);
+        Object host = hostWithConfigAndChannel(cfg, wechat);
+
+        TaskMapper tm = mock(TaskMapper.class);
+        UserNotificationMapper un = mock(UserNotificationMapper.class);
+        inject(host, "taskMapper", tm);
+        inject(host, "userNotificationMapper", un);
+        Task done = doneToday(20L, "任务");
+        done.setExecutorId(200L);
+        when(tm.selectList(any())).thenReturn(new ArrayList<>(java.util.Collections.singletonList(done)));
+        when(un.selectCount(any())).thenReturn(0L);
+
+        call(host, "sendEveningSummary");
+        verify(wechat, atLeastOnce()).send(any(com.etotem.cfc.dto.NotificationMessage.class));
+    }
+
+    @Test
+    @DisplayName("晚默认开-2:显式 daily_enabled=0 → 晚提醒跳过")
+    @SuppressWarnings("unchecked")
+    void eveningOff_explicitZero() throws Exception {
+        TaskReminderConfigService cfg = mock(TaskReminderConfigService.class);
+        com.etotem.cfc.entity.TaskReminderConfig conf = new com.etotem.cfc.entity.TaskReminderConfig();
+        conf.setUserId(300L);
+        conf.setDailyEnabled(0);
+        when(cfg.loadAllConfigs()).thenReturn(new java.util.HashMap<>(
+                java.util.Collections.singletonMap(300L, conf)));
+        com.etotem.cfc.service.notification.NotificationChannel wechat =
+                mock(com.etotem.cfc.service.notification.NotificationChannel.class);
+        when(wechat.getChannelType()).thenReturn("wechat");
+        when(wechat.isEnabled()).thenReturn(true);
+        Object host = hostWithConfigAndChannel(cfg, wechat);
+
+        TaskMapper tm = mock(TaskMapper.class);
+        UserNotificationMapper un = mock(UserNotificationMapper.class);
+        inject(host, "taskMapper", tm);
+        inject(host, "userNotificationMapper", un);
+        Task done = doneToday(30L, "任务");
+        done.setExecutorId(300L);
+        when(tm.selectList(any())).thenReturn(new ArrayList<>(java.util.Collections.singletonList(done)));
+        when(un.selectCount(any())).thenReturn(0L);
+
+        call(host, "sendEveningSummary");
+        verify(wechat, never()).send(any(com.etotem.cfc.dto.NotificationMessage.class));
+    }
+
     // ==================== 晚间 webhook 分发(2026-11-29 增强) ====================
     // ==================== 晚间 webhook 分发(2026-11-29 增强) ====================
 
 
     @Test
     @Test