using System.ComponentModel.DataAnnotations; using SJZY.Track.Application.Contract.Dtos.Forewarning; using SJZY.Track.Application.Helpers; using SJZY.Track.Domain.Aggregates.ForewarningAggregate; 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; } /// /// 获取预警分页列表 /// /// /// [HttpPost("GetWarningPageList")] public async Task> GetWarningPageList(GetWarningPageInputVo inputVo) { if (!inputVo.KeyWord.IsNullOrEmpty()) inputVo.KeyWord = inputVo.KeyWord!.Trim(); return await _warningRepository.GetWarningPageListAsync(inputVo); } /// /// 创建预警 /// /// /// [HttpPost("CreateWarning")] public async Task CreateWarning(CreateWarningInput input) { var warning = ObjectMapper.Map(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; } /// /// 编辑预警 /// /// /// /// [HttpPost("EditWarning")] public async Task EditWarning(int warningId, [FromServices] IBasicQuery statusQuery) { 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 }); return ForewarningHelper.MapWarningSummary(warning, statusList); } /// /// 更新预警 /// /// /// [HttpPost("UpdateWarning")] public async Task 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 = input.NotifyMode, NotifyTemplate = input.NotifyTemplate, NotifyUserId = x.NotifyUserId }).ToList(); await _warningRepository.UpdateForewarningAsync(warning); return true; } /// /// 启用or禁用预警 /// /// 预警id /// 是否启用 /// [HttpPost("TurnWarning")] public async Task 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.DeleteRulesAsync(warning.SubSystemId, warning.CustomerId); return true; } /// /// 查询预警规则列表 /// /// 预警规则id /// /// [HttpGet("GetWarningRuleList")] public async Task> GetWarningRuleListAsync([Required] int warningId, [FromServices] IBasicQuery 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; } /// /// 修改预警规则时效 /// /// /// /// [HttpPost("ModifyTimeSpans")] public async Task ModifyTimeSpans(ModifyTimeSpanInput input) { if (input.Items.IsNullOrEmpty()) return false; var warning = await _warningRepository.GetFirstAsync(x => x.Id == input.WarningId); if (warning == null) { throw new BusinessException(message: "预警不存在"); } var warningRules = ObjectMapper.Map, List>(input.Items); await _warningRepository.ModifyTimeSpansAsync(warningRules); // 清除缓存 await _forewarningCacheService.DeleteRulesAsync(warning.SubSystemId, warning.CustomerId); return true; } }