All checks were successful
Build and Deploy / deploy (push) Successful in 1m2s
Co-authored-by: Cursor <cursoragent@cursor.com>
69 lines
2.0 KiB
C#
69 lines
2.0 KiB
C#
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);
|
||
}
|