81 lines
2.6 KiB
C#
81 lines
2.6 KiB
C#
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;
|
||
|
||
/// <summary>
|
||
/// 轨迹运输时效预警定时任务
|
||
/// </summary>
|
||
[ApiExplorerSettings(GroupName = "定时任务")]
|
||
public class TrackWarningTask : ApplicationService
|
||
{
|
||
private readonly IOptionsSnapshot<Track2InterfaceRelayOptions> _options;
|
||
private readonly IOrderRepository _orderRepository;
|
||
|
||
public TrackWarningTask(IOrderRepository orderRepository, IOptionsSnapshot<Track2InterfaceRelayOptions> options)
|
||
{
|
||
_orderRepository = orderRepository;
|
||
_options = options;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 轨迹运输时效预警通知
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[AllowAnonymous]
|
||
[HttpPost("/TimerTask/SendWarning")]
|
||
public async Task<bool> 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 ttl = configs[item.code].WarningNotifyInterval * 60;
|
||
await TrackWarningBySubSystemAsync(item.id, ttl);
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
private async Task TrackWarningBySubSystemAsync(int subSystemId, int ttl)
|
||
{
|
||
// 如果已经发送过通知,不再发送
|
||
var redisKey = $"{GlobalConst.ForewarningNotifyCheckKey}:{subSystemId}";
|
||
if (await RedisHelper.Client.ExistsAsync(redisKey))
|
||
{
|
||
return;
|
||
}
|
||
|
||
await RedisHelper.Client.SetExAsync(redisKey, ttl, 1);
|
||
|
||
// todo:改mq分流?
|
||
_ = Task.Run(async () =>
|
||
{
|
||
try
|
||
{
|
||
using var serviceScope = LazyServiceProvider.CreateScope();
|
||
|
||
var forewarningNotifyService =
|
||
serviceScope.ServiceProvider.GetRequiredService<IForewarningNotifyService>();
|
||
var cnt = await forewarningNotifyService.SendNotificationAsync(subSystemId);
|
||
LogHelper.Info($"预警通知发送成功,新增预警记录{cnt}条");
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
await RedisHelper.Client.DelAsync(redisKey);
|
||
LogHelper.Error(e.ToString());
|
||
}
|
||
});
|
||
}
|
||
}
|