From 8aa6ceb67796cf3f7f81d4dd23c31270ef0e8dc7 Mon Sep 17 00:00:00 2001 From: zzh <1368735060@qq.com> Date: Fri, 26 Jun 2026 11:58:01 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BB=B7=E6=A0=BC=E5=8F=91=E5=B8=83?= =?UTF-8?q?=E4=BB=8E=E5=8D=96=E4=BB=B7=E6=98=8E=E7=BB=86=E5=88=86=E5=8C=BA?= =?UTF-8?q?=E6=8E=A8=E6=96=AD=20PriceAreaId=20=E5=B9=B6=E5=86=99=E5=85=A5?= =?UTF-8?q?=E5=BF=AB=E7=85=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 产品未配置 PriceAreaId 时,根据卖价明细「分区」值匹配 t_kpi_area 或 t_kpi_area_info 后固化 PriceAreaInfos。 Co-authored-by: Cursor --- .../Shared/PriceAreaSnapshotHelper.cs | 51 +++++++++++++++++-- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/src/Oms.Application/Shared/PriceAreaSnapshotHelper.cs b/src/Oms.Application/Shared/PriceAreaSnapshotHelper.cs index d06e44f..ab0bd80 100644 --- a/src/Oms.Application/Shared/PriceAreaSnapshotHelper.cs +++ b/src/Oms.Application/Shared/PriceAreaSnapshotHelper.cs @@ -1,5 +1,6 @@ using Oms.Application.Dtos.Channels; using Oms.Application.Dtos.Orders; +using Oms.Application.Services.Orders; using Oms.Domain.ChannelsAggregate.Entities; using Oms.Domain.ChannelsAggregate.Repositories; @@ -11,7 +12,7 @@ namespace Oms.Application.Shared; public static class PriceAreaSnapshotHelper { /// - /// 按产品 PriceAreaId 加载分区头与明细写入快照。 + /// 按产品 PriceAreaId(或从卖价明细分区推断)加载分区头与明细写入快照。 /// public static async Task ApplyPriceAreaToSnapshotAsync( SalesProductPricePublishSnapshotDto snapshot, @@ -19,7 +20,12 @@ public static class PriceAreaSnapshotHelper IKpiAreaRepository kpiAreaRepository, CancellationToken cancellationToken = default) { - if (!product.PriceAreaId.HasValue || product.PriceAreaId <= 0) + var priceAreaId = await ResolvePriceAreaIdAsync( + product, + snapshot, + kpiAreaRepository, + cancellationToken); + if (!priceAreaId.HasValue) { snapshot.PriceAreaId = null; snapshot.PriceAreaName = null; @@ -27,13 +33,48 @@ public static class PriceAreaSnapshotHelper return; } - snapshot.PriceAreaId = product.PriceAreaId; - var area = await kpiAreaRepository.FirstOrDefaultAsync(x => x.Id == product.PriceAreaId.Value, cancellationToken); + snapshot.PriceAreaId = priceAreaId; + var area = await kpiAreaRepository.FirstOrDefaultAsync(x => x.Id == priceAreaId.Value, cancellationToken); snapshot.PriceAreaName = area?.Name; - var infos = await kpiAreaRepository.GetAllInfosByAreaIdAsync(product.PriceAreaId.Value, cancellationToken); + var infos = await kpiAreaRepository.GetAllInfosByAreaIdAsync(priceAreaId.Value, cancellationToken); snapshot.PriceAreaInfos = infos.Select(MapInfoOutput).ToList(); } + internal static async Task ResolvePriceAreaIdAsync( + SalesProduct product, + SalesProductPricePublishSnapshotDto snapshot, + IKpiAreaRepository kpiAreaRepository, + CancellationToken cancellationToken = default) + { + if (product.PriceAreaId is > 0) + { + return product.PriceAreaId; + } + + var zoneKeys = ParcelBillingCalculator.CollectDistinctDetailZoneKeys(snapshot); + foreach (var zoneKey in zoneKeys) + { + if (!int.TryParse(zoneKey.Trim(), out var zoneId) || zoneId <= 0) + { + continue; + } + + var area = await kpiAreaRepository.FirstOrDefaultAsync(x => x.Id == zoneId, cancellationToken); + if (area != null) + { + return zoneId; + } + + var info = await kpiAreaRepository.GetInfoByIdAsync(zoneId, cancellationToken); + if (info != null) + { + return info.AreaId; + } + } + + return null; + } + private static KpiAreaInfoOutput MapInfoOutput(KpiAreaInfo entity) => new() {