track/src/SJZY.Track.Application/Services/ForewarningServices/ForewarningNotifyService.cs
2024-07-29 11:55:36 +08:00

119 lines
3.8 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 FreeRedis;
using SJZY.Expand.ABP.Core;
using SJZY.Track.Application.Contract.Services;
using SJZY.Track.Domain.Aggregates.ForewarningAggregate;
using SJZY.Track.Domain.Consts;
using SJZY.Track.Domain.Dtos.Forewarning;
using SJZY.Track.Domain.Enums;
using Volo.Abp.DependencyInjection;
namespace SJZY.Track.Application.Services.ForewarningServices;
/// <summary>
/// 用于处理预警通知
/// </summary>
public class ForewarningNotifyService : IForewarningNotifyService, ITransientDependency
{
IReadOnlyDictionary<string, List<ForewarningRuleItem>> _ruleGroupDict;
IReadOnlyDictionary<string, List<RuleFactor>> _factorDict;
private IReadOnlyDictionary<int, ForewarningRuleItem> _rules;
readonly INotifyService _notifyService;
public ForewarningNotifyService(INotifyService notifyService)
{
_notifyService = notifyService;
}
/// <summary>
/// 创建预警通知
/// </summary>
/// <param name="order">创建预警通知需要的订单信息</param>
/// <returns>创建的通知总量,0表示没有规则</returns>
public async Task WarningCalculateAsync(List<WarningOrderDto> order)
{
throw new NotImplementedException();
}
public Task<int> DeleteForewarningNotify(WarningOrderDto order)
{
throw new NotImplementedException();
}
public void SendNotification(int intervalMinute)
{
var currentTimestamp = GetTimestamp(DateTime.Now);
var notifyMembers =
RedisHelper.Client.ZRangeByScore(GlobalConst.ForewarningNotifyKeyPrefix, 0, currentTimestamp);
if (notifyMembers == null || notifyMembers.Length == 0)
return;
var notifyValues = notifyMembers.Select(val => new RuleNotifyValue(val));
foreach (var notifyValue in notifyValues)
{
if (!_rules.TryGetValue(notifyValue.RuleId, out var rule))
continue;
//todo 发送通知
_notifyService.SendNotification("");
}
}
public void ClearCache()
{
_ruleGroupDict = null;
_factorDict = null;
_rules = null;
}
private List<string> DeleteNotifyValue(ForewarningRuleItem ruleItem, int orderId)
{
var members = new List<string>();
for (var index = 0; index < ruleItem.Count; index++)
{
var notifyValue = NotifyValue(ruleItem, orderId, index);
members.Add(notifyValue);
}
return members;
}
private async ValueTask<int> ExecuteRule(List<ForewarningRuleItem> rules, int orderId)
{
var members = new List<ZMember>();
foreach (var rule in rules)
{
var notifys = ExecuteRule(rule, orderId);
members.AddRange(notifys);
}
await RedisHelper.Client.ZAddAsync(GlobalConst.ForewarningNotifyKeyPrefix, members.ToArray());
return members.Count;
}
private List<ZMember> ExecuteRule(ForewarningRuleItem ruleItem, int orderId)
{
var members = new List<ZMember>();
var intervalMinute = 60 * ((ruleItem.Timeliness - ruleItem.Remind) / ruleItem.Count); // 间隔多少分钟
for (var index = 0; index < ruleItem.Count; index++)
{
var notifyTime = (ruleItem.Remind * 60) + intervalMinute * index;
var notifyTimestamp = GetTimestamp(DateTime.Now.AddMinutes(notifyTime));
var notifyValue = new ZMember(NotifyValue(ruleItem, orderId, index), notifyTimestamp);
members.Add(notifyValue);
}
return members;
}
private long GetTimestamp(DateTime dateTime)
{
return new DateTimeOffset(dateTime).ToUnixTimeSeconds();
}
private string NotifyValue(ForewarningRuleItem ruleItem, int orderId, int sort)
{
var result = new RuleNotifyValue(ruleItem.Id, orderId, sort, ruleItem.TrackEnd);
return result.ToString();
}
}