using Oms.Application.Dtos.Channels;
using Oms.Application.Dtos.Orders;
using Oms.Domain.ChannelsAggregate.Entities;
using Oms.Domain.ChannelsAggregate.Repositories;
namespace Oms.Application.Shared;
///
/// 将价格分区(t_kpi_area + t_kpi_area_info)固化进卖价发布快照。
///
public static class PriceAreaSnapshotHelper
{
///
/// 按产品 PriceAreaId 加载分区头与明细写入快照。
///
public static async Task ApplyPriceAreaToSnapshotAsync(
SalesProductPricePublishSnapshotDto snapshot,
SalesProduct product,
IKpiAreaRepository kpiAreaRepository,
CancellationToken cancellationToken = default)
{
if (!product.PriceAreaId.HasValue || product.PriceAreaId <= 0)
{
snapshot.PriceAreaId = null;
snapshot.PriceAreaName = null;
snapshot.PriceAreaInfos = [];
return;
}
snapshot.PriceAreaId = product.PriceAreaId;
var area = await kpiAreaRepository.FirstOrDefaultAsync(x => x.Id == product.PriceAreaId.Value, cancellationToken);
snapshot.PriceAreaName = area?.Name;
var infos = await kpiAreaRepository.GetAllInfosByAreaIdAsync(product.PriceAreaId.Value, cancellationToken);
snapshot.PriceAreaInfos = infos.Select(MapInfoOutput).ToList();
}
private static KpiAreaInfoOutput MapInfoOutput(KpiAreaInfo entity) =>
new()
{
Id = entity.Id,
AreaId = entity.AreaId,
CountryCode = entity.CountryCode,
CountryName = entity.CountryName,
Province = entity.Province,
City = entity.City,
LeftPostcode = entity.LeftPostcode,
RightPostcode = entity.RightPostcode,
CreateTime = entity.CreateTime,
UpdateTime = OmsDocHelper.NullIfDefault(entity.UpdateTime)
};
}