陈昶聿
9 天以前 e2b0e66b92759eb6a24b0a4e24aa60e76bbdc1a7
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package com.smartor.service.mch;
 
import com.ruoyi.system.service.ISysConfigService;
import com.smartor.common.MchFollowupConstants;
import com.smartor.common.SpecificArchiveConstants;
import com.smartor.domain.MchFollowupPlan;
import com.smartor.domain.MchFollowupPlanItem;
import com.smartor.domain.PatChildArchiveDetail;
import com.smartor.domain.PatMaternityArchiveDetail;
import com.smartor.mapper.MchFollowupPlanItemMapper;
import com.smartor.mapper.MchFollowupPlanMapper;
import com.smartor.mapper.PatChildArchiveDetailMapper;
import com.smartor.mapper.PatMaternityArchiveDetailMapper;
 
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 妇幼随访计划服务自检(纯 main,使用动态代理模拟 Mapper,不依赖数据库)。
 *
 * 覆盖:计划生成、节点时间、幂等重建、拟随访时间为空、儿保家长电话兜底。
 */
public final class MchFollowupPlanServiceCheck {
 
    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 Date date(String text) throws Exception {
        return DATE_TIME.parse(text);
    }
 
    private static void setField(Object target, String name, Object value) throws Exception {
        Field field = target.getClass().getDeclaredField(name);
        field.setAccessible(true);
        field.set(target, value);
    }
 
    private static Object defaultValue(Class<?> type) {
        if (!type.isPrimitive()) {
            return null;
        }
        if (type == boolean.class) {
            return false;
        }
        if (type == byte.class) {
            return (byte) 0;
        }
        if (type == short.class) {
            return (short) 0;
        }
        if (type == int.class) {
            return 0;
        }
        if (type == long.class) {
            return 0L;
        }
        if (type == float.class) {
            return 0F;
        }
        if (type == double.class) {
            return 0D;
        }
        if (type == char.class) {
            return '\0';
        }
        return null;
    }
 
    @SuppressWarnings("unchecked")
    private static <T> T proxy(Class<T> type, InvocationHandler handler) {
        return (T) Proxy.newProxyInstance(type.getClassLoader(), new Class[]{type}, handler);
    }
 
    private static MchFollowupPlanAssembler assembler() throws Exception {
        MchFollowupPlanAssembler assembler = new MchFollowupPlanAssembler();
        setField(assembler, "timeResolver", new MchFollowupTimeResolver());
        return assembler;
    }
 
    private static PatMaternityArchiveDetail maternityDetail(Long detailId, Long archiveId, Date plannedTime) {
        PatMaternityArchiveDetail detail = new PatMaternityArchiveDetail();
        detail.setId(detailId);
        detail.setArchiveid(archiveId);
        detail.setOrgid(SpecificArchiveConstants.LISHUI_MCH_ORG_ID);
        detail.setCampusid("1");
        detail.setSourceType("1");
        detail.setSourceBizNo("MZ001");
        detail.setPatid(100L);
        detail.setPatname("张三");
        detail.setPhone("13800000000");
        detail.setIdcardno("330102199001010000");
        detail.setPlannedFollowTime(plannedTime);
        detail.setFollowupPeriod("{\"items\":["
                + "{\"dayOffset\":0,\"sendTime\":\"09:00\",\"sendType\":\"sms\",\"content\":\"短信1\"},"
                + "{\"dayOffset\":3,\"sendType\":\"wechat\",\"templateId\":16,\"content\":\"微信2\"},"
                + "{\"dayOffset\":7,\"sendType\":\"sms\",\"content\":\"短信3\"}]}");
        return detail;
    }
 
