|
@@ -0,0 +1,190 @@
|
|
|
|
|
+package com.etotem.cfc.task;
|
|
|
|
|
+
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
+
|
|
|
|
|
+import com.etotem.cfc.config.DatabaseInitializer;
|
|
|
|
|
+import com.etotem.cfc.entity.Task;
|
|
|
|
|
+import com.etotem.cfc.entity.TaskReminderConfig;
|
|
|
|
|
+import com.etotem.cfc.dto.NotificationMessage;
|
|
|
|
|
+import com.etotem.cfc.entity.UserNotification;
|
|
|
|
|
+import com.etotem.cfc.service.notification.NotificationChannel;
|
|
|
|
|
+import com.etotem.cfc.mapper.TaskMapper;
|
|
|
|
|
+import com.etotem.cfc.mapper.UserNotificationMapper;
|
|
|
|
|
+import com.etotem.cfc.service.TaskReminderConfigService;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
+
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.Calendar;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 任务五阶段未完成提醒(2026-11-29 新增的提醒方式)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>在起床后/早餐后/午餐后/晚餐后/睡前五个可配置时间点,提醒用户当天尚未完成的任务。
|
|
|
|
|
+ * 只提醒任务(不纳入健康打卡),渠道为站内 user_notifications + 企业微信 webhook
|
|
|
|
|
+ * (复用 NotificationDispatchService.sendByTemplate,in_app 恒兜底 + 模板 channels)。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>触发机制</b>:cron 每分钟扫描,按当前 HH:mm 与用户配置的阶段时间精确匹配;
|
|
|
|
|
+ * 用 user_notifications 当日 (user_id, type) 判重,保证每阶段每用户每天最多一次。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>收件人/任务口径</b>:与现有活跃提醒一致——executor_type='parent' 的 executor_id(users.id),
|
|
|
|
|
+ * status IN (pending, pending_review),deadline 当天,is_template=0。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+@Component
|
|
|
|
|
+public class TaskStageReminderScheduledTask {
|
|
|
|
|
+
|
|
|
|
|
+ private static final String REF_TYPE = "task";
|
|
|
|
|
+ private static final int MAX_TASKS_PER_STAGE = 200;
|
|
|
|
|
+
|
|
|
|
|
+ /** 阶段定义:通知 type + 配置字段默认值(与迁移508/DTO 默认值一致) */
|
|
|
|
|
+ private static final Stage[] STAGES = {
|
|
|
|
|
+ new Stage("task_stage_wake", "起床后", "07:00", "起床后"),
|
|
|
|
|
+ new Stage("task_stage_breakfast", "早餐后", "08:00", "早餐后"),
|
|
|
|
|
+ new Stage("task_stage_lunch", "午餐后", "12:30", "午餐后"),
|
|
|
|
|
+ new Stage("task_stage_dinner", "晚餐后", "19:00", "晚餐后"),
|
|
|
|
|
+ new Stage("task_stage_bedtime", "睡前", "21:30", "睡前")
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ private static class Stage {
|
|
|
|
|
+ final String type;
|
|
|
|
|
+ final String label;
|
|
|
|
|
+ final String defaultTime;
|
|
|
|
|
+ final String titlePrefix;
|
|
|
|
|
+ Stage(String type, String label, String defaultTime, String titlePrefix) {
|
|
|
|
|
+ this.type = type;
|
|
|
|
|
+ this.label = label;
|
|
|
|
|
+ this.defaultTime = defaultTime;
|
|
|
|
|
+ this.titlePrefix = titlePrefix;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private TaskMapper taskMapper;
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private UserNotificationMapper userNotificationMapper;
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private List<NotificationChannel> notificationChannels;
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private TaskReminderConfigService taskReminderConfigService;
|
|
|
|
|
+
|
|
|
|
|
+ @Scheduled(cron = "0 * * * * ?")
|
|
|
|
|
+ public void remindByStage() {
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (!DatabaseInitializer.isMigrationsComplete()) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ Map<Long, TaskReminderConfig> configs = taskReminderConfigService != null
|
|
|
|
|
+ ? taskReminderConfigService.loadAllConfigs() : null;
|
|
|
|
|
+ if (configs == null || configs.isEmpty()) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Calendar now = Calendar.getInstance();
|
|
|
|
|
+ String currentHHmm = String.format("%02d:%02d",
|
|
|
|
|
+ now.get(Calendar.HOUR_OF_DAY), now.get(Calendar.MINUTE));
|
|
|
|
|
+ Date dayStart = dayStart(now);
|
|
|
|
|
+ Date dayEnd = dayEnd(now);
|
|
|
|
|
+
|
|
|
|
|
+ for (Map.Entry<Long, TaskReminderConfig> entry : configs.entrySet()) {
|
|
|
|
|
+ Long userId = entry.getKey();
|
|
|
|
|
+ TaskReminderConfig cfg = entry.getValue();
|
|
|
|
|
+ if (cfg == null || !Integer.valueOf(1).equals(cfg.getStageEnabled())) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ for (Stage stage : STAGES) {
|
|
|
|
|
+ String configured = stageTime(cfg, stage);
|
|
|
|
|
+ if (!currentHHmm.equals(configured)) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (alreadySent(userId, stage.type, dayStart)) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ List<Task> pending = pendingTasks(userId, dayStart, dayEnd);
|
|
|
|
|
+ if (pending.isEmpty()) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
|
|
+ sb.append("【").append(stage.titlePrefix).append("任务提醒】今天还有 ")
|
|
|
|
|
+ .append(pending.size()).append(" 项任务未完成:\n");
|
|
|
|
|
+ for (Task t : pending) {
|
|
|
|
|
+ sb.append("· ").append(t.getTitle() == null ? "未命名任务" : t.getTitle()).append("\n");
|
|
|
|
|
+ }
|
|
|
|
|
+ sb.append("请打开小程序完成任务。");
|
|
|
|
|
+ NotificationMessage msg = new NotificationMessage(userId, stage.type,
|
|
|
|
|
+ "阶段任务提醒 · " + stage.label, sb.toString(), REF_TYPE, 0L, "in_app,wechat");
|
|
|
|
|
+ try {
|
|
|
|
|
+ for (NotificationChannel ch : notificationChannels) {
|
|
|
|
|
+ if (ch == null || !ch.isEnabled()) continue;
|
|
|
|
|
+ if ("in_app".equals(ch.getChannelType()) || "wechat".equals(ch.getChannelType())) {
|
|
|
|
|
+ ch.send(msg);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ log.info("五阶段提醒已发送: userId={}, stage={}, tasks={}", userId, stage.type, pending.size());
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.warn("五阶段提醒发送失败: userId={}, stage={}, error={}", userId, stage.type, e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.warn("五阶段提醒调度异常: {}", e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String stageTime(TaskReminderConfig cfg, Stage stage) {
|
|
|
|
|
+ String v = null;
|
|
|
|
|
+ if (stage.type.equals("task_stage_wake")) v = cfg.getStageWakeTime();
|
|
|
|
|
+ else if (stage.type.equals("task_stage_breakfast")) v = cfg.getStageBreakfastTime();
|
|
|
|
|
+ else if (stage.type.equals("task_stage_lunch")) v = cfg.getStageLunchTime();
|
|
|
|
|
+ else if (stage.type.equals("task_stage_dinner")) v = cfg.getStageDinnerTime();
|
|
|
|
|
+ else if (stage.type.equals("task_stage_bedtime")) v = cfg.getStageBedtimeTime();
|
|
|
|
|
+ return v != null && v.matches("^([01]\\d|2[0-3]):[0-5]\\d$") ? v : stage.defaultTime;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private List<Task> pendingTasks(Long userId, Date dayStart, Date dayEnd) {
|
|
|
|
|
+ LambdaQueryWrapper<Task> w = new LambdaQueryWrapper<>();
|
|
|
|
|
+ w.eq(Task::getExecutorType, "parent")
|
|
|
|
|
+ .eq(Task::getExecutorId, userId)
|
|
|
|
|
+ .eq(Task::getIsTemplate, 0)
|
|
|
|
|
+ .in(Task::getStatus, "pending", "pending_review")
|
|
|
|
|
+ .ge(Task::getDeadline, dayStart)
|
|
|
|
|
+ .lt(Task::getDeadline, dayEnd)
|
|
|
|
|
+ .last("LIMIT " + MAX_TASKS_PER_STAGE);
|
|
|
|
|
+ return taskMapper.selectList(w);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private boolean alreadySent(Long userId, String type, Date dayStart) {
|
|
|
|
|
+ LambdaQueryWrapper<UserNotification> w = new LambdaQueryWrapper<>();
|
|
|
|
|
+ w.eq(UserNotification::getUserId, userId)
|
|
|
|
|
+ .eq(UserNotification::getType, type)
|
|
|
|
|
+ .eq(UserNotification::getRefType, REF_TYPE)
|
|
|
|
|
+ .ge(UserNotification::getCreatedAt, dayStart);
|
|
|
|
|
+ return userNotificationMapper.selectCount(w) > 0;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Date dayStart(Calendar now) {
|
|
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
|
|
+ c.setTime(now.getTime());
|
|
|
|
|
+ c.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
|
|
+ c.set(Calendar.MINUTE, 0);
|
|
|
|
|
+ c.set(Calendar.SECOND, 0);
|
|
|
|
|
+ c.set(Calendar.MILLISECOND, 0);
|
|
|
|
|
+ return c.getTime();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Date dayEnd(Calendar now) {
|
|
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
|
|
+ c.setTime(now.getTime());
|
|
|
|
|
+ c.set(Calendar.HOUR_OF_DAY, 23);
|
|
|
|
|
+ c.set(Calendar.MINUTE, 59);
|
|
|
|
|
+ c.set(Calendar.SECOND, 59);
|
|
|
|
|
+ c.set(Calendar.MILLISECOND, 999);
|
|
|
|
|
+ return c.getTime();
|
|
|
|
|
+ }
|
|
|
|
|
+}
|