礼金过期

This commit is contained in:
雷欧(林平凡)
2025-06-13 15:17:47 +08:00
parent 0a503e04fc
commit 8635c05565
2 changed files with 62 additions and 1 deletions

View File

@@ -7,6 +7,8 @@ import cn.van.business.model.pl.Comment;
import cn.van.business.repository.CommentRepository;
import cn.van.business.repository.OrderRowRepository;
import cn.van.business.util.jdReq.*;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.util.DateUtils;
import com.jd.open.api.sdk.DefaultJdClient;
import com.jd.open.api.sdk.JdClient;
@@ -26,7 +28,10 @@ import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;
@@ -53,6 +58,7 @@ public class JDScheduleJob {
private final OrderUtil orderUtil;
private final JDUtil jdUtil;
private final CommentRepository commentRepository;
private final WXUtil wxUtil;
@Getter
@@ -65,12 +71,13 @@ public class JDScheduleJob {
// 构造函数中注入StringRedisTemplate
@Autowired
public JDScheduleJob(StringRedisTemplate redisTemplate, OrderRowRepository orderRowRepository, OrderUtil orderUtil, JDUtil jdUtil, CommentRepository commentRepository) {
public JDScheduleJob(WXUtil wxUtil, StringRedisTemplate redisTemplate, OrderRowRepository orderRowRepository, OrderUtil orderUtil, JDUtil jdUtil, CommentRepository commentRepository) {
this.redisTemplate = redisTemplate;
this.orderRowRepository = orderRowRepository;
this.orderUtil = orderUtil;
this.jdUtil = jdUtil;
this.commentRepository = commentRepository;
this.wxUtil = wxUtil;
}
/**
@@ -541,4 +548,40 @@ public class JDScheduleJob {
hashOps.putAll(key, hours.stream().collect(Collectors.toMap(h -> h, h -> "1")));
}
@Scheduled(cron = "0 0 0 * * ?") // 每天 0 点执行
public void checkGiftCouponsExpiry() {
Set<String> keys = redisTemplate.keys("gift_coupon:*");
if (keys == null || keys.isEmpty()) return;
for (String key : keys) {
Map<Object, Object> entries = redisTemplate.opsForHash().entries(key);
if (entries.isEmpty()) continue;
for (Map.Entry<Object, Object> entry : entries.entrySet()) {
String giftJson = (String) entry.getValue();
JSONObject gift = JSON.parseObject(giftJson);
String giftKey = gift.getString("giftKey");
String skuName = gift.getString("skuName");
String owner = gift.getString("owner");
LocalDateTime expireTime = LocalDateTime.parse(gift.getString("expireTime"), DateTimeFormatter.ISO_DATE_TIME);
boolean isAboutToExpire = false;
if ("g".equals(owner)) {
// 自营:当天过期
isAboutToExpire = !expireTime.isAfter(LocalDateTime.now().with(LocalTime.MAX));
} else if ("p".equals(owner)) {
// POP7天后过期提前一天提醒
isAboutToExpire = ChronoUnit.HOURS.between(LocalDateTime.now(), expireTime) <= 24;
}
if (isAboutToExpire) {
String message = String.format("[礼金提醒]\n商品%s\n礼金Key%s\n类型%s\n将在 %s 过期", skuName, giftKey, "g".equals(owner) ? "自营" : "POP", expireTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")));
wxUtil.sendTextMessage(WXUtil.default_super_admin_wxid, message, 1, "bot", false);
}
}
}
}
}

View File

@@ -947,6 +947,9 @@ public class JDUtil {
result.append(" ").append(skuName).append(" 礼金创建成功\n");
if (giftKey == null) {
result.append(" ").append(skuName).append(" 礼金创建失败但是转链成功\n");
}else {
// 将开通成功的怼进去redis 做一个定时调度去扫描如果礼金今天会过期就发送通知需要判断是自营还是poppop礼金有效期是7天
saveGiftCouponToRedis(skuId, giftKey, skuName, owner);
}
} else {
@@ -966,7 +969,22 @@ public class JDUtil {
cacheMap.remove("finalWenAn" + wxid);
}
}
/*
将礼金信息写入 Redis **/
public void saveGiftCouponToRedis(String skuId, String giftKey, String skuName, String owner) {
String key = "gift_coupon:" + skuId ;
String hashKey = giftKey;
LocalDateTime expireTime = LocalDateTime.now().plus(owner.equals("g") ? 0 : 7, ChronoUnit.DAYS);
Map<String, Object> data = new HashMap<>();
data.put("giftKey", giftKey);
data.put("skuName", skuName);
data.put("owner", owner);
data.put("expireTime", expireTime.format(DateTimeFormatter.ISO_DATE_TIME));
// 存入 Redis Hash
redisTemplate.opsForHash().put(key, hashKey, JSON.toJSONString(data));
}
/**
* 处理用户输入的推广方案内容
*