track/src/SJZY.Track.Application/CAP/EventHandlers/TrackChangeAfterUpdateOrderEventHandler.cs

154 lines
6.1 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 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 GetOrdersAsync(@event.OrderIds);
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;
}
}
// 执行轨迹兜底时 时区缺失和发生地缺失标记,1表示时区,2表示发生地,3表示两种
if (@event.DeficiencyTagValue > 0 && @event.DeficiencyTagValue<=3)
{
order.DeficiencyTag = @event.DeficiencyTagValue;
}
if (!order.IsCompleted)
{
order.IsCompleted = @event.IsCompleted;
}
if (!string.IsNullOrEmpty(@event.TrackCode) && providerDic.TryGetValue(order.Id, out var provider))
{
var config = await _trackPullCacheService.GetPullParamWithContentConfigAsync(provider.SubSystemId, provider.ServiceProviderId);
if (!string.IsNullOrEmpty(config?.ParamConfig?.TrackContentCode) && config.ParamConfig.TrackContentCode!.ToLower() == @event.TrackCode.ToLower())
{
// 触发拉取节点
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,x.IsTriggerPull,x.DeficiencyTag
});
// 预警计算
await _forewarningNotifyService.WarningCalculateAsync(BuiltWarningDto(orders, @event));
}
private async Task<List<OrderEntity>> GetOrdersAsync(int[] orderIds)
{
return await _orderRepository.GetListAsync(order => orderIds.Contains(order.Id), order => 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
});
}
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()
};
}
/// <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());
}
}