mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-23 23:19:19 +08:00
refactor: 全公司商品编码唯一
This commit is contained in:
@@ -39,7 +39,7 @@ CREATE TABLE `ec_product` (
|
||||
`company_id` bigint UNSIGNED NOT NULL COMMENT '公司ID',
|
||||
`category_id` bigint UNSIGNED NULL DEFAULT NULL COMMENT '类目ID',
|
||||
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '商品名称',
|
||||
`product_code` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '' COMMENT '商品编码',
|
||||
`product_code` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '商品编码(公司内唯一;未填为空)',
|
||||
`main_image_id` bigint UNSIGNED NULL DEFAULT NULL COMMENT '主图图片ID(系统文件)',
|
||||
`main_image_url` varchar(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '主图外链URL(渠道自动建品等;查询时有 id 则覆盖)',
|
||||
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
|
||||
@@ -47,7 +47,7 @@ CREATE TABLE `ec_product` (
|
||||
`create_by` bigint UNSIGNED NULL DEFAULT NULL COMMENT '创建人ID',
|
||||
`update_by` bigint UNSIGNED NULL DEFAULT NULL COMMENT '更新人ID',
|
||||
PRIMARY KEY (`id`) USING BTREE,
|
||||
UNIQUE INDEX `uk_company_code_name`(`company_id` ASC, `product_code` ASC, `name` ASC) USING BTREE,
|
||||
UNIQUE INDEX `uk_ec_product_company_code`(`company_id` ASC, `product_code` ASC) USING BTREE COMMENT '同公司商品编码唯一(NULL 不参与)',
|
||||
INDEX `idx_company_id`(`company_id` ASC) USING BTREE,
|
||||
INDEX `idx_category_id`(`category_id` ASC) USING BTREE,
|
||||
INDEX `fk_ec_product_main_image`(`main_image_id` ASC) USING BTREE,
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
-- ec_product:商品编码改为公司内唯一(去掉与名称的联合唯一)
|
||||
-- 未填编码规范为 NULL,避免原空串 + 不同名称的多行无法加唯一索引
|
||||
-- 同公司非空编码重复:保留最小 id,其余行的 SKU/关联并入后删除
|
||||
|
||||
ALTER TABLE `ec_product`
|
||||
MODIFY COLUMN `product_code` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '商品编码(公司内唯一;未填为空)';
|
||||
|
||||
UPDATE `ec_product`
|
||||
SET `product_code` = NULL
|
||||
WHERE `product_code` IS NOT NULL AND TRIM(`product_code`) = '';
|
||||
|
||||
DROP TABLE IF EXISTS `_tmp_ec_product_code_merge`;
|
||||
CREATE TABLE `_tmp_ec_product_code_merge` (
|
||||
`loser_id` bigint UNSIGNED NOT NULL,
|
||||
`keeper_id` bigint UNSIGNED NOT NULL,
|
||||
PRIMARY KEY (`loser_id`)
|
||||
) ENGINE = InnoDB;
|
||||
|
||||
INSERT INTO `_tmp_ec_product_code_merge` (`loser_id`, `keeper_id`)
|
||||
SELECT p.`id`, MIN(k.`id`)
|
||||
FROM `ec_product` p
|
||||
INNER JOIN `ec_product` k
|
||||
ON k.`company_id` = p.`company_id`
|
||||
AND k.`product_code` = p.`product_code`
|
||||
AND k.`id` < p.`id`
|
||||
WHERE p.`product_code` IS NOT NULL
|
||||
GROUP BY p.`id`;
|
||||
|
||||
-- 店铺-商品:并入后会撞 uk_shop_product 的先删
|
||||
DELETE r
|
||||
FROM `ec_shop_product_relation` r
|
||||
INNER JOIN `_tmp_ec_product_code_merge` m ON r.`product_id` = m.`loser_id`
|
||||
INNER JOIN `ec_shop_product_relation` keep_rel
|
||||
ON keep_rel.`shop_id` = r.`shop_id`
|
||||
AND keep_rel.`product_id` = m.`keeper_id`;
|
||||
|
||||
UPDATE `ec_shop_product_relation` r
|
||||
INNER JOIN `_tmp_ec_product_code_merge` m ON r.`product_id` = m.`loser_id`
|
||||
SET r.`product_id` = m.`keeper_id`;
|
||||
|
||||
-- 推广费:并入后会撞 uk_ec_ad_fee_product_date 的先删
|
||||
DELETE a
|
||||
FROM `ec_ad_fee` a
|
||||
INNER JOIN `_tmp_ec_product_code_merge` m ON a.`product_id` = m.`loser_id`
|
||||
INNER JOIN `ec_ad_fee` k
|
||||
ON k.`product_id` = m.`keeper_id`
|
||||
AND k.`fee_date` = a.`fee_date`;
|
||||
|
||||
UPDATE `ec_ad_fee` a
|
||||
INNER JOIN `_tmp_ec_product_code_merge` m ON a.`product_id` = m.`loser_id`
|
||||
SET a.`product_id` = m.`keeper_id`;
|
||||
|
||||
UPDATE `ec_fake_orders_fee` f
|
||||
INNER JOIN `_tmp_ec_product_code_merge` m ON f.`product_id` = m.`loser_id`
|
||||
SET f.`product_id` = m.`keeper_id`;
|
||||
|
||||
UPDATE `ec_order_item` i
|
||||
INNER JOIN `_tmp_ec_product_code_merge` m ON i.`product_id` = m.`loser_id`
|
||||
SET i.`product_id` = m.`keeper_id`;
|
||||
|
||||
UPDATE `channel_pdd_goods` g
|
||||
INNER JOIN `_tmp_ec_product_code_merge` m ON g.`product_id` = m.`loser_id`
|
||||
SET g.`product_id` = m.`keeper_id`;
|
||||
|
||||
UPDATE `ec_product_sku` s
|
||||
INNER JOIN `_tmp_ec_product_code_merge` m ON s.`product_id` = m.`loser_id`
|
||||
SET s.`product_id` = m.`keeper_id`;
|
||||
|
||||
UPDATE `ec_product_sku_cost` c
|
||||
INNER JOIN `_tmp_ec_product_code_merge` m ON c.`product_id` = m.`loser_id`
|
||||
SET c.`product_id` = m.`keeper_id`;
|
||||
|
||||
DELETE p
|
||||
FROM `ec_product` p
|
||||
INNER JOIN `_tmp_ec_product_code_merge` m ON p.`id` = m.`loser_id`;
|
||||
|
||||
DROP TABLE IF EXISTS `_tmp_ec_product_code_merge`;
|
||||
|
||||
ALTER TABLE `ec_product`
|
||||
DROP INDEX `uk_company_code_name`,
|
||||
ADD UNIQUE INDEX `uk_ec_product_company_code`(`company_id` ASC, `product_code` ASC) USING BTREE COMMENT '同公司商品编码唯一(NULL 不参与)';
|
||||
-3
@@ -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; }
|
||||
|
||||
+19
-43
@@ -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);
|
||||
}
|
||||
|
||||
// ==================== 订单状态 ====================
|
||||
|
||||
+2
-2
@@ -39,9 +39,9 @@ public class ProductPO {
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 商品编码
|
||||
* 商品编码(公司内唯一;未填为空)
|
||||
*/
|
||||
@TableField("product_code")
|
||||
@TableField(value = "product_code", updateStrategy = FieldStrategy.ALWAYS)
|
||||
private String productCode;
|
||||
|
||||
/**
|
||||
|
||||
+31
-28
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user