重构第一版,没有明显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.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<OrderRow> 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<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 * * ?")
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);
}
}
}

View File

@@ -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"

View File

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