From 2054a8bcf6e2826d39d9ab518a677762a9788327 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E8=9F=91=E8=9E=82?= Date: Sat, 18 Apr 2026 10:45:10 +0800 Subject: [PATCH] =?UTF-8?q?fix(ecom-dmb):=20=E5=AF=BC=E5=85=A5=E5=95=86?= =?UTF-8?q?=E5=93=81=E6=97=B6=E6=8D=95=E8=8E=B7=E5=94=AF=E4=B8=80=E9=94=AE?= =?UTF-8?q?=E5=86=B2=E7=AA=81=E5=BC=82=E5=B8=B8=EF=BC=8C=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E5=B7=B2=E5=AD=98=E5=9C=A8=E8=AE=B0=E5=BD=95?= =?UTF-8?q?=EF=BC=8C=E9=81=BF=E5=85=8D=E5=B9=B6=E5=8F=91=E5=AF=BC=E5=85=A5?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E5=95=86=E5=93=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ecom-dmb-backend/sql/ec_design.sql | 1 + .../service/impl/ProductSpecImportHelper.java | 78 ++++++++++++++++--- .../V4__ec_product_uk_company_name.sql | 12 +++ 3 files changed, 80 insertions(+), 11 deletions(-) create mode 100644 ecom-dmb-backend/src/main/resources/db/migration/V4__ec_product_uk_company_name.sql diff --git a/ecom-dmb-backend/sql/ec_design.sql b/ecom-dmb-backend/sql/ec_design.sql index d958cac1..c3d854fb 100644 --- a/ecom-dmb-backend/sql/ec_design.sql +++ b/ecom-dmb-backend/sql/ec_design.sql @@ -239,6 +239,7 @@ CREATE TABLE `ec_product` ( `update_by` bigint UNSIGNED NULL DEFAULT NULL COMMENT '更新人ID', PRIMARY KEY (`id`) USING BTREE, UNIQUE INDEX `uk_product_code`(`product_code` ASC) USING BTREE, + UNIQUE INDEX `uk_company_name`(`company_id` ASC, `name` ASC) USING BTREE, 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, diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/importtask/service/impl/ProductSpecImportHelper.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/importtask/service/impl/ProductSpecImportHelper.java index 922de47a..42b3beba 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/importtask/service/impl/ProductSpecImportHelper.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/importtask/service/impl/ProductSpecImportHelper.java @@ -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 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 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); diff --git a/ecom-dmb-backend/src/main/resources/db/migration/V4__ec_product_uk_company_name.sql b/ecom-dmb-backend/src/main/resources/db/migration/V4__ec_product_uk_company_name.sql new file mode 100644 index 00000000..cf4a44c8 --- /dev/null +++ b/ecom-dmb-backend/src/main/resources/db/migration/V4__ec_product_uk_company_name.sql @@ -0,0 +1,12 @@ +-- 同一公司下商品名称唯一(与导入并发建品一致;若已有重复 (company_id, name) 需先清理数据) +SET @db := DATABASE(); +SET @exists := ( + SELECT COUNT(1) FROM information_schema.statistics + WHERE table_schema = @db AND table_name = 'ec_product' AND index_name = 'uk_company_name' +); +SET @sql := IF(@exists = 0, + 'ALTER TABLE ec_product ADD UNIQUE INDEX uk_company_name (company_id, name)', + 'SELECT 1'); +PREPARE s FROM @sql; +EXECUTE s; +DEALLOCATE PREPARE s;