SMSSendHelper.java
4.53 KB
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
/**
*
*/
package com.yoho.yhmessage.sms.helper;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.yoho.core.config.ConfigReader;
import com.yoho.message.dal.MessageFilterMapper;
import com.yoho.yhmessage.sms.common.SmsProviderCodeEnum;
import com.yoho.yhmessage.sms.filter.IFilterUserService;
import com.yoho.yhmessage.sms.service.ISendSMSService;
import com.yoho.yhmessage.sms.service.model.Response;
import com.yoho.yhmessage.sms.service.model.SendNoticeResultBean;
import com.yoho.yhmessage.sms.util.SpringContextUtil;
/**
* @author ping.huang 2016年6月28日
*/
@Component
public class SMSSendHelper {
private static Logger log = LoggerFactory.getLogger(SMSSendHelper.class);
@Autowired
private IFilterUserService filterUserService;
@Autowired
private MessageFilterMapper messageFilterMapper;
@Value("${message.sender.isDev:true}")
private boolean isDev;
@Resource(name = "core-config-reader")
private ConfigReader configReader;
/**
* 根据运营商选择发送短信
*
* @param mobileList
* @param content
* @param smsProviderCode
* @param params
* @return
* @throws Exception
*/
public Response<SendNoticeResultBean> sendSMS(List<String> mobileList, String content, int smsProviderCode, Map<String, String> params) throws Exception {
// 1、参数校验
if (CollectionUtils.isEmpty(mobileList)) {
log.warn("sendSMS error with mobileList is empty");
return null;
}
if (StringUtils.isEmpty(content)) {
log.warn("sendSMS error with content is empty");
return null;
}
// 不用过滤的场景
if (!this.getIgnoreUserCountKeyList().contains(params.get("sendScene"))) {
mobileList = commonFilter(mobileList, content);
}
if (mobileList.isEmpty()) {
log.warn("sendSMS mobileList is empty");
return null;
}
if (isDev) {
//判断每分钟发送次数是否超过50次,超过则直接返回
if(filterUserService.sendOverLimit(mobileList.size())) {
log.info("sms send times have exceeded the limit in 1 minute, direct return");
return null;
}
}
log.info("sendSMS with mobileList={}, content={}, smsProviderCode={}", Arrays.toString(mobileList.toArray()), content, smsProviderCode);
// 根据运营商id,查询运营商发短信实现类
String beanName = SmsProviderCodeEnum.getBeanNameByValue(smsProviderCode);
if (StringUtils.isEmpty(beanName)) {
// 默认玄武
beanName = "XWSendSMSServiceImpl";
}
ISendSMSService sms = SpringContextUtil.getBean(beanName, ISendSMSService.class);
return sms.sendSMS(mobileList, content, params);
}
/**
* Description: 通用的过滤条件<br>
*
* @author amos.shan<br>
* @taskId <br>
* @param mobileList
* @param content
* @return <br>
*/
private List<String> commonFilter(List<String> mobileList, String content) {
// 1、过滤相同内容
int orginMobileSize = mobileList.size();
mobileList = filterUserService.ignoreRepeatUsers(content, mobileList);
log.info("sendSMS ignoreRepeatUsers, orgin mobile size is [{}], new mobile size is [{}]", orginMobileSize, mobileList.size());
// 如果过滤重复内容后的mobileList比原来少了,则需要记录告警日志
int filterRepeatSize = mobileList.size();
if (orginMobileSize > filterRepeatSize) {
log.warn("exist repeatContentMobile, content is[{}], repeat count is[{}]", content, orginMobileSize - filterRepeatSize);
}
// 2、过滤总次数
orginMobileSize = mobileList.size();
mobileList = filterUserService.ignoreFrequentUsers(mobileList);
log.info("sendSMS ignoreFrequentUsers, orgin mobile size is [{}], new mobile size is [{}]", orginMobileSize, mobileList.size());
return mobileList;
}
/**
*
* Description: 不用过滤的场景<br>
*
* @author amos.shan<br>
* @taskId <br>
* @return <br>
*/
private List<String> getIgnoreUserCountKeyList() {
String ignoreUserCountKeyList = configReader.getString("msgcenter.sms.degrade.ignoreUserCountKeyList",
"EXCHANGE_GOODS_VERIFY_PASS,REFUND_GOODS_VERIFY_PASS,EXCHANGE_GOODS_ONLY_SMS,REFUND_GOODS_ONLY_SMS");
if (StringUtils.isBlank(ignoreUserCountKeyList)) {
return new ArrayList<String>();
}
return Arrays.asList(ignoreUserCountKeyList.split(","));
}
}