重构第一版,没有明显bug

This commit is contained in:
Leo
2025-03-11 14:11:51 +08:00
parent 9aa02430fd
commit 2316951e7f
4 changed files with 101 additions and 70 deletions

View File

@@ -22,6 +22,7 @@ import lombok.Setter;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.HashOperations; import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
@@ -85,6 +86,12 @@ public class JDUtil {
// 添加ObjectMapper来序列化和反序列化UserInteractionState // 添加ObjectMapper来序列化和反序列化UserInteractionState
private final ObjectMapper objectMapper = new ObjectMapper(); private final ObjectMapper objectMapper = new ObjectMapper();
@Value("${isRunning.wx")
private boolean isRunning_wx = false;
@Value("isRunning.jd")
private boolean isRunning_jd = false;
// 构造函数中注入StringRedisTemplate // 构造函数中注入StringRedisTemplate
@Autowired @Autowired
public JDUtil(StringRedisTemplate redisTemplate, ProductOrderRepository productOrderRepository, OrderRowRepository orderRowRepository, WXUtil wxUtil, OrderUtil orderUtil) { public JDUtil(StringRedisTemplate redisTemplate, ProductOrderRepository productOrderRepository, OrderRowRepository orderRowRepository, WXUtil wxUtil, OrderUtil orderUtil) {
@@ -237,24 +244,27 @@ public class JDUtil {
*/ */
@Scheduled(cron = "0 * * * * ?") @Scheduled(cron = "0 * * * * ?")
public void fetchLatestOrder() { public void fetchLatestOrder() {
LocalDateTime now = LocalDateTime.now(); if (isRunning_jd) {
LocalDateTime startTime = now.minusMinutes(10).withSecond(0).withNano(0);
super_admins.values().parallelStream().forEach(admin -> { LocalDateTime now = LocalDateTime.now();
if (Util.isAnyEmpty(admin.getAppKey(), admin.getSecretKey())) return; LocalDateTime startTime = now.minusMinutes(10).withSecond(0).withNano(0);
try { super_admins.values().parallelStream().forEach(admin -> {
UnionOpenOrderRowQueryResponse response = fetchOrdersForDateTime(startTime, true, 1, true, admin.getAppKey(), admin.getSecretKey()); if (Util.isAnyEmpty(admin.getAppKey(), admin.getSecretKey())) return;
if (isValidResponse(response)) { try {
processOrderResponse(response, admin); UnionOpenOrderRowQueryResponse response = fetchOrdersForDateTime(startTime, true, 1, true, admin.getAppKey(), admin.getSecretKey());
if (isValidResponse(response)) {
processOrderResponse(response, admin);
}
} catch (RateLimitExceededException e) {
logger.warn("[限流] {} 请求频率受限", admin.getAppKey().substring(18));
} catch (Exception e) {
logger.error("{} 订单抓取异常: {}", admin.getAppKey().substring(18), e.getMessage());
} }
} catch (RateLimitExceededException e) { });
logger.warn("[限流] {} 请求频率受限", admin.getAppKey().substring(18)); }
} catch (Exception e) {
logger.error("{} 订单抓取异常: {}", admin.getAppKey().substring(18), e.getMessage());
}
});
} }
// 响应校验方法 // 响应校验方法
@@ -271,22 +281,25 @@ public class JDUtil {
* 扫描订单发送到微信 * 扫描订单发送到微信
* 每分钟的30秒执行一次 * 每分钟的30秒执行一次
*/ */
@Scheduled(cron = "10 * * * * ?") @Scheduled(cron = "3 * * * * ?")
public void sendOrderToWx() { public void sendOrderToWx() {
//long start = System.currentTimeMillis(); if (isRunning_wx) {
int[] validCodes = {-1}; //long start = System.currentTimeMillis();
// 只要三个月的,更多的也刷新不出来的 int[] validCodes = {-1};
Date threeMonthsAgo = Date.from(LocalDateTime.now().minusMonths(3).atZone(ZoneId.systemDefault()).toInstant()); // 只要三个月的,更多的也刷新不出来的
List<OrderRow> orderRows = orderRowRepository.findByValidCodeNotInAndOrderTimeGreaterThanOrderByOrderTimeDesc(validCodes, threeMonthsAgo); Date threeMonthsAgo = Date.from(LocalDateTime.now().minusMonths(3).atZone(ZoneId.systemDefault()).toInstant());
List<OrderRow> orderRows = orderRowRepository.findByValidCodeNotInAndOrderTimeGreaterThanOrderByOrderTimeDesc(validCodes, threeMonthsAgo);
for (OrderRow orderRow : orderRows) { for (OrderRow orderRow : orderRows) {
orderUtil.orderToWx(orderRow, true); orderUtil.orderToWx(orderRow, true);
}
//logger.info("扫描订单发送到微信耗时:{} ms, 订单数:{} ", System.currentTimeMillis() - start, orderRows.size());
} }
//logger.info("扫描订单发送到微信耗时:{} ms, 订单数:{} ", System.currentTimeMillis() - start, orderRows.size());
} }
/** /**
@@ -294,18 +307,21 @@ public class JDUtil {
*/ */
@Scheduled(cron = "0 0 */4 * * ?") @Scheduled(cron = "0 0 */4 * * ?")
public void fetchHistoricalOrders3090() { public void fetchHistoricalOrders3090() {
try { if (isRunning_jd) {
OrderFetchStrategy strategy = new Days3090Strategy(); try {
for (WXUtil.SuperAdmin admin : super_admins.values()) { OrderFetchStrategy strategy = new Days3090Strategy();
try { for (WXUtil.SuperAdmin admin : super_admins.values()) {
fetchOrders(strategy, admin.getAppKey(), admin.getSecretKey()); try {
} catch (Exception e) { fetchOrders(strategy, admin.getAppKey(), admin.getSecretKey());
logger.error("账号{}拉取异常: {}", admin.getAppKey().substring(18), e.getMessage()); } catch (Exception e) {
logger.error("账号{}拉取异常: {}", admin.getAppKey().substring(18), e.getMessage());
}
} }
} catch (Exception ex) {
logger.error("策略执行异常", ex);
} }
} catch (Exception ex) {
logger.error("策略执行异常", ex);
} }
} }
/** /**
@@ -313,17 +329,20 @@ public class JDUtil {
*/ */
@Scheduled(cron = "0 0 * * * ?") @Scheduled(cron = "0 0 * * * ?")
public void fetchHistoricalOrders1430() { public void fetchHistoricalOrders1430() {
try { if (isRunning_jd) {
OrderFetchStrategy strategy = new Days1430Strategy(); // 需补充Days1430Strategy实现
for (WXUtil.SuperAdmin admin : super_admins.values()) { try {
try { OrderFetchStrategy strategy = new Days1430Strategy(); // 需补充Days1430Strategy实现
fetchOrders(strategy, admin.getAppKey(), admin.getSecretKey()); for (WXUtil.SuperAdmin admin : super_admins.values()) {
} catch (Exception e) { try {
logger.error("账号{}拉取异常: {}", admin.getAppKey().substring(18), e.getMessage()); fetchOrders(strategy, admin.getAppKey(), admin.getSecretKey());
} catch (Exception e) {
logger.error("账号{}拉取异常: {}", admin.getAppKey().substring(18), e.getMessage());
}
} }
} catch (Exception ex) {
logger.error("1430策略执行异常", ex);
} }
} catch (Exception ex) {
logger.error("1430策略执行异常", ex);
} }
} }
@@ -332,41 +351,47 @@ public class JDUtil {
*/ */
@Scheduled(cron = "0 0 * * * ?") @Scheduled(cron = "0 0 * * * ?")
public void fetchHistoricalOrders0714() { public void fetchHistoricalOrders0714() {
try { if (isRunning_jd) {
OrderFetchStrategy strategy = new Days0714Strategy();
super_admins.values().parallelStream().forEach(admin -> { try {
if (Util.isAnyEmpty(admin.getAppKey(), admin.getSecretKey())) return; OrderFetchStrategy strategy = new Days0714Strategy();
try { super_admins.values().parallelStream().forEach(admin -> {
fetchOrders(strategy, admin.getAppKey(), admin.getSecretKey()); if (Util.isAnyEmpty(admin.getAppKey(), admin.getSecretKey())) return;
} catch (Exception e) { try {
logger.error("账号{}0714拉取异常: {}", admin.getAppKey().substring(18), e.getMessage()); fetchOrders(strategy, admin.getAppKey(), admin.getSecretKey());
} } catch (Exception e) {
}); logger.error("账号{}0714拉取异常: {}", admin.getAppKey().substring(18), e.getMessage());
} catch (Exception ex) { }
logger.error("0714策略执行异常", ex); });
} catch (Exception ex) {
logger.error("0714策略执行异常", ex);
}
} }
} }
@Scheduled(cron = "0 */5 * * * ?") @Scheduled(cron = "0 */5 * * * ?")
public void fetchHistoricalOrders0007() { public void fetchHistoricalOrders0007() {
try { if (isRunning_jd) {
OrderFetchStrategy strategy = new Days0007Strategy();
super_admins.values().parallelStream().forEach(admin -> {
if (Util.isAnyEmpty(admin.getAppKey(), admin.getSecretKey())) return;
try { try {
int count = fetchOrders(strategy, admin.getAppKey(), admin.getSecretKey()); OrderFetchStrategy strategy = new Days0007Strategy();
super_admins.values().parallelStream().forEach(admin -> {
if (Util.isAnyEmpty(admin.getAppKey(), admin.getSecretKey())) return;
logger.info("账号{} 0007订单拉取完成新增{}条", admin.getAppKey().substring(18), count); try {
} catch (RateLimitExceededException e) { int count = fetchOrders(strategy, admin.getAppKey(), admin.getSecretKey());
logger.warn("[限流] {} 0007请求受限", admin.getAppKey().substring(18));
} catch (Exception e) { logger.info("账号{} 0007订单拉取完成新增{}条", admin.getAppKey().substring(18), count);
logger.error("账号{}0007拉取异常: {}", admin.getAppKey().substring(18), e.getMessage()); } catch (RateLimitExceededException e) {
} logger.warn("[限流] {} 0007请求受限", admin.getAppKey().substring(18));
}); } catch (Exception e) {
} catch (Exception ex) { logger.error("账号{}0007拉取异常: {}", admin.getAppKey().substring(18), e.getMessage());
logger.error("0007策略执行异常", ex); }
});
} catch (Exception ex) {
logger.error("0007策略执行异常", ex);
}
} }
} }

View File

@@ -74,7 +74,7 @@ public class OrderUtil {
"状态:" + (converter.getCodeDescription(orderRow.getValidCode())) + "\r" "状态:" + (converter.getCodeDescription(orderRow.getValidCode())) + "\r"
+ "名称:" + orderRow.getSkuName().substring(0, 64) + "\r\n" + "名称:" + orderRow.getSkuName() + "\r\n"
//+ "商品单价:" + orderRow.getPrice() + "\r" //+ "商品单价:" + orderRow.getPrice() + "\r"
//+ "商品数量:" + orderRow.getSkuNum() + "\r" //+ "商品数量:" + orderRow.getSkuNum() + "\r"
//+ "商品总价:" + (orderRow.getPrice() * orderRow.getSkuNum()) + "\r" //+ "商品总价:" + (orderRow.getPrice() * orderRow.getSkuNum()) + "\r"

View File

@@ -60,3 +60,6 @@ rocketmq:
consume-thread-min: 20 # 消费线程池最小线程数 consume-thread-min: 20 # 消费线程池最小线程数
consume-thread-max: 64 # 消费线程池最大线程数 consume-thread-max: 64 # 消费线程池最大线程数
consume-message-batch-max-size: 64 # 批量消费最大消息数 consume-message-batch-max-size: 64 # 批量消费最大消息数
isRunning:
wx: false
jd: false

View File

@@ -60,3 +60,6 @@ rocketmq:
consume-thread-min: 20 # 消费线程池最小线程数 consume-thread-min: 20 # 消费线程池最小线程数
consume-thread-max: 64 # 消费线程池最大线程数 consume-thread-max: 64 # 消费线程池最大线程数
consume-message-batch-max-size: 64 # 批量消费最大消息数 consume-message-batch-max-size: 64 # 批量消费最大消息数
isRunning:
wx: true
jd: true