重构第一版,没有明显bug
This commit is contained in:
@@ -25,12 +25,12 @@ import static cn.van.business.util.WXUtil.WX_BASE_URL;
|
||||
* @description:
|
||||
*/
|
||||
@Service
|
||||
@RocketMQMessageListener(topic = "wx-message", consumerGroup = "${rocketmq.consumer.group}", nameServer = "${rocketmq.name-server}", consumeMode = ConsumeMode.ORDERLY // 顺序消费(单线程)
|
||||
@RocketMQMessageListener(topic = "wx-message", consumerGroup = "${rocketmq.consumer.group}", nameServer = "${rocketmq.name-server}"
|
||||
)
|
||||
public class MessageConsumerService implements RocketMQListener<JSONObject> {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(MessageConsumerService.class);
|
||||
private static final RateLimiter rateLimiter = RateLimiter.create(0.5, // 1 QPS
|
||||
private static final RateLimiter rateLimiter = RateLimiter.create(2, // 1 QPS
|
||||
5, // 预热期 5 秒
|
||||
TimeUnit.SECONDS);
|
||||
|
||||
|
||||
@@ -8,8 +8,11 @@ package cn.van.business.repository;
|
||||
*/
|
||||
|
||||
import cn.van.business.model.jd.OrderRow;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@@ -31,11 +34,16 @@ public interface OrderRowRepository extends JpaRepository<OrderRow, String> {
|
||||
|
||||
// 查找 validCode != 15 或者 !=-1 的订单行 ,并且按orderTime 降序
|
||||
@Query("select o from OrderRow o where o.validCode not in ?1 and o.unionId =?2 order by o.orderTime DESC")
|
||||
List<OrderRow> findByValidCodeNotInOrderByOrderTimeDescAndUnionId(int[] validCodes,Long unionId);
|
||||
List<OrderRow> findByValidCodeNotInOrderByOrderTimeDescAndUnionId(int[] validCodes, Long unionId);
|
||||
|
||||
@Query("select o from OrderRow o where o.validCode not in ?1 and o.orderTime >= ?2 order by o.orderTime DESC")
|
||||
List<OrderRow> findByValidCodeNotInAndOrderTimeGreaterThanOrderByOrderTimeDesc(
|
||||
int[] validCodes,
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd") Date threeMonthsAgo
|
||||
);
|
||||
List<OrderRow> findByValidCodeNotInAndOrderTimeGreaterThanOrderByOrderTimeDesc(int[] validCodes, @DateTimeFormat(pattern = "yyyy-MM-dd") Date threeMonthsAgo);
|
||||
|
||||
@Query("select o from OrderRow o where o.validCode not in ?1 and o.skuId = ?2 and o.unionId = ?3 order by o.orderTime DESC")
|
||||
List<OrderRow> findBySkuIdAndUnionId(int[] validCodes,long skuId, long unionId);
|
||||
|
||||
//// 在OrderRowRepository中添加模糊查询方法
|
||||
//// 模糊查询收件人姓名或地址(包含分页)
|
||||
//@Query("SELECT o FROM OrderRow o WHERE " + "o.recipientName LIKE %:keyword% OR " + "o.address LIKE %:keyword% " + "ORDER BY o.orderTime DESC")
|
||||
//Page<OrderRow> searchByRecipientOrAddress(@Param("keyword") String keyword, Pageable pageable);
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -74,7 +74,7 @@ public class OrderUtil {
|
||||
|
||||
"状态:" + (converter.getCodeDescription(orderRow.getValidCode())) + "\r"
|
||||
|
||||
+ "名称:" + orderRow.getSkuName() + "\r\n"
|
||||
+ "名称:" + orderRow.getSkuName().substring(0, 64) + "\r\n"
|
||||
//+ "商品单价:" + orderRow.getPrice() + "\r"
|
||||
//+ "商品数量:" + orderRow.getSkuNum() + "\r"
|
||||
//+ "商品总价:" + (orderRow.getPrice() * orderRow.getSkuNum()) + "\r"
|
||||
|
||||
@@ -56,10 +56,8 @@ public class WXUtil {
|
||||
SuperAdmin admin2 = new SuperAdmin("wxid_yneqf1implxu12", "源", "2025353364", "e3c161242c8a1416fada5b5564d7ee70", "41ae9aabf03b41e6ba309682e36b323e");
|
||||
super_admins.put(admin2.getWxid(), admin2);
|
||||
jdidToWxidMap.put(admin2.getUnionId(), admin2.getWxid());
|
||||
wxTsUtil.sendNotify("initSuperAdmins 初始化完成");
|
||||
//wxTsUtil.sendNotify("initSuperAdmins 初始化完成");
|
||||
|
||||
|
||||
// add more admins as needed...
|
||||
}
|
||||
|
||||
public static String getWxidFromJdid(String jdid) {
|
||||
|
||||
@@ -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天历史订单抓取策略";
|
||||
}
|
||||
}
|
||||
@@ -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天历史订单抓取策略";
|
||||
}
|
||||
}
|
||||
@@ -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天历史订单抓取策略";
|
||||
}
|
||||
}
|
||||
|
||||
// 其他策略类类似实现
|
||||
@@ -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天历史订单抓取策略";
|
||||
}
|
||||
}
|
||||
@@ -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) {
|
||||
// // 统一响应处理逻辑...
|
||||
// }
|
||||
//}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
14
src/main/java/cn/van/business/util/jdReq/TimeRange.java
Normal file
14
src/main/java/cn/van/business/util/jdReq/TimeRange.java
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user