| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- package com.train.service;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.train.entity.TrainAuditLog;
- import com.train.entity.TrainPlan;
- import com.train.entity.TrainUser;
- import com.train.mapper.TrainAuditLogMapper;
- import com.train.mapper.TrainPlanMapper;
- import com.train.mapper.TrainUserMapper;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.scheduling.annotation.Scheduled;
- import org.springframework.stereotype.Component;
- import javax.annotation.Resource;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Date;
- import java.util.List;
- /**
- * T+ 跟进定时任务:每日定时扫描已提交 7 天行动计划的学员,
- * 相对计划提交日(T0)生成 T+1 / T+3 / T+7 提醒记录,落 audit_log(幂等)。
- * 落账成功后同步推送订阅消息(按 openid 发送;test-mode 打日志)。
- */
- @Slf4j
- @Component
- public class FollowUpTask {
- @Resource
- private TrainPlanMapper trainPlanMapper;
- @Resource
- private TrainAuditLogMapper trainAuditLogMapper;
- @Resource
- private TrainUserMapper trainUserMapper;
- @Resource
- private SubscribeMessageService subscribeMessageService;
- private static final long DAY_MILLIS = 24L * 60 * 60 * 1000;
- /** 各跟进里程碑的订阅消息 key(对应 wechat.message-template 配置)与跳转页 */
- private static final Map<String, String[]> MILESTONE_MESSAGE = new HashMap<>();
- static {
- MILESTONE_MESSAGE.put("followup_t1", new String[]{"followup-t1", "pages/mine/index"});
- MILESTONE_MESSAGE.put("followup_t3", new String[]{"followup-t3", "pages/mine/index"});
- MILESTONE_MESSAGE.put("followup_t7", new String[]{"followup-t7", "pages/mine/index"});
- MILESTONE_MESSAGE.put("followup_t21", new String[]{"followup-t21", "pages/mine/index"});
- MILESTONE_MESSAGE.put("followup_t30", new String[]{"followup-t30", "pages/mine/index"});
- }
- /** 每日 09:30 执行(课后跟进节奏:T+1 成果合集 / T+3 补齐 / T+7 回访) */
- @Scheduled(cron = "0 30 9 * * ?")
- public void runFollowUp() {
- List<TrainPlan> plans = trainPlanMapper.selectList(
- new LambdaQueryWrapper<TrainPlan>().orderByAsc(TrainPlan::getSubmittedAt));
- Date now = new Date();
- for (TrainPlan plan : plans) {
- if (plan.getUid() == null || plan.getSubmittedAt() == null) {
- continue;
- }
- long elapsedDays = (now.getTime() - plan.getSubmittedAt().getTime()) / DAY_MILLIS;
- if (elapsedDays >= 1) {
- ensureLog(plan.getUid(), "followup_t1", "T+1 跟进提醒:课堂成果合集与照片已同步,请查收");
- }
- if (elapsedDays >= 3) {
- ensureLog(plan.getUid(), "followup_t3", "T+3 跟进提醒:未完成项建议补齐(三本账/自动化)");
- }
- if (elapsedDays >= 7) {
- ensureLog(plan.getUid(), "followup_t7", "T+7 回访:7 天行动计划完成度自检,欢迎约 1v1");
- }
- if (elapsedDays >= 21) {
- ensureLog(plan.getUid(), "followup_t21", "T+21 跟进:约线上复盘会(教练陪跑入口)");
- }
- if (elapsedDays >= 30) {
- ensureLog(plan.getUid(), "followup_t30", "T+30 留存统计与续课推荐:L1/L2 工作坊与系统营");
- }
- }
- log.info("FollowUpTask 完成一轮 T+ 跟进扫描,共 {} 条计划", plans.size());
- }
- /** 幂等写入 audit_log:同 action + targetId 已存在则跳过;落账成功后推送订阅消息 */
- private void ensureLog(Long uid, String action, String detail) {
- Long exists = trainAuditLogMapper.selectCount(
- new LambdaQueryWrapper<TrainAuditLog>()
- .eq(TrainAuditLog::getAction, action)
- .eq(TrainAuditLog::getTargetId, uid));
- if (exists != null && exists > 0) {
- return;
- }
- TrainAuditLog entry = new TrainAuditLog();
- entry.setAction(action);
- entry.setActorUid(null);
- entry.setTargetType("user");
- entry.setTargetId(uid);
- entry.setDetail(detail);
- entry.setTs(new Date());
- trainAuditLogMapper.insert(entry);
- pushMessage(uid, action, detail);
- }
- /** 用 openid 推送订阅消息(无 openid/未配置模板时静默跳过) */
- private void pushMessage(Long uid, String action, String detail) {
- TrainUser user = trainUserMapper.selectById(uid);
- if (user == null || user.getOpenid() == null || user.getOpenid().isEmpty()) {
- return;
- }
- String[] msg = MILESTONE_MESSAGE.get(action);
- if (msg == null) {
- return;
- }
- Map<String, Object> data = new HashMap<>();
- data.put("thing1", detail);
- subscribeMessageService.sendToUser(user.getOpenid(), msg[0], msg[1], data);
- }
- }
|