From 2316951e7f9aaf6ab92557da1fb4508468fdc513 Mon Sep 17 00:00:00 2001 From: Leo Date: Tue, 11 Mar 2025 14:11:51 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E7=AC=AC=E4=B8=80=E7=89=88?= =?UTF-8?q?=EF=BC=8C=E6=B2=A1=E6=9C=89=E6=98=8E=E6=98=BEbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/van/business/util/JDUtil.java | 163 ++++++++++-------- .../java/cn/van/business/util/OrderUtil.java | 2 +- src/main/resources/application-dev.yml | 3 + src/main/resources/application-prod.yml | 3 + 4 files changed, 101 insertions(+), 70 deletions(-) diff --git a/src/main/java/cn/van/business/util/JDUtil.java b/src/main/java/cn/van/business/util/JDUtil.java index c742f86..741a727 100644 --- a/src/main/java/cn/van/business/util/JDUtil.java +++ b/src/main/java/cn/van/business/util/JDUtil.java @@ -22,6 +22,7 @@ import lombok.Setter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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.StringRedisTemplate; import org.springframework.scheduling.annotation.Scheduled; @@ -85,6 +86,12 @@ public class JDUtil { // 添加ObjectMapper来序列化和反序列化UserInteractionState private final ObjectMapper objectMapper = new ObjectMapper(); + @Value("${isRunning.wx") + private boolean isRunning_wx = false; + @Value("isRunning.jd") + private boolean isRunning_jd = false; + + // 构造函数中注入StringRedisTemplate @Autowired public JDUtil(StringRedisTemplate redisTemplate, ProductOrderRepository productOrderRepository, OrderRowRepository orderRowRepository, WXUtil wxUtil, OrderUtil orderUtil) { @@ -237,24 +244,27 @@ public class JDUtil { */ @Scheduled(cron = "0 * * * * ?") public void fetchLatestOrder() { - LocalDateTime now = LocalDateTime.now(); - LocalDateTime startTime = now.minusMinutes(10).withSecond(0).withNano(0); + if (isRunning_jd) { - super_admins.values().parallelStream().forEach(admin -> { - if (Util.isAnyEmpty(admin.getAppKey(), admin.getSecretKey())) return; + LocalDateTime now = LocalDateTime.now(); + LocalDateTime startTime = now.minusMinutes(10).withSecond(0).withNano(0); - try { - UnionOpenOrderRowQueryResponse response = fetchOrdersForDateTime(startTime, true, 1, true, admin.getAppKey(), admin.getSecretKey()); + super_admins.values().parallelStream().forEach(admin -> { + if (Util.isAnyEmpty(admin.getAppKey(), admin.getSecretKey())) return; - if (isValidResponse(response)) { - processOrderResponse(response, admin); + try { + 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秒执行一次 */ - @Scheduled(cron = "10 * * * * ?") + @Scheduled(cron = "3 * * * * ?") public void sendOrderToWx() { - //long start = System.currentTimeMillis(); - int[] validCodes = {-1}; - // 只要三个月的,更多的也刷新不出来的 - Date threeMonthsAgo = Date.from(LocalDateTime.now().minusMonths(3).atZone(ZoneId.systemDefault()).toInstant()); - List orderRows = orderRowRepository.findByValidCodeNotInAndOrderTimeGreaterThanOrderByOrderTimeDesc(validCodes, threeMonthsAgo); + if (isRunning_wx) { + //long start = System.currentTimeMillis(); + int[] validCodes = {-1}; + // 只要三个月的,更多的也刷新不出来的 + Date threeMonthsAgo = Date.from(LocalDateTime.now().minusMonths(3).atZone(ZoneId.systemDefault()).toInstant()); + List 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 * * ?") public void fetchHistoricalOrders3090() { - try { - OrderFetchStrategy strategy = new Days3090Strategy(); - for (WXUtil.SuperAdmin admin : super_admins.values()) { - try { - fetchOrders(strategy, admin.getAppKey(), admin.getSecretKey()); - } catch (Exception e) { - logger.error("账号{}拉取异常: {}", admin.getAppKey().substring(18), e.getMessage()); + if (isRunning_jd) { + try { + OrderFetchStrategy strategy = new Days3090Strategy(); + for (WXUtil.SuperAdmin admin : super_admins.values()) { + try { + fetchOrders(strategy, admin.getAppKey(), admin.getSecretKey()); + } 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 * * * ?") public void fetchHistoricalOrders1430() { - try { - OrderFetchStrategy strategy = new Days1430Strategy(); // 需补充Days1430Strategy实现 - for (WXUtil.SuperAdmin admin : super_admins.values()) { - try { - fetchOrders(strategy, admin.getAppKey(), admin.getSecretKey()); - } catch (Exception e) { - logger.error("账号{}拉取异常: {}", admin.getAppKey().substring(18), e.getMessage()); + if (isRunning_jd) { + + try { + OrderFetchStrategy strategy = new Days1430Strategy(); // 需补充Days1430Strategy实现 + for (WXUtil.SuperAdmin admin : super_admins.values()) { + try { + 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 * * * ?") public void fetchHistoricalOrders0714() { - try { - OrderFetchStrategy strategy = new Days0714Strategy(); - super_admins.values().parallelStream().forEach(admin -> { - if (Util.isAnyEmpty(admin.getAppKey(), admin.getSecretKey())) return; - try { - 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); + if (isRunning_jd) { + + try { + OrderFetchStrategy strategy = new Days0714Strategy(); + super_admins.values().parallelStream().forEach(admin -> { + if (Util.isAnyEmpty(admin.getAppKey(), admin.getSecretKey())) return; + try { + 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); + } } } @Scheduled(cron = "0 */5 * * * ?") public void fetchHistoricalOrders0007() { - try { - OrderFetchStrategy strategy = new Days0007Strategy(); - super_admins.values().parallelStream().forEach(admin -> { - if (Util.isAnyEmpty(admin.getAppKey(), admin.getSecretKey())) return; + if (isRunning_jd) { - try { - int count = fetchOrders(strategy, admin.getAppKey(), admin.getSecretKey()); + try { + 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); - } catch (RateLimitExceededException e) { - logger.warn("[限流] {} 0007请求受限", admin.getAppKey().substring(18)); - } catch (Exception e) { - logger.error("账号{}0007拉取异常: {}", admin.getAppKey().substring(18), e.getMessage()); - } - }); - } catch (Exception ex) { - logger.error("0007策略执行异常", ex); + try { + int count = fetchOrders(strategy, admin.getAppKey(), admin.getSecretKey()); + + logger.info("账号{} 0007订单拉取完成,新增{}条", admin.getAppKey().substring(18), count); + } catch (RateLimitExceededException e) { + logger.warn("[限流] {} 0007请求受限", admin.getAppKey().substring(18)); + } catch (Exception e) { + logger.error("账号{}0007拉取异常: {}", admin.getAppKey().substring(18), e.getMessage()); + } + }); + } catch (Exception ex) { + logger.error("0007策略执行异常", ex); + } } } diff --git a/src/main/java/cn/van/business/util/OrderUtil.java b/src/main/java/cn/van/business/util/OrderUtil.java index 19c52b3..c2bc47f 100644 --- a/src/main/java/cn/van/business/util/OrderUtil.java +++ b/src/main/java/cn/van/business/util/OrderUtil.java @@ -74,7 +74,7 @@ public class OrderUtil { "状态:" + (converter.getCodeDescription(orderRow.getValidCode())) + "\r" - + "名称:" + orderRow.getSkuName().substring(0, 64) + "\r\n" + + "名称:" + orderRow.getSkuName() + "\r\n" //+ "商品单价:" + orderRow.getPrice() + "\r" //+ "商品数量:" + orderRow.getSkuNum() + "\r" //+ "商品总价:" + (orderRow.getPrice() * orderRow.getSkuNum()) + "\r" diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 6e7ad01..7ccba0b 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -60,3 +60,6 @@ rocketmq: consume-thread-min: 20 # 消费线程池最小线程数 consume-thread-max: 64 # 消费线程池最大线程数 consume-message-batch-max-size: 64 # 批量消费最大消息数 +isRunning: + wx: false + jd: false diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml index 6e7ad01..6ac3cff 100644 --- a/src/main/resources/application-prod.yml +++ b/src/main/resources/application-prod.yml @@ -60,3 +60,6 @@ rocketmq: consume-thread-min: 20 # 消费线程池最小线程数 consume-thread-max: 64 # 消费线程池最大线程数 consume-message-batch-max-size: 64 # 批量消费最大消息数 +isRunning: + wx: true + jd: true