|
@@ -0,0 +1,172 @@
|
|
|
|
|
+package com.etotem.cfc.task;
|
|
|
|
|
+
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
+import com.etotem.cfc.config.DatabaseInitializer;
|
|
|
|
|
+import com.etotem.cfc.entity.FamilyMember;
|
|
|
|
|
+import com.etotem.cfc.entity.Task;
|
|
|
|
|
+import com.etotem.cfc.entity.TaskReminderConfig;
|
|
|
|
|
+import com.etotem.cfc.entity.UserNotification;
|
|
|
|
|
+import com.etotem.cfc.mapper.FamilyMemberMapper;
|
|
|
|
|
+import com.etotem.cfc.mapper.TaskMapper;
|
|
|
|
|
+import com.etotem.cfc.mapper.UserNotificationMapper;
|
|
|
|
|
+import com.etotem.cfc.service.NotificationDispatchService;
|
|
|
|
|
+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.HashMap;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 周总结提醒(迁移515):每周日按用户配置时间汇总「本人本周执行任务」推送。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>统计范围:按 userId 反查其所有 family_members 成员卡,汇总这些成员名下本周
|
|
|
|
|
+ * (周一 00:00 ~ 下周一 00:00)到期的实例任务(is_template=0),覆盖全部状态。
|
|
|
|
|
+ * 内容含完成数/总数、完成率、连续打卡天数、未完成前 5 条标题。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>开关与时间在提醒设置里配置(weekly_enabled / weekly_time,默认 0 / 21:00)。
|
|
|
|
|
+ * 去重按 userId + type + 周起始日,每周每用户至多一条。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+@Component
|
|
|
|
|
+public class TaskWeeklySummaryScheduledTask {
|
|
|
|
|
+
|
|
|
|
|
+ private static final String NOTIFY_TYPE = "task_weekly_summary";
|
|
|
|
|
+ private static final String REF_TYPE = "task";
|
|
|
|
|
+ private static final String DEFAULT_WEEKLY_TIME = "21:00";
|
|
|
|
|
+ private static final int MAX_UNDONE = 5;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private TaskMapper taskMapper;
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private FamilyMemberMapper familyMemberMapper;
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private UserNotificationMapper userNotificationMapper;
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private TaskReminderConfigService taskReminderConfigService;
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private NotificationDispatchService notificationDispatchService;
|
|
|
|
|
+
|
|
|
|
|
+ @Scheduled(cron = "0 * * * * ?")
|
|
|
|
|
+ public void sendWeeklySummary() {
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (!DatabaseInitializer.isMigrationsComplete()) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ Calendar now = Calendar.getInstance();
|
|
|
|
|
+ if (now.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ String hhmm = String.format("%02d:%02d", now.get(Calendar.HOUR_OF_DAY), now.get(Calendar.MINUTE));
|
|
|
|
|
+
|
|
|
|
|
+ Map<Long, TaskReminderConfig> configs = taskReminderConfigService.loadAllConfigs();
|
|
|
|
|
+ if (configs == null || configs.isEmpty()) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ Date weekStart = weekStart(now);
|
|
|
|
|
+ Date weekEnd = addDays(weekStart, 7);
|
|
|
|
|
+
|
|
|
|
|
+ for (Map.Entry<Long, TaskReminderConfig> entry : configs.entrySet()) {
|
|
|
|
|
+ Long userId = entry.getKey();
|
|
|
|
|
+ TaskReminderConfig cfg = entry.getValue();
|
|
|
|
|
+ if (cfg == null || !Integer.valueOf(1).equals(cfg.getWeeklyEnabled())) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ String wt = cfg.getWeeklyTime() != null
|
|
|
|
|
+ && cfg.getWeeklyTime().matches("^([01]\\d|2[0-3]):[0-5]\\d$")
|
|
|
|
|
+ ? cfg.getWeeklyTime() : DEFAULT_WEEKLY_TIME;
|
|
|
|
|
+ if (!hhmm.equals(wt)) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (alreadySentThisWeek(userId, weekStart)) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 反查该用户所有成员卡(统一后 executor_id=member.id)
|
|
|
|
|
+ List<FamilyMember> members = familyMemberMapper.selectList(
|
|
|
|
|
+ new LambdaQueryWrapper<FamilyMember>().eq(FamilyMember::getUserId, userId));
|
|
|
|
|
+ List<Long> memberIds = new ArrayList<Long>();
|
|
|
|
|
+ int streak = 0;
|
|
|
|
|
+ for (FamilyMember fm : members) {
|
|
|
|
|
+ memberIds.add(fm.getId());
|
|
|
|
|
+ if (fm.getStreakDays() != null) {
|
|
|
|
|
+ streak = Math.max(streak, fm.getStreakDays());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (memberIds.isEmpty()) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ List<Task> tasks = taskMapper.selectList(new LambdaQueryWrapper<Task>()
|
|
|
|
|
+ .in(Task::getExecutorId, memberIds)
|
|
|
|
|
+ .eq(Task::getIsTemplate, 0)
|
|
|
|
|
+ .ge(Task::getDeadline, weekStart)
|
|
|
|
|
+ .lt(Task::getDeadline, weekEnd)
|
|
|
|
|
+ .orderByAsc(Task::getDeadline)
|
|
|
|
|
+ .last("LIMIT 500"));
|
|
|
|
|
+
|
|
|
|
|
+ int done = 0;
|
|
|
|
|
+ List<String> undone = new ArrayList<String>();
|
|
|
|
|
+ for (Task t : tasks) {
|
|
|
|
|
+ if ("completed".equals(t.getStatus())) {
|
|
|
|
|
+ done++;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ undone.add(t.getTitle());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ int rate = tasks.isEmpty() ? 0 : (int) Math.round(done * 100.0 / tasks.size());
|
|
|
|
|
+
|
|
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
|
|
+ sb.append("本周任务总结:完成 ").append(done).append("/").append(tasks.size())
|
|
|
|
|
+ .append("(完成率 ").append(rate).append("%),连续打卡 ").append(streak).append(" 天。");
|
|
|
|
|
+ if (!undone.isEmpty()) {
|
|
|
|
|
+ List<String> top = undone.subList(0, Math.min(MAX_UNDONE, undone.size()));
|
|
|
|
|
+ sb.append("未完成:").append(String.join("、", top));
|
|
|
|
|
+ if (undone.size() > MAX_UNDONE) {
|
|
|
|
|
+ sb.append(" 等 ").append(undone.size()).append(" 项");
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ sb.append("全部完成,太棒了!");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ notificationDispatchService.sendByTemplate(NOTIFY_TYPE, userId, new HashMap<String, Object>(),
|
|
|
|
|
+ REF_TYPE, 0L, "本周任务总结", sb.toString());
|
|
|
|
|
+ log.info("周总结提醒已发送 userId={}, 完成={}/{}, 连续={}天", userId, done, tasks.size(), streak);
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.warn("周总结提醒执行异常: {}", e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private boolean alreadySentThisWeek(Long userId, Date weekStart) {
|
|
|
|
|
+ Long cnt = userNotificationMapper.selectCount(new LambdaQueryWrapper<UserNotification>()
|
|
|
|
|
+ .eq(UserNotification::getUserId, userId)
|
|
|
|
|
+ .eq(UserNotification::getType, NOTIFY_TYPE)
|
|
|
|
|
+ .eq(UserNotification::getRefType, REF_TYPE)
|
|
|
|
|
+ .ge(UserNotification::getCreatedAt, weekStart));
|
|
|
|
|
+ return cnt != null && cnt > 0;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Date weekStart(Calendar now) {
|
|
|
|
|
+ Calendar c = (Calendar) now.clone();
|
|
|
|
|
+ c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
|
|
|
|
|
+ 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 addDays(Date d, int days) {
|
|
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
|
|
+ c.setTime(d);
|
|
|
|
|
+ c.add(Calendar.DAY_OF_MONTH, days);
|
|
|
|
|
+ return c.getTime();
|
|
|
|
|
+ }
|
|
|
|
|
+}
|