From 88d30c88070a81a1caef1aa270976d75786a17de Mon Sep 17 00:00:00 2001 From: gongbiao Date: Fri, 2 Nov 2018 15:05:13 +0800 Subject: [PATCH] =?UTF-8?q?[=E6=A0=87=E7=AD=BE=E7=9F=AD=E4=BF=A1][?= =?UTF-8?q?=E4=BF=AE=E6=94=B9][=E5=88=86=E6=89=B9=E6=AC=A1=E5=8F=91?= =?UTF-8?q?=E9=80=81=E7=9F=AD=E4=BF=A1]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../iformall/controller/WxMsgController.java | 127 +++++++++++- .../com/iformall/service/WxMsgService.java | 16 +- .../service/impl/WxMsgServiceImpl.java | 186 +++++++----------- 3 files changed, 202 insertions(+), 127 deletions(-) diff --git a/mallinkAdmin/src/main/java/com/iformall/controller/WxMsgController.java b/mallinkAdmin/src/main/java/com/iformall/controller/WxMsgController.java index e926ba347..5a0745dc4 100644 --- a/mallinkAdmin/src/main/java/com/iformall/controller/WxMsgController.java +++ b/mallinkAdmin/src/main/java/com/iformall/controller/WxMsgController.java @@ -1,9 +1,13 @@ package com.iformall.controller; import com.github.pagehelper.PageInfo; +import com.iformall.common.ErrorCode; 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.WxMsgConfig; +import com.iformall.mapper.WxMsgConfigMapper; import com.iformall.service.WxCUserTagsService; import com.iformall.service.WxMsgService; import com.iformall.utils.Constant; @@ -19,9 +23,7 @@ import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.FileOutputStream; -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; +import java.util.*; @RestController @RequestMapping("wxMsg") @@ -32,7 +34,10 @@ public class WxMsgController extends BaseController { private WxMsgService wxMsgService; @Autowired - private WxCUserTagsService wxCUserTagsService; + WxMsgConfigMapper wxMsgConfigMapper; + + @Autowired + WxCUserTagsService wxCUserTagsService; @ApiOperation("分页列表接口") @GetMapping("list") @@ -51,16 +56,122 @@ public class WxMsgController extends BaseController { @ApiOperation("新增接口") @PostMapping("add") public ResultData add(@RequestBody WxMsg wxMsg) { - //Assert.notNull(wxMsg.getName(), "角色名不能为空"); - //Assert.isTrue(!checkUnique(sysRole.getName(), null), "重复的角色名"); wxMsg.setTenantId(getTenantId()); - return wxMsgService.saveOrUpdate(wxMsg); + //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 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()); + + //从短信配置中查询密钥 bid 等信息 + WxMsgConfig wxMsgConfig = new WxMsgConfig(); + wxMsgConfig.setTenantId(wxMsg.getTenantId()); + List 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() tagids = new ArrayList<>(); + for (int i = 0; i < arys.length; i++) { + tagids.add(Long.parseLong(arys[i])); + } + List 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 ""; } @ApiOperation("根据id更新接口") @PostMapping("update") public ResultData update(@RequestBody WxMsg wxMsg) { - wxMsgService.saveOrUpdate(wxMsg); + //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 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()); + + //从短信配置中查询密钥 bid 等信息 + WxMsgConfig wxMsgConfig = new WxMsgConfig(); + wxMsgConfig.setTenantId(wxMsg.getTenantId()); + List 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() listAsPage(WxMsg record, Integer pageIndex, Integer pageSize); - - /** + PageInfo listAsPage(WxMsg record, Integer pageIndex, Integer pageSize); + + /** * 根据Id获得实体 * * @param id * @return */ WxMsg getById(Long id); - - /** + + /** * 保存或更新实体 * * @param record + * @param wxMsgConfig */ - ResultData saveOrUpdate(WxMsg record); + ResultData saveOrUpdate(WxMsg record, WxMsgConfig wxMsgConfig); /** * 根据Id删除实体 diff --git a/mallinkService/src/main/java/com/iformall/service/impl/WxMsgServiceImpl.java b/mallinkService/src/main/java/com/iformall/service/impl/WxMsgServiceImpl.java index 50fe7d559..a3e5bc052 100644 --- a/mallinkService/src/main/java/com/iformall/service/impl/WxMsgServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/impl/WxMsgServiceImpl.java @@ -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 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 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 tagids = new ArrayList<>(); - for (int i = 0; i < arys.length; i++) { - tagids.add(Long.parseLong(arys[i])); - } - List 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 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() 0 ? (length/1000+1) : (length/1000); + int index=extracount-1; + for(int i=0;i0 && i==index){ + List phoneList= Arrays.asList(split).subList(i* 1000, length); + String phones = StringUtils.join(phoneList, ","); + wxMsg.setPhones(phones); + wxMsg.setExpectSendNumber(extra); + sendmsg(wxMsg,wxMsgConfig); + }else{ + List 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())) {