mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 01:59:20 +08:00
fix(ecom-dmb): 导入商品时捕获唯一键冲突异常,改为查询已存在记录,避免并发导入重复商品
This commit is contained in:
+67
-11
@@ -1,6 +1,9 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.ecom.importtask.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
|
||||
import top.xyquan.ecomdmbbackend.common.exception.BusinessException;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.order.mapper.OrderMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.order.model.po.OrderPO;
|
||||
@@ -64,6 +67,63 @@ public class ProductSpecImportHelper {
|
||||
return context.productByNameCache.get(productName.trim());
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入商品;若触发唯一约束(同公司同名或全局商品编码冲突),则改为从库中加载已存在行。
|
||||
* 用于并行导入时多线程同时建同一商品的兜底。
|
||||
*/
|
||||
private ProductPO insertProductOrFetchExisting(ProductPO draft, Long companyId,
|
||||
ImportRuntimeContext context) {
|
||||
try {
|
||||
productMapper.insert(draft);
|
||||
return draft;
|
||||
} catch (Exception ex) {
|
||||
if (!isDuplicateKeyException(ex)) {
|
||||
if (ex instanceof RuntimeException re) {
|
||||
throw re;
|
||||
}
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
ProductPO byName = selectProductByCompanyAndNameFromDb(companyId, draft.getName());
|
||||
if (byName != null) {
|
||||
return byName;
|
||||
}
|
||||
String code = draft.getProductCode();
|
||||
if (code != null && !code.trim().isEmpty()) {
|
||||
LambdaQueryWrapper<ProductPO> w = new LambdaQueryWrapper<>();
|
||||
w.eq(ProductPO::getCompanyId, companyId).eq(ProductPO::getProductCode, code.trim());
|
||||
ProductPO byCode = productMapper.selectOne(w);
|
||||
if (byCode != null) {
|
||||
return byCode;
|
||||
}
|
||||
}
|
||||
throw new BusinessException(
|
||||
"商品因唯一约束已存在但重新查询失败,请稍后重试导入或检查是否存在重复名称/编码");
|
||||
}
|
||||
}
|
||||
|
||||
private ProductPO selectProductByCompanyAndNameFromDb(Long companyId, String name) {
|
||||
LambdaQueryWrapper<ProductPO> w = new LambdaQueryWrapper<>();
|
||||
w.eq(ProductPO::getCompanyId, companyId).eq(ProductPO::getName, name);
|
||||
return productMapper.selectOne(w);
|
||||
}
|
||||
|
||||
private static boolean isDuplicateKeyException(Throwable ex) {
|
||||
Throwable t = ex;
|
||||
while (t != null) {
|
||||
if (t instanceof DuplicateKeyException) {
|
||||
return true;
|
||||
}
|
||||
if (t instanceof java.sql.SQLIntegrityConstraintViolationException) {
|
||||
String msg = t.getMessage();
|
||||
if (msg != null && (msg.contains("Duplicate") || msg.contains("duplicate"))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
t = t.getCause();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// ==================== 商品/规格创建 ====================
|
||||
|
||||
/**
|
||||
@@ -127,9 +187,12 @@ public class ProductSpecImportHelper {
|
||||
product = new ProductPO();
|
||||
product.setCompanyId(companyId);
|
||||
product.setProductCode(trimmedCode);
|
||||
product.setName(trimmedName != null ? trimmedName : (trimmedCode != null ? trimmedCode : "未知商品"));
|
||||
String resolvedName =
|
||||
trimmedName != null ? trimmedName : (trimmedCode != null ? trimmedCode : "未知商品");
|
||||
product.setName(resolvedName);
|
||||
product.setLinkName(resolvedName);
|
||||
product.setCreateBy(createBy);
|
||||
productMapper.insert(product);
|
||||
product = insertProductOrFetchExisting(product, companyId, context);
|
||||
cacheProduct(context, product);
|
||||
}
|
||||
|
||||
@@ -224,21 +287,14 @@ public class ProductSpecImportHelper {
|
||||
product.setProductCode(trimmedCode);
|
||||
}
|
||||
product.setCreateBy(createBy);
|
||||
productMapper.insert(product);
|
||||
product = insertProductOrFetchExisting(product, companyId, context);
|
||||
productId = product.getId();
|
||||
}
|
||||
} else {
|
||||
productId = product.getId();
|
||||
}
|
||||
|
||||
// 写入缓存
|
||||
context.productByIdCache.put(productId, product);
|
||||
if (product.getName() != null) {
|
||||
context.productByNameCache.put(product.getName(), product);
|
||||
}
|
||||
if (product.getProductCode() != null) {
|
||||
context.productByCodeCache.put(product.getProductCode(), product);
|
||||
}
|
||||
cacheProduct(context, product);
|
||||
|
||||
// 2. 创建规格(使用原有 ProductPO 重载,保持逻辑一致)
|
||||
return createSpecForProduct(product, specCode, specName, null, companyId, createBy, context);
|
||||
|
||||
Reference in New Issue
Block a user