feat(ec_product): 调整商品表唯一约束为公司ID+商品编码+商品名称联合唯一

- product_code 改为 NOT NULL DEFAULT ''(空字符串代替NULL)
- 删除旧的 uk_product_code 和 uk_company_name 唯一约束
- 新增 uk_company_code_name 联合唯一约束
- 更新 ProductServiceImpl 三处唯一性校验逻辑
- 订单导入逻辑同步更新
- 新增迁移脚本 sql/migration/V20260502__adjust_product_unique_constraint.sql
This commit is contained in:
小蟑螂
2026-05-02 15:09:52 +08:00
parent ddfde20efc
commit 473736ecf6
6 changed files with 180 additions and 127 deletions
@@ -161,6 +161,8 @@ public class ImportRuntimeContext {
final ConcurrentHashMap<String, ProductSpecPO> specByCodeCache = new ConcurrentHashMap<>();
final ConcurrentHashMap<String, ProductPO> productByCodeCache = new ConcurrentHashMap<>();
final ConcurrentHashMap<String, ProductPO> productByNameCache = new ConcurrentHashMap<>();
/** 与 uk_company_code_name 对齐:规范化编码与名称用 U+0001 拼接作为键 */
final ConcurrentHashMap<String, ProductPO> productByCodeAndNameCache = new ConcurrentHashMap<>();
final ConcurrentHashMap<Long, ProductPO> productByIdCache = new ConcurrentHashMap<>();
final ConcurrentHashMap<String, Long> statusIdByNameCache = new ConcurrentHashMap<>();
@@ -228,6 +230,7 @@ public class ImportRuntimeContext {
public Map<String, ProductSpecPO> getSpecByCodeCache() { return specByCodeCache; }
public Map<String, ProductPO> getProductByCodeCache() { return productByCodeCache; }
public Map<String, ProductPO> getProductByNameCache() { return productByNameCache; }
public Map<String, ProductPO> getProductByCodeAndNameCache() { return productByCodeAndNameCache; }
public Map<Long, ProductPO> getProductByIdCache() { return productByIdCache; }
public Map<String, Long> getStatusIdByNameCache() { return statusIdByNameCache; }
public Set<String> getExistingPlatformOrderNos() { return existingPlatformOrderNos; }
@@ -981,13 +981,8 @@ public class OrderImportProcessor {
}
if (productSpec == null) {
top.xyquan.ecomdmbbackend.modules.ecom.product.model.po.ProductPO product = null;
if (productCode != null) {
product = specHelper.findProductByProductCode(productCode, companyId, context);
}
if (product == null && linkProductName != null) {
product = specHelper.findProductByName(linkProductName, companyId, context);
}
top.xyquan.ecomdmbbackend.modules.ecom.product.model.po.ProductPO product =
specHelper.findProductForOrderImport(companyId, productCode, linkProductName, context);
if (product != null) {
String finalSpecName = (linkSpecName != null) ? linkSpecName : "未知规格";
productSpec = specHelper.createSpecForProduct(
@@ -31,6 +31,8 @@ import java.util.Set;
public class ProductSpecImportHelper {
private static final int IN_QUERY_BATCH_SIZE = 1000;
/** 与 {@link ImportRuntimeContext#productByCodeAndNameCache} 键拼接,避免与名称字符冲突 */
private static final char PRODUCT_CODE_NAME_SEP = '\u0001';
private final ProductMapper productMapper;
private final ProductSpecService productSpecService;
@@ -57,22 +59,99 @@ public class ProductSpecImportHelper {
public ProductPO findProductByProductCode(String productCode, Long companyId,
ImportRuntimeContext context) {
if (productCode == null || productCode.trim().isEmpty()) return null;
return context.productByCodeCache.get(productCode.trim());
return findProductForOrderImport(companyId, productCode, null, context);
}
public ProductPO findProductByName(String productName, Long companyId,
ImportRuntimeContext context) {
if (productName == null || productName.trim().isEmpty()) return null;
return context.productByNameCache.get(productName.trim());
return findProductForOrderImport(companyId, null, productName, context);
}
/**
* 插入商品;若触发唯一约束(同公司同名或全局商品编码冲突),则改为从库中加载已存在行。
* 用于并行导入时多线程同时建同一商品的兜底。
* 按 uk_company_code_name(company_id, product_code, name) 语义匹配商品。
* 编码与名称均非空时仅按联合键;仅编码时要求公司下该编码唯一;仅名称时要求同名唯一。
*/
public ProductPO findProductForOrderImport(Long companyId, String productCode, String linkProductName,
ImportRuntimeContext context) {
String code = normalizeProductCode(productCode);
String name = linkProductName != null ? linkProductName.trim() : "";
boolean hasCode = !code.isEmpty();
boolean hasName = !name.isEmpty();
if (!hasCode && !hasName) {
return null;
}
if (hasCode && hasName) {
String key = productCompositeKey(code, name);
ProductPO cached = context.productByCodeAndNameCache.get(key);
if (cached != null) {
return cached;
}
ProductPO fromDb = selectProductByCompanyCodeNameFromDb(companyId, name, code);
if (fromDb != null) {
cacheProduct(context, fromDb);
}
return fromDb;
}
if (hasCode) {
LambdaQueryWrapper<ProductPO> w = new LambdaQueryWrapper<>();
w.eq(ProductPO::getCompanyId, companyId).eq(ProductPO::getProductCode, code);
List<ProductPO> list = productMapper.selectList(w);
if (list.isEmpty()) {
return null;
}
if (list.size() == 1) {
cacheProduct(context, list.get(0));
return list.get(0);
}
throw new BusinessException(
"该公司下存在多条相同商品编码但名称不同,请在本行填写链接商品名称以便唯一匹配");
}
LambdaQueryWrapper<ProductPO> w = new LambdaQueryWrapper<>();
w.eq(ProductPO::getCompanyId, companyId).eq(ProductPO::getName, name);
List<ProductPO> list = productMapper.selectList(w);
if (list.isEmpty()) {
return null;
}
if (list.size() == 1) {
cacheProduct(context, list.get(0));
return list.get(0);
}
throw new BusinessException(
"该公司下存在多条同名商品(编码不同),请在本行填写商品编码以便唯一匹配");
}
private static String normalizeProductCode(String productCode) {
if (productCode == null) {
return "";
}
return productCode.trim();
}
private static String productCompositeKey(String normalizedProductCode, String trimmedProductName) {
return normalizedProductCode + PRODUCT_CODE_NAME_SEP + trimmedProductName;
}
private ProductPO selectProductByCompanyCodeNameFromDb(Long companyId, String name, String normalizedCode) {
LambdaQueryWrapper<ProductPO> w = new LambdaQueryWrapper<>();
w.eq(ProductPO::getCompanyId, companyId)
.eq(ProductPO::getName, name)
.eq(ProductPO::getProductCode, normalizedCode);
return productMapper.selectOne(w);
}
/**
* 插入商品;若触发 uk_company_code_name 冲突,则按公司+编码+名称从库中加载已存在行。
*/
private ProductPO insertProductOrFetchExisting(ProductPO draft, Long companyId,
ImportRuntimeContext context) {
if (draft.getName() == null || draft.getName().trim().isEmpty()) {
throw new BusinessException("商品名称不能为空");
}
draft.setName(draft.getName().trim());
draft.setProductCode(normalizeProductCode(draft.getProductCode()));
try {
productMapper.insert(draft);
return draft;
@@ -83,30 +162,16 @@ public class ProductSpecImportHelper {
}
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;
}
ProductPO existing = selectProductByCompanyCodeNameFromDb(companyId, draft.getName(),
draft.getProductCode());
if (existing != null) {
return existing;
}
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) {
@@ -211,24 +276,23 @@ public class ProductSpecImportHelper {
String specCode, String specName,
BigDecimal productCost, Long companyId,
Long createBy, ImportRuntimeContext context) {
ProductPO product;
String trimmedCode = productCode != null ? productCode.trim() : null;
String trimmedName = productName != null ? productName.trim() : null;
if (trimmedCode != null && !trimmedCode.isEmpty()) {
product = findProductByProductCode(trimmedCode, companyId, context);
} else if (trimmedName != null && !trimmedName.isEmpty()) {
product = findProductByName(trimmedName, companyId, context);
} else {
if ((trimmedCode == null || trimmedCode.isEmpty())
&& (trimmedName == null || trimmedName.isEmpty())) {
throw new BusinessException("商品编码和商品名称不能同时为空");
}
ProductPO product = findProductForOrderImport(companyId, productCode, productName, context);
if (product == null) {
product = new ProductPO();
product.setCompanyId(companyId);
product.setProductCode(trimmedCode);
String codeNorm = trimmedCode != null && !trimmedCode.isEmpty() ? trimmedCode : "";
product.setProductCode(codeNorm);
String resolvedName =
trimmedName != null ? trimmedName : (trimmedCode != null ? trimmedCode : "未知商品");
trimmedName != null && !trimmedName.isEmpty() ? trimmedName
: (trimmedCode != null && !trimmedCode.isEmpty() ? trimmedCode : "未知商品");
product.setName(resolvedName);
product.setLinkName(resolvedName);
product.setCreateBy(createBy);
@@ -295,48 +359,25 @@ public class ProductSpecImportHelper {
String productCode, String specName,
String specCode, Long createBy,
ImportRuntimeContext context) {
// 1. 通过商品名称查找商品
LambdaQueryWrapper<ProductPO> productWrapper =
new LambdaQueryWrapper<>();
productWrapper.eq(ProductPO::getName, productName)
.eq(ProductPO::getCompanyId, companyId);
ProductPO product = productMapper.selectOne(productWrapper);
if (productName == null || productName.trim().isEmpty()) {
throw new BusinessException("商品名称不能为空");
}
String trimmedName = productName.trim();
ProductPO product = findProductForOrderImport(companyId, productCode, trimmedName, context);
Long productId;
if (product == null) {
// 双重检查(避免并发问题)
LambdaQueryWrapper<ProductPO> checkWrapper = new LambdaQueryWrapper<>();
checkWrapper.eq(ProductPO::getName, productName)
.eq(ProductPO::getCompanyId, companyId);
if (productMapper.selectCount(checkWrapper) > 0) {
product = productMapper.selectOne(productWrapper);
productId = product.getId();
} else {
product = new ProductPO();
product.setCompanyId(companyId);
product.setName(productName);
product.setLinkName(productName);
String trimmedCode = productCode != null ? productCode.trim() : null;
if (trimmedCode != null && !trimmedCode.isEmpty()) {
LambdaQueryWrapper<ProductPO> codeCheckWrapper = new LambdaQueryWrapper<>();
codeCheckWrapper.eq(ProductPO::getProductCode, trimmedCode)
.eq(ProductPO::getCompanyId, companyId);
if (productMapper.selectCount(codeCheckWrapper) > 0) {
throw new BusinessException("商品编码已存在: " + trimmedCode);
}
product.setProductCode(trimmedCode);
}
product.setCreateBy(createBy);
product = insertProductOrFetchExisting(product, companyId, context);
productId = product.getId();
}
} else {
productId = product.getId();
product = new ProductPO();
product.setCompanyId(companyId);
product.setName(trimmedName);
product.setLinkName(trimmedName);
String trimmedCode = productCode != null ? productCode.trim() : "";
product.setProductCode(trimmedCode.isEmpty() ? "" : trimmedCode);
product.setCreateBy(createBy);
product = insertProductOrFetchExisting(product, companyId, context);
}
cacheProduct(context, product);
// 2. 创建规格(使用原有 ProductPO 重载,保持逻辑一致)
return createSpecForProduct(product, specCode, specName, null, companyId, createBy, context);
}
@@ -350,12 +391,15 @@ public class ProductSpecImportHelper {
public void cacheProduct(ImportRuntimeContext context, ProductPO product) {
context.productByIdCache.put(product.getId(), product);
if (product.getName() != null) {
context.productByNameCache.put(product.getName(), product);
String nm = product.getName() != null ? product.getName().trim() : "";
String cd = normalizeProductCode(product.getProductCode());
if (!nm.isEmpty()) {
context.productByNameCache.put(nm, product);
}
if (product.getProductCode() != null) {
context.productByCodeCache.put(product.getProductCode(), product);
if (!cd.isEmpty()) {
context.productByCodeCache.put(cd, product);
}
context.productByCodeAndNameCache.put(productCompositeKey(cd, nm), product);
}
// ==================== 订单状态 ====================
@@ -65,21 +65,14 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
// 从token中获取公司ID
Long companyId = currentUserContext.getCurrentCompanyId();
// 验证商品名称是否重复
LambdaQueryWrapper<ProductPO> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(ProductPO::getName, createProductDTO.getName())
.eq(ProductPO::getCompanyId, companyId);
if (this.count(wrapper) > 0) {
throw new BusinessException("商品名称已存在");
}
// 验证商品编码是否重复(如果不为空)
if (createProductDTO.getProductCode() != null && !createProductDTO.getProductCode().trim().isEmpty()) {
LambdaQueryWrapper<ProductPO> codeWrapper = new LambdaQueryWrapper<>();
codeWrapper.eq(ProductPO::getProductCode, createProductDTO.getProductCode().trim());
if (this.count(codeWrapper) > 0) {
throw new BusinessException("商品编码已存在");
}
// 验证商品名称 + 商品编码联合唯一(公司维度)
String productCode = (createProductDTO.getProductCode() != null ? createProductDTO.getProductCode().trim() : "");
LambdaQueryWrapper<ProductPO> uniqueWrapper = new LambdaQueryWrapper<>();
uniqueWrapper.eq(ProductPO::getCompanyId, companyId)
.eq(ProductPO::getName, createProductDTO.getName())
.eq(ProductPO::getProductCode, productCode);
if (this.count(uniqueWrapper) > 0) {
throw new BusinessException("同公司下商品名称与商品编码组合已存在");
}
// 创建商品
@@ -143,17 +136,14 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
@Override
@Transactional
public ProductVO createProductForImport(CreateProductDTO createProductDTO, Long companyId, Long userId) {
LambdaQueryWrapper<ProductPO> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(ProductPO::getName, createProductDTO.getName()).eq(ProductPO::getCompanyId, companyId);
if (this.count(wrapper) > 0) {
throw new BusinessException("商品名称已存在");
}
if (createProductDTO.getProductCode() != null && !createProductDTO.getProductCode().trim().isEmpty()) {
LambdaQueryWrapper<ProductPO> codeWrapper = new LambdaQueryWrapper<>();
codeWrapper.eq(ProductPO::getProductCode, createProductDTO.getProductCode().trim());
if (this.count(codeWrapper) > 0) {
throw new BusinessException("商品编码已存在");
}
// 验证商品名称 + 商品编码联合唯一(公司维度)
String productCode = (createProductDTO.getProductCode() != null ? createProductDTO.getProductCode().trim() : "");
LambdaQueryWrapper<ProductPO> uniqueWrapper = new LambdaQueryWrapper<>();
uniqueWrapper.eq(ProductPO::getCompanyId, companyId)
.eq(ProductPO::getName, createProductDTO.getName())
.eq(ProductPO::getProductCode, productCode);
if (this.count(uniqueWrapper) > 0) {
throw new BusinessException("同公司下商品名称与商品编码组合已存在");
}
ProductPO product = new ProductPO();
BeanUtils.copyProperties(createProductDTO, product);
@@ -227,23 +217,15 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
throw new BusinessException("商品不存在");
}
// 验证商品名称是否重复(排除当前商品)
LambdaQueryWrapper<ProductPO> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(ProductPO::getName, updateProductDTO.getName())
.eq(ProductPO::getCompanyId, companyId)
// 验证商品名称 + 商品编码联合唯一(公司维度,排除当前商品)
String productCode = (updateProductDTO.getProductCode() != null ? updateProductDTO.getProductCode().trim() : "");
LambdaQueryWrapper<ProductPO> uniqueWrapper = new LambdaQueryWrapper<>();
uniqueWrapper.eq(ProductPO::getCompanyId, companyId)
.eq(ProductPO::getName, updateProductDTO.getName())
.eq(ProductPO::getProductCode, productCode)
.ne(ProductPO::getId, id);
if (this.count(wrapper) > 0) {
throw new BusinessException("商品名称已存在");
}
// 验证商品编码是否重复(如果不为空,排除当前商品)
if (updateProductDTO.getProductCode() != null && !updateProductDTO.getProductCode().trim().isEmpty()) {
LambdaQueryWrapper<ProductPO> codeWrapper = new LambdaQueryWrapper<>();
codeWrapper.eq(ProductPO::getProductCode, updateProductDTO.getProductCode().trim())
.ne(ProductPO::getId, id);
if (this.count(codeWrapper) > 0) {
throw new BusinessException("商品编码已存在");
}
if (this.count(uniqueWrapper) > 0) {
throw new BusinessException("同公司下商品名称与商品编码组合已存在");
}
// 更新商品