|
@@ -0,0 +1,60 @@
|
|
|
|
|
+package com.etotem.cfc.task;
|
|
|
|
|
+
|
|
|
|
|
+import com.etotem.cfc.entity.Coupon;
|
|
|
|
|
+import com.etotem.cfc.service.CouponService;
|
|
|
|
|
+import com.etotem.cfc.service.MembershipService;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
+
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
|
+import java.time.LocalDate;
|
|
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 会员优惠券周期性补发任务
|
|
|
|
|
+ * - 每天 5:00 执行,给有效会员按 grant_period 补发 PERIODIC 券
|
|
|
|
|
+ */
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+@Component
|
|
|
|
|
+public class CouponGrantTask {
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private CouponService couponService;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private MembershipService membershipService;
|
|
|
|
|
+
|
|
|
|
|
+ @Scheduled(cron = "0 0 5 * * ?")
|
|
|
|
|
+ public void grantPeriodicCoupons() {
|
|
|
|
|
+ log.info("开始执行周期性会员券补发任务");
|
|
|
|
|
+ try {
|
|
|
|
|
+ List<Coupon> templates = couponService.listPeriodicTemplates();
|
|
|
|
|
+ if (templates.isEmpty()) {
|
|
|
|
|
+ log.info("无PERIODIC券模板,跳过");
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ LocalDate now = LocalDate.now();
|
|
|
|
|
+ String monthKey = now.format(DateTimeFormatter.ofPattern("yyyy-MM"));
|
|
|
|
|
+ int quarter = (now.getMonthValue() - 1) / 3 + 1;
|
|
|
|
|
+ String quarterKey = now.getYear() + "-Q" + quarter;
|
|
|
|
|
+
|
|
|
|
|
+ for (Coupon c : templates) {
|
|
|
|
|
+ String periodKey = "QUARTERLY".equals(c.getGrantPeriod()) ? quarterKey : monthKey;
|
|
|
|
|
+ List<Long> userIds = membershipService.listActiveMemberUserIds(c.getGrantLevelCode());
|
|
|
|
|
+ int granted = 0;
|
|
|
|
|
+ for (Long uid : userIds) {
|
|
|
|
|
+ if (couponService.grant(uid, c.getId(), "PERIODIC", periodKey, null)) {
|
|
|
|
|
+ granted++;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ log.info("PERIODIC券补发: couponId={}, level={}, period={}, 发放用户数={}",
|
|
|
|
|
+ c.getId(), c.getGrantLevelCode(), periodKey, granted);
|
|
|
|
|
+ }
|
|
|
|
|
+ log.info("周期性会员券补发任务执行完成");
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("周期性会员券补发任务执行失败", e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|