BUGFIX:邮件;

This commit is contained in:
shahaibo
2024-04-30 10:19:40 +08:00
parent 7454e16076
commit e70580c388
4 changed files with 171 additions and 17 deletions

View File

@@ -2,13 +2,25 @@ package com.ai.da.common.config;
import com.ai.da.common.utils.SendEmailUtil;
import com.ai.da.mapper.AccountMapper;
import com.ai.da.mapper.TrialOrderMapper;
import com.ai.da.mapper.entity.Account;
import com.ai.da.mapper.entity.TrialOrder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
@Component
@@ -17,6 +29,14 @@ public class MyTaskScheduler {
@Resource
private AccountMapper accountMapper;
@Resource
private TrialOrderMapper trialOrderMapper;
@PostConstruct
public void executeWeeklyHeavyStockReport() {
sendTrialOrderExcelToManagements();
}
// 定时任务,每十五天执行一次
// @Scheduled(cron = "0 0 0 ? * MON")
@Scheduled(cron = "0 0 0 */15 * ?")
@@ -53,4 +73,63 @@ public class MyTaskScheduler {
}
}
}
@Scheduled(cron = "0 0 8 * * ?")
public void sendTrialOrderExcelToManagements() {
// 获取前一天日期
LocalDate yesterday = LocalDate.now().minusDays(1);
// 查询前一天的试用订单
QueryWrapper<TrialOrder> qw = new QueryWrapper<>();
qw.lambda().between(TrialOrder::getCreateTime, yesterday.atStartOfDay(), yesterday.atTime(23, 59, 59));
List<TrialOrder> trialOrders = trialOrderMapper.selectList(qw);
if (!trialOrders.isEmpty()) {
// 创建Excel工作簿
try (Workbook workbook = new XSSFWorkbook()) {
// 创建工作表
Sheet sheet = workbook.createSheet("Trial Orders");
// 创建标题行
Row headerRow = sheet.createRow(0);
headerRow.createCell(0).setCellValue("ID");
headerRow.createCell(1).setCellValue("Title");
headerRow.createCell(2).setCellValue("Surname");
headerRow.createCell(3).setCellValue("Given Name");
headerRow.createCell(4).setCellValue("Username");
headerRow.createCell(5).setCellValue("Email");
headerRow.createCell(6).setCellValue("Country");
headerRow.createCell(7).setCellValue("Occupation");
headerRow.createCell(8).setCellValue("Create Time");
headerRow.createCell(9).setCellValue("Update Time");
headerRow.createCell(10).setCellValue("Status");
// 填充数据
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
int rowNum = 1;
for (TrialOrder trialOrder : trialOrders) {
Row row = sheet.createRow(rowNum++);
row.createCell(0).setCellValue(trialOrder.getId());
row.createCell(1).setCellValue(trialOrder.getTitle());
row.createCell(2).setCellValue(trialOrder.getSurname());
row.createCell(3).setCellValue(trialOrder.getGivenName());
row.createCell(4).setCellValue(trialOrder.getUserName());
row.createCell(5).setCellValue(trialOrder.getEmail());
row.createCell(6).setCellValue(trialOrder.getCountry());
row.createCell(7).setCellValue(trialOrder.getOccupation());
row.createCell(8).setCellValue(trialOrder.getCreateTime().format(formatter));
row.createCell(9).setCellValue(trialOrder.getUpdateTime().format(formatter));
row.createCell(10).setCellValue(trialOrder.getStatus());
}
// 保存Excel文件
String fileName = "trialOrder-" + yesterday.format(DateTimeFormatter.ofPattern("yyyyMMdd")) + ".xlsx";
try (FileOutputStream fileOut = new FileOutputStream(fileName)) {
workbook.write(fileOut);
SendEmailUtil.sendExcelEmail("1023316923@qq.com", null, Files.readAllBytes(Paths.get(fileName)), fileName);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

View File

@@ -9,12 +9,14 @@ import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.ses.v20201002.SesClient;
import com.tencentcloudapi.ses.v20201002.models.Attachment;
import com.tencentcloudapi.ses.v20201002.models.SendEmailRequest;
import com.tencentcloudapi.ses.v20201002.models.SendEmailResponse;
import com.tencentcloudapi.ses.v20201002.models.Template;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import java.util.Base64;
import java.util.Date;
/**
@@ -130,7 +132,8 @@ public class SendEmailUtil {
private final static Long YOUR_TRIAL_TEMPLATE_ID = 117214L;
private final static Long APPROVAL_TEMPLATE_ID = 117215L;
private final static Long NOTIFICATION_TEMPLATE_ID = 117216L;
public static void sendCustomEmail(String receiverAddress, String senderAddress, TrialOrder trialOrder, int emailType) {
private final static Long NOTIFICATION_CHINESE_TEMPLATE_ID = 122229L;
public static void sendCustomEmail(String receiverAddress, String senderAddress, TrialOrder trialOrder, int emailType, String country) {
try {
// 实例化一个认证对象
Credential cred = new Credential(SECRET_ID, SECRET_KEy);
@@ -162,7 +165,11 @@ public class SendEmailUtil {
break;
case 3:
subject = "Approval Confirmation for AiDA System Trial Access";
template.setTemplateID(NOTIFICATION_TEMPLATE_ID);
if (country.equals("China")) {
template.setTemplateID(NOTIFICATION_CHINESE_TEMPLATE_ID);
}else {
template.setTemplateID(NOTIFICATION_TEMPLATE_ID);
}
template.setTemplateData(buildNotificationData(trialOrder));
break;
default:
@@ -180,6 +187,45 @@ public class SendEmailUtil {
throw new BusinessException("failed.to.send.mail");
}
}
public static void sendExcelEmail(String receiverAddress, String senderAddress, byte[] fileContent, String fileName) {
try {
// 实例化一个认证对象
Credential cred = new Credential(SECRET_ID, SECRET_KEy);
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint("ses.tencentcloudapi.com");
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
SesClient client = new SesClient(cred, "ap-hongkong", clientProfile);
SendEmailRequest req = new SendEmailRequest();
if (StringUtils.isEmpty(senderAddress)) {
senderAddress = SEND_ADDRESS;
}
req.setFromEmailAddress(senderAddress);
req.setDestination(new String[]{receiverAddress});
// 根据邮件类型设置不同的主题和模板
String subject = "";
Template template = new Template();
subject = "昨日试用订单数据";
template.setTemplateID(YOUR_TRIAL_TEMPLATE_ID);
req.setSubject(subject);
req.setTemplate(template);
Attachment attachment = new Attachment();
attachment.setFileName(fileName); // 设置附件文件名
// 设置附件内容
attachment.setContent(Base64.getEncoder().encodeToString(fileContent));
req.setAttachments(new Attachment[] {attachment});
// 发送邮件
SendEmailResponse resp = client.SendEmail(req);
log.info("短信发送结果res###{}", SendEmailResponse.toJsonString(resp));
} catch (TencentCloudSDKException e) {
log.info("邮件发送失败###{}", e.toString());
throw new BusinessException("failed.to.send.mail");
}
}
private final static Long WILLBEEXPIRED_TEMPLATE_ID = 118178L;
public static void sendWillBeExpiredEmail(Account account, String senderAddress) {
try {

View File

@@ -9,9 +9,7 @@ import com.ai.da.common.response.ResultEnum;
import com.ai.da.common.security.jwt.JWTTokenHelper;
import com.ai.da.common.utils.*;
import com.ai.da.mapper.AccountMapper;
import com.ai.da.mapper.LibraryMapper;
import com.ai.da.mapper.TrialOrderMapper;
import com.ai.da.mapper.UserLikeGroupMapper;
import com.ai.da.mapper.entity.Account;
import com.ai.da.mapper.entity.AccountLoginLog;
import com.ai.da.mapper.entity.TrialOrder;
@@ -32,7 +30,6 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
@@ -504,9 +501,9 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
trialOrder.setStatus(0);
trialOrder.setIp(ipAddress);
trialOrderMapper.insert(trialOrder);
SendEmailUtil.sendCustomEmail("1023316923@qq.com", null, trialOrder,1);
SendEmailUtil.sendCustomEmail("calvinwong@aidlab.hk", null, trialOrder,1);
SendEmailUtil.sendCustomEmail("kaicpang.pang@connect.polyu.hk", null, trialOrder,1);
// SendEmailUtil.sendCustomEmail("1023316923@qq.com", null, trialOrder,1, trialOrder.getCountry());
// SendEmailUtil.sendCustomEmail("calvinwong@aidlab.hk", null, trialOrder,1, trialOrder.getCountry());
// SendEmailUtil.sendCustomEmail("kaicpang.pang@connect.polyu.hk", null, trialOrder,1, trialOrder.getCountry());
// 判断当前的试用订单是否自动批准
if (AutoApproved.getStatus()) {
// 改变试用订单状态,新增试用用户
@@ -534,10 +531,14 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
accountMapper.insert(account);
}
// 发送邮件提醒用户试用用户已创建
SendEmailUtil.sendCustomEmail("1023316923@qq.com", null, trialOrder,2);
SendEmailUtil.sendCustomEmail("calvinwong@aidlab.hk", null, trialOrder,2);
SendEmailUtil.sendCustomEmail("kaicpang.pang@connect.polyu.hk", null, trialOrder,2);
SendEmailUtil.sendCustomEmail(account.getUserEmail(), null, trialOrder, 3);
// SendEmailUtil.sendCustomEmail("1023316923@qq.com", null, trialOrder,2, trialOrder.getCountry());
// SendEmailUtil.sendCustomEmail("calvinwong@aidlab.hk", null, trialOrder,2, trialOrder.getCountry());
// SendEmailUtil.sendCustomEmail("kaicpang.pang@connect.polyu.hk", null, trialOrder,2, trialOrder.getCountry());
if (trialOrder.getCountry().equals("China")) {
SendEmailUtil.sendCustomEmail(account.getUserEmail(), null, trialOrder, 3, trialOrder.getCountry());
}else {
SendEmailUtil.sendCustomEmail(account.getUserEmail(), null, trialOrder, 3, trialOrder.getCountry());
}
}
return Boolean.TRUE;
}
@@ -584,10 +585,14 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
accountMapper.insert(account);
}
// 发送邮件提醒用户试用用户已创建
SendEmailUtil.sendCustomEmail("1023316923@qq.com", null, trialOrder,2);
SendEmailUtil.sendCustomEmail("calvinwong@aidlab.hk", null, trialOrder,2);
SendEmailUtil.sendCustomEmail("kaicpang.pang@connect.polyu.hk", null, trialOrder,2);
SendEmailUtil.sendCustomEmail(account.getUserEmail(), null, trialOrder, 3);
// SendEmailUtil.sendCustomEmail("1023316923@qq.com", null, trialOrder,2, trialOrder.getCountry());
// SendEmailUtil.sendCustomEmail("calvinwong@aidlab.hk", null, trialOrder,2, trialOrder.getCountry());
// SendEmailUtil.sendCustomEmail("kaicpang.pang@connect.polyu.hk", null, trialOrder,2, trialOrder.getCountry());
if (trialOrder.getCountry().equals("China")) {
SendEmailUtil.sendCustomEmail(account.getUserEmail(), null, trialOrder, 3, trialOrder.getCountry());
}else {
SendEmailUtil.sendCustomEmail(account.getUserEmail(), null, trialOrder, 3, trialOrder.getCountry());
}
}
return Boolean.TRUE;
}