@@ -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 ) ) {
processOrder Response( response , admin ) ;
try {
UnionOpenOrderRowQuery Response 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 . f indByValidCodeNotInAndOrderTimeGreaterThanOrderByOrderTimeDesc ( validCodes , threeMonthsAgo ) ;
if ( isRunning_wx ) {
//long start = System.currentTimeMillis() ;
int [ ] validCodes = { - 1 } ;
// 只要三个月的,更多的也刷新不出来的
Date threeMonthsAgo = Date . from ( LocalDateTime . now ( ) . m inusMonths ( 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 ( s trategy , admin . getAppKey ( ) , admin . getSecretKe y( ) ) ;
try {
OrderFetchS trategy strategy = new Days0007Strateg y( ) ;
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 ) ;
}
}
}