diff --git a/src/SJZY.Track.Application.Contract/Services/ProvideServices/ITrackPushService.cs b/src/SJZY.Track.Application.Contract/Services/ProvideServices/ITrackPushService.cs index a108aac2..4ddeb81d 100644 --- a/src/SJZY.Track.Application.Contract/Services/ProvideServices/ITrackPushService.cs +++ b/src/SJZY.Track.Application.Contract/Services/ProvideServices/ITrackPushService.cs @@ -1,4 +1,5 @@ using SJZY.Track.Application.Contract.Dtos.ProvideServices; +using SJZY.Track.Application.Contract.Dtos.TrackOrder; using SJZY.Track.Application.Contract.Dtos.TrackPush; using Volo.Abp; @@ -20,4 +21,11 @@ public interface ITrackPushService : IRemoteService /// 子系统id /// 客户id public Task> GetTrackPushContentListAsync(int subSystemId, int customerId); + + /// + /// 获取轨迹推送记录 + /// + /// + /// + public Task> GetTrackPushRecordsAsync(OrderNoInfo input); } diff --git a/src/SJZY.Track.Application/BackgroundServices/OrderPullStatusScanner.cs b/src/SJZY.Track.Application/BackgroundServices/OrderPullStatusScanner.cs deleted file mode 100644 index 6875fd82..00000000 --- a/src/SJZY.Track.Application/BackgroundServices/OrderPullStatusScanner.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Diagnostics; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Options; -using SJZY.Expand.ABP.Core.Logger; -using SJZY.Track.Application.Options; -using SJZY.Track.Domain.Aggregates.OrderAggregate; - -namespace SJZY.Track.Application.BackgroundServices; - -/// -/// 订单拉取状态扫描 -/// -public class OrderPullStatusScanner : BackgroundService -{ - private readonly IOptionsMonitor _options; - private readonly IOrderRepository _orderRepository; - - public OrderPullStatusScanner(IOrderRepository orderRepository, IOptionsMonitor options) - { - _orderRepository = orderRepository; - _options = options; - } - - protected override async Task ExecuteAsync(CancellationToken stoppingToken) - { - while (true) - { - await Task.Delay(TimeSpan.FromSeconds(_options.CurrentValue.OrderPullStatusScanInterval), stoppingToken); - - var stopwatch = Stopwatch.StartNew(); - var count1 = await _orderRepository.UpdatePullStatusBySilenceAsync(); - // real del - var count2 = await _orderRepository.ClearPullStatusAsync(); - stopwatch.Stop(); - LogHelper.Info( - $"{nameof(OrderPullStatusScanner)}本次扫描更新了{count1}条,删除了{count2}条订单拉取状态,耗时{stopwatch.ElapsedMilliseconds}ms"); - } - } -} diff --git a/src/SJZY.Track.Application/Services/ProvideServices/TrackPullService.cs b/src/SJZY.Track.Application/Services/ProvideServices/TrackPullService.cs index 6ed62ad9..28b082fd 100644 --- a/src/SJZY.Track.Application/Services/ProvideServices/TrackPullService.cs +++ b/src/SJZY.Track.Application/Services/ProvideServices/TrackPullService.cs @@ -1,16 +1,13 @@ using Microsoft.AspNetCore.Authorization; -using SJZY.Expand.ABP.Core; using SJZY.Expand.ABP.Core.Logger; using SJZY.Expand.ABP.Core.PublicMethords; using SJZY.InterfaceRelay.Application.Contracts; using SJZY.Track.Application.Contract.Dtos.ProvideServices; using SJZY.Track.Application.Contract.Dtos.TrackPull; -using SJZY.Track.Application.Contract.Services; using SJZY.Track.Application.Contract.Services.ProvideServices; using SJZY.Track.Application.Helpers; using SJZY.Track.Application.Rabbit; using SJZY.Track.Application.Track2InterfaceRelay; -using SJZY.Track.Domain.Dtos.TrackPull; using SJZY.Track.Domain.Queries; using Volo.Abp.Http.Client.DynamicProxying; @@ -25,15 +22,13 @@ public class TrackPullService : ApplicationService, ITrackPullService { private readonly IHttpClientProxy _httpClientProxy; private readonly IProvideQueries _provideQueries; - private readonly ITrackPullCacheService _trackPullCacheService; + public TrackPullService(IProvideQueries provideQueries, - IHttpClientProxy httpClientProxy, - ITrackPullCacheService trackPullCacheService) + IHttpClientProxy httpClientProxy) { _provideQueries = provideQueries; _httpClientProxy = httpClientProxy; - _trackPullCacheService = trackPullCacheService; } /// @@ -93,23 +88,6 @@ public class TrackPullService : ApplicationService, ITrackPullService return true; } - /// - /// 定时任务刷新轨迹 - /// - /// - [AllowAnonymous] - [HttpPost("Track/RefreshForTask")] - public async Task RefreshForTaskAsync() - { - var configs = await _provideQueries.GetAllPullConfigsAsync(); - - foreach (var config in configs) - { - await RefreshForConfigAsync(config); - } - - return true; - } /// /// 轨迹刷新测试 @@ -128,60 +106,4 @@ public class TrackPullService : ApplicationService, ITrackPullService return true; } - - private async Task RefreshForConfigAsync(TrackPullConfigVo config) - { - var redisKey = _trackPullCacheService.TrackPullKey(config.SubSystemId, config.ServiceProviderCode); - if (await RedisHelper.Client.ExistsAsync(redisKey)) - { - return; - } - - _ = Task.Run(async () => - { - try - { - using (Track2InterfaceRelaySubSystemContext.Use( - new Track2InterfaceRelaySubSystemContext(config.SubSystemCode))) - { - var page = 1; - var orders = - await _provideQueries.FindOrderWithServiceProviderAsync(config.ServiceProviderCode, page); - while (orders.Any()) - { - var serviceProviderCode = orders.FirstOrDefault()?.ServiceProviderCode; - - foreach (var group in orders.GroupBy(x => x.OrderType)) - { - var data = new List(group.Select(x => new TrackPullPayload - { - OrderNo = x.OrderNo - })); - var attach = Base64Helper.SerializeObject(new TrackRefreshAttach(config.ServiceProviderCode, - config.SubSystemId, group.Key)); - var res = await _httpClientProxy.Service.BatchForwardThenPushToQueueAsync( - InterfaceRelayConst.GetTrackInterfaceCode, serviceProviderCode, data, - GlobalConst.TrackRefreshSubscribe, attach); - - if (res.Code != InterfaceRelayConst.SuccessCode) - { - LogHelper.Error($"刷新轨迹失败:{res.ToJson()}"); - } - } - - orders = await _provideQueries.FindOrderWithServiceProviderAsync(config.ServiceProviderCode, - ++page); - } - } - } - catch (Exception e) - { - LogHelper.Error(e.ToString()); - } - finally - { - await RedisHelper.Client.SetExAsync(redisKey, config.PullInterval * 60, 1); - } - }); - } } diff --git a/src/SJZY.Track.Application/Services/ProvideServices/TrackPushService.cs b/src/SJZY.Track.Application/Services/ProvideServices/TrackPushService.cs index 5ce6f798..7675cf3c 100644 --- a/src/SJZY.Track.Application/Services/ProvideServices/TrackPushService.cs +++ b/src/SJZY.Track.Application/Services/ProvideServices/TrackPushService.cs @@ -1,8 +1,6 @@ using DotNetCore.CAP; -using Microsoft.AspNetCore.Authorization; -using SJZY.Expand.ABP.Core; -using SJZY.Expand.ABP.Core.Logger; using SJZY.Track.Application.Contract.Dtos.ProvideServices; +using SJZY.Track.Application.Contract.Dtos.TrackOrder; using SJZY.Track.Application.Contract.Dtos.TrackPush; using SJZY.Track.Application.Contract.Events; using SJZY.Track.Application.Contract.Services.ProvideServices; @@ -97,7 +95,7 @@ public class TrackPushService : ApplicationService, ITrackPushService /// /// /// - [HttpPost("Track/GetTrackPushContentList")] + [HttpGet("Track/GetTrackPushContentList")] public async Task> GetTrackPushContentListAsync(int subSystemId, int customerId) { var list = await _trackPushConfigRepository.GetTrackPushContentListAsync(subSystemId, customerId); @@ -107,61 +105,13 @@ public class TrackPushService : ApplicationService, ITrackPushService return result; } - - /// - /// 定时任务推送预警(》12小时没有推送的) - /// - /// - [AllowAnonymous] - [HttpPost("Track/PushWarnForTask")] - public async Task PushWarnForTaskAsync() - { - var hasProcess = await RedisHelper.Client.ExistsAsync(GlobalConst.TrackPushTrackWaringLockKey); - if (hasProcess) - { - return "冷却中"; - } - - await RedisHelper.Client.SetAsync(GlobalConst.TrackPushTrackWaringLockKey, 1, TimeSpan.FromHours(3)); - - _ = Task.Run(async () => - { - try - { - int total = 0, pageSize = 1000, waitTime = 1000; - int rows; - var pageindex = 0; - - do - { - rows = await _orderRepository.PushWarnOrdersAsync(pageSize, pageindex); - pageindex++; - total += rows; - await Task.Delay(waitTime); - } while (rows == pageSize); - - LogHelper.Info($"本次预警扫描了:{total}个订单"); - } - catch (Exception e) - { - LogHelper.Error(e.ToString()); - } - finally - { - await RedisHelper.Client.DelAsync(GlobalConst.TrackPushTrackWaringLockKey); - } - }); - - return "预警中"; - } - /// /// 获取轨迹推送记录 /// /// /// [HttpGet("Track/GetTrackPushRecords")] - public async Task> GetTrackPushRecordsAsync(OrderNoInfo input) + public async Task> GetTrackPushRecordsAsync([FromQuery] OrderNoInfo input) { var orderId = await _orderRepository.GetFirstAsync(x => x.SubSystemId == input.SubSystemId && x.OrderType == input.OrderType && input.OrderNo == x.OrderNo, @@ -171,6 +121,8 @@ public class TrackPushService : ApplicationService, ITrackPushService throw new BusinessException(message: "订单不存在"); } - return await _trackPushRecordEsRepository.GetTrackPushRecordsAsync(orderId); + var records = await _trackPushRecordEsRepository.GetTrackPushRecordsAsync(orderId); + + return ObjectMapper.Map, List>(records); } } diff --git a/src/SJZY.Track.Application/Services/TimerTaskss/OrderPullStatusScanTask.cs b/src/SJZY.Track.Application/Services/TimerTaskss/OrderPullStatusScanTask.cs new file mode 100644 index 00000000..8d3bdfd1 --- /dev/null +++ b/src/SJZY.Track.Application/Services/TimerTaskss/OrderPullStatusScanTask.cs @@ -0,0 +1,66 @@ +using System.Diagnostics; +using Microsoft.AspNetCore.Authorization; +using Microsoft.Extensions.Options; +using SJZY.Expand.ABP.Core; +using SJZY.Expand.ABP.Core.Logger; +using SJZY.Track.Application.Options; +using SJZY.Track.Domain.Aggregates.OrderAggregate; + +namespace SJZY.Track.Application.Services.TimerTaskss; + +/// +/// 订单拉取状态扫描定时任务 +/// +[ApiExplorerSettings(GroupName = "定时任务")] +public class OrderPullStatusScanTask : ApplicationService +{ + private readonly IOptionsSnapshot _options; + private readonly IOrderRepository _orderRepository; + + public OrderPullStatusScanTask(IOrderRepository orderRepository, IOptionsSnapshot options) + { + _orderRepository = orderRepository; + _options = options; + } + + /// + /// 定时任务对订单拉取状态进行扫描,计算更新拉取状态 + /// + /// + [AllowAnonymous] + [HttpPost("/TimerTask/OrderPullStatusScan")] + public async Task OrderPullStatusScanAsync() + { + var redisKey = GlobalConst.OrderPullStatusScanCheckKey; + if (await RedisHelper.Client.ExistsAsync(redisKey)) + { + return "冷却中"; + } + + await RedisHelper.Client.SetAsync(redisKey, 1, + TimeSpan.FromSeconds(_options.Value.OrderPullStatusScanInterval)); + + var res = string.Empty; + try + { + var stopwatch = new Stopwatch(); + + stopwatch.Restart(); + var count1 = await _orderRepository.UpdatePullStatusBySilenceAsync(); + // real del + var count2 = await _orderRepository.ClearPullStatusAsync(); + stopwatch.Stop(); + + res = + $"{nameof(OrderPullStatusScanTask)}本次扫描更新了{count1}条,删除了{count2}条订单拉取状态,耗时{stopwatch.ElapsedMilliseconds}ms"; + } + catch (Exception e) + { + await RedisHelper.Client.DelAsync(redisKey); + LogHelper.Error(e.ToString()); + } + + + return res; + } +} diff --git a/src/SJZY.Track.Application/BackgroundServices/TrackButtomUpScanner.cs b/src/SJZY.Track.Application/Services/TimerTaskss/TrackButtomUpScanTask.cs similarity index 81% rename from src/SJZY.Track.Application/BackgroundServices/TrackButtomUpScanner.cs rename to src/SJZY.Track.Application/Services/TimerTaskss/TrackButtomUpScanTask.cs index bce80945..bd1826d2 100644 --- a/src/SJZY.Track.Application/BackgroundServices/TrackButtomUpScanner.cs +++ b/src/SJZY.Track.Application/Services/TimerTaskss/TrackButtomUpScanTask.cs @@ -1,7 +1,9 @@ using System.Linq.Expressions; -using Microsoft.Extensions.Hosting; +using Microsoft.AspNetCore.Authorization; using Microsoft.Extensions.Options; using Newtonsoft.Json.Linq; +using SJZY.Expand.ABP.Core; +using SJZY.Expand.ABP.Core.Logger; using SJZY.Track.Application.Contract.Dtos.TrackPull; using SJZY.Track.Application.Helpers; using SJZY.Track.Application.Options; @@ -9,32 +11,57 @@ using SJZY.Track.Domain.Aggregates.OrderAggregate; using SJZY.Track.Domain.Enums; using SJZY.Track.Domain.Queries; -namespace SJZY.Track.Application.BackgroundServices; +namespace SJZY.Track.Application.Services.TimerTaskss; /// -/// 轨迹(发生地时区)兜底扫描 +/// 轨迹兜底扫描定时任务 /// -public class TrackButtomUpScanner : BackgroundService +[ApiExplorerSettings(GroupName = "定时任务")] +public class TrackButtomUpScanTask : ApplicationService { - private readonly IOptionsMonitor _options; + private readonly IOptionsSnapshot _options; private readonly IOrderRepository _orderRepository; private readonly ITimezoneQueries _timezoneQueries; - public TrackButtomUpScanner(ITimezoneQueries timezoneQueries, IOrderRepository orderRepository, - IOptionsMonitor options) + public TrackButtomUpScanTask(IOptionsSnapshot options, IOrderRepository orderRepository, + ITimezoneQueries timezoneQueries) { - _timezoneQueries = timezoneQueries; - _orderRepository = orderRepository; _options = options; + _orderRepository = orderRepository; + _timezoneQueries = timezoneQueries; } - protected override Task ExecuteAsync(CancellationToken stoppingToken) + /// + /// 定时任务对轨迹进行兜底扫描,尝试弥补轨迹中的缺失信息 + /// + /// + [AllowAnonymous] + [HttpPost("/TimerTask/TrackButtomUpScan")] + public async Task TrackButtomUpScanAsync() { - // 查看哪些要兜底的轨迹(服务商拉取的,且时区发生地是空的),根据子系统、服务商分组尝试兜底 - // 维护轨迹明细、订单主表 - return Task.CompletedTask; + var redisKey = GlobalConst.OrderPullStatusScanCheckKey; + if (await RedisHelper.Client.ExistsAsync(redisKey)) + { + return "冷却中"; + } + + await RedisHelper.Client.SetAsync(redisKey, 1, + TimeSpan.FromSeconds(_options.Value.OrderPullStatusScanInterval)); + + try + { + // do st + } + catch (Exception e) + { + await RedisHelper.Client.DelAsync(redisKey); + LogHelper.Error(e.ToString()); + } + + return "ok"; } + /// /// 获取轨迹时区 /// diff --git a/src/SJZY.Track.Application/Services/TimerTaskss/TrackPullTask.cs b/src/SJZY.Track.Application/Services/TimerTaskss/TrackPullTask.cs new file mode 100644 index 00000000..dbab9348 --- /dev/null +++ b/src/SJZY.Track.Application/Services/TimerTaskss/TrackPullTask.cs @@ -0,0 +1,106 @@ +using Microsoft.AspNetCore.Authorization; +using SJZY.Expand.ABP.Core; +using SJZY.Expand.ABP.Core.Logger; +using SJZY.Expand.ABP.Core.PublicMethords; +using SJZY.InterfaceRelay.Application.Contracts; +using SJZY.Track.Application.Contract.Dtos.TrackPull; +using SJZY.Track.Application.Contract.Services; +using SJZY.Track.Application.Helpers; +using SJZY.Track.Application.Track2InterfaceRelay; +using SJZY.Track.Domain.Dtos.TrackPull; +using SJZY.Track.Domain.Queries; +using Volo.Abp.Http.Client.DynamicProxying; + +namespace SJZY.Track.Application.Services.TimerTaskss; + +/// +/// 轨迹拉取定时任务 +/// +[ApiExplorerSettings(GroupName = "定时任务")] +public class TrackPullTask : ApplicationService +{ + private readonly IHttpClientProxy _httpClientProxy; + private readonly IProvideQueries _provideQueries; + private readonly ITrackPullCacheService _trackPullCacheService; + + public TrackPullTask(IProvideQueries provideQueries, ITrackPullCacheService trackPullCacheService, + IHttpClientProxy httpClientProxy) + { + _provideQueries = provideQueries; + _trackPullCacheService = trackPullCacheService; + _httpClientProxy = httpClientProxy; + } + + /// + /// 定时任务刷新轨迹 + /// + /// + [AllowAnonymous] + [HttpPost("/TimerTask/Refresh")] + public async Task RefreshForTaskAsync() + { + var configs = await _provideQueries.GetAllPullConfigsAsync(); + + foreach (var config in configs) + { + await RefreshForConfigAsync(config); + } + + return true; + } + + private async Task RefreshForConfigAsync(TrackPullConfigVo config) + { + var redisKey = _trackPullCacheService.TrackPullKey(config.SubSystemId, config.ServiceProviderCode); + if (await RedisHelper.Client.ExistsAsync(redisKey)) + { + return; + } + + await RedisHelper.Client.SetExAsync(redisKey, config.PullInterval * 60, 1); + + _ = Task.Run(async () => + { + try + { + using (Track2InterfaceRelaySubSystemContext.Use( + new Track2InterfaceRelaySubSystemContext(config.SubSystemCode))) + { + var page = 1; + var orders = + await _provideQueries.FindOrderWithServiceProviderAsync(config.ServiceProviderCode, page); + while (orders.Any()) + { + var serviceProviderCode = orders.FirstOrDefault()?.ServiceProviderCode; + + foreach (var group in orders.GroupBy(x => x.OrderType)) + { + var data = new List(group.Select(x => new TrackPullPayload + { + OrderNo = x.OrderNo + })); + var attach = Base64Helper.SerializeObject(new TrackRefreshAttach(config.ServiceProviderCode, + config.SubSystemId, group.Key)); + var res = await _httpClientProxy.Service.BatchForwardThenPushToQueueAsync( + InterfaceRelayConst.GetTrackInterfaceCode, serviceProviderCode, data, + GlobalConst.TrackRefreshSubscribe, attach); + + if (res.Code != InterfaceRelayConst.SuccessCode) + { + LogHelper.Error($"刷新轨迹失败:{res.ToJson()}"); + } + } + + orders = await _provideQueries.FindOrderWithServiceProviderAsync(config.ServiceProviderCode, + ++page); + } + } + } + catch (Exception e) + { + await RedisHelper.Client.DelAsync(redisKey); + LogHelper.Error(e.ToString()); + } + }); + } +} diff --git a/src/SJZY.Track.Application/Services/TimerTaskss/TrackPushTask.cs b/src/SJZY.Track.Application/Services/TimerTaskss/TrackPushTask.cs new file mode 100644 index 00000000..161283fd --- /dev/null +++ b/src/SJZY.Track.Application/Services/TimerTaskss/TrackPushTask.cs @@ -0,0 +1,79 @@ +using Microsoft.AspNetCore.Authorization; +using Microsoft.Extensions.Options; +using SJZY.Expand.ABP.Core; +using SJZY.Expand.ABP.Core.Logger; +using SJZY.Track.Application.Track2InterfaceRelay; +using SJZY.Track.Domain.Aggregates.OrderAggregate; + +namespace SJZY.Track.Application.Services.TimerTaskss; + +/// +/// 轨迹推送定时任务 +/// +[ApiExplorerSettings(GroupName = "定时任务")] +public class TrackPushTask : ApplicationService +{ + private readonly IOptionsSnapshot _options; + private readonly IOrderRepository _orderRepository; + + public TrackPushTask(IOrderRepository orderRepository, IOptionsSnapshot options) + { + _orderRepository = orderRepository; + _options = options; + } + + /// + /// 定时任务推送预警(》12小时没有推送的) + /// + /// + [AllowAnonymous] + [HttpPost("/TimerTask/PushWarn")] + public async Task PushWarnForTaskAsync() + { + var configs = _options.Value; + if (configs == null || configs.Keys.Count == 0) + { + throw new BusinessException("未配置子系统"); + } + + var subSystemIds = await _orderRepository.GetSubSystemListByCodesAsync(configs.Keys.ToList()); + foreach (var item in subSystemIds) + { + var subSystemId = item.id; + var redisKey = $"{GlobalConst.TrackPushWaringKeyPrefix}:{subSystemId}"; + if (await RedisHelper.Client.ExistsAsync(redisKey)) + { + continue; + } + + await RedisHelper.Client.SetAsync(redisKey, 1, TimeSpan.FromHours(1)); + + _ = Task.Run(async () => + { + try + { + int total = 0, pageSize = 1000, waitTime = 1000; + int rows; + var pageindex = 0; + + do + { + rows = await _orderRepository.PushWarnOrdersAsync(subSystemId, pageSize, pageindex); + pageindex++; + total += rows; + await Task.Delay(waitTime); + } while (rows == pageSize); + + LogHelper.Info($"本次预警扫描了:{total}个订单"); + } + catch (Exception e) + { + await RedisHelper.Client.DelAsync(redisKey); + LogHelper.Error(e.ToString()); + } + }); + } + + return "预警中"; + } +} diff --git a/src/SJZY.Track.Application/Services/TimerTaskss/TrackWarningTask.cs b/src/SJZY.Track.Application/Services/TimerTaskss/TrackWarningTask.cs new file mode 100644 index 00000000..dbbbfcad --- /dev/null +++ b/src/SJZY.Track.Application/Services/TimerTaskss/TrackWarningTask.cs @@ -0,0 +1,74 @@ +using Microsoft.AspNetCore.Authorization; +using Microsoft.Extensions.Options; +using SJZY.Expand.ABP.Core; +using SJZY.Expand.ABP.Core.Logger; +using SJZY.Track.Application.Contract.Services; +using SJZY.Track.Application.Track2InterfaceRelay; +using SJZY.Track.Domain.Aggregates.OrderAggregate; + +namespace SJZY.Track.Application.Services.TimerTaskss; + +/// +/// 轨迹运输时效预警定时任务 +/// +[ApiExplorerSettings(GroupName = "定时任务")] +public class TrackWarningTask : ApplicationService +{ + private readonly IForewarningNotifyService _forewarningNotifyService; + private readonly IOptionsSnapshot _options; + private readonly IOrderRepository _orderRepository; + + public TrackWarningTask(IForewarningNotifyService forewarningNotifyService, IOrderRepository orderRepository, + IOptionsSnapshot options) + { + _forewarningNotifyService = forewarningNotifyService; + _orderRepository = orderRepository; + _options = options; + } + + /// + /// 轨迹运输时效预警通知 + /// + /// + [AllowAnonymous] + [HttpPost("/TimerTask/SendWarning")] + public async Task SendWarningForTaskAsync() + { + var configs = _options.Value; + if (configs == null || configs.Keys.Count == 0) + { + throw new BusinessException("未配置子系统"); + } + + var subSystemIds = await _orderRepository.GetSubSystemListByCodesAsync(configs.Keys.ToList()); + foreach (var item in subSystemIds) + { + var subSystemId = item.id; + // 如果已经发送过通知,不再发送 + var redisKey = $"{GlobalConst.ForewarningNotifyCheckKey}:{subSystemId}"; + if (await RedisHelper.Client.ExistsAsync(redisKey)) + { + continue; + } + + await RedisHelper.Client.SetExAsync(redisKey, configs[item.code].WarningNotifyInterval * 60, 1); + + // todo:改mq分流? + _ = Task.Run(async () => + { + try + { + var cnt = await _forewarningNotifyService.SendNotificationAsync(subSystemId); + LogHelper.Info($"预警通知发送成功,新增预警记录{cnt}条"); + } + catch (Exception e) + { + await RedisHelper.Client.DelAsync(redisKey); + LogHelper.Error(e.ToString()); + } + }); + } + + return true; + } +} diff --git a/src/SJZY.Track.Application/Services/TrackOrders/OrderWarningService.cs b/src/SJZY.Track.Application/Services/TrackOrders/OrderWarningService.cs index 06decd7c..309c83a8 100644 --- a/src/SJZY.Track.Application/Services/TrackOrders/OrderWarningService.cs +++ b/src/SJZY.Track.Application/Services/TrackOrders/OrderWarningService.cs @@ -1,9 +1,3 @@ -using Microsoft.AspNetCore.Authorization; -using Microsoft.Extensions.Options; -using SJZY.Expand.ABP.Core; -using SJZY.Expand.ABP.Core.Logger; -using SJZY.Track.Application.Contract.Services; -using SJZY.Track.Application.Track2InterfaceRelay; using SJZY.Track.Domain.Aggregates.ForewarningAggregate; using SJZY.Track.Domain.Aggregates.OrderAggregate; using SJZY.Track.Domain.Dtos.Forewarning; @@ -17,16 +11,13 @@ namespace SJZY.Track.Application.Services.TrackOrders; [Route("/Track/Order")] public class OrderWarningService : ApplicationService { - private readonly IForewarningNotifyService _forewarningNotifyService; private readonly IOrderRepository _orderRepository; private readonly IWarningRecordEsRepository _warningRecordEsRepository; - public OrderWarningService(IWarningRecordEsRepository warningRecordEsRepository, IOrderRepository orderRepository, - IForewarningNotifyService forewarningNotifyService) + public OrderWarningService(IWarningRecordEsRepository warningRecordEsRepository, IOrderRepository orderRepository) { _warningRecordEsRepository = warningRecordEsRepository; _orderRepository = orderRepository; - _forewarningNotifyService = forewarningNotifyService; } /// @@ -78,54 +69,4 @@ public class OrderWarningService : ApplicationService return new UnifyPageResultDto(list, inputVo.PageIndex, inputVo.PageSize, (int) result.total); } - - - /// - /// 定时任务轨迹运输时效预警推送 - /// - /// - [AllowAnonymous] - [HttpPost("SendWarningForTask")] - public async Task SendWarningForTaskAsync([FromBody] IOptionsSnapshot options) - { - var configs = options.Value; - if (configs == null || configs.Keys.Count == 0) - { - throw new BusinessException("未配置预警系统"); - } - - var subSystemIds = await _orderRepository.GetSubSystemListByCodesAsync(configs.Keys.ToList()); - foreach (var item in subSystemIds) - { - var subSystemId = item.id; - // 如果已经发送过通知,不再发送 - var redisKey = $"{GlobalConst.ForewarningNotifyCheckKey}:{subSystemId}"; - if (await RedisHelper.Client.ExistsAsync(redisKey)) - { - continue; - } - - await RedisHelper.Client.SetExAsync(redisKey, configs[item.code].WarningNotifyInterval * 60, 1); - - // todo:改mq分流? - _ = Task.Run(async () => - { - try - { - var cnt = await _forewarningNotifyService.SendNotificationAsync(subSystemId); - LogHelper.Info($"预警通知发送成功,新增预警记录{cnt}条"); - } - catch (Exception e) - { - LogHelper.Error(e.ToString()); - } - finally - { - await RedisHelper.Client.DelAsync(redisKey); - } - }); - } - - return true; - } } diff --git a/src/SJZY.Track.Application/Services/TrackPush/TrackPushCacheService.cs b/src/SJZY.Track.Application/Services/TrackPush/TrackPushCacheService.cs index 3fafab67..eb4c6388 100644 --- a/src/SJZY.Track.Application/Services/TrackPush/TrackPushCacheService.cs +++ b/src/SJZY.Track.Application/Services/TrackPush/TrackPushCacheService.cs @@ -40,7 +40,7 @@ public class TrackPushCacheService : ITrackPushCacheService, ITransientDependenc } private string TrackPushContentKey(int subSystemId, int customId) => - $"{GlobalConst.TrackPushKeyPrefix}:{subSystemId}:{customId}"; + $"{GlobalConst.TrackPushContentKeyPrefix}:{subSystemId}:{customId}"; private async Task?> DoGetPushContentConfigsAsync(int subSystemId, int customId) { diff --git a/src/SJZY.Track.Application/SjzyTrackApplicationModule.cs b/src/SJZY.Track.Application/SjzyTrackApplicationModule.cs index 0462b016..61dd5aad 100644 --- a/src/SJZY.Track.Application/SjzyTrackApplicationModule.cs +++ b/src/SJZY.Track.Application/SjzyTrackApplicationModule.cs @@ -8,7 +8,6 @@ using SJZY.Expand.ABP.Core.Localization; using SJZY.Expand.ABP.Core.Logger; using SJZY.Expand.ABP.Core.PublicMethords; using SJZY.InterfaceRelay.Application.Contracts; -using SJZY.Track.Application.BackgroundServices; using SJZY.Track.Application.CAP; using SJZY.Track.Application.Contract; using SJZY.Track.Application.Localization; @@ -153,8 +152,6 @@ public class SjzyTrackApplicationModule : AbpModule }, typeof(SjzyTrackApplicationModule).Assembly); context.Services.Configure(configuration.GetSection("TrackApp")); - context.Services.AddHostedService(); - context.Services.AddHostedService(); } public override void PreConfigureServices(ServiceConfigurationContext context) diff --git a/src/SJZY.Track.Domain/Aggregates/OrderAggregate/IOrderRepository.cs b/src/SJZY.Track.Domain/Aggregates/OrderAggregate/IOrderRepository.cs index 7cbae557..be928230 100644 --- a/src/SJZY.Track.Domain/Aggregates/OrderAggregate/IOrderRepository.cs +++ b/src/SJZY.Track.Domain/Aggregates/OrderAggregate/IOrderRepository.cs @@ -36,7 +36,7 @@ public interface IOrderRepository : IBasicRepository /// 更新推送预警单 /// /// - Task PushWarnOrdersAsync(int pageSize, int pageindex); + Task PushWarnOrdersAsync(int subSystemId, int pageSize, int pageindex); /// /// 更新缺失标记,时区缺失和发生地缺失标记,1表示时区,2表示发生地,3表示两种 diff --git a/src/SJZY.Track.Domain/Consts/GlobalConst.Redis.cs b/src/SJZY.Track.Domain/Consts/GlobalConst.Redis.cs index 6e2e6791..918762fe 100644 --- a/src/SJZY.Track.Domain/Consts/GlobalConst.Redis.cs +++ b/src/SJZY.Track.Domain/Consts/GlobalConst.Redis.cs @@ -32,6 +32,11 @@ public partial class GlobalConst /// public const string ForewarningNotifyCheckKey = "ForewarningNotifyCheck"; + /// + /// 订单拉取状态扫描罚时 redis key + /// + public const string OrderPullStatusScanCheckKey = "OrderPullStatusScanCheck"; + /// /// kpi区域redis key前缀 /// @@ -63,7 +68,7 @@ public partial class GlobalConst public const string TrackWarningKeyPrefix = "TrackWarning"; /// - /// 12小时推送预警轨迹锁 + /// 轨迹推送预警redis key前缀 /// - public const string TrackPushTrackWaringLockKey = "TrackPushTrackWaringLock"; + public const string TrackPushWaringKeyPrefix = "TrackPushWaring"; } diff --git a/src/SJZY.Track.Repository/Repositories/OrderRepository.cs b/src/SJZY.Track.Repository/Repositories/OrderRepository.cs index e99287a3..eb62e350 100644 --- a/src/SJZY.Track.Repository/Repositories/OrderRepository.cs +++ b/src/SJZY.Track.Repository/Repositories/OrderRepository.cs @@ -116,13 +116,14 @@ public class OrderRepository : BasicRepository, IOrderRepository, I /// /// 更新推送预警单 /// + /// /// /// /// - public async Task PushWarnOrdersAsync(int pageSize, int pageindex) + public async Task PushWarnOrdersAsync(int subSystemId, int pageSize, int pageindex) { var ordersToUpdate = await Context.Queryable() - .Where(b => !b.IsCompleted && b.QuestionType != 1) + .Where(b => b.SubSystemId == subSystemId && !b.IsCompleted && b.QuestionType != 1) .Select(b => new { b.LatestPushTime,