Parcourir la source

feat(task-source): 迁移296/297修正存量健康/认知训练方案任务source_type

iwt il y a 2 semaines
Parent
commit
620c869a96

+ 33 - 0
cfc-backend/src/main/java/com/etotem/cfc/config/DatabaseInitializer.java

@@ -10358,6 +10358,39 @@ public class DatabaseInitializer implements CommandLineRunner {
         ensureColumn("task_plan_instances", "activated_at", "DATETIME DEFAULT NULL COMMENT '激活时间'");
         log.info("迁移295: 已补齐 task_plan_instances 审核/确认/激活列");
 
+        // 迁移296: 健康方案任务 source_type 'plan' -> 'health_plan'(消除与 task_plan_instances 的 ID 域冲突)
+        try {
+            // 冲突检测:source_id 同时存在于 health_plans 和 task_plan_instances 的任务(需人工确认)
+            Integer conflict = jdbcTemplate.queryForObject(
+                    "SELECT COUNT(*) FROM tasks t WHERE t.source_type = 'plan' " +
+                    "AND EXISTS (SELECT 1 FROM health_plans h WHERE h.id = t.source_id) " +
+                    "AND EXISTS (SELECT 1 FROM task_plan_instances i WHERE i.id = t.source_id)",
+                    Integer.class);
+            if (conflict != null && conflict > 0) {
+                log.warn("迁移296: 检测到{}条冲突任务(source_id同时存在于health_plans和task_plan_instances),跳过迁移,需人工确认", conflict);
+            } else {
+                int updated = jdbcTemplate.update(
+                        "UPDATE tasks SET source_type = 'health_plan' " +
+                        "WHERE source_type = 'plan' " +
+                        "AND EXISTS (SELECT 1 FROM health_plans h WHERE h.id = tasks.source_id) " +
+                        "AND NOT EXISTS (SELECT 1 FROM task_plan_instances i WHERE i.id = tasks.source_id)");
+                log.info("迁移296: 已修正{}条健康方案任务 source_type -> health_plan", updated);
+            }
+        } catch (Exception e) {
+            log.warn("迁移296执行失败(health_plans 表可能不存在): {}", e.getMessage());
+        }
+
+        // 迁移297: 认知训练方案任务补设 source_type='training_plan'(training_plan_tasks 关联溯源)
+        try {
+            Integer updated = jdbcTemplate.update(
+                    "UPDATE tasks t SET t.source_type = 'training_plan' " +
+                    "WHERE (t.source_type IS NULL OR t.source_type = '') " +
+                    "AND EXISTS (SELECT 1 FROM training_plan_tasks pt WHERE pt.task_id = t.id)");
+            log.info("迁移297: 已补设{}条认知训练方案任务 source_type -> training_plan", updated);
+        } catch (Exception e) {
+            log.warn("迁移297执行失败(training_plan_tasks 表可能不存在): {}", e.getMessage());
+        }
+
         // ===== 生产库对账候选风险记录(已核对生产库,2026-09,按用户决定不迁移/暂不动,仅记录)=====
         // 注:以下为 entity 期望但生产库未对齐.项目,用户已决定保持现状或记为候选,勿重复迁移。
         // 1. children 表缺 phone、id_card(Child 实体+ChildMapper 仍活跃,AccountCleanupService