| | |
| | | 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; |
| | | |
| | | /** |
| | | * 妇幼随访消息发送器。 |
| | | * |
| | | * <p>短信复用 {@link SmsUtils#sendYunMasSms},微信复用 |
| | | * {@link SmsUtils#sendChatLs}(即 {@code /sms/sendChatFuyou} 的内部实现)。 |
| | | * 不经过普通随访的发送方工厂与补偿链路。</p> |
| | | * 发送上下文由 service_subtask + mch_node_json 组装。</p> |
| | | */ |
| | | @Slf4j |
| | | @Component |
| | |
| | | @Autowired |
| | | private ISysConfigService configService; |
| | | |
| | | public MchFollowupSendOutcome sendSms(MchFollowupPlan plan, MchFollowupPlanItem item) { |
| | | String phone = plan == null ? null : plan.getPhone(); |
| | | String content = item == null ? null : item.getContent(); |
| | | 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("手机号为空"); |
| | | } |
| | |
| | | } |
| | | try { |
| | | String configJson = configService.selectConfigByKey( |
| | | MchFollowupConstants.CONFIG_YUNMAS, plan.getOrgid()); |
| | | 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 message = result == null ? "云MAS返回为空" : result.getMessage(); |
| | | return MchFollowupSendOutcome.fail(message, requestBody, responseBody); |
| | | } catch (Exception e) { |
| | | log.error("【妇幼随访】短信发送异常,planId={},itemId={}", plan.getId(), item.getId(), e); |
| | | log.error("【妇幼随访】短信发送异常,subtaskId={}", context == null ? null : context.getSeq(), 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())) { |
| | | 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, plan.getOrgid()); |
| | | MchFollowupConstants.CONFIG_LS_WX, context.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.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(plan.getSourceBizNo()); |
| | | message.setTitleName(item.getTitleName()); |
| | | message.setContent(item.getContent()); |
| | | message.setUrl(item.getUrl()); |
| | | 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, item.getTemplateId()); |
| | | String requestBody = requestSummary("wechat", phone, context.getTemplateId()); |
| | | String resp = SmsUtils.sendChatLs(config, message); |
| | | String messageText = parseWechatMessage(resp); |
| | | boolean ok = parseWechatOk(resp); |
| | |
| | | } |
| | | return MchFollowupSendOutcome.fail(messageText, requestBody, resp); |
| | | } catch (Exception e) { |
| | | log.error("【妇幼随访】微信发送异常,planId={},itemId={}", plan.getId(), item.getId(), e); |
| | | log.error("【妇幼随访】微信发送异常,subtaskSeq={}", context == null ? null : context.getSeq(), e); |
| | | return MchFollowupSendOutcome.fail("微信发送异常:" + e.getMessage(), |
| | | requestSummary("wechat", phone, item == null ? null : item.getTemplateId()), null); |
| | | requestSummary("wechat", phone, context == null ? null : context.getTemplateId()), null); |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 渲染妇幼随访内容占位符。当前支持: |
| | | * ${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) |