cloudroam
2024-12-27 8bfe96b6f370f1d2e79b739c7db50b95dab9a965
src/main/java/com/mzl/flower/service/impl/sms/SmsTaskServiceImpl.java
@@ -5,6 +5,7 @@
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.GetObjectRequest;
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.mzl.flower.config.OssProperties;
@@ -34,7 +35,6 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
@@ -137,10 +137,13 @@
        try {
            // 下载Excel文件到本地临时文件
            File tempFile = File.createTempFile("temp", ".xlsx");
            ossClient.getObject(new GetObjectRequest(bucketName, smsTaskDTO.getFileUrl()), tempFile);
            String fileUrl = smsTaskDTO.getFileUrl();
            String objectKey = fileUrl.replaceFirst("^https?://[^/]+/", ""); // 去掉协议部分
            ossClient.getObject(new GetObjectRequest(bucketName, objectKey), tempFile);
            // 解析Excel文件
            try (FileInputStream inputStream = new FileInputStream(tempFile); Workbook workbook = new XSSFWorkbook(inputStream)) {
            try (FileInputStream inputStream = new FileInputStream(tempFile);
                 Workbook workbook = new XSSFWorkbook(inputStream)) {
                Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表
                int rowCount = sheet.getPhysicalNumberOfRows();
@@ -156,8 +159,14 @@
                    Row row = sheet.getRow(i);
                    if (row != null) {
                        Cell cell = row.getCell(0); // 假设手机号在第一列
                        if (cell != null && cell.getCellType() == CellType.STRING) {
                            String phoneNumber = cell.getStringCellValue();
                        if (cell != null ) {
                            String phoneNumber = "";
                            if (cell.getCellType() == CellType.STRING) {
                                phoneNumber = cell.getStringCellValue();
                            } else if (cell.getCellType() == CellType.NUMERIC) {
                                // 将数字类型的手机号转换为字符串
                                phoneNumber = String.valueOf((long) cell.getNumericCellValue());
                            }
                            if (!PHONE_NUMBER_PATTERN.matcher(phoneNumber).matches()) {
                                message.append("第" + (i + 1) + "行手机号" + phoneNumber + "格式不正确");
                                isValid = false;
@@ -166,7 +175,7 @@
                                phones.append(phoneNumber).append("\n");
                            }
                        } else {
                            message.append("行上的单元格为空或无效 " + (i + 1));
                            message.append("第" + (i + 1)+ "行上的单元格为空或无效 ");
                            isValid = false;
                            break; // 退出循环
                        }
@@ -181,7 +190,7 @@
                    throw new ValidationException(message.toString());
                } else {
                    smsTaskDTO.setPhones(phones.toString());
                    smsTaskDTO.setNum((long) rowCount);
                    smsTaskDTO.setNum((long) rowCount - 1);
                }
            } catch (IOException e) {
                e.printStackTrace();
@@ -288,6 +297,7 @@
            detail.setSmsTaskId(smsTaskDO.getId());
            detail.setSmsTemplateId(smsTaskDO.getSmsTemplateId());
            detail.setPhone(phone);
            detail.create();
            return detail;
        }).collect(Collectors.toList());
    }
@@ -297,8 +307,13 @@
        String templateCode = smsTemplateDO.getCode();
        for (SmsTaskDetailDO detail : smsTaskDetailDOList) {
            try {
                SmsUtil.sendSms(detail.getPhone(), templateCode, null);
                detail.setResult(Constants.SMS_SEND_RESULT.success.name());
                SendSmsResponse sendSmsResponse = SmsUtil.sendSms(detail.getPhone(), templateCode, null);
                if("OK".equals(sendSmsResponse.getCode())){
                    detail.setResult(Constants.SMS_SEND_RESULT.success.name());
                }else{
                    detail.setResult(Constants.SMS_SEND_RESULT.failure.name());
                }
                detail.setResponseResult(sendSmsResponse.toString());
            } catch (Exception e) {
                detail.setResult(Constants.SMS_SEND_RESULT.failure.name());
                System.err.println("Failed to send SMS to " + detail.getPhone() + ": " + e.getMessage());