Merge branch 'dev/dev_xp' into dev/3.1_release_merge

This commit is contained in:
2025-11-17 16:56:50 +08:00
11 changed files with 302 additions and 72 deletions

View File

@@ -87,9 +87,9 @@ public class PaymentTask {
// !!关闭此定时器,改为提前三天站内信提醒!!
// 提前7天向用户发送提醒邮件,每天早上8点执行
// @Scheduled(cron = "0 0 8 * * ?")
public void subscriptionReminder(){
stripeService.subscriptionReminder();
}
// public void subscriptionReminder(){
// stripeService.subscriptionReminder();
// }
// 如果有订阅已创建,但是没有发邮件通知的,需要主动获取回调信息并向用户发送邮件

View File

@@ -0,0 +1,129 @@
package com.ai.da.common.task;
import com.ai.da.common.utils.SendEmailUtil;
import com.ai.da.mapper.primary.AccountMapper;
import com.ai.da.mapper.primary.SubscriptionInfoMapper;
import com.ai.da.mapper.primary.entity.Account;
import com.ai.da.mapper.primary.entity.SubscriptionInfo;
import com.ai.da.service.StripeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@RequiredArgsConstructor
public class SubscriptionReminderTask {
public final AccountMapper accountMapper;
public final SubscriptionInfoMapper subscriptionInfoMapper;
public final StripeService stripeService;
// 订阅类型与提前天数的映射配置
private static final Map<String, Integer> REMINDER_DAYS_CONFIG = new HashMap<>();
static {
REMINDER_DAYS_CONFIG.put("monthly", 7);
REMINDER_DAYS_CONFIG.put("yearly", 14);
}
public void subscriptionReminder() {
// 获取所有需要通知的订阅
List<SubscriptionInfo> subscriptionInfos = getDueSubscriptions();
for (SubscriptionInfo subscriptionInfo : subscriptionInfos) {
Integer daysBefore = REMINDER_DAYS_CONFIG.get(subscriptionInfo.getType());
if (daysBefore == null) {
log.warn("未知的订阅类型: {}", subscriptionInfo.getType());
continue;
}
boolean success = stripeService.sendEmail(subscriptionInfo.getSubscriptionId(), "reminder_subscriber", null);
if (success) {
log.info("提前{}天向用户 {} 发送续订通知邮件,订阅类型: {}",
daysBefore, subscriptionInfo.getAccountId(), subscriptionInfo.getType());
}
}
}
private List<SubscriptionInfo> getDueSubscriptions() {
List<SubscriptionInfo> results = new ArrayList<>();
for (Map.Entry<String, Integer> entry : REMINDER_DAYS_CONFIG.entrySet()) {
String subscriptionType = entry.getKey();
int daysBefore = entry.getValue();
results.addAll(getSubscriptionsDueInDays(subscriptionType, daysBefore));
}
return results;
}
private List<SubscriptionInfo> getSubscriptionsDueInDays(String subscriptionType, int daysBefore) {
LocalDateTime targetDate = LocalDateTime.now().plusDays(daysBefore);
LocalDateTime startOfDay = targetDate.toLocalDate().atStartOfDay();
LocalDateTime endOfDay = targetDate.toLocalDate().atTime(23, 59, 59);
long startTimestamp = startOfDay.toEpochSecond(ZoneOffset.UTC);
long endTimestamp = endOfDay.toEpochSecond(ZoneOffset.UTC);
QueryWrapper<SubscriptionInfo> qw = new QueryWrapper<>();
qw.ge("current_period_end", startTimestamp);
qw.lt("current_period_end", endTimestamp);
qw.eq("status", "active");
qw.eq("subscription_type", subscriptionType);
return subscriptionInfoMapper.selectList(qw);
}
public void trialReminder() {
// 今天的 00:00:00 和 23:59:59
LocalDateTime startOfDay = LocalDateTime.now().toLocalDate().atStartOfDay();
LocalDateTime endOfDay = LocalDateTime.now().toLocalDate().atTime(23, 59, 59);
// 转为时间戳
long startTimestamp = startOfDay.toEpochSecond(ZoneOffset.UTC);
long endTimestamp = endOfDay.toEpochSecond(ZoneOffset.UTC);
QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
queryWrapper.lambda().gt(Account::getValidEndTime, startTimestamp)
.lt(Account::getValidEndTime, endTimestamp)
.eq(Account::getSystemUser, 3);
List<Account> accounts = accountMapper.selectList(queryWrapper);
for (Account account : accounts) {
String language = stripeService.getLanguage(account.getLanguage(), account.getCountry(), "reminder_trial");
SendEmailUtil.subscriptionEmailReminder("reminder_trial", null, language, account.getUserEmail());
}
}
/* public void subscriptionReminder(){
// 提前7天的 00:00:00 和 23:59:59
LocalDateTime startOfDay = LocalDateTime.now().plusDays(7).toLocalDate().atStartOfDay();
LocalDateTime endOfDay = LocalDateTime.now().plusDays(7).toLocalDate().atTime(23, 59, 59);
// 转为时间戳
long startTimestamp = startOfDay.toEpochSecond(ZoneOffset.UTC);
long endTimestamp = endOfDay.toEpochSecond(ZoneOffset.UTC);
QueryWrapper<SubscriptionInfo> qw = new QueryWrapper<>();
qw.ge("current_period_end", startTimestamp);
qw.lt("current_period_end", endTimestamp);
qw.eq("status", "active");
List<SubscriptionInfo> subscriptionInfos = subscriptionInfoMapper.selectList(qw);
for (SubscriptionInfo subscriptionInfo : subscriptionInfos) {
boolean b = sendEmail(subscriptionInfo.getSubscriptionId(), "reminder", null);
if (b) log.info("提前7天向用户 {} 发送续订通知邮件", subscriptionInfo.getAccountId());
}
}*/
}

View File

@@ -828,15 +828,31 @@ public class SendEmailUtil {
templateUser.setTemplateID(RENEWAL_USER_CN);
}
break;
case "reminder":
case "reminder_subscriber":
if (language.equals("ENGLISH")) {
user.setSubject("[Code-Create] AiDA Subscription Renewal Reminder");
templateUser.setTemplateID(RENEWAL_REMINDER_USER_EN);
} else {
templateUser.setTemplateID(156072L);
} else if (language.equals("CHINESE")){
user.setSubject("[Code-Create] AiDA续订提醒");
templateUser.setTemplateID(RENEWAL_REMINDER_USER_CN);
templateUser.setTemplateID(156073L);
} else {
user.setSubject("[Code-Create] AiDA續訂提醒");
templateUser.setTemplateID(156074L);
}
break;
case "reminder_trial":
if (language.equals("ENGLISH")) {
user.setSubject("[Code-Create] AiDA — Free Trial Ending");
templateUser.setTemplateID(156075L);
} else if (language.equals("CHINESE")){
user.setSubject("[Code-Create] AiDA — 免费试用结束提醒");
templateUser.setTemplateID(156076L);
} else {
user.setSubject("[Code-Create] AiDA — 免費試用結束提醒");
templateUser.setTemplateID(156077L);
}
break;
default:
log.error("unknown subscription email type");
return false;
@@ -855,7 +871,7 @@ public class SendEmailUtil {
SendEmailResponse respUser = client.SendEmail(user);
log.info("邮件主题:{}发送结果toUser###{}", user.getSubject(), SendEmailResponse.toJsonString(respUser));
}
if (!type.equals("reminder")) {
if (!type.startsWith("reminder")) {
SendEmailResponse respMerchant = client.SendEmail(merchant);
log.info("邮件主题:{}发送结果toMerchant###{}", merchant.getSubject(), SendEmailResponse.toJsonString(respMerchant));
}