admin/src/Oms.Application/Shared/KpiAreaSnapshotMapper.cs
zzh 74a71991b2
All checks were successful
Build and Deploy / deploy (push) Successful in 1m2s
feat: 统一价格/黑名单/偏远分区匹配,并落库 MatchDim
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-05 16:12:09 +08:00

69 lines
2.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Oms.Application.Dtos.Orders;
using Oms.Domain.ChannelsAggregate.Entities;
namespace Oms.Application.Shared;
/// <summary>
/// KPI 分区快照映射。邮编区间为闭区间,prefix/exact/range 编码进 PostcodeRange。
/// </summary>
public static class KpiAreaSnapshotMapper
{
public static KpiAreaSnapshotRuleDto MapToSnapshotRule(KpiAreaInfo entity) =>
new()
{
Id = entity.Id,
CountryCode = entity.CountryCode,
Province = entity.Province,
City = entity.City,
MatchDim = entity.MatchDim,
MatchMode = entity.MatchMode,
PrefixLen = entity.PrefixLen,
PostcodeRange = BuildPostcodeRange(entity.LeftPostcode, entity.RightPostcode)
};
public static string? BuildPostcodeRange(string? left, string? right)
{
var zipLeft = GeoPartitionMatcher.NormalizeZip(left);
var zipRight = GeoPartitionMatcher.NormalizeZip(right);
if (zipLeft.Length == 0 && zipRight.Length == 0)
{
return null;
}
if (zipLeft.Length > 0 && zipRight.Length == 0)
{
return $"prefix:{zipLeft}";
}
if (zipLeft.Length == 0 && zipRight.Length > 0)
{
return $"prefix:{zipRight}";
}
if (zipLeft == zipRight)
{
return $"exact:{zipLeft}";
}
return $"[{zipLeft},{zipRight}]";
}
public static string? ResolvePostcodeRange(KpiAreaSnapshotRuleDto rule)
{
if (!string.IsNullOrWhiteSpace(rule.PostcodeRange))
{
return rule.PostcodeRange.Trim();
}
return BuildPostcodeRange(rule.LeftPostcode, rule.RightPostcode);
}
public static bool IsRuleMatched(
KpiAreaSnapshotRuleDto rule,
string countryCode,
string? province,
string? city,
string? zipCode) =>
GeoPartitionMatcher.IsHit([rule], countryCode, province, city, zipCode);
}