148 lines
5.6 KiB
C#
148 lines
5.6 KiB
C#
using DotNetCore.CAP;
|
|
using SJZY.Track.Application.Contract.Dtos.Forewarning;
|
|
using SJZY.Track.Application.Contract.Events;
|
|
using SJZY.Track.Application.Contract.Services;
|
|
using SJZY.Track.Domain.Aggregates.OrderAggregate;
|
|
using SJZY.Track.Domain.Consts;
|
|
using SJZY.Track.Domain.Dtos.Forewarning;
|
|
using SJZY.Track.Domain.Dtos.Order;
|
|
using SJZY.Track.Domain.Queries;
|
|
using Volo.Abp.DependencyInjection;
|
|
|
|
namespace SJZY.Track.Application.CAP.EventHandlers;
|
|
|
|
public class TrackChangeAfterUpdateOrderEventHandler : ITransientDependency, ICapSubscribe
|
|
{
|
|
private readonly IOrderRepository _orderRepository;
|
|
private readonly IForewarningNotifyService _forewarningNotifyService;
|
|
private readonly ITrackPullCacheService _trackPullCacheService;
|
|
private readonly IProvideQueries _provideQueries;
|
|
|
|
public TrackChangeAfterUpdateOrderEventHandler(IOrderRepository orderRepository,
|
|
IForewarningNotifyService forewarningNotifyService, ITrackPullCacheService trackPullCacheService, IProvideQueries provideQueries)
|
|
{
|
|
_orderRepository = orderRepository;
|
|
_forewarningNotifyService = forewarningNotifyService;
|
|
_trackPullCacheService = trackPullCacheService;
|
|
_provideQueries = provideQueries;
|
|
}
|
|
|
|
[CapSubscribe(GlobalConst.TrackChangeAfterUpdateOrderSubscribe)]
|
|
public async Task ConsumerAsync(TrackChangeAfterUpdateOrderEvent @event)
|
|
{
|
|
var orders = await _orderRepository.GetListAsync(x => @event.OrderIds.Contains(x.Id), x => MapOrderSummary(x));
|
|
|
|
if (orders.Count == 0) return;
|
|
|
|
var updateOrders = new List<OrderEntity>();
|
|
var providerDic= await GetProviderDic(@event.OrderIds);
|
|
foreach (var order in orders)
|
|
{
|
|
if (order.LatestTrackContent != @event.TrackContent)
|
|
{
|
|
if (order.LatestTrackTime != null &&
|
|
@event.TrackTime.Subtract(TimeSpan.FromHours(@event.TrackTimeZone)) <
|
|
order.LatestTrackTime.Value.Subtract(TimeSpan.FromHours(order.LatestTrackTimeZone ?? 0)))
|
|
{
|
|
continue;
|
|
}
|
|
}
|
|
|
|
if (!order.IsCompleted)
|
|
{
|
|
order.IsCompleted = @event.IsCompleted;
|
|
}
|
|
|
|
if (providerDic.TryGetValue(order.Id, out var provider))
|
|
{
|
|
var config = await _trackPullCacheService.GetPullParamWithContentConfigAsync(provider.SubSystemId, provider.ServiceProviderId);
|
|
|
|
if (config!=null && config.ParamConfig!=null && config.ParamConfig.TrackContentCode == @event.TrackCode)
|
|
{
|
|
// 触发拉取节点
|
|
order.IsTriggerPull = 1;
|
|
}
|
|
}
|
|
|
|
order.LatestTrackContent = @event.TrackContent;
|
|
order.LatestTrackEnContent = @event.TrackEnContent;
|
|
order.LatestTrackPlace = @event.TrackPlace;
|
|
order.LatestTrackTime = @event.TrackTime;
|
|
order.LatestTrackTimeZone = @event.TrackTimeZone;
|
|
updateOrders.Add(order);
|
|
}
|
|
|
|
await _orderRepository.UpdateRangeAsync(updateOrders, x => new
|
|
{
|
|
x.IsCompleted, x.LatestTrackContent, x.LatestTrackEnContent, x.LatestTrackPlace, x.LatestTrackTime,
|
|
x.LatestTrackTimeZone
|
|
});
|
|
|
|
// 预警计算
|
|
await _forewarningNotifyService.WarningCalculateAsync( BuiltWarningDto(orders, @event));
|
|
}
|
|
|
|
private WarningDto BuiltWarningDto(List<OrderEntity> orders, TrackChangeAfterUpdateOrderEvent @event)
|
|
{
|
|
return new WarningDto
|
|
{
|
|
TrackStatusId = @event.TrackStatusId,
|
|
SubSystemId = @event.SubSystemId,
|
|
CustomerId = @event.CustomId,
|
|
Orders = orders.Select(order => new WarningOrderDto
|
|
{
|
|
Id = order.Id,
|
|
|
|
CargoType = order.CargoType,
|
|
Month = order.CreateTime.Month,
|
|
IsTransit = order.IsTransit,
|
|
LeavePort = order.LeavePort,
|
|
ProductCode = order.ProductCode,
|
|
|
|
ArrivalCountry = order.ArrivalCountry,
|
|
ArrivalProvince = order.ArrivalProvince,
|
|
ArrivalCity = order.ArrivalCity,
|
|
ArrivalPostCode = order.ArrivalPostCode,
|
|
}).ToList()
|
|
};
|
|
}
|
|
|
|
private OrderEntity MapOrderSummary(OrderEntity order)
|
|
{
|
|
return new OrderEntity
|
|
{
|
|
Id = order.Id,
|
|
|
|
// 轨迹
|
|
IsCompleted = order.IsCompleted,
|
|
LatestTrackContent = order.LatestTrackContent,
|
|
LatestTrackEnContent = order.LatestTrackEnContent,
|
|
LatestTrackPlace = order.LatestTrackPlace,
|
|
LatestTrackTime = order.LatestTrackTime,
|
|
LatestTrackTimeZone = order.LatestTrackTimeZone,
|
|
|
|
// 因素
|
|
CargoType = order.CargoType,
|
|
CreateTime = order.CreateTime,
|
|
ArrivalCountry = order.ArrivalCountry,
|
|
ArrivalProvince = order.ArrivalProvince,
|
|
ArrivalCity = order.ArrivalCity,
|
|
ArrivalPostCode = order.ArrivalPostCode,
|
|
IsTransit = order.IsTransit,
|
|
LeavePort = order.LeavePort,
|
|
ProductCode = order.ProductCode
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取服务商字典
|
|
/// </summary>
|
|
/// <param name="orderIds"></param>
|
|
/// <returns></returns>
|
|
private async Task<Dictionary<int, OrderTransportDto>> GetProviderDic(int[] orderIds)
|
|
{
|
|
var providerList = await _provideQueries.FindOrderWithServiceProviderAsync(orderIds);
|
|
return providerList.GroupBy(val => val.OrderId)
|
|
.ToDictionary(val => val.Key, val => val.First());
|
|
}
|
|
} |