陈昶聿
3 天以前 3acf9415fce2dd21c0fd5d67805b72a8bde24a81
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
package com.smartor.service.mch;
 
import com.ruoyi.system.service.ISysConfigService;
import com.smartor.common.MchFollowupConstants;
import com.smartor.domain.MchFollowupPlan;
import com.smartor.domain.MchFollowupPlanItem;
import com.smartor.domain.MchFollowupSendLog;
import com.smartor.mapper.MchFollowupPlanItemMapper;
import com.smartor.mapper.MchFollowupPlanMapper;
import com.smartor.mapper.MchFollowupSendLogMapper;
 
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * 妇幼随访发送服务自检(纯 main,动态代理模拟 Mapper,不依赖数据库/第三方接口)。
 */
public final class MchFollowupSendServiceCheck {
 
    private static int passed = 0;
    private static int failed = 0;
 
    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 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 == int.class) {
            return 0;
        }
        if (type == long.class) {
            return 0L;
        }
        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 MchFollowupPlanItem item(String sendType, long sendTimeMillis) {
        MchFollowupPlanItem item = new MchFollowupPlanItem();
        item.setId(1L);
        item.setPlanId(10L);
        item.setSeq(1);
        item.setSendType(sendType);
        item.setSendTime(new Date(sendTimeMillis));
        item.setContent("内容");
        item.setSendStatus(MchFollowupConstants.ITEM_STATUS_PENDING);
        item.setRetryCount(0);
        return item;
    }
 
    private static MchFollowupPlan plan() {
        MchFollowupPlan plan = new MchFollowupPlan();
        plan.setId(10L);
        plan.setOrgid("47226079133110211G1001");
        plan.setPlanStatus(MchFollowupConstants.PLAN_STATUS_ACTIVE);
        plan.setPhone("13800000000");
        return plan;
    }
 
    private static int run(MchFollowupPlanItem targetItem, MchFollowupMessageSender sender,
                           boolean expectClaim) throws Exception {
        List<MchFollowupPlanItem> dueItems = new ArrayList<>();
        dueItems.add(targetItem);
        List<MchFollowupSendLog> logs = new ArrayList<>();
        boolean[] claimed = {false};
        boolean[] planCompleted = {false};
 
        MchFollowupPlanItemMapper itemMapper = proxy(MchFollowupPlanItemMapper.class, (p, method, a) -> {
            String name = method.getName();
            if ("selectDueItems".equals(name)) {
                return dueItems;
            }
            if ("claimItem".equals(name)) {
                claimed[0] = true;
                return 1;
            }
            if ("updateSendResult".equals(name)) {
                return 1;
            }
            if ("countPendingByPlanId".equals(name)) {
                return 0;
            }
            return defaultValue(method.getReturnType());
        });
 
        MchFollowupPlanMapper planMapper = proxy(MchFollowupPlanMapper.class, (p, method, a) -> {
            String name = method.getName();
            if ("selectMchFollowupPlanById".equals(name)) {
                return plan();
            }
            if ("completeById".equals(name)) {
                planCompleted[0] = true;
                return 1;
            }
            return defaultValue(method.getReturnType());
        });
 
        MchFollowupSendLogMapper logMapper = proxy(MchFollowupSendLogMapper.class, (p, method, a) -> {
            if ("insertMchFollowupSendLog".equals(method.getName())) {
                logs.add((MchFollowupSendLog) a[0]);
                return 1;
            }
            return defaultValue(method.getReturnType());
        });
 
        ISysConfigService configService = proxy(ISysConfigService.class, (p, method, a) -> {
            if ("selectConfigByKey".equals(method.getName())) {
                return null;
            }
            return defaultValue(method.getReturnType());
        });
 
        MchFollowupSendServiceImpl service = new MchFollowupSendServiceImpl();
        setField(service, "planMapper", planMapper);
        setField(service, "itemMapper", itemMapper);
        setField(service, "sendLogMapper", logMapper);
        setField(service, "messageSender", sender);
        setField(service, "sendWindow", new MchFollowupSendWindow());
        setField(service, "configService", configService);
 
        int sent = service.sendDue(10);
        check("claimItem调用", expectClaim, claimed[0]);
        check("计划完成更新", true, planCompleted[0]);
        check("发送日志数", 1, logs.size());
        return sent;
    }
 
    public static void main(String[] args) throws Exception {
        System.out.println("========== 妇幼随访发送服务自检 ==========");
        long now = System.currentTimeMillis();
 
        System.out.println("-- 1) 短信发送成功 --");
        MchFollowupPlanItem smsItem = item(MchFollowupConstants.SEND_TYPE_SMS, now - 60L * 60L * 1000L);
        MchFollowupMessageSender okSender = new MchFollowupMessageSender() {
            @Override
            public MchFollowupSendOutcome sendSms(MchFollowupPlan plan, MchFollowupPlanItem item) {
                return MchFollowupSendOutcome.ok("MSG-1", "req", "resp");
            }
 
            @Override
            public MchFollowupSendOutcome sendWechat(MchFollowupPlan plan, MchFollowupPlanItem item) {
                return MchFollowupSendOutcome.fail("微信失败", "req", "resp");
            }
        };
        int success = run(smsItem, okSender, true);
        check("成功数量", 1, success);
        check("节点状态", MchFollowupConstants.ITEM_STATUS_SUCCESS, smsItem.getSendStatus());
        check("第三方ID", "MSG-1", smsItem.getProviderMsgId());
 
        System.out.println("-- 2) 超过 1 天标记过期 --");
        MchFollowupPlanItem expiredItem = item(
                MchFollowupConstants.SEND_TYPE_SMS, now - 25L * 60L * 60L * 1000L);
        int expiredSent = run(expiredItem, okSender, false);
        check("过期不发送", 0, expiredSent);
        check("过期状态", MchFollowupConstants.ITEM_STATUS_EXPIRED, expiredItem.getSendStatus());
 
        System.out.println("-- 3) 微信失败进入重试 --");
        MchFollowupPlanItem retryItem = item(
                MchFollowupConstants.SEND_TYPE_WECHAT, now - 60L * 60L * 1000L);
        int retrySent = run(retryItem, okSender, true);
        check("失败不计数", 0, retrySent);
        check("重试状态", MchFollowupConstants.ITEM_STATUS_PENDING, retryItem.getSendStatus());
        check("重试次数", 1, retryItem.getRetryCount());
        check("下次重试时间非空", true, retryItem.getNextRetryTime() != null);
 
        System.out.println("========== 结果:PASS=" + passed + ",FAIL=" + failed + " ==========");
        if (failed > 0) {
            System.exit(1);
        }
    }
}