陈昶聿
9 天以前 e2b0e66b92759eb6a24b0a4e24aa60e76bbdc1a7
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
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;
 
/**
 * 妇幼随访消息发送器。
 *
 * <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 : item.getContent();
        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(item.getTitleName());
            message.setContent(item.getContent());
            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;
        }
    }
 
    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);
    }
}