diff --git a/src/SJZY.Track.Application.Contract/Events/PushWarningEvent.cs b/src/SJZY.Track.Application.Contract/Events/PushWarningEvent.cs new file mode 100644 index 00000000..c8fbdb20 --- /dev/null +++ b/src/SJZY.Track.Application.Contract/Events/PushWarningEvent.cs @@ -0,0 +1,6 @@ +namespace SJZY.Track.Application.Contract.Events; + +public class PushWarningEvent +{ + public int PageSize { get; set; } +} \ No newline at end of file diff --git a/src/SJZY.Track.Application/CAP/EventHandlers/PushWarningEventHandler.cs b/src/SJZY.Track.Application/CAP/EventHandlers/PushWarningEventHandler.cs new file mode 100644 index 00000000..8197ec1c --- /dev/null +++ b/src/SJZY.Track.Application/CAP/EventHandlers/PushWarningEventHandler.cs @@ -0,0 +1,43 @@ +using DotNetCore.CAP; +using Microsoft.Extensions.Logging; +using SJZY.Track.Application.Contract.Events; +using SJZY.Track.Domain.Aggregates.OrderAggregate; +using SJZY.Track.Domain.Consts; +using Volo.Abp.DependencyInjection; + +namespace SJZY.Track.Application.CAP.EventHandlers; + +public class PushWarningEventHandler:ITransientDependency, ICapSubscribe +{ + private readonly ILogger _logger; + private readonly IOrderRepository _orderRepository; + + public PushWarningEventHandler(IOrderRepository orderRepository, + ILogger logger) + { + _orderRepository = orderRepository; + _logger = logger; + } + + [CapSubscribe(GlobalConst.TrackPushWarning)] + public async Task Handle(PushWarningEvent @event) + { + int total =0, pageSize = @event.PageSize,waitTime = 1000; + var dateTimes = Enumerable.Range(1, 12).Select(val => DateTime.Now.AddHours(-val)).ToList(); + + foreach (var dateTime in dateTimes) + { + var rows = 0; + var pageindex = 1; + do + { + rows = await _orderRepository.PushWarnOrdersAsync(pageSize,pageindex,dateTime); + pageindex++; + total+=rows; + await Task.Delay(waitTime); + }while (rows!= pageSize); + } + + _logger.LogInformation(message: "本次预警扫描了:{total}个订单",total); + } +} \ No newline at end of file diff --git a/src/SJZY.Track.Application/Services/ProvideServices/TrackPushService.cs b/src/SJZY.Track.Application/Services/ProvideServices/TrackPushService.cs index fe78e2dd..6b21c0fb 100644 --- a/src/SJZY.Track.Application/Services/ProvideServices/TrackPushService.cs +++ b/src/SJZY.Track.Application/Services/ProvideServices/TrackPushService.cs @@ -1,6 +1,5 @@ using DotNetCore.CAP; using Microsoft.AspNetCore.Authorization; -using SJZY.Track.Application.CAP.EventHandlers; using SJZY.Track.Application.Contract.Dtos.ProvideServices; using SJZY.Track.Application.Contract.Events; using SJZY.Track.Domain.Aggregates.OrderAggregate; @@ -18,10 +17,10 @@ namespace SJZY.Track.Application.Services.ProvideServices; [Route("/ProviderService")] public class TrackPushService : ApplicationService { - private readonly IOrderRepository _orderRepository; private readonly ICapPublisher _capPublisher; - private readonly ITrackPushRecordsRepository _trackPushRecordsRepository; + private readonly IOrderRepository _orderRepository; private readonly IHttpClientProxy _requestCurrentUserService; + private readonly ITrackPushRecordsRepository _trackPushRecordsRepository; public TrackPushService(IOrderRepository orderRepository , ICapPublisher capPublisher @@ -92,23 +91,12 @@ public class TrackPushService : ApplicationService [HttpPost("Track/PushWarnForTask")] public async Task PushWarnForTaskAsync() { - int total = 0, pageSize = 1000,waitTime = 1000; - var dateTimes = Enumerable.Range(1, 12).Select(val => DateTime.Now.AddHours(-val)).ToList(); - - foreach (var dateTime in dateTimes) + await _capPublisher.PublishAsync(GlobalConst.TrackPushWarning, new PushWarningEvent { - var rows = 0; - var pageindex = 1; - do - { - rows = await _orderRepository.PushWarnOrdersAsync(pageSize,pageindex,dateTime); - total+=rows; - pageindex++; - await Task.Delay(waitTime); - }while (rows!= pageSize); - } - - return $"本次预警扫描了:{total}个订单"; + PageSize = 1000 + }); + + return "推送预警任务已启动"; } /// diff --git a/src/SJZY.Track.Domain/Consts/GlobalConst.MQ.cs b/src/SJZY.Track.Domain/Consts/GlobalConst.MQ.cs index 378f0b89..515afe91 100644 --- a/src/SJZY.Track.Domain/Consts/GlobalConst.MQ.cs +++ b/src/SJZY.Track.Domain/Consts/GlobalConst.MQ.cs @@ -56,4 +56,9 @@ public partial class GlobalConst /// 关联订单后补轨迹 /// public const string OrderLinkAfterFillTrack = "SJZY.Track.OrderLinkAfterFillTrack"; + + /// + /// 推送预警任务 + /// + public const string TrackPushWarning = "SJZY.Track.TrackPushWarning"; } \ No newline at end of file