Files
ruoyi-java/ruoyi-system/src/main/java/com/ruoyi/jarvis/task/LogisticsScanTask.java
2025-12-24 14:29:23 +08:00

103 lines
4.0 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.ruoyi.jarvis.task;
import com.ruoyi.jarvis.domain.JDOrder;
import com.ruoyi.jarvis.service.IJDOrderService;
import com.ruoyi.jarvis.service.ILogisticsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
/**
* 物流信息扫描定时任务
* 每20分钟扫描一次分销标记为F或PDD的订单最近30天获取物流信息并推送
*/
@Component
public class LogisticsScanTask {
private static final Logger logger = LoggerFactory.getLogger(LogisticsScanTask.class);
@Resource
private IJDOrderService jdOrderService;
@Resource
private ILogisticsService logisticsService;
/**
* 定时任务每20分钟执行一次
* Cron表达式格式0 每N分钟 * * * ? 表示每N分钟执行一次
* 只扫描最近30天的订单
*/
@Scheduled(cron = "0 */20 * * * ?")
public void scanAndFetchLogistics() {
logger.info("========== 开始执行物流信息扫描定时任务最近30天订单 ==========");
try {
// 查询分销标记为F或PDD且有物流链接的订单最近30天
List<JDOrder> orders = jdOrderService.selectJDOrderListByDistributionMarkFOrPDD();
if (orders == null || orders.isEmpty()) {
logger.info("未找到需要处理的订单");
return;
}
logger.info("找到 {} 个需要处理的订单", orders.size());
int processedCount = 0;
int skippedCount = 0;
int successCount = 0;
int failedCount = 0;
// 串行处理订单(避免并发调用接口)
for (JDOrder order : orders) {
try {
// 检查Redis中是否已处理过避免重复处理
if (logisticsService.isOrderProcessed(order.getId())) {
logger.debug("订单已处理过,跳过 - 订单ID: {}", order.getId());
skippedCount++;
continue;
}
logger.info("开始处理订单 - 订单ID: {}, 订单号: {}, 分销标识: {}",
order.getId(), order.getOrderId(), order.getDistributionMark());
// 获取物流信息并推送(串行执行,不并发)
boolean success = logisticsService.fetchLogisticsAndPush(order);
if (success) {
successCount++;
logger.info("订单处理成功 - 订单ID: {}, 订单号: {}", order.getId(), order.getOrderId());
} else {
failedCount++;
logger.warn("订单处理失败 - 订单ID: {}, 订单号: {}", order.getId(), order.getOrderId());
}
processedCount++;
// 添加短暂延迟,避免请求过于频繁
Thread.sleep(500); // 每次请求间隔500毫秒
} catch (InterruptedException e) {
logger.error("定时任务被中断", e);
Thread.currentThread().interrupt();
break;
} catch (Exception e) {
failedCount++;
logger.error("处理订单时发生异常 - 订单ID: {}, 错误: {}", order.getId(), e.getMessage(), e);
// 继续处理下一个订单
}
}
logger.info("========== 物流信息扫描定时任务执行完成 ==========");
logger.info("总订单数: {}, 已处理: {}, 跳过: {}, 成功: {}, 失败: {}",
orders.size(), processedCount, skippedCount, successCount, failedCount);
} catch (Exception e) {
logger.error("执行物流信息扫描定时任务时发生异常", e);
}
}
}