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 () => {