陈昶聿
9 天以前 8a73ac17d717c32aa8fd97f4c682094d7b1c0654
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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;
 
/**
 * 妇幼随访消息发送器。
 *
 * <p>短信复用 {@link SmsUtils#sendYunMasSms},微信复用
 * {@link SmsUtils#sendChatLs}(即 {@code /sms/sendChatFuyou} 的内部实现)。
 * 不经过普通随访的发送方工厂与补偿链路。</p>
 */
@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);
    }
}