using Microsoft.AspNetCore.Authorization; using Microsoft.Extensions.Configuration; using SJZY.Expand.ABP.Core; using SJZY.Expand.ABP.Core.Logger; using SJZY.Track.Application.Contract.Services; using SJZY.Track.Domain.Aggregates.ForewarningAggregate; using SJZY.Track.Domain.Aggregates.OrderAggregate; using SJZY.Track.Domain.Dtos.Forewarning; namespace SJZY.Track.Application.Services.TrackOrders; public class OrderWarningService { private readonly IForewarningNotifyService _forewarningNotifyService; private readonly IOrderRepository _orderRepository; private readonly IWarningRecordEsRepository _warningRecordEsRepository; public OrderWarningService(IWarningRecordEsRepository warningRecordEsRepository, IOrderRepository orderRepository, IForewarningNotifyService forewarningNotifyService) { _warningRecordEsRepository = warningRecordEsRepository; _orderRepository = orderRepository; _forewarningNotifyService = forewarningNotifyService; } /// /// 获取订单运输时效预警分页列表 /// /// /// [HttpPost("GetOrderWarningPageList")] public async Task> GetOrderWarningPageListAsync( GetOrderWarningPageListVo inputVo) { var result = await _warningRecordEsRepository.GetOrderWarningPageListAsync(inputVo); var orderIds = result.list.Select(x => x.OrderId).ToList(); var orders = await _orderRepository.GetListAsync(x => orderIds.Contains(x.Id), x => new { x.Id, x.LatestTrackContent, x.LatestTrackTime, x.LatestTrackPlace, x.OrderNo }); var list = new List(); result.list.ForEach(x => { var item = new GetOrderWarningPageOutput { Rule = x.Rule, Counter = x.Counter, WarningName = x.WarningName, WarningStatus = x.WarningStatus, CreateTime = x.CreateTime, NotifyUser = x.NotifyUser, CustomName = x.CustomName, }; var order = orders.FirstOrDefault(o => o.Id == x.OrderId); if (order == default) return; item.OrderNo = order.OrderNo; item.LatestTrackTime = order.LatestTrackTime; item.LatestTrackPlace = order.LatestTrackPlace; item.LatestTrackContent = order.LatestTrackContent; }); return new UnifyPageResultDto(list, inputVo.PageIndex, inputVo.PageSize, (int) result.total); } /// /// 定时任务轨迹运输时效预警推送 /// /// [AllowAnonymous] [HttpPost("Track/SendWarningForTask")] public async Task SendWarningForTaskAsync([FromBody] IConfiguration configuration) { var configs = configuration.GetSection("SubSystemWarning").Get>(); if (configs == null || configs.Keys.Count == 0) { throw new BusinessException("未配置预警系统"); } var subSystemIds = await _orderRepository.GetSubSystemListByCodesAsync(configs.Keys.ToList()); foreach (var item in subSystemIds) { // 如果已经发送过通知,不再发送 var redisKey = $"{GlobalConst.ForewarningNotifyCheckKey}:{subSystemIds}"; if (await RedisHelper.Client.ExistsAsync(redisKey)) { continue; } await RedisHelper.Client.SetExAsync(redisKey, 30 * 60, 1); // todo:改mq分流? _ = Task.Run(async () => { try { var cnt = await _forewarningNotifyService.SendNotificationAsync(item.id); LogHelper.Info($"预警通知发送成功,新增预警记录{cnt}条"); } catch (Exception e) { LogHelper.Error(e.ToString()); } finally { await RedisHelper.Client.DelAsync(redisKey); } }); } return true; } }