feat: 导入订单时用邻近行下单时间填充空行,修复推广费路由

This commit is contained in:
小蟑螂
2026-05-22 15:38:13 +08:00
parent 3b10d8bae1
commit 0b653cf56e
4 changed files with 116 additions and 50 deletions
@@ -86,7 +86,7 @@ public class ImportRuntimeContext {
/**
* 该导入行输入的 order_time 是否为空(仅用于更新跳过策略)。
* <p>
* 创建阶段不受此字段影响:创建时仍会用当前时间兜底。
* 创建阶段不受此字段影响:创建时仍会用邻近行或本行解析出的下单时间写入。
*/
public boolean orderTimeEmptyInInput;
public Long existingOrderId; // 不为空表示是更新操作
@@ -27,6 +27,7 @@ import java.time.temporal.ChronoUnit;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.*;
import java.util.function.Function;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
@@ -252,6 +253,9 @@ public class OrderImportProcessor {
specHelper.preloadStatuses(context, statusNames, statusMapping);
specHelper.preloadShopProductRelations(context, shopProductRelationMapper);
List<LocalDateTime> neighborOrderTimesByRow = buildNeighborOrderTimesForRows(
records, orderDateField, r -> getValue(r, orderDateField), context);
// ========== 并行处理 ==========
// 无界队列:主线程 offer 永不阻塞,workers poll 获取行数据
LinkedBlockingQueue<PendingRow> workQueue = new LinkedBlockingQueue<>();
@@ -273,8 +277,11 @@ public class OrderImportProcessor {
try {
// 在主线程完成 CSV 解析(DTO 字段填充)
LocalDateTime neighborOrderTime = orderDateField != null
? neighborOrderTimesByRow.get(i) : null;
CreateOrderDTO dto = parseOrderFromCSV(record, fieldMapping, statusMapping,
task.getShopId(), context.getCompanyId(), context.getCreateBy(), context);
task.getShopId(), context.getCompanyId(), context.getCreateBy(), context,
neighborOrderTime);
Map<String, String> rowMap = csvRecordToMap(record);
PendingRow row = new PendingRow(rowNumber, rowMap, dto, fieldMapping, statusMapping,
@@ -461,6 +468,9 @@ public class OrderImportProcessor {
specHelper.preloadStatuses(context, statusNames, statusMapping);
specHelper.preloadShopProductRelations(context, shopProductRelationMapper);
List<LocalDateTime> neighborOrderTimesByRow = buildNeighborOrderTimesForRows(
rowDataList, orderDateField, rowMap -> getValue(rowMap, orderDateField), context);
// ========== 并行处理(与 CSV 共用同一套 worker 机制)==========
LinkedBlockingQueue<PendingRow> workQueue = new LinkedBlockingQueue<>();
List<OrderParseResult> results = Collections.synchronizedList(new ArrayList<>());
@@ -478,8 +488,11 @@ public class OrderImportProcessor {
int rowNumber = i + 2; // Excel 行号从 1 开始,减 1 是标题行,再 +1 是数据第 1 行
try {
LocalDateTime neighborOrderTime = orderDateField != null
? neighborOrderTimesByRow.get(i) : null;
CreateOrderDTO dto = parseOrderFromMap(rowMap, fieldMapping, statusMapping,
task.getShopId(), context.getCompanyId(), context.getCreateBy(), context);
task.getShopId(), context.getCompanyId(), context.getCreateBy(), context,
neighborOrderTime);
PendingRow row = new PendingRow(rowNumber, rowMap, dto, fieldMapping, statusMapping,
task.getShopId(), context.getCompanyId(), context.getCreateBy());
@@ -654,7 +667,8 @@ public class OrderImportProcessor {
Map<String, ImportRuntimeContext.MappingField> fieldMapping,
Map<String, Long> statusMapping,
Long shopId, Long companyId, Long createBy,
ImportRuntimeContext context) {
ImportRuntimeContext context,
LocalDateTime neighborRowOrderTime) {
CreateOrderDTO dto = new CreateOrderDTO();
dto.setShopId(shopId);
@@ -803,27 +817,7 @@ public class OrderImportProcessor {
}
}
if (orderDateField != null) {
String dateStr = getValue(rowData, orderDateField);
boolean emptyInInput = (dateStr == null || dateStr.trim().isEmpty());
dto.setOrderTimeEmptyInInput(emptyInInput);
if (!emptyInInput) {
LocalDateTime dateTime = parseDateTime(dateStr, context);
if (dateTime != null) {
dto.setOrderTime(dateTime);
} else {
// 输入非空但解析失败:保持「创建逻辑不变」的兜底行为,填充当前时间
dto.setOrderTime(LocalDateTime.now());
}
} else {
// 输入为空:保持「创建逻辑不变」的兜底行为,填充当前时间
dto.setOrderTime(LocalDateTime.now());
}
} else {
dto.setOrderTime(LocalDateTime.now());
// 未配置映射:不参与「更新跳过」判定
dto.setOrderTimeEmptyInInput(false);
}
applyOrderTimeToDto(dto, getValue(rowData, orderDateField), neighborRowOrderTime, context, orderDateField);
return dto;
}
@@ -1395,7 +1389,8 @@ public class OrderImportProcessor {
Map<String, ImportRuntimeContext.MappingField> fieldMapping,
Map<String, Long> statusMapping,
Long shopId, Long companyId, Long createBy,
ImportRuntimeContext context) {
ImportRuntimeContext context,
LocalDateTime neighborRowOrderTime) {
CreateOrderDTO dto = new CreateOrderDTO();
dto.setShopId(shopId);
@@ -1553,28 +1548,7 @@ public class OrderImportProcessor {
}
}
// 订单日期
if (orderDateField != null) {
String dateStr = getValue(record, orderDateField);
boolean emptyInInput = (dateStr == null || dateStr.trim().isEmpty());
dto.setOrderTimeEmptyInInput(emptyInInput);
if (!emptyInInput) {
LocalDateTime dateTime = parseDateTime(dateStr, context);
if (dateTime != null) {
dto.setOrderTime(dateTime);
} else {
// 输入非空但解析失败:保持「创建逻辑不变」的兜底行为,填充当前时间
dto.setOrderTime(LocalDateTime.now());
}
} else {
// 输入为空:保持「创建逻辑不变」的兜底行为,填充当前时间
dto.setOrderTime(LocalDateTime.now());
}
} else {
dto.setOrderTime(LocalDateTime.now());
// 未配置映射:不参与「更新跳过」判定
dto.setOrderTimeEmptyInInput(false);
}
applyOrderTimeToDto(dto, getValue(record, orderDateField), neighborRowOrderTime, context, orderDateField);
return dto;
}
@@ -1673,6 +1647,98 @@ public class OrderImportProcessor {
context.pendingRelationKeys.add(relationKey);
}
// ==================== 下单时间:邻近行填充 ====================
/**
* 按文件行序预计算每行可用的下单时间:先解析本行非空且可解析的值作为锚点,
* 再对空行或解析失败行用「行号距离最小」的锚点填充;距离相同时取行号更小者。
*/
private <T> List<LocalDateTime> buildNeighborOrderTimesForRows(
List<T> rows,
ImportRuntimeContext.MappingField orderDateField,
Function<T, String> dateStrExtractor,
ImportRuntimeContext context) {
if (orderDateField == null || rows.isEmpty()) {
return Collections.emptyList();
}
List<String> raw = new ArrayList<>(rows.size());
for (T row : rows) {
raw.add(dateStrExtractor.apply(row));
}
return buildNearestNeighborOrderTimes(raw, context);
}
private List<LocalDateTime> buildNearestNeighborOrderTimes(
List<String> rawDateStrs, ImportRuntimeContext context) {
int n = rawDateStrs.size();
LocalDateTime[] resolved = new LocalDateTime[n];
boolean[] anchor = new boolean[n];
for (int i = 0; i < n; i++) {
String dateStr = rawDateStrs.get(i);
if (dateStr == null || dateStr.trim().isEmpty()) {
continue;
}
LocalDateTime dt = parseDateTime(dateStr, context);
if (dt != null) {
resolved[i] = dt;
anchor[i] = true;
}
}
for (int i = 0; i < n; i++) {
if (anchor[i]) {
continue;
}
int nearestIdx = -1;
int minDist = Integer.MAX_VALUE;
for (int j = 0; j < n; j++) {
if (!anchor[j]) {
continue;
}
int d = Math.abs(i - j);
if (d < minDist || (d == minDist && nearestIdx >= 0 && j < nearestIdx)) {
minDist = d;
nearestIdx = j;
}
}
if (nearestIdx >= 0) {
resolved[i] = resolved[nearestIdx];
}
}
List<LocalDateTime> out = new ArrayList<>(n);
for (int i = 0; i < n; i++) {
out.add(resolved[i]);
}
return out;
}
/**
* 将下单时间写入 DTO:本行有有效解析则用本行;否则用 {@code neighborRowOrderTime}(由全表邻近行预计算)。
*/
private void applyOrderTimeToDto(CreateOrderDTO dto, String dateStr,
LocalDateTime neighborRowOrderTime,
ImportRuntimeContext context,
ImportRuntimeContext.MappingField orderDateField) {
if (orderDateField == null) {
dto.setOrderTime(LocalDateTime.now());
dto.setOrderTimeEmptyInInput(false);
return;
}
boolean emptyInInput = dateStr == null || dateStr.trim().isEmpty();
dto.setOrderTimeEmptyInInput(emptyInInput);
if (!emptyInInput) {
LocalDateTime dateTime = parseDateTime(dateStr, context);
if (dateTime != null) {
dto.setOrderTime(dateTime);
return;
}
}
if (neighborRowOrderTime != null) {
dto.setOrderTime(neighborRowOrderTime);
return;
}
throw new BusinessException("下单时间为空且导入文件中无可参照的下单时间");
}
// ==================== 日期解析 ====================
private LocalDateTime parseDateTime(String dateTimeStr, ImportRuntimeContext context) {
@@ -53,7 +53,7 @@ public class CreateOrderDTO {
/**
* 导入时输入的下单时间是否为空(仅用于「更新时跳过」的业务决策)。
* <p>
* 注意:即便输入为空,orderTime 在创建/解析阶段仍会被填充为当前时间;
* 注意:即便输入为空,orderTime 在解析阶段仍可能被填充为邻近行的下单时间;
* 该字段用于区分「输入是否为空」而不是区分 orderTime 是否为空。
*/
@Schema(description = "导入时下单时间是否为空(仅用于更新跳过逻辑)")
@@ -46,7 +46,7 @@ function goToImportOrder() {
}
function goToImportPromotionFee() {
void router.push('/data/import/promotion-fee');
void router.push('/import/promotion-fee');
}
/** 与全店概览一致:快捷区间 + 自定义 RangePicker */