package com.smartor.service.mch;
import com.smartor.common.MchFollowupConstants;
import com.smartor.domain.MchFollowupNode;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
* 妇幼随访 service_subtask 方案自检(纯 main,不依赖数据库)。
*
*
覆盖:followupPeriod 解析、发送时间计算、占位符渲染、Mapper XML、SQL 迁移脚本。
*/
public final class MchFollowupSubtaskLogicCheck {
private static int passed = 0;
private static int failed = 0;
private static final SimpleDateFormat DATE_TIME = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static void check(String key, Object expected, Object actual) {
boolean ok = expected == null ? actual == null : expected.equals(actual);
if (ok) {
passed++;
System.out.println(" [OK] " + key + " = " + actual);
} else {
failed++;
System.out.println(" [FAIL] " + key + " 期望=" + expected + " 实际=" + actual);
}
}
public static void main(String[] args) throws Exception {
System.out.println("========== 妇幼随访 service_subtask 方案自检 ==========");
System.out.println("-- 1) items 解析 --");
MchFollowupPeriodParser parser = new MchFollowupPeriodParser();
List nodes = parser.parse("{\"items\":["
+ "{\"seq\":1,\"dayOffset\":0,\"sendType\":\"sms\",\"content\":\"A\"},"
+ "{\"seq\":2,\"dayOffset\":3,\"sendType\":\"wechat\",\"templateId\":16,\"content\":\"B\"}]}", null);
check("节点数", 2, nodes.size());
check("节点1发送方式", MchFollowupConstants.SEND_TYPE_SMS, nodes.get(0).getSendType());
check("节点2模板ID", 16L, nodes.get(1).getTemplateId());
System.out.println("-- 2) intervalDays 解析 --");
List interval = parser.parse(
"{\"intervalDays\":3,\"count\":3,\"sendType\":\"sms\",\"content\":\"X\"}", null);
check("间隔节点数", 3, interval.size());
check("第3节点偏移", 6, interval.get(2).getDayOffset());
System.out.println("-- 3) 发送时间计算 --");
Date base = DATE_TIME.parse("2026-11-20 00:00:00");
MchFollowupTimeResolver resolver = new MchFollowupTimeResolver();
Date sendTime = resolver.resolve(base, interval.get(1));
check("第2节点时间", "2026-11-23 09:00:00", DATE_TIME.format(sendTime));
System.out.println("-- 4) 占位符渲染 --");
MchFollowupMessageSender sender = new MchFollowupMessageSender();
MchFollowupSendContext context = new MchFollowupSendContext();
context.setPatname("张三");
context.setPatid(100L);
context.setPhone("13800000000");
context.setIdcardno("330102199001010000");
context.setSourceBizNo("MZ001");
context.setArchiveType("1");
context.setOrgid("47226079133110211G1001");
context.setSeq(2);
context.setPlannedFollowTime(base);
String rendered = sender.renderContent(
"您好${patname},ID=${patid},电话=${phone},基准=${plannedFollowTime},序号=${seq}", context);
check("占位符", "您好张三,ID=100,电话=13800000000,基准=2026-11-20 00:00:00,序号=2", rendered);
System.out.println("-- 5) Mapper XML 可解析 --");
check("MchFollowupSubtaskMapper.xml", true, xmlOk(
"smartor/src/main/resources/mapper/smartor/MchFollowupSubtaskMapper.xml"));
System.out.println("-- 6) SQL 迁移脚本 --");
String sql = new String(Files.readAllBytes(
Paths.get("sql/service_subtask_add_mch_fields_20261123.sql")), StandardCharsets.UTF_8);
check("含 mch_archive_detail_id", true, sql.contains("mch_archive_detail_id"));
check("含 mch_node_json", true, sql.contains("mch_node_json"));
check("含唯一键", true, sql.contains("uk_mch_node"));
check("含懒生成游标", true, sql.contains("mch_next_due_time"));
check("含计划哈希", true, sql.contains("mch_plan_hash"));
System.out.println("========== 结果:PASS=" + passed + ",FAIL=" + failed + " ==========");
if (failed > 0) {
System.exit(1);
}
}
private static boolean xmlOk(String path) {
try {
File file = new File(path);
if (!file.exists()) {
System.out.println(" [WARN] 文件不存在: " + path);
return false;
}
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
factory.newDocumentBuilder().parse(file);
return true;
} catch (Exception e) {
System.out.println(" [WARN] XML 解析失败: " + e.getMessage());
return false;
}
}
}