refactor: 全公司商品编码唯一

This commit is contained in:
2026-08-18 23:18:46 +08:00
parent 12bb03a43f
commit 63aeb330a2
6 changed files with 135 additions and 78 deletions
@@ -165,8 +165,6 @@ public class ImportRuntimeContext {
final ConcurrentHashMap<String, ProductSkuPO> skuByCodeCache = 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, String> statusCodeByNameCache = new ConcurrentHashMap<>();
@@ -248,7 +246,6 @@ public class ImportRuntimeContext {
public Map<String, ProductSkuPO> getSpecByCodeCache() { return skuByCodeCache; }
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, String> getStatusCodeByNameCache() { return statusCodeByNameCache; }
public Set<String> getExistingPlatformOrderNos() { return existingPlatformOrderNos; }
@@ -38,8 +38,6 @@ import java.util.Set;
public class ProductSkuImportHelper {
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 ProductSkuService productSkuService;
@@ -85,47 +83,30 @@ public class ProductSkuImportHelper {
}
/**
* 按 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 hasCode = code != null;
boolean hasName = !name.isEmpty();
if (!hasCode && !hasName) {
return null;
}
if (hasCode && hasName) {
String key = productCompositeKey(code, name);
ProductPO cached = context.productByCodeAndNameCache.get(key);
if (hasCode) {
ProductPO cached = context.productByCodeCache.get(code);
if (cached != null) {
return cached;
}
ProductPO fromDb = selectProductByCompanyCodeNameFromDb(companyId, name, code);
ProductPO fromDb = selectProductByCompanyCodeFromDb(companyId, 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);
@@ -140,27 +121,22 @@ public class ProductSkuImportHelper {
"该公司下存在多条同名商品(编码不同),请在本行填写商品编码以便唯一匹配");
}
/** 空白编码规范为 null。 */
private static String normalizeProductCode(String productCode) {
if (productCode == null) {
return "";
if (productCode == null || productCode.isBlank()) {
return null;
}
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) {
private ProductPO selectProductByCompanyCodeFromDb(Long companyId, String normalizedCode) {
LambdaQueryWrapper<ProductPO> w = new LambdaQueryWrapper<>();
w.eq(ProductPO::getCompanyId, companyId)
.eq(ProductPO::getName, name)
.eq(ProductPO::getProductCode, normalizedCode);
w.eq(ProductPO::getCompanyId, companyId).eq(ProductPO::getProductCode, normalizedCode);
return productMapper.selectOne(w);
}
/**
* 插入商品;若触发 uk_company_code_name 冲突,则按公司+编码+名称从库中加载已存在行。
* 插入商品;若触发 uk_ec_product_company_code 冲突,则按公司+编码从库中加载已存在行。
*/
private ProductPO insertProductOrFetchExisting(ProductPO draft, Long companyId,
ImportRuntimeContext context) {
@@ -179,13 +155,15 @@ public class ProductSkuImportHelper {
}
throw new RuntimeException(ex);
}
ProductPO existing = selectProductByCompanyCodeNameFromDb(companyId, draft.getName(),
draft.getProductCode());
if (draft.getProductCode() == null) {
throw new BusinessException("商品因唯一约束已存在但编码为空,无法按编码回查");
}
ProductPO existing = selectProductByCompanyCodeFromDb(companyId, draft.getProductCode());
if (existing != null) {
return existing;
}
throw new BusinessException(
"商品因唯一约束已存在但重新查询失败,请稍后重试导入或检查公司、商品编码与名称是否与已存在记录完全一致");
"商品因唯一约束已存在但重新查询失败,请稍后重试导入或检查公司与商品编码是否与已存在记录一致");
}
}
@@ -321,8 +299,7 @@ public class ProductSkuImportHelper {
if (product == null) {
product = new ProductPO();
product.setCompanyId(companyId);
String codeNorm = trimmedCode != null && !trimmedCode.isEmpty() ? trimmedCode : "";
product.setProductCode(codeNorm);
product.setProductCode(trimmedCode != null && !trimmedCode.isEmpty() ? trimmedCode : null);
String resolvedName =
trimmedName != null && !trimmedName.isEmpty() ? trimmedName
: (trimmedCode != null && !trimmedCode.isEmpty() ? trimmedCode : "未知商品");
@@ -403,7 +380,7 @@ public class ProductSkuImportHelper {
product.setCompanyId(companyId);
product.setName(trimmedName);
String trimmedCode = productCode != null ? productCode.trim() : "";
product.setProductCode(trimmedCode.isEmpty() ? "" : trimmedCode);
product.setProductCode(trimmedCode.isEmpty() ? null : trimmedCode);
product.setCreateBy(createBy);
product = insertProductOrFetchExisting(product, companyId, context);
}
@@ -427,10 +404,9 @@ public class ProductSkuImportHelper {
if (!nm.isEmpty()) {
context.productByNameCache.put(nm, product);
}
if (!cd.isEmpty()) {
if (cd != null) {
context.productByCodeCache.put(cd, product);
}
context.productByCodeAndNameCache.put(productCompositeKey(cd, nm), product);
}
// ==================== 订单状态 ====================
@@ -39,9 +39,9 @@ public class ProductPO {
private String name;
/**
* 商品编码
* 商品编码(公司内唯一;未填为空)
*/
@TableField("product_code")
@TableField(value = "product_code", updateStrategy = FieldStrategy.ALWAYS)
private String productCode;
/**
@@ -76,19 +76,12 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
// 从token中获取公司ID
Long companyId = currentUserContext.getCurrentCompanyId();
// 验证商品名称 + 商品编码联合唯一(公司维度)
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("同公司下商品名称与商品编码组合已存在");
}
assertProductCodeUnique(companyId, createProductDTO.getProductCode(), null);
// 创建商品
ProductPO product = new ProductPO();
BeanCopyUtils.copyProperties(createProductDTO, product);
product.setProductCode(normalizeProductCode(createProductDTO.getProductCode()));
// 设置公司ID
product.setCompanyId(companyId);
@@ -151,17 +144,10 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
@Override
@Transactional
public ProductVO createProductForImport(CreateProductDTO createProductDTO, Long companyId, Long userId) {
// 验证商品名称 + 商品编码联合唯一(公司维度)
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("同公司下商品名称与商品编码组合已存在");
}
assertProductCodeUnique(companyId, createProductDTO.getProductCode(), null);
ProductPO product = new ProductPO();
BeanCopyUtils.copyProperties(createProductDTO, product);
product.setProductCode(normalizeProductCode(createProductDTO.getProductCode()));
product.setCompanyId(companyId);
product.setCreateBy(userId);
this.save(product);
@@ -404,22 +390,14 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
throw new BusinessException("商品不存在");
}
// 验证商品名称 + 商品编码联合唯一(公司维度,排除当前商品)
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(uniqueWrapper) > 0) {
throw new BusinessException("同公司下商品名称与商品编码组合已存在");
}
assertProductCodeUnique(companyId, updateProductDTO.getProductCode(), id);
// 更新商品
ProductPO product = new ProductPO();
BeanCopyUtils.copyProperties(updateProductDTO, product);
product.setId(id);
product.setCompanyId(companyId);
product.setProductCode(normalizeProductCode(updateProductDTO.getProductCode()));
// 处理主图图片关联
if (updateProductDTO.getMainImageId() != null) {
@@ -640,4 +618,29 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
private ProductSkuVO convertSkuToVO(ProductSkuPO productSku) {
return productSkuService.toVo(productSku);
}
/** 公司内商品编码唯一;未填不校验。 */
private void assertProductCodeUnique(Long companyId, String productCode, Long excludeId) {
String code = normalizeProductCode(productCode);
if (code == null) {
return;
}
LambdaQueryWrapper<ProductPO> uniqueWrapper = new LambdaQueryWrapper<>();
uniqueWrapper.eq(ProductPO::getCompanyId, companyId)
.eq(ProductPO::getProductCode, code);
if (excludeId != null) {
uniqueWrapper.ne(ProductPO::getId, excludeId);
}
if (this.count(uniqueWrapper) > 0) {
throw new BusinessException("同公司下商品编码已存在");
}
}
/** 空白编码规范为 null,不参与唯一约束。 */
private static String normalizeProductCode(String productCode) {
if (productCode == null || productCode.isBlank()) {
return null;
}
return productCode.trim();
}
}