fix: 修复并行导入两大bug — 死锁+FK外键;修复订单时间字段名order_date→order_time;修复successCount双重累加

This commit is contained in:
小蟑螂
2026-04-16 12:47:05 +08:00
parent 0a27d4ab57
commit 3ce5eba22e
3 changed files with 618 additions and 326 deletions
@@ -7,11 +7,17 @@ import top.xyquan.ecomdmbbackend.modules.ecom.shop.model.po.ShopProductRelationP
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* 导入运行时上下文(贯穿整个导入过程)
* <p>
* 所有共享类型(内部类/枚举)也定义在此文件,避免循环依赖。
* <p>
* 线程安全:并行导入时,多个 Worker 线程并发读写集合,
* 故缓存Map/Set使用 ConcurrentHashMap / ConcurrentHashMap.newKeySet(),
* pendingOrders 使用 CopyOnWriteArrayList(写时复制,并行添加,flush时整体替换)。
*/
public class ImportRuntimeContext {
@@ -123,37 +129,49 @@ public class ImportRuntimeContext {
public StringBuilder errorMessages = new StringBuilder();
}
// ==================== 上下文字段 ====================
// ==================== 上下文字段(线程安全) ====================
private final Long companyId;
private final Long shopId;
private final Long createBy;
private final Long taskId;
private DateTimeFormatPattern detectedDatePattern;
final Map<String, ProductSpecPO> specByCodeCache = new HashMap<>();
final Map<String, ProductPO> productByCodeCache = new HashMap<>();
final Map<String, ProductPO> productByNameCache = new HashMap<>();
final Map<Long, ProductPO> productByIdCache = new HashMap<>();
final Map<String, Long> statusIdByNameCache = new HashMap<>();
final Set<String> existingPlatformOrderNos = new HashSet<>();
final Map<String, Long> existingOrderIdByPlatformNo = new HashMap<>();
final Set<String> existingRelationKeys = new HashSet<>();
final List<PendingOrderInsert> pendingOrders = new ArrayList<>();
final Map<String, PendingOrderInsert> pendingOrderByPlatformNo = new HashMap<>();
final List<ShopProductRelationPO> pendingRelations = new ArrayList<>();
final Set<String> pendingRelationKeys = new HashSet<>();
final Set<String> pendingPlatformOrderNos = new HashSet<>();
// 缓存:只读(预加载后不再写入),使用 ConcurrentHashMap 确保 workers 并行读取安全
final ConcurrentHashMap<String, ProductSpecPO> specByCodeCache = new ConcurrentHashMap<>();
final ConcurrentHashMap<String, ProductPO> productByCodeCache = new ConcurrentHashMap<>();
final ConcurrentHashMap<String, ProductPO> productByNameCache = new ConcurrentHashMap<>();
final ConcurrentHashMap<Long, ProductPO> productByIdCache = new ConcurrentHashMap<>();
final ConcurrentHashMap<String, Long> statusIdByNameCache = new ConcurrentHashMap<>();
public ImportRuntimeContext(Long companyId, Long shopId, Long createBy) {
// 已存在于 DB 的数据:预加载时填充(只写一次),后续只读
final Set<String> existingPlatformOrderNos = ConcurrentHashMap.newKeySet();
final Map<String, Long> existingOrderIdByPlatformNo = new ConcurrentHashMap<>();
final Set<String> existingRelationKeys = ConcurrentHashMap.newKeySet();
// pendingOrders:由 workers 并发添加(CopyOnWriteArrayList 读多写少场景高效)
final CopyOnWriteArrayList<PendingOrderInsert> pendingOrders = new CopyOnWriteArrayList<>();
// pendingOrderByPlatformNo:workers 并发 put,flush 时读取并清空
final Map<String, PendingOrderInsert> pendingOrderByPlatformNo = Collections.synchronizedMap(new HashMap<>());
// pendingPlatformOrderNos:workers 并发 add,flush 时读取并清空
final Set<String> pendingPlatformOrderNos = Collections.synchronizedSet(new HashSet<>());
// pendingRelations:workers 并发添加,flush 时整体替换
final CopyOnWriteArrayList<ShopProductRelationPO> pendingRelations = new CopyOnWriteArrayList<>();
final Set<String> pendingRelationKeys = Collections.synchronizedSet(new HashSet<>());
public ImportRuntimeContext(Long companyId, Long shopId, Long createBy, Long taskId) {
this.companyId = companyId;
this.shopId = shopId;
this.createBy = createBy;
this.taskId = taskId;
}
public Long getCompanyId() { return companyId; }
public Long getShopId() { return shopId; }
public Long getCreateBy() { return createBy; }
public Long getTaskId() { return taskId; }
public DateTimeFormatPattern getDetectedDatePattern() { return detectedDatePattern; }
public void setDetectedDatePattern(DateTimeFormatPattern detectedDatePattern) { this.detectedDatePattern = detectedDatePattern; }
public Map<String, ProductSpecPO> getSpecByCodeCache() { return specByCodeCache; }
@@ -167,7 +167,7 @@ public class ImportTaskProcessorServiceImpl implements ImportTaskProcessorServic
}
Charset charset = detectFileEncoding(fileInputStream);
ImportRuntimeContext context = new ImportRuntimeContext(task.getCompanyId(), task.getShopId(), task.getCreateBy());
ImportRuntimeContext context = new ImportRuntimeContext(task.getCompanyId(), task.getShopId(), task.getCreateBy(), task.getId());
java.util.function.BiConsumer<Integer, Integer> progressCallback = (total, done) -> {
updateTaskStatus(task.getId(), "processing", null, total, done, 0);