package com.smartor.service.mch;
|
|
import com.smartor.common.MchFollowupConstants;
|
import com.smartor.domain.MchFollowupNode;
|
import com.smartor.domain.MchFollowupPlanItem;
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
import java.io.File;
|
import java.lang.reflect.Field;
|
import java.text.SimpleDateFormat;
|
import java.util.Date;
|
import java.util.List;
|
|
/**
|
* 妇幼随访逻辑自检(纯 main,不依赖 JUnit / Spring / 数据库)。
|
*
|
* 覆盖:followupPeriod JSON 解析、计划基准日时间计算、1 天补发窗口、
|
* 节点组装、Mapper XML 可解析性。
|
*/
|
public final class MchFollowupLogicCheck {
|
|
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);
|
}
|
}
|
|
private static void expectFail(String key, Runnable action) {
|
try {
|
action.run();
|
failed++;
|
System.out.println(" [FAIL] " + key + " 期望抛异常,实际未抛");
|
} catch (Exception e) {
|
passed++;
|
System.out.println(" [OK] " + key + " 抛异常:" + e.getMessage());
|
}
|
}
|
|
private static Date date(String text) throws Exception {
|
return DATE_TIME.parse(text);
|
}
|
|
private static MchFollowupPlanAssembler assembler() throws Exception {
|
MchFollowupPlanAssembler assembler = new MchFollowupPlanAssembler();
|
Field field = MchFollowupPlanAssembler.class.getDeclaredField("timeResolver");
|
field.setAccessible(true);
|
field.set(assembler, new MchFollowupTimeResolver());
|
return assembler;
|
}
|
|
private static MchFollowupNode node(int seq, int dayOffset, String sendTime, String sendType) {
|
MchFollowupNode node = new MchFollowupNode();
|
node.setSeq(seq);
|
node.setDayOffset(dayOffset);
|
node.setSendTime(sendTime);
|
node.setSendType(sendType);
|
node.setContent("内容" + seq);
|
return node;
|
}
|
|
public static void main(String[] args) throws Exception {
|
System.out.println("========== 妇幼随访逻辑自检 ==========");
|
MchFollowupPeriodParser parser = new MchFollowupPeriodParser();
|
MchFollowupTimeResolver resolver = new MchFollowupTimeResolver();
|
MchFollowupSendWindow window = new MchFollowupSendWindow();
|
|
System.out.println("-- 1) items JSON 解析 --");
|
String itemsJson = "{"
|
+ "\"version\":1,"
|
+ "\"items\":["
|
+ "{\"seq\":1,\"dayOffset\":0,\"sendTime\":\"09:00\",\"sendType\":\"sms\",\"content\":\"短信1\"},"
|
+ "{\"seq\":2,\"dayOffset\":3,\"sendType\":\"5\",\"templateId\":16,\"titleName\":\"提醒\",\"content\":\"微信2\"},"
|
+ "{\"seq\":3,\"dayOffset\":7,\"sendType\":\"短信\",\"content\":\"短信3\"}"
|
+ "]}";
|
List<MchFollowupNode> itemNodes = parser.parse(itemsJson, null);
|
check("节点数", 3, itemNodes.size());
|
check("seq1", 1, itemNodes.get(0).getSeq());
|
check("dayOffset1", 0, itemNodes.get(0).getDayOffset());
|
check("sendType1", "sms", itemNodes.get(0).getSendType());
|
check("sendType2", "wechat", itemNodes.get(1).getSendType());
|
check("templateId2", 16L, itemNodes.get(1).getTemplateId());
|
check("sendTime1", "09:00", itemNodes.get(0).getSendTime());
|
|
System.out.println("-- 2) intervalDays + count 解析 --");
|
String intervalJson = "{\"intervalDays\":3,\"count\":4,\"sendType\":\"sms\",\"content\":\"间隔短信\",\"sendTime\":\"08:30\"}";
|
List<MchFollowupNode> intervalNodes = parser.parse(intervalJson, null);
|
check("间隔节点数", 4, intervalNodes.size());
|
check("节点1偏移", 0, intervalNodes.get(0).getDayOffset());
|
check("节点2偏移", 3, intervalNodes.get(1).getDayOffset());
|
check("节点3偏移", 6, intervalNodes.get(2).getDayOffset());
|
check("节点4偏移", 9, intervalNodes.get(3).getDayOffset());
|
check("间隔sendTime", "08:30", intervalNodes.get(0).getSendTime());
|
|
System.out.println("-- 3) intervalDays + followupCycleTimes 兜底 --");
|
List<MchFollowupNode> cycleNodes = parser.parse(
|
"{\"intervalDays\":2,\"sendType\":\"wechat\",\"content\":\"微信\"}", "3");
|
check("兜底次数", 3, cycleNodes.size());
|
check("兜底节点3偏移", 4, cycleNodes.get(2).getDayOffset());
|
|
System.out.println("-- 4) days 数组解析 --");
|
List<MchFollowupNode> dayNodes = parser.parse(
|
"{\"days\":[0,7,14],\"sendType\":\"sms\",\"content\":\"按天\"}", null);
|
check("days节点数", 3, dayNodes.size());
|
check("days节点2偏移", 7, dayNodes.get(1).getDayOffset());
|
|
System.out.println("-- 5) 非法 JSON / 非法字段 --");
|
expectFail("非法JSON", () -> parser.parse("{bad", null));
|
expectFail("空节点", () -> parser.parse("{\"items\":[]}", null));
|
expectFail("非法sendType", () -> parser.parse(
|
"{\"items\":[{\"dayOffset\":0,\"sendType\":\"phone\",\"content\":\"x\"}]}", null));
|
expectFail("短信缺content", () -> parser.parse(
|
"{\"items\":[{\"dayOffset\":0,\"sendType\":\"sms\"}]}", null));
|
expectFail("非法sendTime", () -> parser.parse(
|
"{\"items\":[{\"dayOffset\":0,\"sendTime\":\"25:00\",\"sendType\":\"sms\",\"content\":\"x\"}]}", null));
|
|
System.out.println("-- 6) 基准日时间计算 --");
|
Date baseline = date("2026-11-20 14:30:00");
|
check("day0沿用14:30", "2026-11-20 14:30:00",
|
DATE_TIME.format(resolver.resolve(baseline, node(1, 0, null, "sms"))));
|
check("day3沿用14:30", "2026-11-23 14:30:00",
|
DATE_TIME.format(resolver.resolve(baseline, node(2, 3, null, "sms"))));
|
check("day7显式09:00", "2026-11-27 09:00:00",
|
DATE_TIME.format(resolver.resolve(baseline, node(3, 7, "09:00", "wechat"))));
|
Date midnight = date("2026-11-20 00:00:00");
|
check("基准日00:00默认09:00", "2026-11-20 09:00:00",
|
DATE_TIME.format(resolver.resolve(midnight, node(1, 0, null, "sms"))));
|
|
System.out.println("-- 7) 1 天补发窗口 --");
|
Date now = date("2026-11-20 12:00:00");
|
check("未来节点待发送", MchFollowupDueDecision.PENDING,
|
window.decide(date("2026-11-20 12:01:00"), now));
|
check("1小时内补发", MchFollowupDueDecision.SEND,
|
window.decide(date("2026-11-20 11:00:00"), now));
|
check("恰好24小时补发", MchFollowupDueDecision.SEND,
|
window.decide(date("2026-11-19 12:00:00"), now));
|
check("超过24小时过期", MchFollowupDueDecision.EXPIRED,
|
window.decide(date("2026-11-19 11:59:59"), now));
|
|
System.out.println("-- 8) 节点组装 --");
|
List<MchFollowupPlanItem> items = assembler().assemble(99L, baseline, itemNodes);
|
check("组装节点数", 3, items.size());
|
check("planId", 99L, items.get(0).getPlanId());
|
check("初始状态", MchFollowupConstants.ITEM_STATUS_PENDING, items.get(0).getSendStatus());
|
check("重试次数", 0, items.get(0).getRetryCount());
|
check("组装时间1", "2026-11-20 09:00:00", DATE_TIME.format(items.get(0).getSendTime()));
|
check("组装时间3沿用基准日14:30", "2026-11-27 14:30:00", DATE_TIME.format(items.get(2).getSendTime()));
|
|
System.out.println("-- 9) Mapper XML 可解析性 --");
|
String[] xmlFiles = new String[]{
|
"smartor/src/main/resources/mapper/smartor/MchFollowupPlanMapper.xml",
|
"smartor/src/main/resources/mapper/smartor/MchFollowupPlanItemMapper.xml",
|
"smartor/src/main/resources/mapper/smartor/MchFollowupSendLogMapper.xml"
|
};
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
|
for (String xmlFile : xmlFiles) {
|
try {
|
factory.newDocumentBuilder().parse(new File(xmlFile));
|
check(xmlFile, "OK", "OK");
|
} catch (Exception e) {
|
check(xmlFile, "OK", e.getClass().getSimpleName() + ":" + e.getMessage());
|
}
|
}
|
|
System.out.println("-- 10) SQL 脚本存在性 --");
|
check("DDL脚本", true, new File("sql/mch_followup_plan_20261117.sql").isFile());
|
|
System.out.println("========== 结果:PASS=" + passed + ",FAIL=" + failed + " ==========");
|
if (failed > 0) {
|
System.exit(1);
|
}
|
}
|
}
|