mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 01:59:20 +08:00
import: 优化导入性能(去除多余查库+日期格式预判+合并CSV遍历)
- createProductAndSpecIfNotExists: 插入product后不再selectById验证 - createSpecForProduct: 新增直接接收ProductPO的重载,避免重复查库 - preloadImportContext: 合并到第一遍历,不再单独遍历CSV收集预加载数据 - parseDateTime: 首行检测日期格式并缓存到context,优先使用 - 新增detectDateFormat: 通过字符串特征快速判断格式,不抛异常
This commit is contained in:
+124
-65
@@ -162,11 +162,11 @@ public class ImportTaskProcessorServiceImpl implements ImportTaskProcessorServic
|
||||
}
|
||||
}
|
||||
|
||||
private static final int ORDER_BATCH_SIZE = 500; // 订单批量插入大小
|
||||
private static final int ORDER_BATCH_SIZE = 1000; // 订单批量插入大小
|
||||
private static final int RELATION_BATCH_SIZE = 500; // 商品关系批量插入大小
|
||||
private static final int PROGRESS_UPDATE_INTERVAL_ROWS = 50; // 进度更新间隔行数
|
||||
private static final long PROGRESS_NOTIFY_INTERVAL_MS = 2000L; // 进度通知间隔时间
|
||||
private static final int IN_QUERY_BATCH_SIZE = 500; // 内查询批量大小
|
||||
private static final int PROGRESS_UPDATE_INTERVAL_ROWS = 1000; // 进度更新间隔行数
|
||||
private static final long PROGRESS_NOTIFY_INTERVAL_MS = 1000L; // 进度通知间隔时间
|
||||
private static final int IN_QUERY_BATCH_SIZE = 1000; // 内查询批量大小
|
||||
|
||||
private static class PendingOrderInsert {
|
||||
private int rowNumber;
|
||||
@@ -200,6 +200,9 @@ public class ImportTaskProcessorServiceImpl implements ImportTaskProcessorServic
|
||||
private final Long shopId;
|
||||
private final Long createBy;
|
||||
|
||||
// 首行检测到的日期格式(null表示未检测)
|
||||
private DateTimeFormatPattern detectedDatePattern = null;
|
||||
|
||||
private final Map<String, ProductSpecPO> specByCodeCache = new HashMap<>();
|
||||
private final Map<String, ProductPO> productByCodeCache = new HashMap<>();
|
||||
private final Map<String, ProductPO> productByNameCache = new HashMap<>();
|
||||
@@ -388,14 +391,56 @@ public class ImportTaskProcessorServiceImpl implements ImportTaskProcessorServic
|
||||
.build())) {
|
||||
|
||||
|
||||
List<CSVRecord> records = csvParser.getRecords();
|
||||
totalCount = records.size();
|
||||
// 验证店铺
|
||||
ShopPO shop = shopService.getById(task.getShopId());
|
||||
if (shop == null || !shop.getCompanyId().equals(companyId)) {
|
||||
throw new BusinessException("店铺不存在");
|
||||
}
|
||||
|
||||
// 更新总行数
|
||||
// 第一 pass:边读 CSV 边收集预加载数据,同时构建 records 列表(合并原来第二次遍历)
|
||||
MappingField specCodeField = fieldMapping.get("spec_code");
|
||||
MappingField productCodeField = fieldMapping.get("product_code");
|
||||
MappingField linkProductNameField = fieldMapping.get("link_product_name");
|
||||
MappingField orderStatusField = fieldMapping.get("status");
|
||||
MappingField platformOrderNoField = fieldMapping.get("platform_order_no");
|
||||
MappingField orderDateField = fieldMapping.get("order_date");
|
||||
|
||||
Set<String> specCodes = new HashSet<>();
|
||||
Set<String> productCodes = new HashSet<>();
|
||||
Set<String> productNames = new HashSet<>();
|
||||
Set<String> platformOrderNos = new HashSet<>();
|
||||
Set<String> statusNames = new HashSet<>();
|
||||
DateTimeFormatPattern detectedDatePattern = null;
|
||||
List<CSVRecord> records = new ArrayList<>();
|
||||
|
||||
for (CSVRecord record : csvParser) {
|
||||
records.add(record);
|
||||
addNormalizedValue(specCodes, getValue(record, specCodeField));
|
||||
addNormalizedValue(productCodes, getValue(record, productCodeField));
|
||||
addNormalizedValue(productNames, getValue(record, linkProductNameField));
|
||||
addNormalizedValue(platformOrderNos, getValue(record, platformOrderNoField));
|
||||
addNormalizedValue(statusNames, getValue(record, orderStatusField));
|
||||
// 日期格式检测
|
||||
if (detectedDatePattern == null && orderDateField != null) {
|
||||
String dateStr = getValue(record, orderDateField);
|
||||
if (dateStr != null && !dateStr.trim().isEmpty()) {
|
||||
detectedDatePattern = detectDateFormat(dateStr.trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
totalCount = records.size();
|
||||
updateTaskStatus(task.getId(), "processing", null, totalCount, 0, 0);
|
||||
|
||||
context = new ImportRuntimeContext(companyId, task.getShopId(), task.getCreateBy());
|
||||
preloadImportContext(context, records, fieldMapping, statusMapping);
|
||||
context.detectedDatePattern = detectedDatePattern;
|
||||
|
||||
// 预加载(原来在 preloadImportContext 里,现在直接调用)
|
||||
preloadProductSpecs(context, specCodes);
|
||||
preloadProductsByCode(context, productCodes);
|
||||
preloadProductsByName(context, productNames);
|
||||
preloadPlatformOrderNos(context, platformOrderNos);
|
||||
preloadStatuses(context, statusNames, statusMapping);
|
||||
preloadShopProductRelations(context);
|
||||
|
||||
for (int i = 0; i < records.size(); i++) {
|
||||
CSVRecord record = records.get(i);
|
||||
@@ -792,7 +837,7 @@ public class ImportTaskProcessorServiceImpl implements ImportTaskProcessorServic
|
||||
// 解析下单时间(支持多种格式)
|
||||
String orderDateStr = getValue(record, orderDateField);
|
||||
if (orderDateStr != null && !orderDateStr.trim().isEmpty()) {
|
||||
LocalDateTime orderTime = parseDateTime(orderDateStr.trim());
|
||||
LocalDateTime orderTime = parseDateTime(orderDateStr.trim(), context);
|
||||
if (orderTime != null) {
|
||||
orderDTO.setOrderTime(orderTime);
|
||||
} else {
|
||||
@@ -827,10 +872,25 @@ public class ImportTaskProcessorServiceImpl implements ImportTaskProcessorServic
|
||||
* @param dateTimeStr 日期时间字符串
|
||||
* @return LocalDateTime对象,如果解析失败则返回null
|
||||
*/
|
||||
private LocalDateTime parseDateTime(String dateTimeStr) {
|
||||
private LocalDateTime parseDateTime(String dateTimeStr, ImportRuntimeContext context) {
|
||||
if (dateTimeStr == null || dateTimeStr.trim().isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
String trimmed = dateTimeStr.trim();
|
||||
|
||||
// 优先尝试首行检测到的格式(无异常,开销极低)
|
||||
if (context != null && context.detectedDatePattern != null) {
|
||||
try {
|
||||
DateTimeFormatPattern p = context.detectedDatePattern;
|
||||
if (p.hasTime) {
|
||||
return LocalDateTime.parse(trimmed, p.formatter);
|
||||
} else {
|
||||
return java.time.LocalDate.parse(trimmed, p.formatter).atStartOfDay();
|
||||
}
|
||||
} catch (DateTimeParseException ignored) {
|
||||
// 检测格式失败,继续尝试所有格式
|
||||
}
|
||||
}
|
||||
|
||||
// 常见的日期时间格式列表(按使用频率排序,提高解析效率)
|
||||
DateTimeFormatPattern[] patterns = {
|
||||
@@ -880,6 +940,35 @@ public class ImportTaskProcessorServiceImpl implements ImportTaskProcessorServic
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据日期字符串特征快速判断格式(不抛异常)
|
||||
*/
|
||||
private DateTimeFormatPattern detectDateFormat(String dateStr) {
|
||||
// 带时分秒的格式判断
|
||||
if (dateStr.contains("/")) {
|
||||
if (dateStr.contains(":")) {
|
||||
return new DateTimeFormatPattern("yyyy/MM/dd HH:mm:ss", true);
|
||||
}
|
||||
if (dateStr.contains("年")) {
|
||||
return new DateTimeFormatPattern("yyyy年MM月dd日", false);
|
||||
}
|
||||
return new DateTimeFormatPattern("yyyy/MM/dd", false);
|
||||
}
|
||||
if (dateStr.contains("-")) {
|
||||
if (dateStr.contains(":")) {
|
||||
return new DateTimeFormatPattern("yyyy-MM-dd HH:mm:ss", true);
|
||||
}
|
||||
if (dateStr.contains("年")) {
|
||||
return new DateTimeFormatPattern("yyyy年MM月dd日", false);
|
||||
}
|
||||
return new DateTimeFormatPattern("yyyy-MM-dd", false);
|
||||
}
|
||||
if (dateStr.contains("年")) {
|
||||
return new DateTimeFormatPattern("yyyy年MM月dd日", false);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从CSV记录获取值
|
||||
*
|
||||
@@ -1070,14 +1159,30 @@ public class ImportTaskProcessorServiceImpl implements ImportTaskProcessorServic
|
||||
* @param createBy 创建人ID
|
||||
* @return 商品规格
|
||||
*/
|
||||
/**
|
||||
* 直接用已知的 ProductPO 创建规格(避免重复 selectById)
|
||||
*/
|
||||
private ProductSpecPO createSpecForProduct(ProductPO product, String specName, String specCode, Long createBy,
|
||||
ImportRuntimeContext context) {
|
||||
Long productId = product.getId();
|
||||
Long companyId = product.getCompanyId();
|
||||
return createSpecForProductWithValidation(productId, companyId, specName, specCode, createBy, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 productId 查库后创建规格(保留给其他调用方)
|
||||
*/
|
||||
private ProductSpecPO createSpecForProduct(Long companyId, Long productId, String specName, String specCode, Long createBy,
|
||||
ImportRuntimeContext context) {
|
||||
// 验证商品是否存在
|
||||
ProductPO existingProduct = productMapper.selectById(productId);
|
||||
if (existingProduct == null || !existingProduct.getCompanyId().equals(companyId)) {
|
||||
throw new BusinessException("商品不存在");
|
||||
}
|
||||
|
||||
return createSpecForProductWithValidation(productId, companyId, specName, specCode, createBy, context);
|
||||
}
|
||||
|
||||
private ProductSpecPO createSpecForProductWithValidation(Long productId, Long companyId, String specName, String specCode, Long createBy,
|
||||
ImportRuntimeContext context) {
|
||||
// 验证规格名称是否重复
|
||||
com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<ProductSpecPO> specCheckWrapper =
|
||||
new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<>();
|
||||
@@ -1087,7 +1192,7 @@ public class ImportTaskProcessorServiceImpl implements ImportTaskProcessorServic
|
||||
if (productSpecService.count(specCheckWrapper) > 0) {
|
||||
throw new BusinessException("规格名称已存在");
|
||||
}
|
||||
|
||||
|
||||
// 验证规格编码是否重复
|
||||
com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<ProductSpecPO> specCodeCheckWrapper =
|
||||
new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<>();
|
||||
@@ -1096,7 +1201,7 @@ public class ImportTaskProcessorServiceImpl implements ImportTaskProcessorServic
|
||||
if (productSpecService.count(specCodeCheckWrapper) > 0) {
|
||||
throw new BusinessException("规格编码已存在");
|
||||
}
|
||||
|
||||
|
||||
// 创建商品规格
|
||||
ProductSpecPO productSpec = new ProductSpecPO();
|
||||
productSpec.setCompanyId(companyId);
|
||||
@@ -1104,9 +1209,9 @@ public class ImportTaskProcessorServiceImpl implements ImportTaskProcessorServic
|
||||
productSpec.setName(specName);
|
||||
productSpec.setLinkName(specName); // 导入时链接名称默认等于规格名称
|
||||
productSpec.setSpecCode(specCode);
|
||||
productSpec.setProductCost(BigDecimal.ZERO); // 默认商品成本为0,后续可以手动更新
|
||||
productSpec.setShippingCost(BigDecimal.ZERO); // 默认运费成本为0
|
||||
productSpec.setOtherCost(BigDecimal.ZERO); // 默认其他成本为0
|
||||
productSpec.setProductCost(BigDecimal.ZERO);
|
||||
productSpec.setShippingCost(BigDecimal.ZERO);
|
||||
productSpec.setOtherCost(BigDecimal.ZERO);
|
||||
productSpec.setCreateBy(createBy);
|
||||
productSpecService.save(productSpec);
|
||||
context.specByCodeCache.put(specCode, productSpec);
|
||||
@@ -1170,22 +1275,11 @@ public class ImportTaskProcessorServiceImpl implements ImportTaskProcessorServic
|
||||
product.setCreateBy(createBy);
|
||||
productMapper.insert(product);
|
||||
productId = product.getId();
|
||||
|
||||
// 验证商品确实创建成功
|
||||
if (productId == null) {
|
||||
throw new BusinessException("商品创建失败,未返回商品ID");
|
||||
}
|
||||
|
||||
// 验证商品确实存在(刷新实体,确保数据已提交)
|
||||
ProductPO verifyProduct = productMapper.selectById(productId);
|
||||
if (verifyProduct == null) {
|
||||
throw new BusinessException("商品创建后验证失败,商品ID: " + productId);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
productId = product.getId();
|
||||
}
|
||||
|
||||
|
||||
// 2. 创建商品规格(复用ProductSpecService.createProductSpec的逻辑)
|
||||
context.productByIdCache.put(productId, product);
|
||||
if (product.getName() != null) {
|
||||
@@ -1194,7 +1288,7 @@ public class ImportTaskProcessorServiceImpl implements ImportTaskProcessorServic
|
||||
if (product.getProductCode() != null) {
|
||||
context.productByCodeCache.put(product.getProductCode(), product);
|
||||
}
|
||||
return createSpecForProduct(companyId, productId, specName, specCode, createBy, context);
|
||||
return createSpecForProduct(product, specName, specCode, createBy, context);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1321,41 +1415,6 @@ public class ImportTaskProcessorServiceImpl implements ImportTaskProcessorServic
|
||||
}
|
||||
}
|
||||
|
||||
private void preloadImportContext(ImportRuntimeContext context, List<CSVRecord> records, Map<String, MappingField> fieldMapping,
|
||||
Map<String, Long> statusMapping) {
|
||||
ShopPO shop = shopService.getById(context.shopId);
|
||||
if (shop == null || !shop.getCompanyId().equals(context.companyId)) {
|
||||
throw new BusinessException("店铺不存在");
|
||||
}
|
||||
|
||||
MappingField specCodeField = fieldMapping.get("spec_code");
|
||||
MappingField productCodeField = fieldMapping.get("product_code");
|
||||
MappingField linkProductNameField = fieldMapping.get("link_product_name");
|
||||
MappingField orderStatusField = fieldMapping.get("status");
|
||||
MappingField platformOrderNoField = fieldMapping.get("platform_order_no");
|
||||
|
||||
Set<String> specCodes = new HashSet<>();
|
||||
Set<String> productCodes = new HashSet<>();
|
||||
Set<String> productNames = new HashSet<>();
|
||||
Set<String> platformOrderNos = new HashSet<>();
|
||||
Set<String> statusNames = new HashSet<>();
|
||||
|
||||
for (CSVRecord record : records) {
|
||||
addNormalizedValue(specCodes, getValue(record, specCodeField));
|
||||
addNormalizedValue(productCodes, getValue(record, productCodeField));
|
||||
addNormalizedValue(productNames, getValue(record, linkProductNameField));
|
||||
addNormalizedValue(platformOrderNos, getValue(record, platformOrderNoField));
|
||||
addNormalizedValue(statusNames, getValue(record, orderStatusField));
|
||||
}
|
||||
|
||||
preloadProductSpecs(context, specCodes);
|
||||
preloadProductsByCode(context, productCodes);
|
||||
preloadProductsByName(context, productNames);
|
||||
preloadPlatformOrderNos(context, platformOrderNos);
|
||||
preloadStatuses(context, statusNames, statusMapping);
|
||||
preloadShopProductRelations(context);
|
||||
}
|
||||
|
||||
private void addNormalizedValue(Set<String> target, String rawValue) {
|
||||
if (rawValue == null) {
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user