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);
|
}
|
}
|
}
|