210 lines
6.2 KiB
C#
210 lines
6.2 KiB
C#
using TrackCenter.Application.Dtos.Warning.Transport;
|
||
using TrackCenter.Domain.Aggregates.OrderAggregate;
|
||
using TrackCenter.Domain.Aggregates.WarningAggregate.Transport;
|
||
using TrackCenter.Domain.Dtos.Forewarning;
|
||
|
||
namespace TrackCenter.Application.Services.Warning.Model;
|
||
|
||
public sealed class TransportWarningSceneBO
|
||
{
|
||
/// <summary>
|
||
/// 场景唯一id (传0视为新增)
|
||
/// </summary>
|
||
public int Id { get; }
|
||
|
||
/// <summary>
|
||
/// 预警规则主体id
|
||
/// </summary>
|
||
public int WarningId { get; }
|
||
|
||
/// <summary>
|
||
/// 货物类型(多个,分割)
|
||
/// </summary>
|
||
public string? CargoType { get; }
|
||
|
||
/// <summary>
|
||
/// 月份(多个/分割)
|
||
/// </summary>
|
||
public string? Months { get; }
|
||
|
||
/// <summary>
|
||
/// 区域Id(单一)
|
||
/// </summary>
|
||
public int? AreaId { get; }
|
||
|
||
/// <summary>
|
||
/// 是否中转(单一)
|
||
/// </summary>
|
||
public bool? IsTransit { get; }
|
||
|
||
/// <summary>
|
||
/// 起运港口(多个,分割)
|
||
/// </summary>
|
||
public string? LeavePort { get; }
|
||
|
||
/// <summary>
|
||
/// 产品编码(多个,分割)
|
||
/// </summary>
|
||
public string? ProductCode { get; }
|
||
|
||
public TransportWarningSceneBO(int id, int warningId, string? cargoType, string? months, int? areaId, bool? isTransit, string? leavePort, string? productCode)
|
||
{
|
||
Id = id;
|
||
WarningId = warningId;
|
||
CargoType = cargoType;
|
||
|
||
// 如果有月份则排序之后在写入
|
||
if (!string.IsNullOrWhiteSpace(months))
|
||
{
|
||
var sorted = months.Split('/').Select(int.Parse).OrderBy(x => x).ToList();
|
||
Months = string.Join('/', sorted);
|
||
}
|
||
|
||
AreaId = areaId;
|
||
IsTransit = isTransit;
|
||
LeavePort = leavePort;
|
||
ProductCode = productCode;
|
||
}
|
||
|
||
public bool CanHitScene(OrderEntity order, List<KpiAreaInfo>? ruleAreas)
|
||
{
|
||
// 是否中转
|
||
if (IsTransit != null && IsTransit != order.IsTransit)
|
||
return false;
|
||
|
||
// 货物类型
|
||
if (!string.IsNullOrWhiteSpace(CargoType) && order.CargoType != CargoType)
|
||
return false;
|
||
|
||
// 启运港口
|
||
if (!string.IsNullOrWhiteSpace(LeavePort) && order.LeavePort != LeavePort)
|
||
return false;
|
||
|
||
// 产品编码
|
||
if (!string.IsNullOrWhiteSpace(ProductCode) && order.ProductCode != ProductCode)
|
||
return false;
|
||
|
||
// 月份
|
||
if (!string.IsNullOrWhiteSpace(Months))
|
||
{
|
||
var months = string.IsNullOrEmpty(Months)
|
||
? new HashSet<int>()
|
||
: Months.Split('/').Select(int.Parse).ToHashSet();
|
||
// 订单创建时间月份
|
||
var orderCreateTimeMonth = order.CreateTime.Month;
|
||
if (!months.Contains(orderCreateTimeMonth))
|
||
return false;
|
||
}
|
||
|
||
// 区域
|
||
if (ruleAreas != null && ruleAreas.Count > 0)
|
||
{
|
||
// 使用订单到达国家的二字码匹配区域
|
||
var areas = ruleAreas.Where(area => order.ArrivalCountry == area.CountryCode).ToList();
|
||
foreach (var area in areas)
|
||
{
|
||
// 邮编是否在关注的范围内?
|
||
if (!string.IsNullOrWhiteSpace(area.LeftPostcode) && !string.IsNullOrWhiteSpace(area.RightPostcode))
|
||
{
|
||
// tips: 参考小包的邮编匹配,字典序
|
||
if (string.CompareOrdinal(order.ArrivalPostCode, area.LeftPostcode) < 0 ||
|
||
string.CompareOrdinal(order.ArrivalPostCode, area.RightPostcode) > 0)
|
||
return false;
|
||
}
|
||
|
||
// 城市是否匹配?
|
||
if (!string.IsNullOrWhiteSpace(area.City) && order.ArrivalCity != area.City)
|
||
return false;
|
||
|
||
// 省份是否匹配?
|
||
if (!string.IsNullOrWhiteSpace(area.Province) && order.ArrivalProvince != area.Province)
|
||
return false;
|
||
}
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
public static TransportWarningSceneEntity ToEntity(TransportWarningSceneBO bo)
|
||
{
|
||
return new TransportWarningSceneEntity(
|
||
bo.Id,
|
||
bo.WarningId,
|
||
bo.CargoType,
|
||
bo.Months,
|
||
bo.AreaId,
|
||
bo.IsTransit,
|
||
bo.LeavePort,
|
||
bo.ProductCode
|
||
);
|
||
}
|
||
|
||
public TransportWarningSceneEntity ToEntity()
|
||
{
|
||
return new TransportWarningSceneEntity(
|
||
Id,
|
||
WarningId,
|
||
CargoType,
|
||
Months,
|
||
AreaId,
|
||
IsTransit,
|
||
LeavePort,
|
||
ProductCode
|
||
);
|
||
}
|
||
|
||
public static TransportWarningSceneBO From(TransportWarningSceneEntity entity)
|
||
{
|
||
return new TransportWarningSceneBO(
|
||
entity.Id,
|
||
entity.WarningId,
|
||
entity.CargoType,
|
||
entity.Months,
|
||
entity.AreaId,
|
||
entity.IsTransit,
|
||
entity.LeavePort,
|
||
entity.ProductCode
|
||
);
|
||
}
|
||
|
||
public static List<TransportWarningSceneBO> From(List<TransportWarningSceneEntity> entities)
|
||
{
|
||
return entities.Select(From).ToList();
|
||
}
|
||
|
||
public static TransportWarningSceneBO From(UpsertTransportWarningSceneInput input)
|
||
{
|
||
return new TransportWarningSceneBO(
|
||
input.Id,
|
||
input.WarningId,
|
||
input.CargoType,
|
||
input.Months,
|
||
input.AreaId,
|
||
input.IsTransit,
|
||
input.LeavePort,
|
||
input.ProductCode
|
||
);
|
||
}
|
||
|
||
private bool Equals(TransportWarningSceneBO other)
|
||
{
|
||
return WarningId == other.WarningId
|
||
&& IsTransit == other.IsTransit
|
||
&& CargoType == other.CargoType
|
||
&& LeavePort == other.LeavePort
|
||
&& ProductCode == other.ProductCode
|
||
&& Months == other.Months
|
||
&& AreaId == other.AreaId;
|
||
}
|
||
|
||
public override bool Equals(object? obj)
|
||
{
|
||
return ReferenceEquals(this, obj) || obj is TransportWarningSceneBO other && Equals(other);
|
||
}
|
||
|
||
public override int GetHashCode()
|
||
{
|
||
return HashCode.Combine(WarningId, IsTransit, CargoType, LeavePort, ProductCode, Months, AreaId);
|
||
}
|
||
}
|