Revert "temp:remove notify table"

This reverts commit 9de31445a9.
This commit is contained in:
xiaolipro 2024-08-05 09:57:15 +08:00
parent 9de31445a9
commit 2f2805268e
16 changed files with 200 additions and 142 deletions

View File

@ -20,10 +20,20 @@ public class CreateWarningInput
/// </summary>
public int CustomerId { get; set; }
/// <summary>
/// 通知方式
/// </summary>
public NotifyMode NotifyMode { get; set; }
/// <summary>
/// 通知模板
/// </summary>
public string NotifyTemplate { get; set; } = null!;
/// <summary>
/// 轨迹状态跨度列表
/// </summary>
public List<WarningNotifyDto> TrackStatusSpans { get; set; } = new();
public List<WarningNitifyDto> TrackStatusSpans { get; set; } = new();
/// <summary>
/// 预警因素列表。

View File

@ -1,4 +1,5 @@
using SJZY.Track.Domain.Dtos.Forewarning;
using SJZY.Track.Domain.Enums;
namespace SJZY.Track.Application.Contract.Dtos.Forewarning;
@ -19,10 +20,20 @@ public class EditWarningOutput
/// </summary>
public int CustomerId { get; set; }
/// <summary>
/// 通知方式
/// </summary>
public NotifyMode NotifyMode { get; set; }
/// <summary>
/// 通知模板
/// </summary>
public string NotifyTemplate { get; set; } = null!;
/// <summary>
/// 通知列表
/// </summary>
public List<WarningNotifyDto> TrackStatusSpans { get; set; } = new();
public List<WarningNotifyWithIdDto> TrackStatusSpans { get; set; } = new();
/// <summary>
/// 预警因素列表。
@ -30,6 +41,13 @@ public class EditWarningOutput
public List<WarningFactorWithIdDto> WarningFactors { get; set; } = new();
}
public class WarningNotifyWithIdDto: WarningNitifyDto
{
/// <summary>
/// 通知id
/// </summary>
public int Id { get; set; }
}
public class WarningFactorWithIdDto : WarningFactorDto
{

View File

@ -14,9 +14,32 @@ public class UpdateWarningInput
/// 模板名称
/// </summary>
public string Name { get; set; } = null!;
/// <summary>
/// 通知方式
/// </summary>
public NotifyMode NotifyMode { get; set; }
/// <summary>
/// 通知模板
/// </summary>
public string NotifyTemplate { get; set; } = null!;
/// <summary>
/// 通知列表
/// </summary>
public List<WarningNotifyDto> Notifies { get; set; } = new();
public List<NotifyDto> Notifies { get; set; } = new();
}
public class NotifyDto
{
/// <summary>
/// 通知id
/// </summary>
public int Id { get; set; }
/// <summary>
/// 通知人id
/// </summary>
public int NotifyUserId { get; set; }
}

View File

