This commit is contained in:
xiaolipro 2025-01-10 09:33:35 +08:00
parent b2c7a6aef1
commit cc84be2d84

View File

@ -1,5 +1,6 @@
using System.Diagnostics;
using DotNetCore.CAP; using DotNetCore.CAP;
using NetTaste; using Microsoft.Extensions.Logging;
using SJZY.Expand.ABP.Core.Logger; using SJZY.Expand.ABP.Core.Logger;
using SJZY.Expand.ABP.Core.PublicMethords; using SJZY.Expand.ABP.Core.PublicMethords;
using SJZY.Track.Application.Contract.Events; using SJZY.Track.Application.Contract.Events;
@ -21,6 +22,7 @@ public class TrackUpsertEventHandler : ITransientDependency, ICapSubscribe
private readonly IProvideQueries _provideQueries; private readonly IProvideQueries _provideQueries;
private readonly ITrackDetailsEsRepository _trackDetailsEsRepository; private readonly ITrackDetailsEsRepository _trackDetailsEsRepository;
private readonly IBroadcastingService _broadcastingService; private readonly IBroadcastingService _broadcastingService;
private readonly ILogger<TrackUpsertEventHandler> _logger;
private readonly IOrderOperationRecordEsRepository _orderOperationRecordEsRepository; private readonly IOrderOperationRecordEsRepository _orderOperationRecordEsRepository;
public TrackUpsertEventHandler( public TrackUpsertEventHandler(
@ -29,7 +31,8 @@ public class TrackUpsertEventHandler : ITransientDependency, ICapSubscribe
IProvideQueries provideQueries, IProvideQueries provideQueries,
ITrackDetailsEsRepository trackDetailsEsRepository, ITrackDetailsEsRepository trackDetailsEsRepository,
IOrderOperationRecordEsRepository orderOperationRecordEsRepository, IOrderOperationRecordEsRepository orderOperationRecordEsRepository,
IBroadcastingService broadcastingService) IBroadcastingService broadcastingService,
ILogger<TrackUpsertEventHandler> logger)
{ {
_orderRepository = orderRepository; _orderRepository = orderRepository;
_capPublisher = capPublisher; _capPublisher = capPublisher;
@ -37,6 +40,7 @@ public class TrackUpsertEventHandler : ITransientDependency, ICapSubscribe
_trackDetailsEsRepository = trackDetailsEsRepository; _trackDetailsEsRepository = trackDetailsEsRepository;
_orderOperationRecordEsRepository = orderOperationRecordEsRepository; _orderOperationRecordEsRepository = orderOperationRecordEsRepository;
_broadcastingService = broadcastingService; _broadcastingService = broadcastingService;
_logger = logger;
} }
[CapSubscribe(GlobalConst.TrackUpsertSubscribe, Group = GlobalConst.TrackUpsertSubscribe)] [CapSubscribe(GlobalConst.TrackUpsertSubscribe, Group = GlobalConst.TrackUpsertSubscribe)]
@ -262,21 +266,18 @@ public class TrackUpsertEventHandler : ITransientDependency, ICapSubscribe
} }
else else
{ {
Debug.Assert(@event.CompareFields != null, "@event.CompareFields != null");
var extraDict = GetExtraDictForTextJson(@event.Extra); var extraDict = GetExtraDictForTextJson(@event.Extra);
if (@event.Extra == null || if (extraDict == null || !extraDict.TryGetValue(@event.CompareFields, out var compareValue))
extraDict == null
|| extraDict.TryGetValue(@event.CompareFields, out var compareValue) == false)
{ {
throw new BusinessException(message: "轨迹比较字段不存在,请检查拓展字段"); throw new BusinessException(message: $"请检查拓展字段,为什么找不到比较字段:{@event.CompareFields}");
} }
existTracks = existTracks.Where(x => existTracks = existTracks.Where(x =>
{ {
var oldExtraDict = GetExtraDictForTextJson(x.Extra); var oldExtraDict = GetExtraDictForTextJson(x.Extra);
// tips:注意兼容性,历史轨迹的字段可能为空 // tips:注意兼容性,历史轨迹的字段可能为空
if (x.Extra == null if (oldExtraDict == null|| !oldExtraDict.TryGetValue(@event.CompareFields, out var oldValue))
|| oldExtraDict == null
|| !oldExtraDict.TryGetValue(@event.CompareFields, out var oldValue))
{ {
return false; return false;
} }
@ -291,26 +292,19 @@ public class TrackUpsertEventHandler : ITransientDependency, ICapSubscribe
{ {
if (existTracks.Count > 1) if (existTracks.Count > 1)
{ {
// 修改轨迹时,订单存在多条轨迹不允许修改 // todo: 所有的event handle 多写log,取缔注释,特别是continue,return,这种跳出的地方,方便排查问题
_logger.LogInformation("订单存在多条轨迹不允许修改:{@event}", @event);
continue; continue;
} }
// todo:例如这里
if (track == null) continue; // 不存在不让改 if (track == null) continue; // 不存在不让改
updateTracks.Add(BuiltUpdateTrack(@event, track, order, trackContent)); updateTracks.Add(BuiltUpdateTrack(@event, track, order, trackContent));
} }
else else
{ {
// 【批量新增】所有可能遇到的场景 // 不允许重复轨迹,跳过
// 标识:允许重复,不允许重复,零条,一条,多条;
// 场景A:允许重复,零条 => 新增
// 场景B:允许重复,一条 => 新增(约束受限新增,否则跳过新增)
// 场景C:允许重复,多条 => 新增(约束受限新增,否则跳过新增)
// 场景D:不允许重复,零条 => 新增
// 场景E:不允许重复,一条 => 跳过
// 场景F:不允许重复,(理想不会出现多条) => 跳过
// 不允许重复轨迹
if (!trackContent.IsRepeatable && existTracks.Count > 0) continue; if (!trackContent.IsRepeatable && existTracks.Count > 0) continue;
insertTracks.Add(BuiltNewTrack(@event, order, trackContent)); insertTracks.Add(BuiltNewTrack(@event, order, trackContent));
@ -369,6 +363,7 @@ public class TrackUpsertEventHandler : ITransientDependency, ICapSubscribe
track.EstimatedDeliveryDate = @event.EstimatedDeliveryDate; track.EstimatedDeliveryDate = @event.EstimatedDeliveryDate;
track.Extra = @event.Extra; track.Extra = @event.Extra;
// todo:如果后面写es异常了,这里日志不就不准了,这里应该和写轨迹一样,做成批量的,放到写完轨迹只会写日志
// 写入操作日志 // 写入操作日志
_ = _orderOperationRecordEsRepository.AddAsync(new OrderOperationRecordIndex _ = _orderOperationRecordEsRepository.AddAsync(new OrderOperationRecordIndex
{ {
@ -434,6 +429,7 @@ public class TrackUpsertEventHandler : ITransientDependency, ICapSubscribe
? track.TrackTime ? track.TrackTime
: track.TrackTime.Subtract(TimeSpan.FromHours(track.TrackTimeZone)); : track.TrackTime.Subtract(TimeSpan.FromHours(track.TrackTimeZone));
// todo:如果后面写es异常了,这里日志不就不准了,这里应该和写轨迹一样,做成批量的,放到写完轨迹只会写日志
// 写入操作日志 // 写入操作日志
_ = _orderOperationRecordEsRepository.AddAsync(new OrderOperationRecordIndex _ = _orderOperationRecordEsRepository.AddAsync(new OrderOperationRecordIndex
{ {