FollowUpTask.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package com.train.service;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.train.entity.TrainAuditLog;
  4. import com.train.entity.TrainPlan;
  5. import com.train.entity.TrainUser;
  6. import com.train.mapper.TrainAuditLogMapper;
  7. import com.train.mapper.TrainPlanMapper;
  8. import com.train.mapper.TrainUserMapper;
  9. import lombok.extern.slf4j.Slf4j;
  10. import org.springframework.scheduling.annotation.Scheduled;
  11. import org.springframework.stereotype.Component;
  12. import javax.annotation.Resource;
  13. import java.util.HashMap;
  14. import java.util.Map;
  15. import java.util.Date;
  16. import java.util.List;
  17. /**
  18. * T+ 跟进定时任务:每日定时扫描已提交 7 天行动计划的学员,
  19. * 相对计划提交日(T0)生成 T+1 / T+3 / T+7 提醒记录,落 audit_log(幂等)。
  20. * 落账成功后同步推送订阅消息(按 openid 发送;test-mode 打日志)。
  21. */
  22. @Slf4j
  23. @Component
  24. public class FollowUpTask {
  25. @Resource
  26. private TrainPlanMapper trainPlanMapper;
  27. @Resource
  28. private TrainAuditLogMapper trainAuditLogMapper;
  29. @Resource
  30. private TrainUserMapper trainUserMapper;
  31. @Resource
  32. private SubscribeMessageService subscribeMessageService;
  33. private static final long DAY_MILLIS = 24L * 60 * 60 * 1000;
  34. /** 各跟进里程碑的订阅消息 key(对应 wechat.message-template 配置)与跳转页 */
  35. private static final Map<String, String[]> MILESTONE_MESSAGE = new HashMap<>();
  36. static {
  37. MILESTONE_MESSAGE.put("followup_t1", new String[]{"followup-t1", "pages/mine/index"});
  38. MILESTONE_MESSAGE.put("followup_t3", new String[]{"followup-t3", "pages/mine/index"});
  39. MILESTONE_MESSAGE.put("followup_t7", new String[]{"followup-t7", "pages/mine/index"});
  40. MILESTONE_MESSAGE.put("followup_t21", new String[]{"followup-t21", "pages/mine/index"});
  41. MILESTONE_MESSAGE.put("followup_t30", new String[]{"followup-t30", "pages/mine/index"});
  42. }
  43. /** 每日 09:30 执行(课后跟进节奏:T+1 成果合集 / T+3 补齐 / T+7 回访) */
  44. @Scheduled(cron = "0 30 9 * * ?")
  45. public void runFollowUp() {
  46. List<TrainPlan> plans = trainPlanMapper.selectList(
  47. new LambdaQueryWrapper<TrainPlan>().orderByAsc(TrainPlan::getSubmittedAt));
  48. Date now = new Date();
  49. for (TrainPlan plan : plans) {
  50. if (plan.getUid() == null || plan.getSubmittedAt() == null) {
  51. continue;
  52. }
  53. long elapsedDays = (now.getTime() - plan.getSubmittedAt().getTime()) / DAY_MILLIS;
  54. if (elapsedDays >= 1) {
  55. ensureLog(plan.getUid(), "followup_t1", "T+1 跟进提醒:课堂成果合集与照片已同步,请查收");
  56. }
  57. if (elapsedDays >= 3) {
  58. ensureLog(plan.getUid(), "followup_t3", "T+3 跟进提醒:未完成项建议补齐(三本账/自动化)");
  59. }
  60. if (elapsedDays >= 7) {
  61. ensureLog(plan.getUid(), "followup_t7", "T+7 回访:7 天行动计划完成度自检,欢迎约 1v1");
  62. }
  63. if (elapsedDays >= 21) {
  64. ensureLog(plan.getUid(), "followup_t21", "T+21 跟进:约线上复盘会(教练陪跑入口)");
  65. }
  66. if (elapsedDays >= 30) {
  67. ensureLog(plan.getUid(), "followup_t30", "T+30 留存统计与续课推荐:L1/L2 工作坊与系统营");
  68. }
  69. }
  70. log.info("FollowUpTask 完成一轮 T+ 跟进扫描,共 {} 条计划", plans.size());
  71. }
  72. /** 幂等写入 audit_log:同 action + targetId 已存在则跳过;落账成功后推送订阅消息 */
  73. private void ensureLog(Long uid, String action, String detail) {
  74. Long exists = trainAuditLogMapper.selectCount(
  75. new LambdaQueryWrapper<TrainAuditLog>()
  76. .eq(TrainAuditLog::getAction, action)
  77. .eq(TrainAuditLog::getTargetId, uid));
  78. if (exists != null && exists > 0) {
  79. return;
  80. }
  81. TrainAuditLog entry = new TrainAuditLog();
  82. entry.setAction(action);
  83. entry.setActorUid(null);
  84. entry.setTargetType("user");
  85. entry.setTargetId(uid);
  86. entry.setDetail(detail);
  87. entry.setTs(new Date());
  88. trainAuditLogMapper.insert(entry);
  89. pushMessage(uid, action, detail);
  90. }
  91. /** 用 openid 推送订阅消息(无 openid/未配置模板时静默跳过) */
  92. private void pushMessage(Long uid, String action, String detail) {
  93. TrainUser user = trainUserMapper.selectById(uid);
  94. if (user == null || user.getOpenid() == null || user.getOpenid().isEmpty()) {
  95. return;
  96. }
  97. String[] msg = MILESTONE_MESSAGE.get(action);
  98. if (msg == null) {
  99. return;
  100. }
  101. Map<String, Object> data = new HashMap<>();
  102. data.put("thing1", detail);
  103. subscribeMessageService.sendToUser(user.getOpenid(), msg[0], msg[1], data);
  104. }
  105. }