SMSSendHelper.java 4.53 KB
/**
 * 
 */
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(","));
	}

}