|
|
|
@@ -7,11 +7,13 @@ import com.iformall.common.ErrorCode; |
|
|
|
import com.iformall.common.IdWorker; |
|
|
|
import com.iformall.common.Result; |
|
|
|
import com.iformall.common.ResultData; |
|
|
|
import com.iformall.domain.po.WxCUserBasicInfo; |
|
|
|
import com.iformall.domain.po.WxMsg; |
|
|
|
import com.iformall.domain.po.WxMsgCallback; |
|
|
|
import com.iformall.domain.po.WxMsgConfig; |
|
|
|
import com.iformall.enums.*; |
|
|
|
import com.iformall.enums.EnumMsgSend; |
|
|
|
import com.iformall.enums.EnumMsgSendStatus; |
|
|
|
import com.iformall.enums.EnumMsgStatus; |
|
|
|
import com.iformall.enums.EnumVerifyCode; |
|
|
|
import com.iformall.exception.MallinkException; |
|
|
|
import com.iformall.mapper.WxCUserBasicInfoMapper; |
|
|
|
import com.iformall.mapper.WxMsgCallbackMapper; |
|
|
|
@@ -20,14 +22,18 @@ import com.iformall.mapper.WxMsgMapper; |
|
|
|
import com.iformall.service.PushLimitService; |
|
|
|
import com.iformall.service.WxCUserTagsService; |
|
|
|
import com.iformall.service.WxMsgService; |
|
|
|
import com.iformall.utils.*; |
|
|
|
import com.iformall.utils.DateUtils; |
|
|
|
import com.iformall.utils.WiwideUtil; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.scheduling.annotation.Async; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
import java.util.*; |
|
|
|
|
|
|
|
|
|
|
|
@Service |
|
|
|
public class WxMsgServiceImpl implements WxMsgService { |
|
|
|
private final Logger logger = LoggerFactory.getLogger(this.getClass()); |
|
|
|
@@ -86,9 +92,9 @@ public class WxMsgServiceImpl implements WxMsgService { |
|
|
|
return wxMsg; |
|
|
|
} |
|
|
|
|
|
|
|
@Async |
|
|
|
@Override |
|
|
|
public ResultData saveOrUpdate(WxMsg wxMsg) { |
|
|
|
|
|
|
|
public ResultData saveOrUpdate(WxMsg wxMsg, WxMsgConfig wxMsgConfig) { |
|
|
|
|
|
|
|
//新增记录 |
|
|
|
if (wxMsg.getId() == null) { |
|
|
|
@@ -102,34 +108,6 @@ public class WxMsgServiceImpl implements WxMsgService { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
//1、手机多条以逗号分隔,直接通过实体得到wxmsg.getphones |
|
|
|
//2、通过标签 |
|
|
|
String phones=wxMsg.getPhones(); |
|
|
|
String label = wxMsg.getLabel(); |
|
|
|
if(!phones.equals("") && !label.equals("")){//两种方式只能选其一 |
|
|
|
return new ResultData(ErrorCode.MSG_SEND_WAY_CHOOSE_ERROR); |
|
|
|
} |
|
|
|
|
|
|
|
if(phones.equals("")){//没有手工输入手机号时解析标签,有,继续流转 |
|
|
|
phones=parselabel(wxMsg.getTenantId(), wxMsg); |
|
|
|
if(phones.equals("")){//解析之后手机号依然不存在时返回 |
|
|
|
return new ResultData(ErrorCode.MSG_PHONE_NOT_FOUND); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//保证手机号惟一 |
|
|
|
String[] phoneSplit = phones.split(","); |
|
|
|
Set<String> phoneSet=new HashSet<>(); |
|
|
|
for(String phone:phoneSplit){ |
|
|
|
phoneSet.add(phone); |
|
|
|
} |
|
|
|
wxMsg.setExpectSendNumber(phoneSet.size());//预计发送数量 |
|
|
|
StringBuffer sb=new StringBuffer(); |
|
|
|
for(String phone:phoneSet){ |
|
|
|
sb.append(phone).append(","); |
|
|
|
} |
|
|
|
wxMsg.setPhones(sb.deleteCharAt(sb.length()-1).toString()); |
|
|
|
|
|
|
|
//是否立即发送 |
|
|
|
if (wxMsg.getIsright().equals(EnumMsgSend.MSG_SEND_IMMEDIATELY.getCode())) { |
|
|
|
wxMsg.setStatus(EnumMsgStatus.MSG_STATUS_SENDED.getCode()); |
|
|
|
@@ -139,7 +117,12 @@ public class WxMsgServiceImpl implements WxMsgService { |
|
|
|
logger.error(e.getMessage()); |
|
|
|
return new ResultData(e.getErrorCode(),e.getMessage()); |
|
|
|
} |
|
|
|
return sendmsg(wxMsg); |
|
|
|
if(wxMsg.getExpectSendNumber().intValue()>1000){ |
|
|
|
batchSendMsg(wxMsg,wxMsgConfig); |
|
|
|
}else{ |
|
|
|
sendmsg(wxMsg,wxMsgConfig); |
|
|
|
} |
|
|
|
return new ResultData(Result.SUCCESS, "短信已发送"); |
|
|
|
} else { |
|
|
|
wxMsg.setStatus(EnumMsgStatus.MSG_STATUS_NOT_SEND.getCode()); |
|
|
|
final IdWorker idWorker = IdWorker.get(); |
|
|
|
@@ -157,34 +140,6 @@ public class WxMsgServiceImpl implements WxMsgService { |
|
|
|
return new ResultData(Result.SUCCESS, "已保存到草稿箱"); |
|
|
|
} |
|
|
|
|
|
|
|
//1、手机多条以逗号分隔,直接通过实体得到wxmsg.getphones |
|
|
|
//2、通过标签 |
|
|
|
String phones=wxMsg.getPhones(); |
|
|
|
String label = wxMsg.getLabel(); |
|
|
|
if(!phones.equals("") && !label.equals("")){//两种方式只能选其一 |
|
|
|
return new ResultData(ErrorCode.MSG_SEND_WAY_CHOOSE_ERROR); |
|
|
|
} |
|
|
|
|
|
|
|
if(phones.equals("")){//没有手工输入手机号时解析标签,有,继续流转 |
|
|
|
phones=parselabel(wxMsg.getTenantId(), wxMsg); |
|
|
|
if(phones.equals("")){//解析之后手机号依然不存在时返回 |
|
|
|
return new ResultData(ErrorCode.MSG_PHONE_NOT_FOUND); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//保证手机号惟一 |
|
|
|
String[] phoneSplit = phones.split(","); |
|
|
|
Set<String> phoneSet=new HashSet<>(); |
|
|
|
for(String phone:phoneSplit){ |
|
|
|
phoneSet.add(phone); |
|
|
|
} |
|
|
|
wxMsg.setExpectSendNumber(phoneSet.size());//预计发送数量 |
|
|
|
StringBuffer sb=new StringBuffer(); |
|
|
|
for(String phone:phoneSet){ |
|
|
|
sb.append(phone).append(","); |
|
|
|
} |
|
|
|
wxMsg.setPhones(sb.deleteCharAt(sb.length()-1).toString()); |
|
|
|
|
|
|
|
//是否立即发送 |
|
|
|
if (wxMsg.getIsright().equals(EnumMsgSend.MSG_SEND_IMMEDIATELY.getCode())) { |
|
|
|
wxMsg.setStatus(EnumMsgStatus.MSG_STATUS_SENDED.getCode()); |
|
|
|
@@ -194,7 +149,12 @@ public class WxMsgServiceImpl implements WxMsgService { |
|
|
|
logger.error(e.getMessage()); |
|
|
|
return new ResultData(e.getErrorCode(),e.getMessage()); |
|
|
|
} |
|
|
|
return sendmsg(wxMsg); |
|
|
|
if(wxMsg.getExpectSendNumber().intValue()>1000){ |
|
|
|
batchSendMsg(wxMsg,wxMsgConfig); |
|
|
|
}else{ |
|
|
|
sendmsg(wxMsg,wxMsgConfig); |
|
|
|
} |
|
|
|
return new ResultData(Result.SUCCESS, "短信已发送"); |
|
|
|
} else { |
|
|
|
//定时 |
|
|
|
wxMsg.setStatus(EnumMsgStatus.MSG_STATUS_NOT_SEND.getCode()); |
|
|
|
@@ -205,24 +165,7 @@ public class WxMsgServiceImpl implements WxMsgService { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private String parselabel(String tenantId, WxMsg wxmsg) { |
|
|
|
String label = wxmsg.getLabel(); |
|
|
|
String[] arys = label.split(","); |
|
|
|
List<Long> tagids = new ArrayList<>(); |
|
|
|
for (int i = 0; i < arys.length; i++) { |
|
|
|
tagids.add(Long.parseLong(arys[i])); |
|
|
|
} |
|
|
|
List<WxCUserBasicInfo> list = wxCUserTagsService.findByTag(tenantId,tagids); |
|
|
|
|
|
|
|
StringBuilder sb=new StringBuilder(); |
|
|
|
if(list.size()>0){ |
|
|
|
for(WxCUserBasicInfo cUserInfo:list){ |
|
|
|
sb.append(cUserInfo.getPhone()).append(","); |
|
|
|
} |
|
|
|
return sb.toString(); |
|
|
|
} |
|
|
|
return ""; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void deleteById(Long id) { |
|
|
|
@@ -230,57 +173,76 @@ public class WxMsgServiceImpl implements WxMsgService { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public ResultData sendmsg(WxMsg wxMsg) { |
|
|
|
logger.info("发送短信开始..."); |
|
|
|
//从短信配置中查询密钥 bid 等信息 |
|
|
|
WxMsgConfig wxMsgConfig = new WxMsgConfig(); |
|
|
|
wxMsgConfig.setTenantId(wxMsg.getTenantId()); |
|
|
|
List<WxMsgConfig> wxMsgConfigs = wxMsgConfigMapper.findList(wxMsgConfig); |
|
|
|
if (wxMsgConfigs.size() == 0) return new ResultData(ErrorCode.MSG_SERVER_NOT_FIND, "您还未接入短信运营商,请联系平台管理员"); |
|
|
|
wxMsgConfig = wxMsgConfigs.get(0); |
|
|
|
|
|
|
|
if(wxMsgConfig.getRemains()==0){ |
|
|
|
logger.info("短信数量为0"); |
|
|
|
return new ResultData(ErrorCode.MSG_SUM_ZERO); |
|
|
|
} |
|
|
|
|
|
|
|
if(wxMsgConfig.getRemains()<wxMsg.getExpectSendNumber()){ |
|
|
|
logger.info("短信数量不足"); |
|
|
|
return new ResultData(ErrorCode.MSG_SUM_INSUFFICENT); |
|
|
|
public void batchSendMsg(WxMsg wxMsg,WxMsgConfig wxMsgConfig){ |
|
|
|
|
|
|
|
String[] split = wxMsg.getPhones().split(","); |
|
|
|
int length=split.length; |
|
|
|
int extra = length % 1000; |
|
|
|
int extracount = extra > 0 ? (length/1000+1) : (length/1000); |
|
|
|
int index=extracount-1; |
|
|
|
for(int i=0;i<extracount;i++){ |
|
|
|
if(extra>0 && i==index){ |
|
|
|
List<String> phoneList= Arrays.asList(split).subList(i* 1000, length); |
|
|
|
String phones = StringUtils.join(phoneList, ","); |
|
|
|
wxMsg.setPhones(phones); |
|
|
|
wxMsg.setExpectSendNumber(extra); |
|
|
|
sendmsg(wxMsg,wxMsgConfig); |
|
|
|
}else{ |
|
|
|
List<String> phoneList = Arrays.asList(split).subList(i * 1000, (i+1) * 1000); |
|
|
|
String phones = StringUtils.join(phoneList, ","); |
|
|
|
wxMsg.setExpectSendNumber(1000); |
|
|
|
wxMsg.setPhones(phones); |
|
|
|
sendmsg(wxMsg,wxMsgConfig); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public ResultData sendmsg(WxMsg record,WxMsgConfig wxMsgConfig) { |
|
|
|
logger.info("发送短信开始..."); |
|
|
|
|
|
|
|
String secret = wxMsgConfig.getSecret(); |
|
|
|
String bid = wxMsgConfig.getBid(); |
|
|
|
String publickey = wxMsgConfig.getPublickey(); |
|
|
|
|
|
|
|
String phone = wxMsg.getPhones(); |
|
|
|
String signature = wxMsg.getSignature(); |
|
|
|
String msg = wxMsg.getMsg(); |
|
|
|
String phone = record.getPhones(); |
|
|
|
String signature = record.getSignature(); |
|
|
|
String msg = record.getMsg(); |
|
|
|
String notifyUrl = wxMsgConfig.getNotifyurl(); |
|
|
|
logger.info("=============迈外迪API开始调用"); |
|
|
|
String result = WiwideUtil.sendMsg(secret, bid, publickey, phone, signature, msg, notifyUrl,EnumVerifyCode.NO.getCode().toString()); |
|
|
|
logger.info("=============迈外迪API返回结果:"+result); |
|
|
|
JSONObject jsonObjectResult = JSONObject.parseObject(result); |
|
|
|
String ret = jsonObjectResult.get("ret").toString(); |
|
|
|
String batchNo = jsonObjectResult.get("data").toString(); |
|
|
|
String resultMsg = jsonObjectResult.get("msg").toString(); |
|
|
|
wxMsg.setReturnResult(jsonObjectResult.toJSONString()); |
|
|
|
record.setReturnResult(jsonObjectResult.toJSONString()); |
|
|
|
if (ret.equals(EnumMsgSendStatus.MSG_SEND_SUCCESS.getCode().toString())) { |
|
|
|
wxMsg.setSendstatus(EnumMsgSendStatus.MSG_SEND_SUCCESS.getCode()); |
|
|
|
record.setSendstatus(EnumMsgSendStatus.MSG_SEND_SUCCESS.getCode()); |
|
|
|
} else { |
|
|
|
wxMsg.setSendstatus(EnumMsgSendStatus.MSG_SEND_FAIL.getCode()); |
|
|
|
record.setSendstatus(EnumMsgSendStatus.MSG_SEND_FAIL.getCode()); |
|
|
|
} |
|
|
|
|
|
|
|
final IdWorker idWorker = IdWorker.get(); |
|
|
|
long id = idWorker.nextId(); |
|
|
|
if (wxMsg.getId() == null) { |
|
|
|
wxMsg.setId(id); |
|
|
|
String systemTime = DateUtils.getSystemTime("yyyy-MM-dd HH:mm:ss"); |
|
|
|
wxMsg.setSendtime(systemTime); |
|
|
|
wxMsg.setCreatetime(DateUtils.stringToDate(systemTime,"yyyy-MM-dd HH:mm:ss")); |
|
|
|
wxMsgMapper.insertSelective(wxMsg); |
|
|
|
} else { |
|
|
|
wxMsgMapper.updateByPrimaryKeySelective(wxMsg); |
|
|
|
} |
|
|
|
WxMsg wxMsg = new WxMsg(); |
|
|
|
wxMsg.setTenantId(record.getTenantId()); |
|
|
|
wxMsg.setModelId(record.getModelId()); |
|
|
|
wxMsg.setMsg(record.getMsg()); |
|
|
|
wxMsg.setExpectSendNumber(record.getExpectSendNumber()); |
|
|
|
wxMsg.setPhones(record.getPhones()); |
|
|
|
wxMsg.setSignature(record.getSignature()); |
|
|
|
wxMsg.setIsright(record.getIsright()); |
|
|
|
wxMsg.setName(record.getName()); |
|
|
|
wxMsg.setSendstatus(record.getSendstatus()); |
|
|
|
wxMsg.setLabel(record.getLabel()); |
|
|
|
wxMsg.setStatus(record.getStatus()); |
|
|
|
wxMsg.setWay(record.getWay()); |
|
|
|
wxMsg.setId(id); |
|
|
|
String systemTime = DateUtils.getSystemTime("yyyy-MM-dd HH:mm:ss"); |
|
|
|
wxMsg.setSendtime(systemTime); |
|
|
|
wxMsg.setCreatetime(DateUtils.stringToDate(systemTime,"yyyy-MM-dd HH:mm:ss")); |
|
|
|
wxMsgMapper.insertSelective(wxMsg); |
|
|
|
|
|
|
|
|
|
|
|
logger.info("发送短信结束..."); |
|
|
|
if (ret.equals(EnumMsgSendStatus.MSG_SEND_SUCCESS.getCode().toString())) { |
|
|
|
|