track/src/SJZY.Track.Application/Services/ForewarningServices/ForewarningService.cs
2024-10-06 11:09:15 +08:00

222 lines
7.9 KiB
C#

using System.ComponentModel.DataAnnotations;
using SJZY.Expand.ABP.Core.PublicMethords;
using SJZY.Track.Application.Contract.Dtos.Forewarning;
using SJZY.Track.Application.Contract.Services;
using SJZY.Track.Application.Helpers;
using SJZY.Track.Domain.Aggregates.ForewarningAggregate;
using SJZY.Track.Domain.Aggregates.KPIAreaAggregate;
using SJZY.Track.Domain.Aggregates.TrackStatusAggregate;
using SJZY.Track.Domain.Dtos.Common;
using SJZY.Track.Domain.Dtos.Forewarning;
using SJZY.Track.Domain.Queries;
namespace SJZY.Track.Application.Services.ForewarningServices;
[ApiExplorerSettings(GroupName = "预警服务")]
[Route("/ForewarningService")]
public class ForewarningService : ApplicationService
{
private readonly IForewarningCacheService _forewarningCacheService;
private readonly IWarningRepository _warningRepository;
public ForewarningService(IWarningRepository warningRepository, IForewarningCacheService forewarningCacheService)
{
_warningRepository = warningRepository;
_forewarningCacheService = forewarningCacheService;
}
/// <summary>
/// 获取预警分页列表
/// </summary>
/// <param name="inputVo"></param>
/// <returns></returns>
[HttpPost("GetWarningPageList")]
public async Task<UnifyPageResultDto<WarningPageOutput>> GetWarningPageList(GetWarningPageInputVo inputVo)
{
if (!inputVo.KeyWord.IsNullOrEmpty()) inputVo.KeyWord = inputVo.KeyWord!.Trim();
return await _warningRepository.GetWarningPageListAsync(inputVo);
}
/// <summary>
/// 创建预警
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("CreateWarning")]
public async Task<bool> CreateWarning(CreateWarningInput input)
{
var warning = ObjectMapper.Map<CreateWarningInput, WarningEntity>(input);
if (await _warningRepository.IsAnyAsync(x =>
x.SubSystemId == warning.SubSystemId && x.CustomerId == warning.CustomerId && x.Name == warning.Name))
{
throw new BusinessException(message: "该子系统下的客户已经存在一个同名预警: " + warning.Name);
}
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;
}
/// <summary>
/// 编辑预警
/// </summary>
/// <param name="warningId"></param>
/// <param name="statusQuery"></param>
/// <param name="kpiAreaRepository"></param>
/// <returns></returns>
[HttpGet("EditWarning")]
public async Task<EditWarningOutput> EditWarning(int warningId,
[FromServices] IBasicQuery<TrackStatusEntity> statusQuery,
[FromServices] IKpiAreaRepository kpiAreaRepository)
{
var warning = await _warningRepository.GetByIdAsync(warningId);
if (warning == null)
{
throw new BusinessException(message: "预警不存在");
}
var statusList = await statusQuery.GetListAsync(x => x.SubSystemId == warning.SubSystemId, x =>
new IdWithNameDto
{
Id = x.Id,
Name = x.Status
});
var areaList = await kpiAreaRepository.GetListAsync(x => x.SubSystemId == warning.SubSystemId, x =>
new IdWithNameDto
{
Id = x.Id,
Name = x.Name
});
return ForewarningHelper.MapWarningSummary(warning, statusList, areaList);
}
/// <summary>
/// 更新预警
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("UpdateWarning")]
public async Task<bool> UpdateWarning(UpdateWarningInput input)
{
var warning = await _warningRepository.GetFirstAsync(x => x.Id == input.Id);
if (warning == null)
{
throw new BusinessException(message: "预警不存在");
}
if (await _warningRepository.IsAnyAsync(x =>
x.SubSystemId == warning.SubSystemId && x.CustomerId == warning.CustomerId && x.Id != warning.Id &&
x.Name == input.Name))
{
throw new BusinessException(message: "该子系统下的客户已经存在一个同名预警: " + warning.Name);
}
warning.SetName(input.Name);
warning.WarningNotifies = input.Notifies.Select(x => new WarningNotifyEntity
{
Id = x.Id,
NotifyMode = x.NotifyModes.JoinAsString(","),
NotifyUser = x.NotifyUsers.ToJson(),
}).ToList();
await _warningRepository.UpdateForewarningAsync(warning);
return true;
}
/// <summary>
/// 启用or禁用预警
/// </summary>
/// <param name="warningId">预警id</param>
/// <param name="isEnable">是否启用</param>
/// <returns></returns>
[HttpPost("TurnWarning")]
public async Task<bool> TurnWarning(int warningId, bool isEnable)
{
var warning = await _warningRepository.GetFirstAsync(x => x.Id == warningId);
if (warning == null)
{
throw new BusinessException(message: "预警不存在");
}
if (isEnable)
{
if (await _warningRepository.IsAnyAsync(x =>
x.SubSystemId == warning.SubSystemId && x.CustomerId == warning.CustomerId && x.IsEnable))
{
throw new BusinessException(message: "该子系统下的客户已经存在一个启用的预警,请先禁用");
}
}
warning.SetEnable(isEnable);
await _warningRepository.TurnWarningAsync(warning);
// 清除缓存
await _forewarningCacheService.ClearRedisBySystemIdAndCustomer(warning.SubSystemId, warning.CustomerId);
return true;
}
/// <summary>
/// 查询预警规则列表
/// </summary>
/// <param name="warningId">预警规则id</param>
/// <param name="factorQuery"></param>
/// <returns></returns>
[HttpGet("GetWarningRuleList")]
public async Task<List<dynamic>> GetWarningRuleListAsync([Required] int warningId,
[FromServices] IBasicQuery<WarningFactorEntity> factorQuery)
{
var factors = await factorQuery.GetListAsync(x => x.WarningId == warningId, x => x.Type);
var rules = await _warningRepository.GetWarningRuleListAsync(warningId);
var dynamicList = ForewarningHelper.DynamicCalcRuleList(rules, factors);
return dynamicList;
}
/// <summary>
/// 修改预警规则时效
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
/// <exception cref="BusinessException"></exception>
[HttpPost("ModifyTimeSpans")]
public async Task<bool> ModifyTimeSpans(ModifyTimeSpanInput input)
{
if (input.Items.IsNullOrEmpty()) return false;
if (input.Items.Any(x => x.TotalTimeSpan < x.WarnTimeSpan))
{
throw new BusinessException(message: "预警时效不能小于预警提醒时效");
}
if (input.Items.Any(x => x.WarnCount is < 1 or > 10))
{
throw new BusinessException(message: "预警次数范围为1-10");
}
var warning = await _warningRepository.GetFirstAsync(x => x.Id == input.WarningId);
if (warning == null)
{
throw new BusinessException(message: "预警不存在");
}
var warningRules = ObjectMapper.Map<List<WarningTimeSpanDto>, List<WarningRuleEntity>>(input.Items);
await _warningRepository.ModifyTimeSpansAsync(warningRules);
// 清除缓存
await _forewarningCacheService.ClearRedisBySystemIdAndCustomer(warning.SubSystemId, warning.CustomerId);
return true;
}
}