重构前:
public String saveMessage(SmsSendFormPo smsSendFormPo) {
List<String> userIdList = DrinStringUtils.str2List(smsSendFormPo.getUserIds(), ",");
String otherReceiverPhone = smsSendFormPo.getOtherReceiverPhone();
List<String> myContactIds = DrinStringUtils.str2List(smsSendFormPo.getMyContactIds(), ",");
StringBuilder failureMsg = new StringBuilder();
int successNum = STATIC_ZERO_INT;
int failureNum = STATIC_ZERO_INT;
if (isEmptyList(userIdList) && StringUtils.isBlank(otherReceiverPhone) && isEmptyList(myContactIds)) {
failureMsg.append("xxxxxx");
return failureMsg.toString();
}
Integer batch = null;
if(STATIC_ONE.equals(smsSendFormPo.getUpMessageFlag())){
batch = dao.getMaxMsgBatch();
if (null == batch || batch == 999) {
// xxxxx
batch = 0;
} else {
// xxxxx
batch = batch + 1;
}
}else{
batch = dao.getOtherBatch();
if (null == batch) {
batch = 1000;
} else {
batch = batch + 1;
}
}
//xxxxx
if (!isEmptyList(userIdList)) {
Map<String,User> userMap = UserUtils.getUserMap(userIdList,"id,user_name,mobile,is_receive_msg");
for (String userId : userIdList) {
User user = userMap.get(userId);
if (isNull(user.getIsReceiveMsg()) || StaticValue.STATIC_ZERO_INT==user.getIsReceiveMsg()) {
//xxxxx
continue;
}
String mobile = user.getMobile();
String userName = user.getUserName();
if (StringUtils.isBlank(mobile)) {
failureMsg.append("</br>" + userName + ":用户手机号为空,短信发送失败!");
failureNum++;
continue;
}
/*if(isContainSensitiveWords(smsSendFormPo.getContent())){
failureMsg.append("</br>" + userName + ":xxxxx");
failureNum++;
continue;
}*/
SmsSend smsSend = smsSendFormPo.toSmsSend();
smsSend.setPhone(mobile);
smsSend.setBatch(batch);
smsSend.setUserId(userId);
smsSend.setCompanyId(UserUtils.getLoginAreaId());
dao.insertSelective(smsSend);
successNum++;
}
}
//xxxxx
if (StringUtils.isNotBlank(otherReceiverPhone)) {
List<String> tempList = DrinStringUtils.str2List(otherReceiverPhone, ",");
ArrayList<String> otherReceiverPhoneList = new ArrayList<>(new HashSet<>(tempList));//xx
//String dateNumber = DateUtils.getDate("yyyyMMddHHmm");
for (String phone : otherReceiverPhoneList) {
SmsSend smsSend = smsSendFormPo.toSmsSend();
smsSend.setPhone(phone);
//xxxxx
WholeUserQuery userQuery = new WholeUserQuery();
userQuery.setMobile(phone);
List<WholeUser> users = userDao.findByQuery(userQuery);
if(CollectionUtils.isNotEmpty(users)) {
smsSend.setUserId(users.get(STATIC_ZERO_INT).getId());
}
// smsSend.setUserId(UserUtils.getLoginUserId());
smsSend.setCompanyId(UserUtils.getLoginAreaId());
smsSend.setBatch(batch);
/*if(isContainSensitiveWords(smsSendFormPo.getContent())){
failureMsg.append("</br>" xxxxx");
failureNum++;
continue;
}*/
dao.insertSelective(smsSend);
successNum++;
}
}
//xxxxx
if (!isEmptyList(myContactIds)) {
Map<String, ZyUserContact> zyUserContactMap = zyUserContactDao.getMapByIds(myContactIds,null);
for (String zyContactId : myContactIds) {
ZyUserContact zyUserContact = zyUserContactMap.get(zyContactId);
if (zyUserContact == null || isNull(zyUserContact.getContactPhone())) {
//xxxxx
continue;
}
String mobile = zyUserContact.getContactPhone();
String userName = zyUserContact.getContactName();
if (StringUtils.isBlank(mobile)) {
failureMsg.append("</br>" + userName + "xxxxx");
failureNum++;
continue;
}
/*if(isContainSensitiveWords(smsSendFormPo.getContent())){
failureMsg.append("</br>" + userName + "xxxxx");
failureNum++;
continue;
}*/
SmsSend smsSend = smsSendFormPo.toSmsSend();
smsSend.setPhone(mobile);
smsSend.setUserId(userName);
smsSend.setCompanyId(UserUtils.getLoginAreaId());
smsSend.setBatch(batch);
/*if(isContainSensitiveWords(smsSendFormPo.getContent())){
failureMsg.append("</br>" +xxxxx");
failureNum++;
continue;
}*/
dao.insertSelective(smsSend);
successNum++;
}
}
failureMsg.append("&" + successNum + "&" + failureNum);
return failureMsg.toString();
}
改造后:
@Autowired(required = false)
List<AbstractMessageCreator> creators;
public String saveMessage(SmsSendFormPo smsSendFormPo) {
if (null != creators&& creators.size() > 0) {
return "xxxxx";
}
StringBuilder saveResult = new StringBuilder();
int successNum = 0;
int failureNum = 0;
for (AbstractMessageCreator creator: creators) {
SaveMessageResult saveMessage = creator.saveMessages(smsSendFormPo);
successNum += saveMessage.getSuccessNum();
failureNum += saveMessage.getFailureNum();
saveMessage.appendMessages(saveResult);
}
boolean hasSupport= successNum+failureNum>0;
saveResult.append("&" + successNum + "&" + failureNum);
return hasSupport ? saveResult.toString() : "xxxxx";
}
public abstract class AbstractMessageCreator {
@Autowired
protected SmsSendDao dao;
public abstract boolean support(SmsSendFormPo smsSendFormPo);
public abstract void saveMessage(SmsSendFormPo smsSendFormPo,Integer batch,SaveMessageResult saveMessageResult);
public SaveMessageResult saveMessages(SmsSendFormPo smsSendFormPo) {
SaveMessageResult saveMessageResult = new SaveMessageResult();
if(support(smsSendFormPo)) {
Integer batch = matcherBatch(smsSendFormPo);
saveMessage(smsSendFormPo,batch,saveMessageResult);
}
return saveMessageResult;
}
private Integer matcherBatch(SmsSendFormPo smsSendFormPo) {
Integer batch = null;
if (STATIC_ONE.equals(smsSendFormPo.getUpMessageFlag())) {
batch = getMaxBatch();
} else {
batch = getOtherBatch();
}
return batch;
}
private Integer getMaxBatch() {
Integer batch = dao.getMaxMsgBatch();
if (null == batch || batch == 999) {
// xxxxx
batch = 0;
} else {
// xxxxx
batch = batch + 1;
}
return batch;
}
private Integer getOtherBatch() {
Integer batch = dao.getOtherBatch();
if (null == batch) {
batch = 1000;
} else {
batch = batch + 1;
}
return batch;
}
}
@Order(1)
public class UserMessageCreator extends AbstractMessageCreator {
@Override
public boolean support(SmsSendFormPo smsSendFormPo) {
List<String> receiveUserIds = getUserIds(smsSendFormPo);
return null!=receiveUserIds&&receiveUserIds.size()>0;
}
@Override
public void saveMessage(SmsSendFormPo smsSendFormPo,Integer batch,SaveMessageResult result) {
List<String> receiveUserIds = getUserIds(smsSendFormPo);
Map<String, User> userMap = UserUtils.getUserMap(receiveUserIds, "id,user_name,mobile,is_receive_msg");
for (String userId : receiveUserIds) {
User user = userMap.get(userId);
if (isNotReceiveMsg(user)) {
continue;
}
String mobile = user.getMobile();
String userName = user.getUserName();
if (StringUtils.isBlank(mobile)) {
result.failure();
result.putMessage(userName + "xxxxx");
continue;
}
SmsSend smsSend = smsSendFormPo.toSmsSend();
smsSend.setPhone(mobile);
smsSend.setBatch(batch);
smsSend.setUserId(userId);
smsSend.setCompanyId(UserUtils.getLoginAreaId());
dao.insertSelective(smsSend);
result.success();
}
}
private boolean isNotReceiveMsg(User user) {
return isNull(user.getIsReceiveMsg()) || StaticValue.STATIC_ZERO_INT == user.getIsReceiveMsg();
}
private List<String> getUserIds(SmsSendFormPo smsSendFormPo) {
return str2List(smsSendFormPo.getUserIds());
}
}
@Order(2)
public class MobileMessageCreator extends AbstractMessageCreator {
@Autowired
private WholeUserDao userDao;
@Override
public boolean support(SmsSendFormPo smsSendFormPo) {
List<String> mobiles = getMobiles(smsSendFormPo);
return null!=mobiles&&mobiles.size()>0;
}
@Override
public void saveMessage(SmsSendFormPo smsSendFormPo, Integer batch, SaveMessageResult result) {
List<String> mobiles = getMobiles(smsSendFormPo);
for (String phone : mobiles) {
SmsSend smsSend = smsSendFormPo.toSmsSend();
smsSend.setPhone(phone);
// xxxxx
smsSend.setUserId(try2findUserId(phone));
smsSend.setCompanyId(UserUtils.getLoginAreaId());
smsSend.setBatch(batch);
dao.insertSelective(smsSend);
result.success();
}
}
private String try2findUserId(String phone) {
WholeUserQuery userQuery = new WholeUserQuery();
userQuery.setMobile(phone);
userQuery.setSqlFields("id,user_name");
WholeUser user = userDao.findOne(userQuery);
return null!=user?user.getId():null;
}
private List<String> getMobiles(SmsSendFormPo smsSendFormPo) {
return DrinStringUtils.str2List(smsSendFormPo.getOtherReceiverPhone());
}
}
@Order(3)
public class ContactMessageCreator extends AbstractMessageCreator{
@Autowired
private ZyUserContactDao zyUserContactDao;
@Override
public boolean support(SmsSendFormPo smsSendFormPo) {
List<String> myContactIds = getContactIds(smsSendFormPo);
return null!=myContactIds&&myContactIds.size()>0;
}
private List<String> getContactIds(SmsSendFormPo smsSendFormPo) {
List<String> myContactIds = str2List(smsSendFormPo.getMyContactIds());
return myContactIds;
}
@Override
public void saveMessage(SmsSendFormPo smsSendFormPo, Integer batch, SaveMessageResult result) {
List<String> myContactIds = getContactIds(smsSendFormPo);
Map<String, ZyUserContact> userContactMap = zyUserContactDao.getMapByIds(myContactIds, null);
for (String zyContactId : myContactIds) {
ZyUserContact userContact = userContactMap .get(zyContactId);
if (userContact == null || isNull(zyUserContact.getContactPhone())) {
continue;
}
String mobile = userContact .getContactPhone();
String userName = userContact .getContactName();
if (StringUtils.isBlank(mobile)) {
result.putMessage(userName + "xxxxx");
result.failure();
continue;
}
SmsSend smsSend = smsSendFormPo.toSmsSend();
smsSend.setPhone(mobile);
smsSend.setUserId(userName);
smsSend.setCompanyId(UserUtils.getLoginAreaId());
smsSend.setBatch(batch);
dao.insertSelective(smsSend);
result.success();
}
}
}
@Order(4)
public class ChaterMessageCreator extends AbstractMessageCreator{
............................
}