Authored by linlong

update

@@ -3,6 +3,7 @@ package com.yoho.unions.channel.service.impl; @@ -3,6 +3,7 @@ package com.yoho.unions.channel.service.impl;
3 import com.alibaba.fastjson.JSONObject; 3 import com.alibaba.fastjson.JSONObject;
4 import com.google.common.collect.Lists; 4 import com.google.common.collect.Lists;
5 import com.google.common.collect.Maps; 5 import com.google.common.collect.Maps;
  6 +import com.google.common.collect.Sets;
6 import com.yoho.error.ServiceError; 7 import com.yoho.error.ServiceError;
7 import com.yoho.error.exception.ServiceException; 8 import com.yoho.error.exception.ServiceException;
8 import com.yoho.message.sdk.common.model.SendMessageRspBo; 9 import com.yoho.message.sdk.common.model.SendMessageRspBo;
@@ -23,10 +24,7 @@ import org.springframework.beans.factory.annotation.Autowired; @@ -23,10 +24,7 @@ import org.springframework.beans.factory.annotation.Autowired;
23 import org.springframework.stereotype.Service; 24 import org.springframework.stereotype.Service;
24 import org.springframework.util.CollectionUtils; 25 import org.springframework.util.CollectionUtils;
25 26
26 -import java.util.Arrays;  
27 -import java.util.HashMap;  
28 -import java.util.List;  
29 -import java.util.Map; 27 +import java.util.*;
30 import java.util.stream.Collectors; 28 import java.util.stream.Collectors;
31 29
32 /** 30 /**
@@ -117,26 +115,36 @@ public class ChannelGroupServiceImpl implements ChannelGroupService { @@ -117,26 +115,36 @@ public class ChannelGroupServiceImpl implements ChannelGroupService {
117 //1.根据groupId查询条件 115 //1.根据groupId查询条件
118 List<ChannelGroupCondition> conditions = channelGroupConditionDAO.selectByGroupId(groupId); 116 List<ChannelGroupCondition> conditions = channelGroupConditionDAO.selectByGroupId(groupId);
119 logger.info("sendMessage:conditions is {}",conditions); 117 logger.info("sendMessage:conditions is {}",conditions);
120 - Map<String, Object> params = Maps.newHashMap();  
121 - if (!CollectionUtils.isEmpty(conditions)) {  
122 - for (ChannelGroupCondition channelGroupCondition : conditions) {  
123 - params.put(channelGroupCondition.getKey(), channelGroupCondition.getValue());  
124 - }  
125 - }  
126 -  
127 - //2.根据条件分批查询手机号发送短信  
128 - int total = channelUserDAO.selectCount(params);  
129 -  
130 - //3.记录分组批次表 118 + Map<String, Object> params = conditions.stream().collect(Collectors.toMap(ChannelGroupCondition::getKey,ChannelGroupCondition::getValue));
  119 +// Map<String, Object> params = Maps.newHashMap();
  120 +// if (!CollectionUtils.isEmpty(conditions)) {
  121 +// for (ChannelGroupCondition channelGroupCondition : conditions) {
  122 +// params.put(channelGroupCondition.getKey(), channelGroupCondition.getValue());
  123 +// }
  124 +// }
  125 +
  126 + //2.记录分组批次表
131 int sendTime = DateUtils.getCurrentTimeSecond(); 127 int sendTime = DateUtils.getCurrentTimeSecond();
132 ChannelGroupBatch channelGroupBatch = new ChannelGroupBatch(groupId, content, sendTime, sendUserId); 128 ChannelGroupBatch channelGroupBatch = new ChannelGroupBatch(groupId, content, sendTime, sendUserId);
133 channelGroupBatchDAO.insertSelective(channelGroupBatch); 129 channelGroupBatchDAO.insertSelective(channelGroupBatch);
134 logger.info("sendMessage:insert channelGroupBatch,channelGroupBatch ={}",channelGroupBatch); 130 logger.info("sendMessage:insert channelGroupBatch,channelGroupBatch ={}",channelGroupBatch);
135 int groupBatchId = channelGroupBatch.getId(); 131 int groupBatchId = channelGroupBatch.getId();
136 132
  133 + //3.根据条件分批查询手机号
  134 + int total = channelUserDAO.selectCount(params);
  135 + Set<String> mobileSet = Sets.newHashSet();
  136 + //分批从数据库中查询,每次1000,使用mobileSet去重
137 for (int i = 0; i < total; i += BATCH_MESSAGE_NUMBER) { 137 for (int i = 0; i < total; i += BATCH_MESSAGE_NUMBER) {
138 List<ChannelUser> channelUsers = channelUserDAO.selectPage(params, i, i + BATCH_MESSAGE_NUMBER); 138 List<ChannelUser> channelUsers = channelUserDAO.selectPage(params, i, i + BATCH_MESSAGE_NUMBER);
139 - List<String> mobiles = channelUsers.stream().map(ChannelUser::getMobile).collect(Collectors.toList()); 139 + Set<String> channelMobiles= channelUsers.stream().map(ChannelUser::getMobile).collect(Collectors.toSet());
  140 + mobileSet.addAll(channelMobiles);
  141 + }
  142 +
  143 + List<String> mobileList = Lists.newArrayList(mobileSet);
  144 + List<ChannelSmsDetail> smsDetails = Lists.newArrayList();
  145 + //4.用户去重、黑名单后,分批发送短信
  146 + for (int i = 0; i < mobileList.size(); i += BATCH_MESSAGE_NUMBER) {
  147 + List<String> mobiles = mobileList.subList(i, i + BATCH_MESSAGE_NUMBER);
140 String mobile = StringUtils.join(mobiles.toArray(), ","); 148 String mobile = StringUtils.join(mobiles.toArray(), ",");
141 //调用SDK发送短信接口 149 //调用SDK发送短信接口
142 SendMessageRspBo sendMessageRspBo = null; 150 SendMessageRspBo sendMessageRspBo = null;
@@ -149,7 +157,6 @@ public class ChannelGroupServiceImpl implements ChannelGroupService { @@ -149,7 +157,6 @@ public class ChannelGroupServiceImpl implements ChannelGroupService {
149 } 157 }
150 158
151 159
152 - List<ChannelSmsDetail> smsDetails = Lists.newArrayList();  
153 //4.1短信发送成功,记录短信日志表(状态为1) 160 //4.1短信发送成功,记录短信日志表(状态为1)
154 if (sendMessageRspBo!=null&&sendMessageRspBo.getCode() == 200 && sendMessageRspBo.getMessage().equals("SUCCESS")) { 161 if (sendMessageRspBo!=null&&sendMessageRspBo.getCode() == 200 && sendMessageRspBo.getMessage().equals("SUCCESS")) {
155 for (String sms : mobiles) { 162 for (String sms : mobiles) {
@@ -163,9 +170,11 @@ public class ChannelGroupServiceImpl implements ChannelGroupService { @@ -163,9 +170,11 @@ public class ChannelGroupServiceImpl implements ChannelGroupService {
163 smsDetails.add(channelSmsDetail); 170 smsDetails.add(channelSmsDetail);
164 } 171 }
165 } 172 }
166 - channelSmsDetailDAO.batchInsert(smsDetails);  
167 - logger.info("sendMessage:insert channelSmsDetai,smsDetails is {}",smsDetails);  
168 } 173 }
  174 +
  175 + channelSmsDetailDAO.batchInsert(smsDetails);
  176 + logger.info("sendMessage:insert channelSmsDetai,smsDetails is {}",smsDetails);
  177 +
169 //5.修改分组表的分组人数、发送时间、修改时间、内容、发送人 178 //5.修改分组表的分组人数、发送时间、修改时间、内容、发送人
170 ChannelGroup channelGroup = new ChannelGroup(); 179 ChannelGroup channelGroup = new ChannelGroup();
171 channelGroup.setId(groupId); 180 channelGroup.setId(groupId);
@@ -224,12 +233,12 @@ public class ChannelGroupServiceImpl implements ChannelGroupService { @@ -224,12 +233,12 @@ public class ChannelGroupServiceImpl implements ChannelGroupService {
224 if(beginTime!=null){ 233 if(beginTime!=null){
225 beginTime = DateUtils.int2DateStr(Integer.parseInt(beginTime),"yyyy-MM-dd HH:mm:ss"); 234 beginTime = DateUtils.int2DateStr(Integer.parseInt(beginTime),"yyyy-MM-dd HH:mm:ss");
226 }else{ 235 }else{
227 - beginTime = ""; 236 + beginTime = "未设置";
228 } 237 }
229 if(endTime!=null){ 238 if(endTime!=null){
230 endTime = DateUtils.int2DateStr(Integer.parseInt(endTime),"yyyy-MM-dd HH:mm:ss"); 239 endTime = DateUtils.int2DateStr(Integer.parseInt(endTime),"yyyy-MM-dd HH:mm:ss");
231 }else{ 240 }else{
232 - endTime = ""; 241 + endTime = "未设置";
233 } 242 }
234 sb.append("<div>").append(paramMap.get("beginChannelOrderTime")).append(":").append( beginTime).append("-").append(endTime).append("</div>"); 243 sb.append("<div>").append(paramMap.get("beginChannelOrderTime")).append(":").append( beginTime).append("-").append(endTime).append("</div>");
235 map.remove(beginTimeKey); 244 map.remove(beginTimeKey);