短信优化
This commit is contained in:
parent
1f6912a794
commit
cb79c296d1
|
|
@ -16,6 +16,7 @@ import com.bonus.message.service.WorkerService;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.quartz.SchedulerException;
|
import org.quartz.SchedulerException;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import com.bonus.job.mapper.PmTaskMapper;
|
import com.bonus.job.mapper.PmTaskMapper;
|
||||||
import com.bonus.job.domain.PmTask;
|
import com.bonus.job.domain.PmTask;
|
||||||
|
|
@ -41,6 +42,9 @@ public class PmTaskServiceImpl implements PmTaskService{
|
||||||
@Resource
|
@Resource
|
||||||
private WorkerService workerService;
|
private WorkerService workerService;
|
||||||
|
|
||||||
|
@Value("${message.signature}")
|
||||||
|
private String msgSign;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<PmTask> getMsgTaskList(PmTask task) {
|
public List<PmTask> getMsgTaskList(PmTask task) {
|
||||||
|
|
@ -257,9 +261,10 @@ public class PmTaskServiceImpl implements PmTaskService{
|
||||||
List<WorkerVo> userList = new ArrayList<>();
|
List<WorkerVo> userList = new ArrayList<>();
|
||||||
userList.add(o);
|
userList.add(o);
|
||||||
MessageSendUtil messageSendUtil = new MessageSendUtil();
|
MessageSendUtil messageSendUtil = new MessageSendUtil();
|
||||||
String msgResult = messageSendUtil.sendMessage(userList, o.getSendContent());
|
String content = msgSign +o.getSendContent();
|
||||||
|
String msgResult = messageSendUtil.sendMessage(userList, content);
|
||||||
if (msgResult == null){
|
if (msgResult == null){
|
||||||
return AjaxResult.error("短信发送返回异常:"+msgResult);
|
return AjaxResult.error("短信发送返回异常:"+ null);
|
||||||
}
|
}
|
||||||
String[] split = msgResult.split(",");
|
String[] split = msgResult.split(",");
|
||||||
if (split.length == 2 && "ok".equals(split[0])) {
|
if (split.length == 2 && "ok".equals(split[0])) {
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ import org.quartz.Job;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
|
@ -39,6 +40,9 @@ public class WorkerSendMsgTask{
|
||||||
@Autowired
|
@Autowired
|
||||||
private ISysJobService jobService;
|
private ISysJobService jobService;
|
||||||
|
|
||||||
|
@Value("${message.signature}")
|
||||||
|
private String msgSign;
|
||||||
|
|
||||||
SnowflakeIdGenerator idGen = new SnowflakeIdGenerator(1, 1);
|
SnowflakeIdGenerator idGen = new SnowflakeIdGenerator(1, 1);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -49,7 +53,7 @@ public class WorkerSendMsgTask{
|
||||||
try{
|
try{
|
||||||
//查询任务发送人人员信息
|
//查询任务发送人人员信息
|
||||||
List<WorkerVo> list = mapper.getWorkerByJobId(jobId);
|
List<WorkerVo> list = mapper.getWorkerByJobId(jobId);
|
||||||
sendInBatches(list);
|
sendInBatches(list, true);
|
||||||
//如果任务是指执行一次,就将任务status停掉
|
//如果任务是指执行一次,就将任务status停掉
|
||||||
SysJob job = mapper.getJobByJobId(jobId);
|
SysJob job = mapper.getJobByJobId(jobId);
|
||||||
if ("2".equals(job.getMisfirePolicy())){
|
if ("2".equals(job.getMisfirePolicy())){
|
||||||
|
|
@ -69,7 +73,7 @@ public class WorkerSendMsgTask{
|
||||||
try{
|
try{
|
||||||
//查询过生日的人员和短信内容
|
//查询过生日的人员和短信内容
|
||||||
List<WorkerVo> list = mapper.getHappyBirthDayWorkerByJobId(jobId);
|
List<WorkerVo> list = mapper.getHappyBirthDayWorkerByJobId(jobId);
|
||||||
sendInBatches(list);
|
sendInBatches(list, false);
|
||||||
}catch (Exception e){
|
}catch (Exception e){
|
||||||
logger.error("人员短信调度,{}",e.getMessage());
|
logger.error("人员短信调度,{}",e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
@ -77,20 +81,21 @@ public class WorkerSendMsgTask{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 批量发送短信
|
* 批量发送短信
|
||||||
|
* @param workers 人员
|
||||||
|
* @param flag 是否为普通短信
|
||||||
*/
|
*/
|
||||||
public void sendInBatches(List<WorkerVo> workers) {
|
public void sendInBatches(List<WorkerVo> workers, Boolean flag) {
|
||||||
String content = workers.get(0).getSendContent();
|
|
||||||
long loopId = idGen.nextId();
|
long loopId = idGen.nextId();
|
||||||
final int BATCH_SIZE = 200;
|
final int BATCH_SIZE = 200;
|
||||||
if (workers.size() <= BATCH_SIZE) {
|
if (workers.size() <= BATCH_SIZE) {
|
||||||
// 不超过200人,单次发送
|
// 不超过200人,单次发送
|
||||||
msgFlow(workers, content, loopId);
|
msgFlow(workers, loopId, flag);
|
||||||
} else {
|
} else {
|
||||||
// 超过200人,分批次发送
|
// 超过200人,分批次发送
|
||||||
for (int i = 0; i < workers.size(); i += BATCH_SIZE) {
|
for (int i = 0; i < workers.size(); i += BATCH_SIZE) {
|
||||||
int end = Math.min(i + BATCH_SIZE, workers.size());
|
int end = Math.min(i + BATCH_SIZE, workers.size());
|
||||||
List<WorkerVo> batch = workers.subList(i, end);
|
List<WorkerVo> batch = workers.subList(i, end);
|
||||||
msgFlow(batch, content, loopId);
|
msgFlow(batch, loopId, flag);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -98,10 +103,21 @@ public class WorkerSendMsgTask{
|
||||||
/**
|
/**
|
||||||
* 短信发送
|
* 短信发送
|
||||||
*/
|
*/
|
||||||
private String msgFlow(List<WorkerVo> userList, String content, long loopId) {
|
private String msgFlow(List<WorkerVo> userList, long loopId, Boolean flag) {
|
||||||
//1.发送短信
|
//1.发送短信
|
||||||
MessageSendUtil messageSendUtil = new MessageSendUtil();
|
MessageSendUtil messageSendUtil = new MessageSendUtil();
|
||||||
String msgResult = messageSendUtil.sendMessage(userList, content);
|
String msgResult;
|
||||||
|
if(flag){
|
||||||
|
String sendContent = userList.get(0).getSendContent();
|
||||||
|
sendContent = msgSign + sendContent;
|
||||||
|
msgResult = messageSendUtil.sendMessage(userList,sendContent);
|
||||||
|
}else{
|
||||||
|
for (WorkerVo vo : userList) {
|
||||||
|
String sex = "1".equals(vo.getSex()) ? "先生" : "女士";
|
||||||
|
vo.setSendContent(msgSign + vo.getWorkerName()+sex+"您好,"+vo.getSendContent());
|
||||||
|
}
|
||||||
|
msgResult = messageSendUtil.sendStyleMessage(userList);
|
||||||
|
}
|
||||||
String[] split = msgResult.split(",");
|
String[] split = msgResult.split(",");
|
||||||
if (split.length == 2 && "ok".equals(split[0])) {
|
if (split.length == 2 && "ok".equals(split[0])) {
|
||||||
// 批量发送成功,更新任务状态为成功
|
// 批量发送成功,更新任务状态为成功
|
||||||
|
|
|
||||||
|
|
@ -45,9 +45,6 @@ public class MessageSendUtil {
|
||||||
ERROR_CODE_TO_MESSAGE.put("-1003", "用户通道异常");
|
ERROR_CODE_TO_MESSAGE.put("-1003", "用户通道异常");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 短信发送
|
|
||||||
*/
|
|
||||||
/**
|
/**
|
||||||
* 短信发送
|
* 短信发送
|
||||||
*/
|
*/
|
||||||
|
|
@ -57,7 +54,6 @@ public class MessageSendUtil {
|
||||||
String mobiles = userList.stream()
|
String mobiles = userList.stream()
|
||||||
.map(WorkerVo::getPhone)
|
.map(WorkerVo::getPhone)
|
||||||
.collect(Collectors.joining(","));
|
.collect(Collectors.joining(","));
|
||||||
content = "【博诺思】"+content;
|
|
||||||
// 构建请求URL
|
// 构建请求URL
|
||||||
String url = API_URL + msgUrl + "?ddtkey=" + DDTKEY + "&secretkey=" + SECRET_KEY;
|
String url = API_URL + msgUrl + "?ddtkey=" + DDTKEY + "&secretkey=" + SECRET_KEY;
|
||||||
String uri = url + "&mobile=" + mobiles + "&content=" + content;
|
String uri = url + "&mobile=" + mobiles + "&content=" + content;
|
||||||
|
|
@ -69,6 +65,29 @@ public class MessageSendUtil {
|
||||||
return convertToMessage(HttpRequestHelper.doPost(uri, param));
|
return convertToMessage(HttpRequestHelper.doPost(uri, param));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 变量短信发送
|
||||||
|
*/
|
||||||
|
public String sendStyleMessage(List<WorkerVo> userList) {
|
||||||
|
String msgUrl = "/styleSms_token";
|
||||||
|
// 构建手机号字符串,用逗号分隔
|
||||||
|
String mobiles = userList.stream()
|
||||||
|
.map(WorkerVo::getPhone)
|
||||||
|
.collect(Collectors.joining(","));
|
||||||
|
String contents = userList.stream()
|
||||||
|
.map(WorkerVo::getSendContent)
|
||||||
|
.collect(Collectors.joining("※"));
|
||||||
|
// 构建请求URL
|
||||||
|
String url = API_URL + msgUrl + "?ddtkey=" + DDTKEY + "&secretkey=" + SECRET_KEY;
|
||||||
|
String uri = url + "&mobile=" + mobiles + "&content=" + contents;
|
||||||
|
// 构建JSON参数
|
||||||
|
String param = String.format(
|
||||||
|
"{\"ddtkey\":\"%s\",\"secretkey\":\"%s\",\"mobile\":\"%s\",\"content\":\"%s\"}",
|
||||||
|
DDTKEY, SECRET_KEY, mobiles, contents
|
||||||
|
);
|
||||||
|
return convertToMessage(HttpRequestHelper.doPost(uri, param));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将短信平台返回的错误码转换为中文说明
|
* 将短信平台返回的错误码转换为中文说明
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -94,4 +94,6 @@ public class WorkerVo {
|
||||||
private String sendStatus;
|
private String sendStatus;
|
||||||
private String reason;
|
private String reason;
|
||||||
private String submitTime;
|
private String submitTime;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -128,3 +128,6 @@ xss:
|
||||||
excludes: /system/notice
|
excludes: /system/notice
|
||||||
# 匹配链接
|
# 匹配链接
|
||||||
urlPatterns: /system/*,/monitor/*,/tool/*
|
urlPatterns: /system/*,/monitor/*,/tool/*
|
||||||
|
|
||||||
|
message:
|
||||||
|
signature: 【博诺思】
|
||||||
|
|
|
||||||
|
|
@ -80,6 +80,7 @@
|
||||||
pt.send_content,
|
pt.send_content,
|
||||||
w.worker_name,
|
w.worker_name,
|
||||||
w.phone,
|
w.phone,
|
||||||
|
w.sex,
|
||||||
pt.id AS taskId,
|
pt.id AS taskId,
|
||||||
w.id
|
w.id
|
||||||
FROM pm_task pt, pm_worker w
|
FROM pm_task pt, pm_worker w
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue