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