fix: 修复下单时间不能正确解析问题

This commit is contained in:
ah--desktop
2025-12-17 23:34:14 +08:00
parent 736dbfec02
commit 8581cccca9
@@ -39,6 +39,8 @@ import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@@ -334,6 +336,7 @@ public class ImportTaskProcessorServiceImpl implements ImportTaskProcessorServic
MappingField productCodeField = mapping.get("productCode"); // 商品编码
MappingField specNameField = mapping.get("specName"); // 规格名称
MappingField platformOrderNoField = mapping.get("platformOrderNo"); // 平台订单号
MappingField orderDateField = mapping.get("orderDate"); // 下单时间
// 调试日志:记录字段映射
if (logger.isDebugEnabled()) {
@@ -495,12 +498,112 @@ public class ImportTaskProcessorServiceImpl implements ImportTaskProcessorServic
}
}
// 解析下单时间(如果有映射,这里简化处理,使用当前时间)
orderDTO.setOrderDate(LocalDateTime.now());
// 解析下单时间(支持多种格式)
String orderDateStr = getValue(record, orderDateField);
if (orderDateStr != null && !orderDateStr.trim().isEmpty()) {
LocalDateTime orderDate = parseDateTime(orderDateStr.trim());
if (orderDate != null) {
orderDTO.setOrderDate(orderDate);
logger.info("成功解析下单时间: '{}' -> {}", orderDateStr.trim(), orderDate);
} else {
logger.warn("无法解析下单时间: '{}' (可能格式不支持),使用当前时间", orderDateStr);
orderDTO.setOrderDate(LocalDateTime.now());
}
} else {
// 如果没有提供下单时间,使用当前时间
logger.debug("未提供下单时间字段,使用当前时间");
orderDTO.setOrderDate(LocalDateTime.now());
}
return orderDTO;
}
/**
* 日期时间格式定义(内部类)
*/
private static class DateTimeFormatPattern {
String pattern;
DateTimeFormatter formatter;
boolean hasTime;
DateTimeFormatPattern(String pattern, boolean hasTime) {
this.pattern = pattern;
this.formatter = DateTimeFormatter.ofPattern(pattern);
this.hasTime = hasTime;
}
}
/**
* 解析日期时间字符串,支持多种常见格式
*
* @param dateTimeStr 日期时间字符串
* @return LocalDateTime对象,如果解析失败则返回null
*/
private LocalDateTime parseDateTime(String dateTimeStr) {
if (dateTimeStr == null || dateTimeStr.trim().isEmpty()) {
return null;
}
// 常见的日期时间格式列表(按使用频率排序,提高解析效率)
DateTimeFormatPattern[] patterns = {
// 带时分秒的格式(最常用)
new DateTimeFormatPattern("yyyy/MM/dd HH:mm:ss", true), // 2025/12/15 16:21:19
new DateTimeFormatPattern("yyyy-MM-dd HH:mm:ss", true), // 2025-12-15 16:21:19
new DateTimeFormatPattern("yyyy/M/d H:m:s", true), // 2025/1/5 8:5:9
new DateTimeFormatPattern("yyyy-M-d H:m:s", true), // 2025-1-5 8:5:9
// 带时分的格式(秒数默认为00)
new DateTimeFormatPattern("yyyy/MM/dd HH:mm", true), // 2025/12/15 16:21
new DateTimeFormatPattern("yyyy-MM-dd HH:mm", true), // 2025-12-15 16:21
new DateTimeFormatPattern("yyyy/M/d H:m", true), // 2025/1/5 8:5
new DateTimeFormatPattern("yyyy-M-d H:m", true), // 2025-1-5 8:5
// 中文格式
new DateTimeFormatPattern("yyyy年MM月dd日 HH:mm:ss", true), // 2025年12月15日 16:21:19
new DateTimeFormatPattern("yyyy年MM月dd日 HH:mm", true), // 2025年12月15日 16:21
new DateTimeFormatPattern("yyyy年M月d日 H:m:s", true), // 2025年1月5日 8:5:9
new DateTimeFormatPattern("yyyy年M月d日 H:m", true), // 2025年1月5日 8:5
// 仅日期格式(时间默认为00:00:00)
new DateTimeFormatPattern("yyyy/MM/dd", false), // 2025/12/15
new DateTimeFormatPattern("yyyy-MM-dd", false), // 2025-12-15
new DateTimeFormatPattern("yyyy/M/d", false), // 2025/1/5
new DateTimeFormatPattern("yyyy-M-d", false), // 2025-1-5
new DateTimeFormatPattern("yyyy年MM月dd日", false), // 2025年12月15日
new DateTimeFormatPattern("yyyy年M月d日", false) // 2025年1月5日
};
// 尝试使用每种格式解析
for (DateTimeFormatPattern pattern : patterns) {
try {
if (pattern.hasTime) {
// 包含时间部分,直接解析为LocalDateTime
LocalDateTime result = LocalDateTime.parse(dateTimeStr, pattern.formatter);
if (logger.isDebugEnabled()) {
logger.debug("成功使用格式 '{}' 解析时间 '{}' -> {}", pattern.pattern, dateTimeStr, result);
}
return result;
} else {
// 只有日期部分,解析为LocalDate后转换为LocalDateTime(时间为00:00:00)
LocalDateTime result = java.time.LocalDate.parse(dateTimeStr, pattern.formatter).atStartOfDay();
if (logger.isDebugEnabled()) {
logger.debug("成功使用格式 '{}' 解析日期 '{}' -> {}", pattern.pattern, dateTimeStr, result);
}
return result;
}
} catch (DateTimeParseException e) {
// 当前格式不匹配,尝试下一个(trace级别,避免太多日志)
if (logger.isTraceEnabled()) {
logger.trace("尝试使用格式 '{}' 解析时间 '{}' 失败: {}", pattern.pattern, dateTimeStr, e.getMessage());
}
}
}
// 所有格式都失败
logger.warn("无法解析日期时间字符串: '{}',支持的格式包括: yyyy/MM/dd HH:mm:ss, yyyy-MM-dd HH:mm:ss 等", dateTimeStr);
return null;
}
/**
* 从CSV记录获取值
*