package com.smartor.service.mch;
|
|
import com.ruoyi.common.utils.StringUtils;
|
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.MchFollowupSendLog;
|
import com.smartor.mapper.MchFollowupPlanItemMapper;
|
import com.smartor.mapper.MchFollowupPlanMapper;
|
import com.smartor.mapper.MchFollowupSendLogMapper;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
|
import java.util.Date;
|
import java.util.List;
|
|
/**
|
* 妇幼随访发送服务实现。
|
*
|
* <p>只发送丽水妇幼的计划节点;不进入普通随访的补偿发送链路。</p>
|
*/
|
@Slf4j
|
@Service
|
public class MchFollowupSendServiceImpl implements IMchFollowupSendService {
|
|
@Autowired
|
private MchFollowupPlanMapper planMapper;
|
|
@Autowired
|
private MchFollowupPlanItemMapper itemMapper;
|
|
@Autowired
|
private MchFollowupSendLogMapper sendLogMapper;
|
|
@Autowired
|
private MchFollowupMessageSender messageSender;
|
|
@Autowired
|
private MchFollowupSendWindow sendWindow;
|
|
@Autowired
|
private ISysConfigService configService;
|
|
@Override
|
public int sendDue(int limit) {
|
if (!isEnabled()) {
|
return 0;
|
}
|
int batch = limit <= 0 ? MchFollowupConstants.DEFAULT_BATCH_SIZE : limit;
|
Date now = new Date();
|
List<MchFollowupPlanItem> dueItems = itemMapper.selectDueItems(
|
SpecificArchiveConstants.LISHUI_MCH_ORG_ID, now, batch);
|
if (dueItems == null || dueItems.isEmpty()) {
|
return 0;
|
}
|
|
int success = 0;
|
for (MchFollowupPlanItem item : dueItems) {
|
try {
|
MchFollowupDueDecision decision = sendWindow.decide(item.getSendTime(), now);
|
if (decision == MchFollowupDueDecision.PENDING) {
|
continue;
|
}
|
if (decision == MchFollowupDueDecision.EXPIRED) {
|
markExpired(item, now);
|
refreshPlanStatus(item.getPlanId());
|
continue;
|
}
|
if (itemMapper.claimItem(item.getId(), now) != 1) {
|
continue;
|
}
|
|
MchFollowupPlan plan = planMapper.selectMchFollowupPlanById(item.getPlanId());
|
if (plan == null || !MchFollowupConstants.PLAN_STATUS_ACTIVE.equals(plan.getPlanStatus())) {
|
markCanceled(item, now, "计划不存在或已失效");
|
refreshPlanStatus(item.getPlanId());
|
continue;
|
}
|
|
MchFollowupSendOutcome outcome = send(plan, item);
|
Date finishTime = new Date();
|
if (outcome.isSkipped()) {
|
markSkipped(item, finishTime, outcome.getMessage());
|
} else if (outcome.isOk()) {
|
markSuccess(item, finishTime, outcome);
|
success++;
|
} else {
|
markFailureOrRetry(item, finishTime, outcome);
|
}
|
writeLog(item, outcome, now);
|
refreshPlanStatus(plan.getId());
|
} catch (Exception e) {
|
log.error("【妇幼随访】发送节点异常,itemId={},planId={}",
|
item.getId(), item.getPlanId(), e);
|
try {
|
markFailureOrRetry(item, new Date(),
|
MchFollowupSendOutcome.fail("发送异常:" + e.getMessage(), null, null));
|
refreshPlanStatus(item.getPlanId());
|
} catch (Exception ignore) {
|
log.error("【妇幼随访】异常回写失败,itemId={}", item.getId(), ignore);
|
}
|
}
|
}
|
return success;
|
}
|
|
private MchFollowupSendOutcome send(MchFollowupPlan plan, MchFollowupPlanItem item) {
|
if (MchFollowupConstants.SEND_TYPE_SMS.equals(item.getSendType())) {
|
return messageSender.sendSms(plan, item);
|
}
|
if (MchFollowupConstants.SEND_TYPE_WECHAT.equals(item.getSendType())) {
|
return messageSender.sendWechat(plan, item);
|
}
|
return MchFollowupSendOutcome.fail("不支持的发送方式:" + item.getSendType(), null, null);
|
}
|
|
private void markExpired(MchFollowupPlanItem item, Date now) {
|
item.setSendStatus(MchFollowupConstants.ITEM_STATUS_EXPIRED);
|
item.setNextRetryTime(null);
|
item.setSendTimeActual(null);
|
item.setProviderMsgId(null);
|
item.setResultMsg("超过补发窗口(1天),标记过期");
|
item.setUpdateTime(now);
|
itemMapper.updateSendResult(item);
|
writeLog(item, MchFollowupSendOutcome.fail(item.getResultMsg(), null, null), now);
|
}
|
|
private void markCanceled(MchFollowupPlanItem item, Date now, String message) {
|
item.setSendStatus(MchFollowupConstants.ITEM_STATUS_CANCELED);
|
item.setNextRetryTime(null);
|
item.setResultMsg(message);
|
item.setUpdateTime(now);
|
itemMapper.updateSendResult(item);
|
}
|
|
private void markSkipped(MchFollowupPlanItem item, Date now, String message) {
|
item.setSendStatus(MchFollowupConstants.ITEM_STATUS_SKIPPED);
|
item.setNextRetryTime(null);
|
item.setResultMsg(message);
|
item.setUpdateTime(now);
|
itemMapper.updateSendResult(item);
|
}
|
|
private void markSuccess(MchFollowupPlanItem item, Date now, MchFollowupSendOutcome outcome) {
|
item.setSendStatus(MchFollowupConstants.ITEM_STATUS_SUCCESS);
|
item.setNextRetryTime(null);
|
item.setSendTimeActual(now);
|
item.setProviderMsgId(outcome.getProviderMsgId());
|
item.setResultMsg("发送成功");
|
item.setUpdateTime(now);
|
itemMapper.updateSendResult(item);
|
}
|
|
private void markFailureOrRetry(MchFollowupPlanItem item, Date now, MchFollowupSendOutcome outcome) {
|
int retryCount = item.getRetryCount() == null ? 0 : item.getRetryCount();
|
retryCount++;
|
item.setRetryCount(retryCount);
|
item.setSendTimeActual(now);
|
item.setProviderMsgId(outcome.getProviderMsgId());
|
if (retryCount < MchFollowupConstants.DEFAULT_MAX_RETRY) {
|
item.setSendStatus(MchFollowupConstants.ITEM_STATUS_PENDING);
|
item.setNextRetryTime(new Date(now.getTime()
|
+ MchFollowupConstants.DEFAULT_RETRY_DELAY_MINUTES * 60L * 1000L));
|
item.setResultMsg((outcome.getMessage() == null ? "发送失败" : outcome.getMessage())
|
+ ",第" + retryCount + "次重试后进入等待");
|
} else {
|
item.setSendStatus(MchFollowupConstants.ITEM_STATUS_FAILED);
|
item.setNextRetryTime(null);
|
item.setResultMsg((outcome.getMessage() == null ? "发送失败" : outcome.getMessage())
|
+ ",重试次数已达上限");
|
}
|
item.setUpdateTime(now);
|
itemMapper.updateSendResult(item);
|
}
|
|
private void writeLog(MchFollowupPlanItem item, MchFollowupSendOutcome outcome, Date now) {
|
MchFollowupSendLog sendLog = new MchFollowupSendLog();
|
sendLog.setPlanId(item.getPlanId());
|
sendLog.setItemId(item.getId());
|
sendLog.setSendType(item.getSendType());
|
sendLog.setRequestBody(outcome.getRequestBody());
|
sendLog.setResponseBody(outcome.getResponseBody());
|
sendLog.setSendStatus(item.getSendStatus());
|
sendLog.setResultMsg(item.getResultMsg());
|
sendLog.setCreateTime(now);
|
sendLogMapper.insertMchFollowupSendLog(sendLog);
|
}
|
|
private void refreshPlanStatus(Long planId) {
|
if (planId == null) {
|
return;
|
}
|
if (itemMapper.countPendingByPlanId(planId) == 0) {
|
planMapper.completeById(planId, new Date());
|
}
|
}
|
|
private boolean isEnabled() {
|
try {
|
String value = configService.selectConfigByKey(MchFollowupConstants.CONFIG_ENABLED);
|
return value == null || !"false".equalsIgnoreCase(value.trim());
|
} catch (Exception e) {
|
log.warn("【妇幼随访】读取启用开关失败,按启用处理:{}", e.getMessage());
|
return true;
|
}
|
}
|
}
|