track/src/SJZY.Track.Application/Services/TrackOrders/OrderWarningService.cs

122 lines
4.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
}
/// <summary>
/// 获取订单运输时效预警分页列表
/// </summary>
/// <param name="inputVo"></param>
/// <returns></returns>
[HttpPost("GetOrderWarningPageList")]
public async Task<UnifyPageResultDto<GetOrderWarningPageOutput>> 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<GetOrderWarningPageOutput>();
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<GetOrderWarningPageOutput>(list, inputVo.PageIndex, inputVo.PageSize,
(int) result.total);
}
/// <summary>
/// 定时任务轨迹运输时效预警推送
/// </summary>
/// <returns></returns>
[AllowAnonymous]
[HttpPost("Track/SendWarningForTask")]
public async Task<bool> SendWarningForTaskAsync([FromBody] IConfiguration configuration)
{
var configs = configuration.GetSection("SubSystemWarning").Get<Dictionary<string, int>>();
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;
}
}