@@ -16,11 +16,15 @@ import com.jd.open.api.sdk.request.kplunion.UnionOpenOrderRowQueryRequest;
import com.jd.open.api.sdk.request.kplunion.UnionOpenPromotionBysubunionidGetRequest ;
import com.jd.open.api.sdk.response.kplunion.UnionOpenOrderRowQueryResponse ;
import com.jd.open.api.sdk.response.kplunion.UnionOpenPromotionBysubunionidGetResponse ;
import io.github.resilience4j.core.functions.CheckedRunnable ;
import io.github.resilience4j.ratelimiter.RateLimiter ;
import io.github.resilience4j.ratelimiter.RequestNotPermitted ;
import lombok.Getter ;
import lombok.Setter ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.beans.factory.annotation.Qualifier ;
import org.springframework.data.redis.core.HashOperations ;
import org.springframework.data.redis.core.StringRedisTemplate ;
import org.springframework.scheduling.annotation.Scheduled ;
@@ -85,8 +89,10 @@ public class JDUtil {
// 构造函数中注入StringRedisTemplate
@Autowired
public JDUtil ( StringRedisTemplate redisTemplate ,
ProductOrderRepository productOrderRepository , OrderRowRepository orderRowRepository , WXUtil wxUtil , OrderUtil orderUtil ) {
this . redisTemplate = redisTemplate ;
ProductOrderRepository productOrderRepository ,
OrderRowRepository orderRowRepository , WXUtil wxUtil ,
OrderUtil orderUtil ) {
this . redisTemplate = redisTemplate ;
this . orderRowRepository = orderRowRepository ;
this . productOrderRepository = productOrderRepository ;
this . wxUtil = wxUtil ;
@@ -122,7 +128,7 @@ private static class UserInteractionState {
/**
* 将 响应参数转化为 OrderRow, 并返回
*/
private static OrderRow createOrderRow ( OrderRowResp orderRowResp ) {
private OrderRow createOrderRow ( OrderRowResp orderRowResp ) {
OrderRow orderRow = new OrderRow ( ) ;
orderRow . setOrderId ( orderRowResp . getOrderId ( ) ) ;
orderRow . setSkuId ( orderRowResp . getSkuId ( ) ) ;
@@ -210,50 +216,47 @@ private static class UserInteractionState {
return todayOrders . stream ( ) . filter ( orderRow - > orderRow . getValidCode ( ) = = 13 | | orderRow . getValidCode ( ) = = 25 | | orderRow . getValidCode ( ) = = 26 | | orderRow . getValidCode ( ) = = 27 | | orderRow . getValidCode ( ) = = 28 | | orderRow . getValidCode ( ) = = 29 ) ;
}
/**
* 实时刷新最近10分钟的订单
*/
@Scheduled ( cron = " 0 * * * * ? " )
public void fetchLatestOrder ( ) {
LocalDateTime now = LocalDateTime . now ( ) ;
LocalDateTime lastMinute = now . minusMinutes ( 10 ) . withSecond ( 0 ) . withNa no( 0 ) ;
/**
* 实时刷新最近10分钟的订单( Resilience4j限流集成)
*/
@Scheduled ( cron = " 0 * * * * ? " )
public void fetchLatestOrder ( ) {
try {
LocalDateTime now = LocalDateTime . now ( ) ;
LocalDateTime lastMinute = now . minusMinutes ( 10 ) . withSecond ( 0 ) . withNano ( 0 ) ;
for ( Map . Entry < String , WXUtil . SuperAdmin > entry : super_admins . entrySet ( ) ) {
WXUtil . SuperAdmin admin = entry . getValue ( ) ;
String appKey = admin . getAppKey ( ) ;
String secretKey = admin . getSecretKey ( ) ;
/**临时代码*/
if ( Util . isAnyEmpty ( appKey , secretKey ) ) {
continue ;
}
// 如果当前分钟数刚好是整10, 就打印
if ( now . getMinute ( ) % 10 = = 0 ) {
logger . info ( " 实时订单 {} " , appKey . substring ( appKey . length ( ) - 4 ) ) ;
}
UnionOpenOrderRowQueryResponse response = fetchOrdersForDateTime (
lastMinute , true , 1 , true , appKey , secretKey
) ;
if ( response ! = null & & response . getQueryResult ( ) ! = null
& & response . getQueryResult ( ) . getCode ( ) = = 200 ) {
OrderRowResp [ ] orderRowResps = response . getQueryResult ( ) . getData ( ) ;
if ( orderRowResps = = null ) continue ;
/**下面是原先的代码*/
for ( Map . Entry < String , WXUtil . SuperAdmin > entry : super_admins . entrySet ( ) ) {
//String wxid = entry.getKey() ;
WXUtil . SuperAdmin admin = entry . getValue ( ) ;
String appKey = admin . getAppKey ( ) ;
String secretKey = admin . getSecretKey ( ) ;
if ( Util . isAnyEmpty ( appKey , secretKey ) ) {
continue ;
}
logger . info ( " 实时订单 {} " , appKey . substring ( appKey . length ( ) - 4 ) ) ;
UnionOpenOrderRowQueryResponse response = fetchOrdersForDateTime ( lastMinute , true , 1 , true , appKey , secretKey ) ; // 真实代表实时订单
if ( response ! = null ) {
int code = response . getQueryResult ( ) . getCode ( ) ;
if ( code = = 200 ) {
if ( response . getQueryResult ( ) . getCode ( ) = = 200 ) {
OrderRowResp [ ] orderRowResps = response . getQueryResult ( ) . getData ( ) ;
if ( orderRowResps = = null ) {
continue ;
}
for ( OrderRowResp orderRowResp : orderRowResps ) {
// 固化到数据库
OrderRow orderRow = createOrderRow ( orderRowResp ) ;
// 订单号不存在就保存,存在就更新订单状态
orderRowRepository . save ( orderRow ) ;
}
}
Arrays . stream ( orderRowResps )
. map ( this : : createOrderRow )
. forEach ( orderRowRepository : : save ) ;
}
}
}
} catch ( Exception e ) {
logger . error ( " 调度任务异常 " , e ) ;
}
}
public void test01 ( ) {
@@ -291,7 +294,7 @@ private static class UserInteractionState {
*/
@Scheduled ( cron = " 10 * * * * ? " )
public void sendOrderToWx ( ) {
long start = System. currentTimeMillis( ) ;
// long start = System. currentTimeMillis() ;
int [ ] validCodes = { - 1 } ;
// 只要三个月的,更多的也刷新不出来的
Date threeMonthsAgo = Date . from ( LocalDateTime . now ( ) . minusMonths ( 3 ) . atZone ( ZoneId . systemDefault ( ) ) . toInstant ( ) ) ;
@@ -303,7 +306,7 @@ private static class UserInteractionState {
}
logger. info ( " 扫描订单发送到微信耗时:{} ms, 订单数:{} " , System. currentTimeMillis( ) - start , orderRows . size ( ) ) ;
// logger.info(" 扫描订单发送到微信耗时:{} ms, 订单数:{} ", System. currentTimeMillis() - start, orderRows.size()) ;
}
@@ -620,7 +623,8 @@ private static class UserInteractionState {
}
// 打印方法调用和开始结束时间
if ( isRealTime ) {
if ( isRealTime & & ( LocalDateTime . now ( ) . getMinute ( ) % 10 = = 0 ) ) {
logger . info ( " {} --- 拉取订单, 分钟还是秒 {} , 开始时间:{} --- 结束时间:{} " , appKey . substring ( appKey . length ( ) - 4 ) , hourMinuteTag , startTime . format ( DATE_TIME_FORMATTER ) , endTime . format ( DATE_TIME_FORMATTER ) ) ;
}