Commit 94617f21 by 章家宝

feat:【优惠券需求】优惠券定时任务 - 发送短信通知

1 parent c2f594e5
...@@ -119,6 +119,15 @@ public interface CouponAccountRepository extends BaseRepository<CouponAccount, L ...@@ -119,6 +119,15 @@ public interface CouponAccountRepository extends BaseRepository<CouponAccount, L
List<CouponAccount> findByStatusAndDeleteFlag(Integer status, String deleteFlag); List<CouponAccount> findByStatusAndDeleteFlag(Integer status, String deleteFlag);
/** /**
* 根据通知状态和删除标识查询优惠券列表
*
* @param notificationStatus 通知状态 (0:待通知;1:已通知;2:已忽略)
* @param deleteFlag 删除标识
* @return 优惠券列表
*/
List<CouponAccount> findByNotificationStatusAndDeleteFlag(Integer notificationStatus, String deleteFlag);
/**
* 统计指定时间范围内发放的优惠券数量(根据创建时间) * 统计指定时间范围内发放的优惠券数量(根据创建时间)
* *
* @param startTime 开始时间 * @param startTime 开始时间
......
...@@ -542,7 +542,7 @@ public class CouponActivityService { ...@@ -542,7 +542,7 @@ public class CouponActivityService {
if (!isExpiredOrReturned || !isManualDistribution) { if (!isExpiredOrReturned || !isManualDistribution) {
// 不满足显示条件,按钮不显示(canReissue设为null表示不显示) // 不满足显示条件,按钮不显示(canReissue设为null表示不显示)
vo.setCanReissue(null); vo.setCanReissue(false);
vo.setReissueDenyReason(null); vo.setReissueDenyReason(null);
return; return;
} }
......
...@@ -68,3 +68,5 @@ SMS.INSPECTION.WO.UPDATE=\u68c0\u6d4b\u5de5\u5355{0}\u4efb\u52a1\u5df2\u66f4\u65 ...@@ -68,3 +68,5 @@ SMS.INSPECTION.WO.UPDATE=\u68c0\u6d4b\u5de5\u5355{0}\u4efb\u52a1\u5df2\u66f4\u65
SMS.INSPECTION.WO.UPDATE.INSPECTOR_NEW=\u60a8\u597d\uff0c\u60a8\u5df2\u88ab\u66f4\u65b0\u4e3a\u68c0\u6d4b\u4eba\u5458\u4fe1\u606f\uff0c\u5de5\u5355\u53f7{0}\u3002\u540e\u7eed\u8bf7\u6309\u8981\u6c42\u53ca\u65f6\u5f00\u5c55\u68c0\u6d4b\u5de5\u4f5c\uff0c\u4fdd\u6301\u901a\u8baf\u7545\u901a\u3002 SMS.INSPECTION.WO.UPDATE.INSPECTOR_NEW=\u60a8\u597d\uff0c\u60a8\u5df2\u88ab\u66f4\u65b0\u4e3a\u68c0\u6d4b\u4eba\u5458\u4fe1\u606f\uff0c\u5de5\u5355\u53f7{0}\u3002\u540e\u7eed\u8bf7\u6309\u8981\u6c42\u53ca\u65f6\u5f00\u5c55\u68c0\u6d4b\u5de5\u4f5c\uff0c\u4fdd\u6301\u901a\u8baf\u7545\u901a\u3002
SMS.INSPECTION.WO.UPDATE.INSPECTOR_ORIGIN=\u60a8\u597d\uff0c\u7cfb\u7edf\u5df2\u66f4\u65b0\u68c0\u6d4b\u4eba\u5458\u540d\u5355\uff0c\u60a8\u4ecd\u4fdd\u7559\u68c0\u6d4b\u4eba\u5458\u8d44\u683c\uff0c\u8bf7\u7ee7\u7eed\u6309\u89c4\u8303\u5f00\u5c55\u68c0\u6d4b\u5de5\u4f5c\uff0c\u4fdd\u6301\u901a\u8baf\u7545\u901a\uff0c\u5982\u6709\u53d8\u52a8\u5c06\u53e6\u884c\u901a\u77e5\u3002 SMS.INSPECTION.WO.UPDATE.INSPECTOR_ORIGIN=\u60a8\u597d\uff0c\u7cfb\u7edf\u5df2\u66f4\u65b0\u68c0\u6d4b\u4eba\u5458\u540d\u5355\uff0c\u60a8\u4ecd\u4fdd\u7559\u68c0\u6d4b\u4eba\u5458\u8d44\u683c\uff0c\u8bf7\u7ee7\u7eed\u6309\u89c4\u8303\u5f00\u5c55\u68c0\u6d4b\u5de5\u4f5c\uff0c\u4fdd\u6301\u901a\u8baf\u7545\u901a\uff0c\u5982\u6709\u53d8\u52a8\u5c06\u53e6\u884c\u901a\u77e5\u3002
SMS.INSPECTION.WO.REJECT=\u60a8\u63d0\u4ea4\u7684\u68c0\u6d4b\u5de5\u5355\u7f16\u53f7\uff1a{0}\u5df2\u9a73\u56de\uff0c\u8bf7\u67e5\u770b SMS.INSPECTION.WO.REJECT=\u60a8\u63d0\u4ea4\u7684\u68c0\u6d4b\u5de5\u5355\u7f16\u53f7\uff1a{0}\u5df2\u9a73\u56de\uff0c\u8bf7\u67e5\u770b
SMS.COUPON.SINGLE=\u5c0a\u656c\u7684\u8f66\u5546\u7528\u6237\uff0c\u60a8\u67091\u5f20\u3010{0}\u3011\u3010{1}\u5143\u3011\u5df2\u5230\u8d26\uff01\u8bf7\u5c3d\u5feb\u4f7f\u7528\uff0c\u597d\u8f66\u4e0d\u7b49\u4eba\uff0c\u53c2\u62cd\u5373\u53ef\u4f7f\u7528\u3002
SMS.COUPON.MULTIPLE=\u5c0a\u656c\u7684\u8f66\u5546\u7528\u6237\uff0c\u60a8\u6709\u591a\u5f20\u4f18\u60e0\u5238\u5df2\u5230\u8d26\uff0c\u6700\u5927\u9762\u989d\u3010{0}\u5143\u3011\uff01\u8bf7\u5c3d\u5feb\u4f7f\u7528\uff0c\u597d\u8f66\u4e0d\u7b49\u4eba\uff0c\u53c2\u62cd\u5373\u53ef\u4f7f\u7528\u3002
package com.glsx.usedcar.module.coupon.task; package com.glsx.usedcar.task;
import com.glsx.usedcar.gsc.GscApiInvoker;
import com.glsx.usedcar.module.account.dao.AccountDao;
import com.glsx.usedcar.module.account.entity.Account;
import com.glsx.usedcar.module.coupon.dao.CouponAccountRepository; import com.glsx.usedcar.module.coupon.dao.CouponAccountRepository;
import com.glsx.usedcar.module.coupon.dao.CouponActivityRepository; import com.glsx.usedcar.module.coupon.dao.CouponActivityRepository;
import com.glsx.usedcar.module.coupon.dao.CouponPopupNotificationRepository;
import com.glsx.usedcar.module.coupon.entity.CouponAccount; import com.glsx.usedcar.module.coupon.entity.CouponAccount;
import com.glsx.usedcar.module.coupon.entity.CouponActivity; import com.glsx.usedcar.module.coupon.entity.CouponActivity;
import com.glsx.usedcar.module.coupon.entity.CouponPopupNotification;
import com.glsx.usedcar.module.coupon.enums.CouponActivityStatusEnum; import com.glsx.usedcar.module.coupon.enums.CouponActivityStatusEnum;
import com.glsx.usedcar.module.coupon.enums.CouponUserStatusEnum; import com.glsx.usedcar.module.coupon.enums.CouponUserStatusEnum;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.math.BigDecimal;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
/** /**
* 优惠券定时任务 * 优惠券定时任务
...@@ -26,6 +38,10 @@ public class CouponScheduledTask { ...@@ -26,6 +38,10 @@ public class CouponScheduledTask {
private final CouponActivityRepository couponActivityRepository; private final CouponActivityRepository couponActivityRepository;
private final CouponAccountRepository couponAccountRepository; private final CouponAccountRepository couponAccountRepository;
private final CouponPopupNotificationRepository couponPopupNotificationRepository;
private final AccountDao accountDao;
private final GscApiInvoker gscApiInvoker;
private final MessageSource messageSource;
/** /**
* 每天凌晨 2 点执行 * 每天凌晨 2 点执行
...@@ -120,4 +136,128 @@ public class CouponScheduledTask { ...@@ -120,4 +136,128 @@ public class CouponScheduledTask {
log.error("【定时任务】过期弹窗通知清理失败", e); log.error("【定时任务】过期弹窗通知清理失败", e);
} }
} }
/**
* 每小时整点执行一次
* 发送优惠券到账短信通知(仅发送给未读弹窗的用户)
*/
@Scheduled(cron = "0 0 * * * ?")
@Transactional(rollbackFor = Exception.class)
public void sendCouponSmsNotification() {
log.info("【定时任务】开始执行优惠券到账短信通知任务");
LocalDateTime now = LocalDateTime.now();
// 查询当前小时前1小时内发放的优惠券(整点执行,查询上一个小时发放的)
LocalDateTime startTime = now.minusHours(1).withMinute(0).withSecond(0).withNano(0);
LocalDateTime endTime = startTime.plusHours(1);
int smsCount = 0;
try {
// 1. 查询上一个小时内发放的、待通知状态的优惠券
List<CouponAccount> pendingCoupons = couponAccountRepository
.findByNotificationStatusAndDeleteFlag(0, "N");
if (CollectionUtils.isEmpty(pendingCoupons)) {
log.info("【定时任务】没有待通知的优惠券,跳过短信发送");
return;
}
// 过滤出上一个小时内发放的优惠券
List<CouponAccount> recentCoupons = pendingCoupons.stream()
.filter(coupon -> coupon.getCreateTime() != null
&& !coupon.getCreateTime().isBefore(startTime)
&& coupon.getCreateTime().isBefore(endTime))
.collect(Collectors.toList());
if (CollectionUtils.isEmpty(recentCoupons)) {
log.info("【定时任务】上一个小时内没有新发放的优惠券,跳过短信发送");
return;
}
// 2. 按用户分组
Map<Integer, List<CouponAccount>> couponGroupByAccount = recentCoupons.stream()
.collect(Collectors.groupingBy(CouponAccount::getAccountId));
// 3. 遍历每个用户,检查是否已读弹窗,未读则发送短信
for (Map.Entry<Integer, List<CouponAccount>> entry : couponGroupByAccount.entrySet()) {
Integer accountId = entry.getKey();
List<CouponAccount> coupons = entry.getValue();
try {
// 检查用户是否有已读的弹窗通知(状态为2:已确认)
List<CouponPopupNotification> readNotifications = couponPopupNotificationRepository
.findByAccountIdAndStatusAndDeleteFlag(accountId, 2, "N");
// 如果用户已读弹窗,则跳过短信发送
if (!CollectionUtils.isEmpty(readNotifications)) {
log.info("用户已读弹窗通知,跳过短信发送:accountId={}", accountId);
continue;
}
// 获取用户信息
Account account = accountDao.findTopByAccountId(accountId);
if (account == null || account.getMobile() == null) {
log.warn("用户不存在或手机号为空:accountId={}", accountId);
continue;
}
// 发送短信
boolean sent = sendCouponSms(account, coupons);
if (sent) {
smsCount++;
// 更新优惠券通知状态为已通知
for (CouponAccount coupon : coupons) {
coupon.setNotificationStatus(1); // 1:已通知
coupon.setNotificationTime(now);
couponAccountRepository.save(coupon);
}
}
} catch (Exception e) {
log.error("发送优惠券短信通知失败:accountId={}", accountId, e);
}
}
log.info("【定时任务】优惠券到账短信通知完成,发送数量={}", smsCount);
} catch (Exception e) {
log.error("【定时任务】优惠券到账短信通知任务失败", e);
}
}
/**
* 发送优惠券到账短信
*
* @param account 用户账户
* @param coupons 优惠券列表
* @return 是否发送成功
*/
private boolean sendCouponSms(Account account, List<CouponAccount> coupons) {
if (CollectionUtils.isEmpty(coupons)) {
return false;
}
String mobile = account.getMobile();
String message;
if (coupons.size() == 1) {
// 单张优惠券短信模板
CouponAccount coupon = coupons.get(0);
Object[] args = {coupon.getTemplateName(), coupon.getFaceAmount()};
message = messageSource.getMessage("SMS.COUPON.SINGLE", args, Locale.CHINESE);
} else {
// 多张优惠券短信模板,取最大面额
BigDecimal maxAmount = coupons.stream()
.map(CouponAccount::getFaceAmount)
.filter(Objects::nonNull)
.max(BigDecimal::compareTo)
.orElse(BigDecimal.ZERO);
Object[] args = {maxAmount};
message = messageSource.getMessage("SMS.COUPON.MULTIPLE", args, Locale.CHINESE);
}
log.info("发送优惠券到账短信: 手机号={}, 内容={}", mobile, message);
return gscApiInvoker.sendMsg(mobile, message);
}
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!