package com.smartor.service.mch; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.sms.SmsUtils; import com.ruoyi.system.service.ISysConfigService; import com.smartor.common.MchFollowupConstants; import com.smartor.domain.MchFollowupPlan; import com.smartor.domain.MchFollowupPlanItem; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.text.SimpleDateFormat; /** * 妇幼随访消息发送器。 * *

短信复用 {@link SmsUtils#sendYunMasSms},微信复用 * {@link SmsUtils#sendChatLs}(即 {@code /sms/sendChatFuyou} 的内部实现)。 * 不经过普通随访的发送方工厂与补偿链路。

*/ @Slf4j @Component public class MchFollowupMessageSender { @Autowired private ISysConfigService configService; public MchFollowupSendOutcome sendSms(MchFollowupPlan plan, MchFollowupPlanItem item) { String phone = plan == null ? null : plan.getPhone(); String content = item == null ? null : renderContent(item.getContent(), plan, item); if (StringUtils.isBlank(phone)) { return MchFollowupSendOutcome.skip("手机号为空"); } if (StringUtils.isBlank(content)) { return MchFollowupSendOutcome.fail("短信内容为空", requestSummary("sms", phone, null), null); } try { String configJson = configService.selectConfigByKey( MchFollowupConstants.CONFIG_YUNMAS, plan.getOrgid()); SmsUtils.YunMasConfig config = SmsUtils.YunMasConfig.of(configJson); SmsUtils.SmsResult result = SmsUtils.sendYunMasSms(config, phone, content); String requestBody = requestSummary("sms", phone, null); String responseBody = JSON.toJSONString(result); if (result != null && result.isOk()) { return MchFollowupSendOutcome.ok(result.getMsgGroup(), requestBody, responseBody); } String message = result == null ? "云MAS返回为空" : result.getMessage(); return MchFollowupSendOutcome.fail(message, requestBody, responseBody); } catch (Exception e) { log.error("【妇幼随访】短信发送异常,planId={},itemId={}", plan.getId(), item.getId(), e); return MchFollowupSendOutcome.fail("短信发送异常:" + e.getMessage(), requestSummary("sms", phone, null), null); } } public MchFollowupSendOutcome sendWechat(MchFollowupPlan plan, MchFollowupPlanItem item) { String phone = plan == null ? null : plan.getPhone(); if (StringUtils.isBlank(phone) && StringUtils.isBlank(plan == null ? null : plan.getIdcardno())) { return MchFollowupSendOutcome.skip("手机号和证件号都为空"); } try { String configJson = configService.selectConfigByKey( MchFollowupConstants.CONFIG_LS_WX, plan.getOrgid()); SmsUtils.LsWxConfig config = SmsUtils.LsWxConfig.of(configJson); SmsUtils.LsWxMessage message = new SmsUtils.LsWxMessage(); message.setId(item.getTemplateId()); message.setPatientId(plan.getPatid() == null ? null : String.valueOf(plan.getPatid())); message.setPatientName(plan.getPatname()); message.setIdCard(plan.getIdcardno()); message.setMobile(phone); message.setMedCardNo(plan.getSourceBizNo()); message.setTitleName(renderContent(item.getTitleName(), plan, item)); message.setContent(renderContent(item.getContent(), plan, item)); message.setUrl(item.getUrl()); String requestBody = requestSummary("wechat", phone, item.getTemplateId()); String resp = SmsUtils.sendChatLs(config, message); String messageText = parseWechatMessage(resp); boolean ok = parseWechatOk(resp); if (ok) { return MchFollowupSendOutcome.ok(null, requestBody, resp); } return MchFollowupSendOutcome.fail(messageText, requestBody, resp); } catch (Exception e) { log.error("【妇幼随访】微信发送异常,planId={},itemId={}", plan.getId(), item.getId(), e); return MchFollowupSendOutcome.fail("微信发送异常:" + e.getMessage(), requestSummary("wechat", phone, item == null ? null : item.getTemplateId()), null); } } private boolean parseWechatOk(String resp) { if (StringUtils.isBlank(resp)) { return false; } try { JSONObject object = JSON.parseObject(resp); return object != null && object.getIntValue("code") == 0; } catch (Exception e) { return false; } } private String parseWechatMessage(String resp) { if (StringUtils.isBlank(resp)) { return "微信返回为空"; } try { JSONObject object = JSON.parseObject(resp); if (object == null) { return "微信返回为空"; } String msg = object.getString("msg"); return StringUtils.isBlank(msg) ? resp : msg; } catch (Exception e) { return resp; } } /** * 渲染妇幼随访内容占位符。当前支持: * ${patname}、${patid}、${phone}、${idcardno}、${plannedFollowTime}、 * ${sourceBizNo}、${archiveType}、${orgid}、${seq}。 */ String renderContent(String text, MchFollowupPlan plan, MchFollowupPlanItem item) { if (StringUtils.isBlank(text) || plan == null) { return text; } String result = text; result = result.replace("${patname}", nvl(plan.getPatname())); result = result.replace("${patid}", plan.getPatid() == null ? "" : String.valueOf(plan.getPatid())); result = result.replace("${phone}", nvl(plan.getPhone())); result = result.replace("${idcardno}", nvl(plan.getIdcardno())); result = result.replace("${sourceBizNo}", nvl(plan.getSourceBizNo())); result = result.replace("${archiveType}", nvl(plan.getArchiveType())); result = result.replace("${orgid}", nvl(plan.getOrgid())); result = result.replace("${seq}", item == null || item.getSeq() == null ? "" : String.valueOf(item.getSeq())); if (plan.getPlannedFollowTime() != null) { result = result.replace("${plannedFollowTime}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(plan.getPlannedFollowTime())); } else { result = result.replace("${plannedFollowTime}", ""); } return result; } private String nvl(String value) { return value == null ? "" : value; } private String requestSummary(String sendType, String phone, Long templateId) { return "sendType=" + sendType + ";phone=" + maskPhone(phone) + (templateId == null ? "" : ";templateId=" + templateId); } private String maskPhone(String phone) { if (StringUtils.isBlank(phone) || phone.length() < 7) { return phone; } return phone.substring(0, 3) + "****" + phone.substring(phone.length() - 4); } }