重构第一版,没有明显bug

This commit is contained in:
Leo
2025-03-06 10:56:35 +08:00
parent 645b025172
commit 9aa02430fd
13 changed files with 603 additions and 563 deletions

View File

@@ -0,0 +1,26 @@
package cn.van.business.util.jdReq;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
@Component
public class Days0007Strategy implements OrderFetchStrategy {
@Override
public TimeRange calculateRange(LocalDateTime baseTime) {
LocalDateTime end = baseTime.truncatedTo(ChronoUnit.HOURS);
LocalDateTime start = end.minusDays(7).truncatedTo(ChronoUnit.HOURS);
if (start.isAfter(end)) { // 防御性校验
throw new IllegalArgumentException("时间范围错误");
}
return new TimeRange(start, end);
}
@Override
public String strategyName() {
return "00-07天历史订单抓取策略";
}
}

View File

@@ -0,0 +1,25 @@
package cn.van.business.util.jdReq;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
@Component
public class Days0714Strategy implements OrderFetchStrategy {
@Override
public TimeRange calculateRange(LocalDateTime baseTime) {
LocalDateTime end = baseTime.truncatedTo(ChronoUnit.HOURS).minusDays(7);
LocalDateTime start = end.minusDays(14).truncatedTo(ChronoUnit.HOURS);
if (start.isAfter(end)) { // 防御性校验
throw new IllegalArgumentException("时间范围错误");
}
return new TimeRange(start, end);
}
@Override
public String strategyName() {
return "07-14天历史订单抓取策略";
}
}

View File

@@ -0,0 +1,24 @@
package cn.van.business.util.jdReq;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
// 在jdReq包中补充策略类
public class Days1430Strategy implements OrderFetchStrategy {
@Override
public TimeRange calculateRange(LocalDateTime baseTime) {
LocalDateTime end = baseTime.minusDays(30).truncatedTo(ChronoUnit.HOURS);
LocalDateTime start = baseTime.minusDays(14).truncatedTo(ChronoUnit.HOURS);
if (start.isAfter(end)) { // 防御性校验
throw new IllegalArgumentException("时间范围错误");
}
return new TimeRange(start, end);
}
@Override
public String strategyName() {
return "14-30天历史订单抓取策略";
}
}
// 其他策略类类似实现

View File

@@ -0,0 +1,24 @@
package cn.van.business.util.jdReq;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
@Component
public class Days3090Strategy implements OrderFetchStrategy {
@Override
public TimeRange calculateRange(LocalDateTime baseTime) {
LocalDateTime end = baseTime.minusMonths(1);
LocalDateTime start = end.minusMonths(2);
if (start.isAfter(end)) { // 防御性校验
throw new IllegalArgumentException("时间范围错误");
}
return new TimeRange(start, end);
}
@Override
public String strategyName() {
return "30-90天历史订单抓取策略";
}
}

View File

@@ -0,0 +1,40 @@
//package cn.van.business.util.jdReq;
//
//import cn.van.business.repository.OrderRowRepository;
//import com.jd.open.api.sdk.response.kplunion.UnionOpenOrderRowQueryResponse;
//import org.springframework.beans.factory.annotation.Autowired;
//
//import java.time.LocalDateTime;
//
//public abstract class HistoricalOrderFetcher {
// @Autowired
// protected OrderRowRepository orderRowRepository;
//
// protected int fetchOrders(OrderFetchStrategy strategy) {
// int count = 0;
// LocalDateTime start = strategy.getStartTime();
// LocalDateTime end = strategy.getEndTime();
//
// while (!start.isEqual(end)) {
// Integer pageIndex = 1;
// boolean hasMore;
//
// do {
// UnionOpenOrderRowQueryResponse response = fetchPage(strategy, start, pageIndex);
// hasMore = processResponse(response, strategy);
// pageIndex++;
// } while (hasMore);
//
// start = start.plusHours(1);
// }
// return count;
// }
//
// protected abstract UnionOpenOrderRowQueryResponse fetchPage(OrderFetchStrategy strategy,
// LocalDateTime startTime,
// Integer pageIndex);
//
// private boolean processResponse(UnionOpenOrderRowQueryResponse response, OrderFetchStrategy strategy) {
// // 统一响应处理逻辑...
// }
//}

View File

@@ -0,0 +1,18 @@
package cn.van.business.util.jdReq;
import java.time.LocalDateTime;
public interface OrderFetchStrategy {
/**
* 计算要抓取的时间范围
* @param baseTime 基准时间(通常用当前时间)
* @return 包含开始时间和结束时间的值对象
*/
TimeRange calculateRange(LocalDateTime baseTime);
/**
* 策略标识
*/
String strategyName();
}

View File

@@ -0,0 +1,11 @@
package cn.van.business.util.jdReq;
public class StrategyFactory {
public static OrderFetchStrategy getStrategy(String type) {
switch (type) {
case "30-90": return new Days3090Strategy();
//case "14-30": return new Days1430Strategy();
default: throw new IllegalArgumentException();
}
}
}

View File

@@ -0,0 +1,14 @@
package cn.van.business.util.jdReq;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.time.LocalDateTime;
// 时间范围值对象
@Getter
@AllArgsConstructor
public class TimeRange {
private LocalDateTime start;
private LocalDateTime end;
}