@ -15,12 +15,17 @@ public class ForewarningHelper
/// <summary>
/// 递推分裂预警规则列表
/// </summary>
/// <param name="input"></param>
/// <param name="statusSpans"></param>
/// <param name="factors"></param>
/// <returns></returns>
public static List<WarningRuleEntity> CalcRuleList(CreateWarningInput input)
public static List<WarningRuleEntity> CalcRuleList(List<WarningNitifyDto> statusSpans,
List<WarningFactorDto> factors)
{
var statusSpans = input.TrackStatusSpans;
var factors = input.WarningFactors;
if (statusSpans.Count == 0 || factors.Count == 0)
{
throw new BusinessException(message: "轨迹状态区间或因素列表为空");
}
var len = statusSpans.Count * factors.Select(f => f.FactorValue.Count).Aggregate((pre, cur) => pre * cur);
if (len == 0)
{
@ -47,7 +52,7 @@ public class ForewarningHelper
return rules;
}
private static void FillStatus(List<WarningNotifyDto> statusSpans, List<WarningRuleEntity> rules, int repeat)
private static void FillStatus(List<WarningNitifyDto> statusSpans, List<WarningRuleEntity> rules, int repeat)
{
var row = 0;
foreach (var span in statusSpans)
@ -58,8 +63,6 @@ public class ForewarningHelper
rules[row].StartTrackStatus = span.StartTrackStatus;
rules[row].EndTrackStatusId = span.EndTrackStatusId;
rules[row].EndTrackStatus = span.EndTrackStatus;
rules[row].NotifyMode = span.NotifyMode;
rules[row].NotifyUserId = span.NotifyUserId;
}
}
}
@ -149,24 +152,37 @@ public class ForewarningHelper
}).ToList();
}
public static EditWarningOutput MapWarningSummary(WarningEntity warning)
public static List<WarningNotifyEntity> MapNotifyEntities(CreateWarningInput input)
{
return input.TrackStatusSpans.Select(x => new WarningNotifyEntity
{
StartTrackStatusId = x.StartTrackStatusId,
EndTrackStatusId = x.EndTrackStatusId,
NotifyMode = input.NotifyMode,
NotifyTemplate = input.NotifyTemplate,
NotifyUserId = x.NotifyUserId
}).ToList();
}
public static EditWarningOutput MapWarningSummary(WarningEntity warning, List<IdWithNameDto> statusList)
{
var statusDic = statusList.ToDictionary(x => x.Id, x => x.Name);
return new EditWarningOutput
{
Name = warning.Name,
SubSystemId = warning.SubSystemId,
CustomerId = warning.CustomerId,
TrackStatusSpans = warning.WarningRules.GroupBy(x => new
{ x.StartTrackStatusId, x.StartTrackStatus, x.EndTrackStatusId, x.EndTrackStatus })
.Select(x => new WarningNotifyDto
{
StartTrackStatusId = x.Key.StartTrackStatusId,
StartTrackStatus = x.Key.StartTrackStatus,
EndTrackStatusId = x.Key.EndTrackStatusId,
EndTrackStatus = x.Key.EndTrackStatus,
NotifyMode = x.First().NotifyMode,
NotifyUserId = x.First().NotifyUserId,
}).ToList(),
NotifyMode = warning.WarningNotifies.First().NotifyMode,
NotifyTemplate = warning.WarningNotifies.First().NotifyTemplate,
TrackStatusSpans = warning.WarningNotifies.Select(x => new WarningNotifyWithIdDto
{
Id = x.Id,
StartTrackStatusId = x.StartTrackStatusId,
StartTrackStatus = statusDic.TryGetValue(x.StartTrackStatusId, out var start) ? start : "",
EndTrackStatusId = x.EndTrackStatusId,
EndTrackStatus = statusDic.TryGetValue(x.EndTrackStatusId, out var end) ? end : "",
NotifyUserId = x.NotifyUserId,
}).ToList(),
WarningFactors = warning.WarningFactors.Select(x => new WarningFactorWithIdDto
{
Id = x.Id,

View File

@ -6,6 +6,7 @@ using SJZY.Track.Application.Helpers;
using SJZY.Track.Domain.Aggregates.ForewarningAggregate;
using SJZY.Track.Domain.Aggregates.TrackStatusAggregate;
using SJZY.Track.Domain.Dtos.Forewarning;
using SJZY.Track.Domain.Dtos.TrackStatus;
using SJZY.Track.Domain.Queries;
namespace SJZY.Track.Application.Services.ForewarningServices;
@ -16,14 +17,11 @@ public class ForewarningService : ApplicationService
{
private readonly IWarningRepository _warningRepository;
private readonly IForewarningCacheService _forewarningCacheService;
private readonly IBasicQuery<TrackStatusEntity> _trackStatusQueries;
public ForewarningService(IWarningRepository warningRepository, IForewarningCacheService forewarningCacheService,
IBasicQuery<TrackStatusEntity> trackStatusQueries)
public ForewarningService(IWarningRepository warningRepository, IForewarningCacheService forewarningCacheService)
{
_warningRepository = warningRepository;
_forewarningCacheService = forewarningCacheService;
_trackStatusQueries = trackStatusQueries;
}
/// <summary>
@ -46,11 +44,6 @@ public class ForewarningService : ApplicationService
[HttpPost("CreateWarning")]
public async Task<bool> CreateWarning(CreateWarningInput input)
{
if (input.TrackStatusSpans.Count == 0 || input.WarningFactors.Count == 0)
{
throw new BusinessException(message: "轨迹状态区间或因素列表为空");
}
var warning = ObjectMapper.Map<CreateWarningInput, WarningEntity>(input);
if (await _warningRepository.IsAnyAsync(x =>
@ -59,9 +52,11 @@ public class ForewarningService : ApplicationService
throw new BusinessException(message: "该子系统下的客户已经存在一个同名预警: " + warning.Name);
}
warning.WarningRules = ForewarningHelper.CalcRuleList(input);
warning.WarningRules = ForewarningHelper.CalcRuleList(input.TrackStatusSpans, input.WarningFactors);
warning.FactorTypeSort = input.WarningFactors.Select(x => x.FactorType).JoinAsString(",");
warning.WarningFactors = ForewarningHelper.MapFactorEntities(input.WarningFactors);
warning.WarningNotifies = ForewarningHelper.MapNotifyEntities(input);
await _warningRepository.CreateForewarningAsync(warning);
return true;
}
@ -70,18 +65,27 @@ public class ForewarningService : ApplicationService
/// 编辑预警
/// </summary>
/// <param name="warningId"></param>
/// <param name="statusQuery"></param>
/// <returns></returns>
[HttpPost("EditWarning")]
public async Task<EditWarningOutput> EditWarning(int warningId)
public async Task<EditWarningOutput> EditWarning(int warningId,
[FromServices] IBasicQuery<TrackStatusEntity> statusQuery)
{
var warning = await _warningRepository.GetWithRulesAsync(warningId);
var warning = await _warningRepository.GetByIdAsync(warningId);
if (warning == null)
{
throw new BusinessException(message: "预警不存在");
}
return ForewarningHelper.MapWarningSummary(warning);
var statusList = await statusQuery.GetListAsync(x => x.SubSystemId == warning.SubSystemId, x =>
new IdWithNameDto
{
Id = x.Id,
Name = x.Status
});
return ForewarningHelper.MapWarningSummary(warning, statusList);
}
/// <summary>
@ -107,61 +111,19 @@ public class ForewarningService : ApplicationService
warning.SetName(input.Name);
await VerifyAndFillStatusAsync(input.Notifies, warning.SubSystemId);
warning.WarningRules = input.Notifies.Select(x => new WarningRuleEntity()
warning.WarningNotifies = input.Notifies.Select(x => new WarningNotifyEntity
{
WarningId = warning.Id,
StartTrackStatusId = x.StartTrackStatusId,
EndTrackStatusId = x.EndTrackStatusId,
NotifyMode = x.NotifyMode,
Id = x.Id,
NotifyMode = input.NotifyMode,
NotifyTemplate = input.NotifyTemplate,
NotifyUserId = x.NotifyUserId
}).ToList();
await _warningRepository.UpdateForewarningAsync(warning);
return true;
}
private async Task VerifyAndFillStatusAsync(List<WarningNotifyDto> notifies, int subSystemId)
{
var statusIds = new List<int>();
notifies.ForEach(x =>
{
if (x.StartTrackStatusId == x.EndTrackStatusId)
{
throw new BusinessException(message: "起止轨迹状态不能相同");
}
statusIds.Add(x.StartTrackStatusId);
statusIds.Add(x.EndTrackStatusId);
});
var statusList = await _trackStatusQueries.GetListAsync(
x => x.SubSystemId == subSystemId && statusIds.Contains(x.Id), x =>
new IdWithNameDto
{
Id = x.Id,
Name = x.Status
});
var statusDic = statusList.ToDictionary(x => x.Id, x => x.Name);
notifies.ForEach(x =>
{
if (!statusDic.ContainsKey(x.StartTrackStatusId))
{
throw new BusinessException(message: "存在非法轨迹状态(包含子系统下不存在的轨迹状态)");
}
if (!statusDic.ContainsKey(x.EndTrackStatusId))
{
throw new BusinessException(message: "存在非法轨迹状态(包含子系统下不存在的轨迹状态)");
}
x.StartTrackStatus = statusDic[x.StartTrackStatusId];
x.EndTrackStatus = statusDic[x.EndTrackStatusId];
});
}
/// <summary>
/// 启用or禁用预警
/// </summary>
@ -189,7 +151,7 @@ public class ForewarningService : ApplicationService
warning.SetEnable(isEnable);
await _warningRepository.TurnWarningAsync(warning);
// 清除缓存
await _forewarningCacheService.DeleteRulesAsync(warning.SubSystemId, warning.CustomerId);
@ -233,7 +195,7 @@ public class ForewarningService : ApplicationService
var warningRules = ObjectMapper.Map<List<WarningTimeSpanDto>, List<WarningRuleEntity>>(input.Items);
await _warningRepository.ModifyTimeSpansAsync(warningRules);
// 清除缓存
await _forewarningCacheService.DeleteRulesAsync(warning.SubSystemId, warning.CustomerId);
return true;

View File

@ -10,6 +10,9 @@ public class CreateRuleFactorValidation : AbstractValidator<CreateWarningInput>
{
RuleFor(x => x.WarningFactors).NotNull().WithMessage("因素列表不能为空");
RuleFor(x => x.TrackStatusSpans).NotNull().WithMessage("轨迹状态列表不能为空");
RuleFor(x => x.NotifyTemplate).NotEmpty().WithMessage("通知模板必填")
.MaximumLength(256).WithMessage("通知模板长度不能超过256");
// todo: more validation
}

View File

@ -3,7 +3,7 @@ using SJZY.Track.Domain.Dtos.Forewarning;
namespace SJZY.Track.Application.Validations.Forewarning;
public class RuleTrackStateDtoValidator : AbstractValidator<WarningNotifyDto>
public class RuleTrackStateDtoValidator : AbstractValidator<WarningNitifyDto>
{
public RuleTrackStateDtoValidator()
{

View File

@ -54,9 +54,9 @@ public interface IWarningRepository : IBasicRepository<WarningEntity>
Task<bool> TurnWarningAsync(WarningEntity warning);
/// <summary>
/// 获取预警 附带规则
/// 获取预警,include factors, notifies
/// </summary>
/// <param name="warningId"></param>
/// <returns></returns>
Task<WarningEntity?> GetWithRulesAsync(int warningId);
Task<WarningEntity?> GetByIdAsync(int warningId);
}

View File

@ -53,6 +53,12 @@ public class WarningEntity: IFullAuditedBasicEntitys, IAggregateRoot
/// </summary>
[SugarColumn(IsIgnore = true)]
public List<WarningFactorEntity> WarningFactors { get; set; } = new();
/// <summary>
/// 预警通知
/// </summary>
[SugarColumn(IsIgnore = true)]
public List<WarningNotifyEntity> WarningNotifies { get; set; } = new();
public void SetName(string name)
{

View File

@ -0,0 +1,49 @@
using SJZY.Track.Domain.Enums;
namespace SJZY.Track.Domain.Aggregates.ForewarningAggregate;
[SugarTable("t_warning_notify")]
public class WarningNotifyEntity : IFullAuditedBasicEntitys, IEntity
{
/// <summary>
/// 自增主键
/// </summary>
[SugarColumn(ColumnName = "Id", IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; }
/// <summary>
/// 预警id
/// </summary>
[SugarColumn(ColumnName = "WarningId")]
public int WarningId { get; set; }
/// <summary>
/// 起始轨迹状态id
/// </summary>
[SugarColumn(ColumnName = "StartTrackStatusId", IsNullable = false)]
public int StartTrackStatusId { get; set; }
/// <summary>
/// 终止轨迹状态id
/// </summary>
[SugarColumn(ColumnName = "EndTrackStatusId", IsNullable = false)]
public int EndTrackStatusId { get; set; }
/// <summary>
/// 通知方式
/// </summary>
[SugarColumn(ColumnName = "NotifyMode")]
public NotifyMode NotifyMode { get; set; }
/// <summary>
/// 通知模板
/// </summary>
[SugarColumn(ColumnName = "NotifyTemplate", Length = 256)]
public string NotifyTemplate { get; set; } = null!;
/// <summary>
/// 通知人
/// </summary>
[SugarColumn(ColumnName = "NotifyUserId")]
public int NotifyUserId { get; set; }
}

View File

@ -1,5 +1,3 @@
using SJZY.Track.Domain.Enums;
namespace SJZY.Track.Domain.Aggregates.ForewarningAggregate;
[SugarTable("t_warning_rule")]
@ -94,16 +92,4 @@ public class WarningRuleEntity : IFullAuditedBasicEntitys, IEntity
/// </summary>
[SugarColumn(ColumnName = "ProductCode")]
public string? ProductCode { get; set; }
/// <summary>
/// 通知方式
/// </summary>
[SugarColumn(ColumnName = "NotifyMode")]
public NotifyMode NotifyMode { get; set; }
/// <summary>
/// 通知人
/// </summary>
[SugarColumn(ColumnName = "NotifyUserId")]
public int NotifyUserId { get; set; }
}

View File

@ -11,12 +11,6 @@ public class TrackStatusEntity : IFullAuditedBasicEntitys, IAggregateRoot
/// </summary>
[SugarColumn(ColumnName = "Id", IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; }
/// <summary>
/// 子系统id
/// </summary>
[SugarColumn(ColumnName = "SubSystemId")]
public int SubSystemId { get; set; }
/// <summary>
/// 轨迹状态代码
@ -41,4 +35,10 @@ public class TrackStatusEntity : IFullAuditedBasicEntitys, IAggregateRoot
/// </summary>
[SugarColumn(ColumnName = "Status")]
public string Status { get; set; } = null!;
/// <summary>
/// 子系统id
/// </summary>
[SugarColumn(ColumnName = "SubSystemId")]
public int SubSystemId { get; set; }
}

View File

@ -1,8 +1,6 @@
using SJZY.Track.Domain.Enums;
namespace SJZY.Track.Domain.Dtos.Forewarning;
public class WarningNotifyDto
public class WarningNitifyDto
{
/// <summary>
/// 起始轨迹状态id
@ -23,11 +21,6 @@ public class WarningNotifyDto
/// 终止轨迹状态
/// </summary>
public string EndTrackStatus { get; set; } = null!;
/// <summary>
/// 通知方式
/// </summary>
public NotifyMode NotifyMode { get; set; }
/// <summary>
/// 通知人id

View File

@ -1,15 +1,14 @@
namespace SJZY.Track.Domain.Enums;
[Flags]
public enum NotifyMode
{
/// <summary>
/// 站内信
/// </summary>
SiteInternal = 0x1,
SiteInternal = 1,
/// <summary>
/// 飞书
/// </summary>
FeiShu = 0x10,
FeiShu = 2
}

View File

@ -20,9 +20,12 @@ public class WarningQueries : ISugarRepository, IWarningQueries,
return await Context.Queryable<WarningEntity>()
.LeftJoin<WarningRuleEntity>((w, wr) => w.Id == wr.WarningId)
.LeftJoin<RuleAreaEntity>((w, wr, a) => wr.AreaId == a.Id)
.LeftJoin<WarningNotifyEntity>((w, wr, a, wn) =>
wr.WarningId == wn.WarningId && wr.StartTrackStatusId == wn.StartTrackStatusId &&
wr.EndTrackStatusId == wn.EndTrackStatusId)
.Filter(null, true) // 支持历史规则
.Where((w, wr) => ruleIds.Contains(wr.Id))
.Select<GetWarningNotifiesVo>((w, wr, a) => new GetWarningNotifiesVo
.Select<GetWarningNotifiesVo>((w, wr, a, wn) => new GetWarningNotifiesVo
{
RuleId = wr.Id,
WarningName = w.Name,
@ -37,8 +40,9 @@ public class WarningQueries : ISugarRepository, IWarningQueries,
IsTransit = wr.IsTransit,
LeavePort = wr.LeavePort,
ProductCode = wr.ProductCode,
NotifyMode = wr.NotifyMode,
NotifyUserId = wr.NotifyUserId,
NotifyMode = wn.NotifyMode,
NotifyTemplate = wn.NotifyTemplate,
NotifyUserId = wn.NotifyUserId,
FactorTypeSort = w.FactorTypeSort
})
.ToListAsync();

View File

@ -14,12 +14,13 @@ public class WarningRepository : BasicRepository<WarningEntity>, IWarningReposit
using var tran = TransactionHelper.GetReadCommitted();
var ruleId = await InsertReturnIdentityAsync(warning);
warning.WarningNotifies.ForEach(x => x.WarningId = ruleId);
warning.WarningFactors.ForEach(x => x.WarningId = ruleId);
await Context.Insertable(warning.WarningFactors).ExecuteCommandAsync();
warning.WarningRules.ForEach(x => x.WarningId = ruleId);
await Context.Insertable(warning.WarningFactors).ExecuteCommandAsync();
await Context.Insertable(warning.WarningNotifies).ExecuteCommandAsync();
await Context.Insertable(warning.WarningRules).ExecuteCommandAsync();
tran.Complete();
return ruleId;
}
@ -72,19 +73,8 @@ public class WarningRepository : BasicRepository<WarningEntity>, IWarningReposit
using var tran = TransactionHelper.GetReadCommitted();
await Context.Updateable(warning).ExecuteCommandAsync();
await Context.Updateable(warning.WarningRules).WhereColumns(x => new
{
x.WarningId,
x.StartTrackStatusId,
x.EndTrackStatusId
}).UpdateColumns(x => new
{
x.NotifyMode,
x.NotifyUserId,
x.UpdateTime,
x.UpdateUserId,
}, true)
await Context.Updateable(warning.WarningNotifies)
.UpdateColumns(x => new { x.NotifyMode, x.NotifyTemplate, x.NotifyUserId, x.UpdateTime, x.UpdateUserId })
.ExecuteCommandAsync();
tran.Complete();
@ -104,17 +94,16 @@ public class WarningRepository : BasicRepository<WarningEntity>, IWarningReposit
.ExecuteCommandAsync() > 0;
}
public async Task<WarningEntity?> GetWithRulesAsync(int warningId)
public async Task<WarningEntity?> GetByIdAsync(int warningId)
{
var warning = await GetFirstAsync(x => x.Id == warningId);
if (warning == null) return null;
warning.WarningFactors = await Context.Queryable<WarningFactorEntity>()
.Where(x => x.WarningId == warningId)
.ToListAsync();
warning.WarningRules = await Context.Queryable<WarningRuleEntity>()
warning.WarningNotifies = await Context.Queryable<WarningNotifyEntity>()
.Where(x => x.WarningId == warningId)
.ToListAsync();