admin/src/Oms.Application/Shared/PublishSnapshotNormalizer.cs
2026-07-23 12:03:47 +08:00

131 lines
3.9 KiB
C#

using Oms.Application.Dtos.Channels;
using Oms.Application.Dtos.Orders;
namespace Oms.Application.Shared;
/// <summary>
/// 发布快照读取后规范化(旧字段迁移、去掉冗余、稳定排序)。
/// </summary>
public static class PublishSnapshotNormalizer
{
public static void NormalizeRulesSnapshot(SalesProductRulesPublishSnapshotDto? snapshot)
{
if (snapshot is null)
{
return;
}
NormalizeKpiAreas(snapshot.BlacklistAreaInfos);
NormalizeKpiAreas(snapshot.RemoteAreaInfos);
NormalizeWeightRoundingRanges(snapshot.WeightRoundingInfos);
}
public static void NormalizePriceSnapshot(SalesProductPricePublishSnapshotDto? snapshot)
{
if (snapshot is null)
{
return;
}
NormalizeKpiAreas(snapshot.PriceAreaInfos);
snapshot.Prices = SortSalesProductPriceItems(snapshot.Prices);
}
public static void NormalizeChannelPriceSnapshot(ServiceChannelPricePublishSnapshotDto? snapshot)
{
if (snapshot is null)
{
return;
}
snapshot.Prices = SortChannelPriceItems(snapshot.Prices);
}
public static void NormalizeKpiAreas(List<KpiAreaSnapshotRuleDto>? areas)
{
if (areas is null)
{
return;
}
foreach (var area in areas)
{
if (string.IsNullOrWhiteSpace(area.PostcodeRange)
&& (!string.IsNullOrWhiteSpace(area.LeftPostcode) || !string.IsNullOrWhiteSpace(area.RightPostcode)))
{
area.PostcodeRange = KpiAreaSnapshotMapper.BuildPostcodeRange(area.LeftPostcode, area.RightPostcode);
}
area.LeftPostcode = null;
area.RightPostcode = null;
}
}
public static void NormalizeWeightRoundingRanges(List<WeightRoundingSnapshotRangeDto>? ranges)
{
if (ranges is null)
{
return;
}
foreach (var range in ranges)
{
if (range.StartValue != 0)
{
range.Start = range.StartValue;
}
if (range.EndValue != 0)
{
range.End = range.EndValue;
}
range.StartValue = 0;
range.EndValue = 0;
if (range.Start == 0 && range.End == 0 && !string.IsNullOrWhiteSpace(range.Public))
{
// 无效区间占位,避免误用导致「计重进位区间未匹配」;后续会降级读 DB。
range.Public = null;
}
}
}
private static List<SalesProductPriceSnapshotItemDto> SortSalesProductPriceItems(
List<SalesProductPriceSnapshotItemDto>? prices) =>
prices?
.OrderBy(price => price.SerialNumber)
.ThenBy(price => price.Id)
.Select(price =>
{
price.Factors = SortFactors(price.Factors);
price.Details = SortDetails(price.Details);
return price;
})
.ToList() ?? [];
private static List<ServiceChannelPriceSnapshotItemDto> SortChannelPriceItems(
List<ServiceChannelPriceSnapshotItemDto>? prices) =>
prices?
.OrderBy(price => price.SerialNumber)
.ThenBy(price => price.Id)
.Select(price =>
{
price.Factors = SortFactors(price.Factors);
price.Details = SortDetails(price.Details);
return price;
})
.ToList() ?? [];
private static List<ChannelPriceFactorOutput> SortFactors(List<ChannelPriceFactorOutput>? factors) =>
factors?
.OrderBy(factor => factor.SerialNumber)
.ThenBy(factor => factor.Id)
.ToList() ?? [];
private static List<ChannelPriceDetailOutput> SortDetails(List<ChannelPriceDetailOutput>? details) =>
details?
.OrderBy(detail => detail.Id)
.ToList() ?? [];
}