    private static PatChildArchiveDetail childDetail(Long detailId, Long archiveId, Date plannedTime) {
        PatChildArchiveDetail detail = new PatChildArchiveDetail();
        detail.setId(detailId);
        detail.setArchiveid(archiveId);
        detail.setOrgid(SpecificArchiveConstants.LISHUI_MCH_ORG_ID);
        detail.setCampusid("1");
        detail.setSourceType("1");
        detail.setSourceBizNo("MZ002");
        detail.setPatid(200L);
        detail.setPatname("李四");
        detail.setPhone("13900000000");
        detail.setParentPhone("13700000000");
        detail.setIdcardno("330102199202020000");
        detail.setPlannedFollowTime(plannedTime);
        detail.setFollowupPeriod("{\"items\":[{\"dayOffset\":1,\"sendType\":\"sms\",\"content\":\"儿保短信\"}]}");
        return detail;
    }
 
    public static void main(String[] args) throws Exception {
        System.out.println("========== 妇幼随访计划服务自检 ==========");
 
        List<MchFollowupPlan> activePlans = new ArrayList<>();
        List<MchFollowupPlanItem> insertedItems = new ArrayList<>();
        long[] planId = {1L};
        long[] itemId = {1L};
 
        MchFollowupPlanMapper planMapper = proxy(MchFollowupPlanMapper.class, (p, method, a) -> {
            String name = method.getName();
            if ("selectActiveByArchive".equals(name)) {
                return new ArrayList<>(activePlans);
            }
            if ("insertMchFollowupPlan".equals(name)) {
                MchFollowupPlan plan = (MchFollowupPlan) a[0];
                plan.setId(planId[0]++);
                activePlans.add(plan);
                return 1;
            }
            if ("cancelById".equals(name)) {
                Long id = (Long) a[0];
                for (MchFollowupPlan plan : activePlans) {
                    if (id.equals(plan.getId())) {
                        plan.setPlanStatus(MchFollowupConstants.PLAN_STATUS_CANCELED);
                    }
                }
                return 1;
            }
            if ("completeById".equals(name)) {
                return 1;
            }
            return defaultValue(method.getReturnType());
        });
 
        MchFollowupPlanItemMapper itemMapper = proxy(MchFollowupPlanItemMapper.class, (p, method, a) -> {
            String name = method.getName();
            if ("insertMchFollowupPlanItem".equals(name)) {
                MchFollowupPlanItem item = (MchFollowupPlanItem) a[0];
                item.setId(itemId[0]++);
                insertedItems.add(item);
                return 1;
            }
            if ("cancelPendingByPlanId".equals(name)) {
                return 1;
            }
            if ("countPendingByPlanId".equals(name)) {
                return 1;
            }
            return defaultValue(method.getReturnType());
        });
 
        Map<Long, PatMaternityArchiveDetail> maternityMap = new HashMap<>();
        maternityMap.put(20L, maternityDetail(20L, 10L, date("2026-11-20 14:30:00")));
        maternityMap.put(21L, maternityDetail(21L, 11L, null));
        PatMaternityArchiveDetailMapper maternityMapper = proxy(PatMaternityArchiveDetailMapper.class, (p, method, a) -> {
            String name = method.getName();
            if ("selectPatMaternityArchiveDetailById".equals(name)) {
                return maternityMap.get((Long) a[0]);
            }
            if ("selectPatMaternityArchiveDetailList".equals(name)) {
                PatMaternityArchiveDetail query = (PatMaternityArchiveDetail) a[0];
                List<PatMaternityArchiveDetail> list = new ArrayList<>();
                for (PatMaternityArchiveDetail detail : maternityMap.values()) {
                    if (query.getArchiveid() == null || query.getArchiveid().equals(detail.getArchiveid())) {
                        list.add(detail);
                    }
                }
                return list;
            }
            return defaultValue(method.getReturnType());
        });
 
        Map<Long, PatChildArchiveDetail> childMap = new HashMap<>();
        childMap.put(30L, childDetail(30L, 12L, date("2026-12-01 08:00:00")));
        PatChildArchiveDetailMapper childMapper = proxy(PatChildArchiveDetailMapper.class, (p, method, a) -> {
            String name = method.getName();
            if ("selectPatChildArchiveDetailById".equals(name)) {
                return childMap.get((Long) a[0]);
            }
            if ("selectPatChildArchiveDetailList".equals(name)) {
                return new ArrayList<>(childMap.values());
            }
            return defaultValue(method.getReturnType());
        });
 
        ISysConfigService configService = proxy(ISysConfigService.class, (p, method, a) -> {
            if ("selectConfigByKey".equals(method.getName())) {
                return null;
            }
            return defaultValue(method.getReturnType());
        });
 
        MchFollowupPlanServiceImpl service = new MchFollowupPlanServiceImpl();
        setField(service, "planMapper", planMapper);
        setField(service, "itemMapper", itemMapper);
        setField(service, "maternityDetailMapper", maternityMapper);
        setField(service, "childDetailMapper", childMapper);
        setField(service, "periodParser", new MchFollowupPeriodParser());
        setField(service, "planAssembler", assembler());
        setField(service, "configService", configService);
 
        System.out.println("-- 1) 妇保计划生成 --");
        int generated = service.rebuildForArchive(MchFollowupConstants.ARCHIVE_TYPE_MATERNITY, 10L, 20L);
        check("生成节点数", 3, generated);
        check("计划数", 1, activePlans.size());
        check("计划状态", MchFollowupConstants.PLAN_STATUS_ACTIVE, activePlans.get(0).getPlanStatus());
        check("计划机构", SpecificArchiveConstants.LISHUI_MCH_ORG_ID, activePlans.get(0).getOrgid());
        check("节点数", 3, insertedItems.size());
        check("节点1时间", "2026-11-20 09:00:00", DATE_TIME.format(insertedItems.get(0).getSendTime()));
        check("节点2时间", "2026-11-23 14:30:00", DATE_TIME.format(insertedItems.get(1).getSendTime()));
        check("节点3时间", "2026-11-27 14:30:00", DATE_TIME.format(insertedItems.get(2).getSendTime()));
 
        System.out.println("-- 2) 相同基准日/周期幂等 --");
        int again = service.rebuildForArchive(MchFollowupConstants.ARCHIVE_TYPE_MATERNITY, 10L, 20L);
        check("幂等返回", 0, again);
        check("计划数不变", 1, activePlans.size());
        check("节点数不变", 3, insertedItems.size());
 
        System.out.println("-- 3) 拟随访时间为空 --");
        int waiting = service.rebuildForArchive(MchFollowupConstants.ARCHIVE_TYPE_MATERNITY, 11L, 21L);
        check("等待计划不生成节点", 0, waiting);
        check("等待计划数", 2, activePlans.size());
        check("等待计划状态", MchFollowupConstants.PLAN_STATUS_WAITING, activePlans.get(1).getPlanStatus());
 
        System.out.println("-- 4) 儿保家长电话兜底 --");
        int childGenerated = service.rebuildForArchive(MchFollowupConstants.ARCHIVE_TYPE_CHILD, 12L, 30L);
        check("儿保节点数", 1, childGenerated);
        check("儿保计划数", 3, activePlans.size());
        check("儿保电话", "13700000000", activePlans.get(2).getPhone());
        check("儿保节点时间", "2026-12-02 08:00:00",
                DATE_TIME.format(insertedItems.get(insertedItems.size() - 1).getSendTime()));
 
        System.out.println("-- 5) 非丽水妇幼不生成 --");
        PatMaternityArchiveDetail other = maternityDetail(40L, 20L, date("2026-11-20 14:30:00"));
        other.setOrgid("OTHER_ORG");
        maternityMap.put(40L, other);
        int otherGenerated = service.rebuildForArchive(MchFollowupConstants.ARCHIVE_TYPE_MATERNITY, 20L, 40L);
        check("其他机构不生成", 0, otherGenerated);
 
        System.out.println("========== 结果:PASS=" + passed + ",FAIL=" + failed + " ==========");
        if (failed > 0) {
            System.exit(1);
        }
    }
}