陈昶聿
9 天以前 8a73ac17d717c32aa8fd97f4c682094d7b1c0654
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package com.smartor.service.mch;
 
import com.smartor.common.MchFollowupConstants;
import com.smartor.domain.MchFollowupNode;
import com.smartor.domain.MchFollowupPlan;
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("-- 8.1) 内容占位符渲染 --");
        MchFollowupPlan plan = new MchFollowupPlan();
        plan.setPatid(100L);
        plan.setPatname("张三");
        plan.setPhone("13800000000");
        plan.setOrgid("47226079133110211G1001");
        plan.setPlannedFollowTime(baseline);
        MchFollowupPlanItem seqItem = new MchFollowupPlanItem();
        seqItem.setSeq(2);
        MchFollowupMessageSender messageSender = new MchFollowupMessageSender();
        check("占位符渲染",
                "您好张三,ID=100,电话=13800000000,基准=2026-11-20 14:30:00,序号=2,机构=47226079133110211G1001",
                messageSender.renderContent(
                        "您好${patname},ID=${patid},电话=${phone},基准=${plannedFollowTime},序号=${seq},机构=${orgid}",
                        plan, seqItem));
 
        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);
        }
    }
}