From 474c0c88c031545dafa0e6c8d6366a89a3c3377a Mon Sep 17 00:00:00 2001 From: MSuMshk <2039814060@qq.com> Date: Wed, 25 Feb 2026 09:55:38 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=8F=90=E5=89=8D=E6=A0=A1=E9=AA=8CSKU?= =?UTF-8?q?=E6=9C=80=E7=BB=88=E7=BC=96=E7=A0=81=E5=86=B2=E7=AA=81=E5=B9=B6?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E6=98=8E=E7=A1=AE=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/ProductSkuSaveService.cs | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/Api/TakeoutSaaS.TenantApi/Services/ProductSkuSaveService.cs b/src/Api/TakeoutSaaS.TenantApi/Services/ProductSkuSaveService.cs index a5dc240..75fb21c 100644 --- a/src/Api/TakeoutSaaS.TenantApi/Services/ProductSkuSaveService.cs +++ b/src/Api/TakeoutSaaS.TenantApi/Services/ProductSkuSaveService.cs @@ -89,6 +89,7 @@ public sealed class ProductSkuSaveService(TakeoutAppDbContext dbContext) .ToDictionary(group => group.Key, group => new Queue(group), StringComparer.Ordinal); var usedExistingSkuIds = new HashSet(); + var plannedSkuCodes = new HashSet(StringComparer.Ordinal); var currentProductSkuCodes = existingSkus .Where(item => !string.IsNullOrWhiteSpace(item.SkuCode)) .Select(item => item.SkuCode) @@ -119,6 +120,20 @@ public sealed class ProductSkuSaveService(TakeoutAppDbContext dbContext) if (matched is not null) { usedExistingSkuIds.Add(matched.Id); + var targetSkuCode = !string.IsNullOrWhiteSpace(normalizedSkuCode) + ? normalizedSkuCode + : NormalizeSkuCode(matched.SkuCode); + if (string.IsNullOrWhiteSpace(targetSkuCode)) + { + targetSkuCode = GenerateUniqueSkuCode(productId, currentProductSkuCodes); + } + + if (!plannedSkuCodes.Add(targetSkuCode)) + { + throw new BusinessException(ErrorCodes.BadRequest, $"SKU 编码冲突: {targetSkuCode}"); + } + + currentProductSkuCodes.Add(targetSkuCode); matched.Price = sku.Price; matched.OriginalPrice = sku.OriginalPrice; matched.StockQuantity = Math.Max(0, sku.Stock); @@ -127,15 +142,17 @@ public sealed class ProductSkuSaveService(TakeoutAppDbContext dbContext) matched.IsEnabled = sku.IsEnabled; matched.DeletedAt = null; matched.DeletedBy = null; - if (!string.IsNullOrWhiteSpace(normalizedSkuCode)) - { - matched.SkuCode = normalizedSkuCode; - } + matched.SkuCode = targetSkuCode; continue; } var generatedCode = normalizedSkuCode ?? GenerateUniqueSkuCode(productId, currentProductSkuCodes); + if (!plannedSkuCodes.Add(generatedCode)) + { + throw new BusinessException(ErrorCodes.BadRequest, $"SKU 编码冲突: {generatedCode}"); + } + currentProductSkuCodes.Add(generatedCode); createdSkus.Add(new ProductSku { @@ -157,6 +174,11 @@ public sealed class ProductSkuSaveService(TakeoutAppDbContext dbContext) continue; } + if (!string.IsNullOrWhiteSpace(existing.SkuCode) && plannedSkuCodes.Contains(existing.SkuCode)) + { + throw new BusinessException(ErrorCodes.BadRequest, $"SKU 编码已存在: {existing.SkuCode}"); + } + existing.IsEnabled = false; existing.StockQuantity = 0; }