From 10827e25dd96eda810fc21cb100d07351bbe34cd 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 14:14:27 +0800 Subject: [PATCH] =?UTF-8?q?feat(ecom-dmb):=20=E5=95=86=E5=93=81=E8=A7=84?= =?UTF-8?q?=E6=A0=BC=E8=A1=A8=E6=B7=BB=E5=8A=A0(=E5=85=AC=E5=8F=B8ID,?= =?UTF-8?q?=E8=A7=84=E6=A0=BC=E7=BC=96=E7=A0=81)=E5=A4=8D=E5=90=88?= =?UTF-8?q?=E5=94=AF=E4=B8=80=E7=BA=A6=E6=9D=9F=EF=BC=8C=E5=AF=BC=E5=85=A5?= =?UTF-8?q?=E6=97=B6=E8=87=AA=E5=8A=A8=E6=B8=85=E7=90=86=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E8=A7=84=E6=A0=BC=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ecom-dmb-backend/sql/ec_design.sql | 2 +- .../service/impl/ProductSpecImportHelper.java | 48 +++++++++++++++++-- ...__ec_product_spec_uk_company_spec_code.sql | 12 +++++ .../dashboard/company-overview/index.vue | 23 +++++++-- 4 files changed, 75 insertions(+), 10 deletions(-) create mode 100644 ecom-dmb-backend/src/main/resources/db/migration/V5__ec_product_spec_uk_company_spec_code.sql diff --git a/ecom-dmb-backend/sql/ec_design.sql b/ecom-dmb-backend/sql/ec_design.sql index c3d854fb..cda007f8 100644 --- a/ecom-dmb-backend/sql/ec_design.sql +++ b/ecom-dmb-backend/sql/ec_design.sql @@ -274,7 +274,7 @@ CREATE TABLE `ec_product_spec` ( PRIMARY KEY (`id`) USING BTREE, INDEX `idx_company_id`(`company_id` ASC) USING BTREE, INDEX `idx_product_id`(`product_id` ASC) USING BTREE, - INDEX `idx_spec_code`(`spec_code` ASC) USING BTREE, + UNIQUE INDEX `uk_company_spec_code`(`company_id` ASC, `spec_code` ASC) USING BTREE, INDEX `fk_ec_spec_creator`(`create_by` ASC) USING BTREE, INDEX `fk_ec_spec_updater`(`update_by` ASC) USING BTREE, CONSTRAINT `fk_ec_spec_company` FOREIGN KEY (`company_id`) REFERENCES `org_company` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, 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 42b3beba..1b108299 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 @@ -124,6 +124,44 @@ public class ProductSpecImportHelper { return false; } + private ProductSpecPO selectSpecByCompanyAndSpecCodeFromDb(Long companyId, String specCode) { + if (specCode == null || specCode.isEmpty()) { + return null; + } + LambdaQueryWrapper w = new LambdaQueryWrapper<>(); + w.eq(ProductSpecPO::getCompanyId, companyId).eq(ProductSpecPO::getSpecCode, specCode); + return productSpecService.getOne(w); + } + + /** + * 插入规格;若触发唯一约束(同公司同 spec_code),则改为从库中加载已存在行。 + */ + private ProductSpecPO insertSpecOrFetchExisting(ProductSpecPO draft, Long companyId, + ImportRuntimeContext context) { + try { + productSpecService.save(draft); + return draft; + } catch (Exception ex) { + if (!isDuplicateKeyException(ex)) { + if (ex instanceof RuntimeException re) { + throw re; + } + throw new RuntimeException(ex); + } + String code = draft.getSpecCode(); + if (code == null || code.trim().isEmpty()) { + throw new BusinessException( + "规格因唯一约束已存在但规格编码为空,无法回查已存在记录"); + } + ProductSpecPO existing = selectSpecByCompanyAndSpecCodeFromDb(companyId, code.trim()); + if (existing != null) { + return existing; + } + throw new BusinessException( + "规格因唯一约束已存在但重新查询失败,请稍后重试导入"); + } + } + // ==================== 商品/规格创建 ==================== /** @@ -139,10 +177,12 @@ public class ProductSpecImportHelper { String trimmedCode = specCode != null ? specCode.trim() : null; String trimmedName = specName != null ? specName.trim() : null; - // 检查缓存 + // 检查缓存(插入前再读一次,减少并行线程重复 insert) if (trimmedCode != null) { ProductSpecPO existing = context.specByCodeCache.get(trimmedCode); - if (existing != null) return existing; + if (existing != null) { + return existing; + } } // 创建规格 @@ -155,9 +195,9 @@ public class ProductSpecImportHelper { spec.setLinkName(finalSpecName); spec.setProductCost(productCost != null ? productCost : BigDecimal.ZERO); spec.setCreateBy(createBy); - productSpecService.save(spec); + spec = insertSpecOrFetchExisting(spec, companyId, context); - // 写入缓存 + // 写入缓存(有编码时以编码为键;并发冲突回查得到的行也写入) if (trimmedCode != null) { context.specByCodeCache.put(trimmedCode, spec); } diff --git a/ecom-dmb-backend/src/main/resources/db/migration/V5__ec_product_spec_uk_company_spec_code.sql b/ecom-dmb-backend/src/main/resources/db/migration/V5__ec_product_spec_uk_company_spec_code.sql new file mode 100644 index 00000000..f19c3f7a --- /dev/null +++ b/ecom-dmb-backend/src/main/resources/db/migration/V5__ec_product_spec_uk_company_spec_code.sql @@ -0,0 +1,12 @@ +-- 同一公司下规格编码唯一(与导入并发建规格一致) +SET @db := DATABASE(); +SET @exists := ( + SELECT COUNT(1) FROM information_schema.statistics + WHERE table_schema = @db AND table_name = 'ec_product_spec' AND index_name = 'uk_company_spec_code' +); +SET @sql := IF(@exists = 0, + 'ALTER TABLE ec_product_spec ADD UNIQUE INDEX uk_company_spec_code (company_id, spec_code)', + 'SELECT 1'); +PREPARE s FROM @sql; +EXECUTE s; +DEALLOCATE PREPARE s; diff --git a/ecom-dmb-frontend/apps/web-antd/src/views/dashboard/company-overview/index.vue b/ecom-dmb-frontend/apps/web-antd/src/views/dashboard/company-overview/index.vue index 39310263..3df7b692 100644 --- a/ecom-dmb-frontend/apps/web-antd/src/views/dashboard/company-overview/index.vue +++ b/ecom-dmb-frontend/apps/web-antd/src/views/dashboard/company-overview/index.vue @@ -6,7 +6,14 @@ import type { CompanyOverviewVO, } from '#/api/ecommerce/company-overview'; -import { computed, onActivated, onMounted, ref, watch } from 'vue'; +import { + computed, + onActivated, + onMounted, + onUnmounted, + ref, + watch, +} from 'vue'; import { useRoute, useRouter } from 'vue-router'; import { Page } from '@vben/common-ui'; @@ -528,23 +535,29 @@ watch( }, ); +/** 深色模式 class 变化时重绘图表;须在 setup 顶层 onUnmounted 中断开,不可写在 onMounted 异步回调里 */ +let darkThemeObserver: MutationObserver | null = null; + // 初始化 onMounted(async () => { await loadShopCount(); await refreshOverviewByCurrentParams(true); - // 监听深色模式切换,重新渲染图表 - const darkObserver = new MutationObserver(() => { + darkThemeObserver = new MutationObserver(() => { if (overviewData.value) { renderShopBarChart(); renderShopScatterChart(); } }); - darkObserver.observe(document.documentElement, { + darkThemeObserver.observe(document.documentElement, { attributes: true, attributeFilter: ['class'], }); - onUnmounted(() => darkObserver.disconnect()); +}); + +onUnmounted(() => { + darkThemeObserver?.disconnect(); + darkThemeObserver = null; }); onActivated(async () => {