This commit is contained in:
雷欧(林平凡)
2024-12-05 10:58:32 +08:00
parent 8fd6bad912
commit 1deb229328
2 changed files with 117 additions and 45 deletions

View File

@@ -33,7 +33,7 @@ public class OrderController {
@ResponseBody
public String refreshHistory(String token) throws Exception {
if (checkToken(token)) {
jdUtils.fetchHistoricalOrders();
jdUtils.fetchHistoricalOrders3060();
}
return "OK";
}

View File

@@ -45,14 +45,11 @@ import java.util.stream.Stream;
*/
@Component
public class JDUtils {
private static final String SERVER_URL =
"https://api.jd.com/routerjson";
private static final String SERVER_URL = "https://api.jd.com/routerjson";
// van论坛
private static final String APP_KEY =
"98e21c89ae5610240ec3f5f575f86a59";
private static final String SECRET_KEY =
"3dcb6b23a1104639ac433fd07adb6dfb";
//标记唯一订单行:订单+sku维度的唯一标识
private static final String APP_KEY = "98e21c89ae5610240ec3f5f575f86a59";
private static final String SECRET_KEY = "3dcb6b23a1104639ac433fd07adb6dfb";
// 标记是否拉取过小时的订单空订单会set 一个 tag避免重复拉取
private static final String JD_REFRESH_TAG = "jd:refresh:tag:";
// 导购的
//private static final String APP_KEY = "faf410cb9587dc80dc7b31e321d7d322";
@@ -71,10 +68,7 @@ public class JDUtils {
// 通过构造函数注入所有依赖项Spring将自动注入这些依赖
@Autowired // @Autowired 在构造函数上可以省略,如果类只有一个构造函数
public JDUtils(StringRedisTemplate redisTemplate,
OrderRowRepository orderRowRepository,
WXUtil wxUtil,
OrderUtil orderUtil) {
public JDUtils(StringRedisTemplate redisTemplate, OrderRowRepository orderRowRepository, WXUtil wxUtil, OrderUtil orderUtil) {
this.redisTemplate = redisTemplate;
this.orderRowRepository = orderRowRepository;
this.wxUtil = wxUtil;
@@ -159,36 +153,26 @@ public class JDUtils {
private static List<OrderRow> filterOrdersByDate(List<OrderRow> orderRows, int daysBack) {
LocalDate now = LocalDate.now();
return orderRows.stream()
.filter(order -> {
return orderRows.stream().filter(order -> {
// 将 Date 转换为 LocalDate
LocalDate orderDate = order.getOrderTime().toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDate();
LocalDate orderDate = order.getOrderTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
// 计算是否在给定的天数内
return !orderDate.isBefore(now.minusDays(daysBack)) && !orderDate.isAfter(now);
})
.collect(Collectors.toList());
}).collect(Collectors.toList());
}
private static Stream<OrderRow> getStreamForWeiGui(List<OrderRow> todayOrders) {
return todayOrders.stream().filter(
orderRow -> orderRow.getValidCode() == 13
|| orderRow.getValidCode() == 25
|| orderRow.getValidCode() == 26
|| orderRow.getValidCode() == 27
|| orderRow.getValidCode() == 28
|| orderRow.getValidCode() == 29);
return todayOrders.stream().filter(orderRow -> orderRow.getValidCode() == 13 || orderRow.getValidCode() == 25 || orderRow.getValidCode() == 26 || orderRow.getValidCode() == 27 || orderRow.getValidCode() == 28 || orderRow.getValidCode() == 29);
}
/**
* 拉取最新的订单 1440分钟
* 实时刷新最近10分钟的订单
*/
@Scheduled(cron = "0 * * * * ?") // 每分钟执行一次
public void fetchLatestOrder() throws Exception {
LocalDateTime now = LocalDateTime.now();
LocalDateTime lastMinute = now.minusMinutes(55).withSecond(0).withNano(0);
LocalDateTime lastMinute = now.minusMinutes(10).withSecond(0).withNano(0);
UnionOpenOrderRowQueryResponse response = fetchOrdersForDateTime(lastMinute, true, 1); // 真实代表实时订单
if (response != null) {
@@ -233,15 +217,14 @@ public class JDUtils {
}
/**
* 每小时拉取过去两个月的订单
* 因为有的延迟发货,而接口只能拉取两个月前的数据
* 一天拉取三次 30天到60天前的订单
*/
@Scheduled(cron = "0 0 * * * ?")
public void fetchHistoricalOrders() {
@Scheduled(cron = "0 0 */12 * * ?")
public void fetchHistoricalOrders3060() {
LocalDateTime now = LocalDateTime.now();
logger.info("拉取历史订单---> , {} 点", now.getHour());
LocalDateTime lastHour = now.truncatedTo(ChronoUnit.HOURS);
LocalDateTime lastHour = now.truncatedTo(ChronoUnit.HOURS).minusMonths(1);
LocalDateTime startDate = lastHour.minusMonths(2).truncatedTo(ChronoUnit.HOURS);
while (!startDate.isEqual(lastHour)) {
@@ -272,7 +255,6 @@ public class JDUtils {
hasMore = false;
}
} catch (Exception e) {
//System.err.println("处理时间 " + startDate + " 时出错:" + e.getMessage());
hasMore = false; // Optionally break out of the while loop if required
}
if (hasMore) pageIndex++;
@@ -281,13 +263,107 @@ public class JDUtils {
}
}
/**
* 一天拉取6次 14天到30天前的订单
*/
@Scheduled(cron = "0 0 */4 * * ?")
public void fetchHistoricalOrders1430() {
LocalDateTime now = LocalDateTime.now();
logger.info("拉取历史订单---> , {} 点", now.getHour());
LocalDateTime lastHour = now.truncatedTo(ChronoUnit.HOURS).minusDays(14);
LocalDateTime startDate = lastHour.minusMonths(1).truncatedTo(ChronoUnit.HOURS);
while (!startDate.isEqual(lastHour)) {
Integer pageIndex = 1;
boolean hasMore = true;
while (hasMore) {
try {
UnionOpenOrderRowQueryResponse response = fetchOrdersForDateTime(startDate, false, pageIndex);
if (response != null && response.getQueryResult() != null) {
if (response.getQueryResult().getCode() == 200) {
OrderRowResp[] orderRowResps = response.getQueryResult().getData();
if (orderRowResps != null) {
for (OrderRowResp orderRowResp : orderRowResps) {
if (orderRowResp != null) { // Check each orderRowResp is not null
OrderRow orderRow = createOrderRow(orderRowResp);
if (orderRow != null) { // Ensure orderRow is not null after creation
orderRowRepository.save(orderRow);
}
}
}
}
hasMore = Boolean.TRUE.equals(response.getQueryResult().getHasMore());
} else {
hasMore = false;
}
} else {
hasMore = false;
}
} catch (Exception e) {
hasMore = false; // Optionally break out of the while loop if required
}
if (hasMore) pageIndex++;
}
startDate = startDate.plusHours(1);
}
}
/**
* 每10分钟拉取最近14天的订单
*/
@Scheduled(cron = "0 */10 * * * ?")
public void fetchHistoricalOrders0014() {
LocalDateTime now = LocalDateTime.now();
logger.info("拉取历史订单---> , {} 点", now.getHour());
LocalDateTime lastHour = now.truncatedTo(ChronoUnit.HOURS);
LocalDateTime startDate = lastHour.minusDays(14).truncatedTo(ChronoUnit.HOURS);
while (!startDate.isEqual(lastHour)) {
Integer pageIndex = 1;
boolean hasMore = true;
while (hasMore) {
try {
UnionOpenOrderRowQueryResponse response = fetchOrdersForDateTime(startDate, false, pageIndex);
if (response != null && response.getQueryResult() != null) {
if (response.getQueryResult().getCode() == 200) {
OrderRowResp[] orderRowResps = response.getQueryResult().getData();
if (orderRowResps != null) {
for (OrderRowResp orderRowResp : orderRowResps) {
if (orderRowResp != null) { // Check each orderRowResp is not null
OrderRow orderRow = createOrderRow(orderRowResp);
if (orderRow != null) { // Ensure orderRow is not null after creation
orderRowRepository.save(orderRow);
}
}
}
}
hasMore = Boolean.TRUE.equals(response.getQueryResult().getHasMore());
} else {
hasMore = false;
}
} else {
hasMore = false;
}
} catch (Exception e) {
hasMore = false; // Optionally break out of the while loop if required
}
if (hasMore) pageIndex++;
}
startDate = startDate.plusMinutes(10);
}
}
/**
* 根据指定的日期时间拉取订单
*/
public UnionOpenOrderRowQueryResponse fetchOrdersForDateTime(LocalDateTime startTime, boolean isRealTime, Integer page) throws Exception {
LocalDateTime endTime = isRealTime ? startTime.plusMinutes(50) : startTime.plusHours(1);
LocalDateTime endTime = isRealTime ? startTime.plusMinutes(10) : startTime.plusHours(1);
String hourMinuteTag = isRealTime ? "minute" : "hour";
String timeTag = JD_REFRESH_TAG + startTime.format(DATE_TIME_FORMATTER);
@@ -610,11 +686,8 @@ public class JDUtils {
content.append("违规排行:");
content.append(daysInt).append("").append("\r\n");
Map<String, Long> skuIdViolationCountMap = filterOrdersByDays.stream().filter(orderRow -> orderRow.getValidCode() == 27
|| orderRow.getValidCode() == 28
).filter(orderRow -> orderRow.getSkuName() != null).collect(Collectors.groupingBy(OrderRow::getSkuName, Collectors.counting()));
List<Map.Entry<String, Long>> sortedViolationCounts = skuIdViolationCountMap.entrySet().stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).collect(Collectors.toList());
Map<String, Long> skuIdViolationCountMap = filterOrdersByDays.stream().filter(orderRow -> orderRow.getValidCode() == 27 || orderRow.getValidCode() == 28).filter(orderRow -> orderRow.getSkuName() != null).collect(Collectors.groupingBy(OrderRow::getSkuName, Collectors.counting()));
List<Map.Entry<String, Long>> sortedViolationCounts = skuIdViolationCountMap.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).collect(Collectors.toList());
Integer num = 0;
for (Map.Entry<String, Long> entry : sortedViolationCounts) {
num++;
@@ -723,8 +796,7 @@ public class JDUtils {
PromotionCodeReq promotionCodeReq = new PromotionCodeReq();
promotionCodeReq.setMaterialId(url);
promotionCodeReq.setSiteId(
"4101253066");
promotionCodeReq.setSiteId("4101253066");
promotionCodeReq.setSceneId(1);
promotionCodeReq.setCommand(1);
promotionCodeReq.setProType(5);