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 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} 的内部实现)。 * 发送上下文由 service_subtask + mch_node_json 组装。
*/ @Slf4j @Component public class MchFollowupMessageSender { @Autowired private ISysConfigService configService; public MchFollowupSendOutcome sendSms(MchFollowupSendContext context) { String phone = context == null ? null : context.getPhone(); String content = context == null ? null : renderContent(context.getContent(), context); 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, context.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("【妇幼随访】短信发送异常,subtaskId={}", context == null ? null : context.getSeq(), e); return MchFollowupSendOutcome.fail("短信发送异常:" + e.getMessage(), requestSummary("sms", phone, null), null); } } public MchFollowupSendOutcome sendWechat(MchFollowupSendContext context) { String phone = context == null ? null : context.getPhone(); if (context == null || (StringUtils.isBlank(phone) && StringUtils.isBlank(context.getIdcardno()))) { return MchFollowupSendOutcome.skip("手机号和证件号都为空"); } try { String configJson = configService.selectConfigByKey( MchFollowupConstants.CONFIG_LS_WX, context.getOrgid()); SmsUtils.LsWxConfig config = SmsUtils.LsWxConfig.of(configJson); SmsUtils.LsWxMessage message = new SmsUtils.LsWxMessage(); message.setId(context.getTemplateId()); message.setPatientId(context.getPatid() == null ? null : String.valueOf(context.getPatid())); message.setPatientName(context.getPatname()); message.setIdCard(context.getIdcardno()); message.setMobile(phone); message.setMedCardNo(context.getSourceBizNo()); message.setTitleName(renderContent(context.getTitleName(), context)); message.setContent(renderContent(context.getContent(), context)); message.setUrl(context.getUrl()); String requestBody = requestSummary("wechat", phone, context.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("【妇幼随访】微信发送异常,subtaskSeq={}", context == null ? null : context.getSeq(), e); return MchFollowupSendOutcome.fail("微信发送异常:" + e.getMessage(), requestSummary("wechat", phone, context == null ? null : context.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, MchFollowupSendContext context) { if (StringUtils.isBlank(text) || context == null) { return text; } String result = text; result = result.replace("${patname}", nvl(context.getPatname())); result = result.replace("${patid}", context.getPatid() == null ? "" : String.valueOf(context.getPatid())); result = result.replace("${phone}", nvl(context.getPhone())); result = result.replace("${idcardno}", nvl(context.getIdcardno())); result = result.replace("${sourceBizNo}", nvl(context.getSourceBizNo())); result = result.replace("${archiveType}", nvl(context.getArchiveType())); result = result.replace("${orgid}", nvl(context.getOrgid())); result = result.replace("${seq}", context.getSeq() == null ? "" : String.valueOf(context.getSeq())); if (context.getPlannedFollowTime() != null) { result = result.replace("${plannedFollowTime}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(context.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); } }