From 4e5c47ee19caf98810b4ee50661fc141c4d7478f Mon Sep 17 00:00:00 2001
From: cloudroam <cloudroam>
Date: 星期五, 28 三月 2025 08:51:19 +0800
Subject: [PATCH] fix: 11
---
src/main/java/com/mzl/flower/service/impl/sms/SmsTaskServiceImpl.java | 142 +++++++++++++++++++++++++++++++++++++++++++----
1 files changed, 130 insertions(+), 12 deletions(-)
diff --git a/src/main/java/com/mzl/flower/service/impl/sms/SmsTaskServiceImpl.java b/src/main/java/com/mzl/flower/service/impl/sms/SmsTaskServiceImpl.java
index ffe5ad4..1c05dcd 100644
--- a/src/main/java/com/mzl/flower/service/impl/sms/SmsTaskServiceImpl.java
+++ b/src/main/java/com/mzl/flower/service/impl/sms/SmsTaskServiceImpl.java
@@ -8,12 +8,16 @@
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
import com.mzl.flower.config.OssProperties;
import com.mzl.flower.config.exception.ValidationException;
import com.mzl.flower.config.security.SecurityUtils;
import com.mzl.flower.constant.Constants;
import com.mzl.flower.dto.request.sms.SmsTaskDTO;
import com.mzl.flower.dto.request.sms.SmsTaskQueryDTO;
+import com.mzl.flower.dto.request.sms.SmsUserDTO;
+import com.mzl.flower.dto.response.sms.SmsSelectVO;
import com.mzl.flower.dto.response.sms.SmsTaskVO;
import com.mzl.flower.entity.SmsTaskDO;
import com.mzl.flower.entity.SmsTaskDetailDO;
@@ -21,8 +25,10 @@
import com.mzl.flower.mapper.SmsTaskDetailMapper;
import com.mzl.flower.mapper.SmsTaskMapper;
import com.mzl.flower.mapper.SmsTemplateMapper;
+import com.mzl.flower.mapper.system.UserMapper;
import com.mzl.flower.service.sms.SmsTaskDetailService;
import com.mzl.flower.service.sms.SmsTaskService;
+import com.mzl.flower.service.system.UserService;
import com.mzl.flower.utils.SmsUtil;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.usermodel.*;
@@ -30,6 +36,8 @@
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.CollectionUtils;
+import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import java.io.File;
@@ -57,6 +65,8 @@
private final OssProperties ossProperties;
private final SmsTaskMapper smsTaskMapper;
+ private final UserMapper userMapper;
+
private final SmsTemplateMapper smsTemplateMapper;
private final SmsTaskDetailMapper smsTaskDetailMapper;
@@ -76,7 +86,7 @@
}
@Override
- public void saveSmsTask(SmsTaskDTO smsTaskDTO) {
+ public void saveSmsTask(SmsTaskDTO smsTaskDTO) throws IOException {
//校验
if (StringUtils.isEmpty(smsTaskDTO.getName())) {
throw new ValidationException("短信名称不能为空");
@@ -90,7 +100,7 @@
throw new ValidationException("接收号码类型不能为空");
}
- if ((Constants.SMS_RECEIVE_TYPE.INPUT.name().equals(smsTaskDTO.getType()) || Constants.SMS_RECEIVE_TYPE.SELECT.name().equals(smsTaskDTO.getType())) && StringUtils.isEmpty(smsTaskDTO.getPhones())) {
+ if (Constants.SMS_RECEIVE_TYPE.INPUT.name().equals(smsTaskDTO.getType()) && StringUtils.isEmpty(smsTaskDTO.getPhones())) {
throw new ValidationException("手机号不能为空");
}
@@ -98,7 +108,7 @@
throw new ValidationException("导入文件不能为空");
}
- if (Constants.SMS_RECEIVE_TYPE.INPUT.name().equals(smsTaskDTO.getType()) || Constants.SMS_RECEIVE_TYPE.SELECT.name().equals(smsTaskDTO.getType())) {
+ if (Constants.SMS_RECEIVE_TYPE.INPUT.name().equals(smsTaskDTO.getType()) ) {
//解析手机号,包含不同平台的换行符
String text = smsTaskDTO.getPhones();
@@ -119,15 +129,52 @@
if (Constants.SMS_RECEIVE_TYPE.IMPORT.name().equals(smsTaskDTO.getType())) {
dealImportExcel(smsTaskDTO);
}
+ List<SmsUserDTO> smsUserDTOS = smsTaskDTO.getSmsUserDTOS();
SmsTaskDO smsTaskDO = new SmsTaskDO();
BeanUtils.copyProperties(smsTaskDTO, smsTaskDO);
+ if(!CollectionUtils.isEmpty(smsUserDTOS)){
+ List<String> userIds = smsUserDTOS.stream()
+ .map(SmsUserDTO::getUserId)
+ .collect(Collectors.toList());
+
+ List<String> userPhones = smsUserDTOS.stream()
+ .map(SmsUserDTO::getUserPhone)
+ .collect(Collectors.toList());
+ if(!CollectionUtils.isEmpty(userPhones)){
+ String phones = userPhones.stream()
+ .map(Object::toString) // 确保每个元素都转换为字符串
+ .collect(Collectors.joining("\n")); // 使用换行符连接字符串
+ smsTaskDTO.setPhones(phones); // 假设有一个setPhones方法用于设置phones字段
+ }
+ if (!CollectionUtils.isEmpty(userIds)) {
+ String userIdInfos = userIds.stream().map(Object::toString) // 确保每个元素都转换为字符串
+ .collect(Collectors.joining(";")); // 使用换行符连接字符串
+ smsTaskDO.setUserIds(userIdInfos);
+ smsTaskDTO.setNum((long) userIds.size());
+ }
+ }
smsTaskDO.setStatus(Constants.SMS_TASK_STATUS.wait_publish.name());
smsTaskDO.setPhones(smsTaskDTO.getPhones());
smsTaskDO.create(SecurityUtils.getUserId());
smsTaskMapper.insert(smsTaskDO);
}
- private void dealImportExcel(SmsTaskDTO smsTaskDTO) {
+ private void dealImportExcel(SmsTaskDTO smsTaskDTO) throws IOException {
+ String fileUrlMessage = "";
+ // 创建ObjectMapper实例
+ ObjectMapper objectMapper = new ObjectMapper();
+ JsonNode rootNode = objectMapper.readTree(smsTaskDTO.getFileUrl());
+ if (rootNode.isArray()) {
+ JsonNode firstElement = rootNode.get(0);
+ if (firstElement.has("url")) {
+ fileUrlMessage = firstElement.get("url").asText();
+ } else {
+ throw new ValidationException("URL字段不存在");
+ }
+ } else {
+ throw new ValidationException("JSON数组为空或不是数组");
+ }
+
String endPoint = ossProperties.getEndpoint();
String accessKeyId = ossProperties.getKeyid();
String accessKeySecret = ossProperties.getKeysecret();
@@ -137,7 +184,7 @@
try {
// 下载Excel文件到本地临时文件
File tempFile = File.createTempFile("temp", ".xlsx");
- String fileUrl = smsTaskDTO.getFileUrl();
+ String fileUrl = fileUrlMessage;
String objectKey = fileUrl.replaceFirst("^https?://[^/]+/", ""); // 去掉协议部分
ossClient.getObject(new GetObjectRequest(bucketName, objectKey), tempFile);
@@ -208,7 +255,7 @@
}
@Override
- public void updateSmsTask(SmsTaskDTO smsTaskDTO) {
+ public void updateSmsTask(SmsTaskDTO smsTaskDTO) throws IOException {
SmsTaskDO smsTaskDO = smsTaskMapper.selectById(smsTaskDTO.getId());
if (!smsTaskDO.getStatus().equals(Constants.SMS_TASK_STATUS.wait_publish.name())) {
throw new ValidationException("非待发布的任务不可编辑");
@@ -226,7 +273,7 @@
throw new ValidationException("接收号码类型不能为空");
}
- if ((Constants.SMS_RECEIVE_TYPE.INPUT.name().equals(smsTaskDTO.getType()) || Constants.SMS_RECEIVE_TYPE.SELECT.name().equals(smsTaskDTO.getType())) && StringUtils.isEmpty(smsTaskDTO.getPhones())) {
+ if (Constants.SMS_RECEIVE_TYPE.INPUT.name().equals(smsTaskDTO.getType()) && StringUtils.isEmpty(smsTaskDTO.getPhones())) {
throw new ValidationException("手机号不能为空");
}
@@ -234,11 +281,51 @@
throw new ValidationException("导入文件不能为空");
}
- if(!smsTaskDTO.getFileUrl().equals(smsTaskDO.getFileUrl())){
- dealImportExcel(smsTaskDTO);
+ if (Constants.SMS_RECEIVE_TYPE.INPUT.name().equals(smsTaskDTO.getType()) ) {
+
+ //解析手机号,包含不同平台的换行符
+ String text = smsTaskDTO.getPhones();
+
+ // 使用正则表达式匹配所有类型的换行符
+ String[] lines = text.split("\\r?\\n|\\r");
+
+ // 将数组转换为 List
+ List<String> lineList = Arrays.asList(lines);
+ lineList.forEach(l -> {
+ boolean validPhoneNumber = isValidPhoneNumber(l);
+ if (!validPhoneNumber) {
+ throw new ValidationException(l + "不是合法的手机号");
+ }
+ });
+ smsTaskDTO.setNum((long) lineList.size());
}
- BeanUtils.copyProperties(smsTaskDTO, smsTaskDO);
+ if(Constants.SMS_RECEIVE_TYPE.IMPORT.name().equals(smsTaskDTO.getType()) && !smsTaskDTO.getFileUrl().equals(smsTaskDO.getFileUrl())){
+ dealImportExcel(smsTaskDTO);
+ }
+ List<SmsUserDTO> smsUserDTOS = smsTaskDTO.getSmsUserDTOS();
+ if(!CollectionUtils.isEmpty(smsUserDTOS)){
+ List<String> userIds = smsUserDTOS.stream()
+ .map(SmsUserDTO::getUserId)
+ .collect(Collectors.toList());
+
+ List<String> userPhones = smsUserDTOS.stream()
+ .map(SmsUserDTO::getUserPhone)
+ .collect(Collectors.toList());
+ if(!CollectionUtils.isEmpty(userPhones)){
+ String phones = userPhones.stream()
+ .map(Object::toString) // 确保每个元素都转换为字符串
+ .collect(Collectors.joining("\n")); // 使用换行符连接字符串
+ smsTaskDTO.setPhones(phones); // 假设有一个setPhones方法用于设置phones字段
+ }
+ if (!CollectionUtils.isEmpty(userIds)) {
+ String userIdInfos = userIds.stream().map(Object::toString) // 确保每个元素都转换为字符串
+ .collect(Collectors.joining(";")); // 使用换行符连接字符串
+ smsTaskDO.setUserIds(userIdInfos);
+ smsTaskDTO.setNum((long) userIds.size());
+ }
+ }
+ BeanUtils.copyProperties(smsTaskDTO, smsTaskDO,"userIds");
smsTaskDO.update(SecurityUtils.getUserId());
smsTaskDO.setPhones(smsTaskDTO.getPhones());
smsTaskMapper.updateById(smsTaskDO);
@@ -287,8 +374,35 @@
// 异步保存任务明细信息并发送短信
CompletableFuture.runAsync(() -> {
smsTaskDetailService.saveBatch(smsTaskDetailDOList);
- sendSmsToAll(smsTaskDetailDOList, smsTaskDO.getSmsTemplateId());
+ sendSmsToAll(smsTaskDetailDOList, smsTaskDO.getSmsTemplateId(),smsTaskDTO.getId());
});
+ }
+
+ @Override
+ public List<SmsSelectVO> getSelectList(Long id) {
+ List<SmsSelectVO> smsSelectVOList = null;
+ SmsTaskDO smsTaskDO = smsTaskMapper.selectById(id);
+ String ids = smsTaskDO.getUserIds();
+ if (StringUtils.isEmpty(ids)) {
+ return smsSelectVOList;
+ } else {
+ String[] idArray = ids.split(";");
+ List<String> idList = Arrays.asList(idArray);
+ return userMapper.getSelectList(idList);
+ }
+ }
+
+ @Override
+ public SmsTaskVO getDetailById(Long id) {
+ SmsTaskDO smsTaskDO = smsTaskMapper.selectById(id);
+ SmsTaskVO smsTaskVO=new SmsTaskVO();
+ BeanUtils.copyProperties(smsTaskDO,smsTaskVO);
+ if(!ObjectUtils.isEmpty(smsTaskDO)){
+ List<SmsSelectVO> selectList = getSelectList(id);
+ smsTaskVO.setSmsUserDTOS(selectList);
+ return smsTaskVO;
+ }
+ return null;
}
private List<SmsTaskDetailDO> createSmsTaskDetails(SmsTaskDO smsTaskDO, List<String> phoneNumbers) {
@@ -302,7 +416,7 @@
}).collect(Collectors.toList());
}
- private void sendSmsToAll(List<SmsTaskDetailDO> smsTaskDetailDOList, Long smsTemplateId) {
+ private void sendSmsToAll(List<SmsTaskDetailDO> smsTaskDetailDOList, Long smsTemplateId,Long id) {
SmsTemplateDO smsTemplateDO = smsTemplateMapper.selectById(smsTemplateId);
String templateCode = smsTemplateDO.getCode();
for (SmsTaskDetailDO detail : smsTaskDetailDOList) {
@@ -311,6 +425,7 @@
if("OK".equals(sendSmsResponse.getCode())){
detail.setResult(Constants.SMS_SEND_RESULT.success.name());
}else{
+ detail.setFailReason(sendSmsResponse.getMessage());
detail.setResult(Constants.SMS_SEND_RESULT.failure.name());
}
detail.setResponseResult(sendSmsResponse.toString());
@@ -320,6 +435,9 @@
} finally {
//无论如何都更新结果
smsTaskDetailMapper.updateById(detail);
+ SmsTaskDO smsTaskDO = smsTaskMapper.selectById(id);
+ smsTaskDO.setStatus(Constants.SMS_TASK_STATUS.complete.name());
+ smsTaskMapper.updateById(smsTaskDO);
}
}
}
--
Gitblit v1.9.3