cloudroam
2024-12-30 b9903ead016b8b1aa68eb04b48fca3b53fdab0d3
src/main/java/com/mzl/flower/service/impl/sms/SmsTaskServiceImpl.java
@@ -8,6 +8,8 @@
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;
@@ -35,6 +37,7 @@
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;
@@ -83,7 +86,7 @@
    }
    @Override
    public void saveSmsTask(SmsTaskDTO smsTaskDTO) {
    public void saveSmsTask(SmsTaskDTO smsTaskDTO) throws IOException {
        //校验
        if (StringUtils.isEmpty(smsTaskDTO.getName())) {
            throw new ValidationException("短信名称不能为空");
@@ -147,6 +150,7 @@
                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());
@@ -155,7 +159,22 @@
        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();
@@ -165,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);
@@ -236,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("非待发布的任务不可编辑");
@@ -303,6 +322,7 @@
                String userIdInfos = userIds.stream().map(Object::toString) // 确保每个元素都转换为字符串
                        .collect(Collectors.joining(";")); // 使用换行符连接字符串
                smsTaskDO.setUserIds(userIdInfos);
                smsTaskDTO.setNum((long) userIds.size());
            }
        }
        BeanUtils.copyProperties(smsTaskDTO, smsTaskDO,"userIds");
@@ -358,7 +378,7 @@
        });
    }
    @Override
    @Override
    public List<SmsSelectVO> getSelectList(Long id) {
        List<SmsSelectVO> smsSelectVOList = null;
        SmsTaskDO smsTaskDO = smsTaskMapper.selectById(id);
@@ -372,6 +392,19 @@
        }
    }
    @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) {
        return phoneNumbers.stream().map(phone -> {
            SmsTaskDetailDO detail = new SmsTaskDetailDO();