track/src/TrackCenter.Application/Services/Warning/WarningService.cs
2025-08-05 10:41:46 +08:00

184 lines
7.3 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 TrackCenter.Application.Dtos.Warning;
using TrackCenter.Application.Caching;
using TrackCenter.Application.Services.Warning.Handlers;
using TrackCenter.Domain.Aggregates.WarningAggregate;
namespace TrackCenter.Application.Services.Warning;
/// <summary>
/// 预警规则主体
/// </summary>
[ApiExplorerSettings(GroupName = "预警服务")]
[Route("Warning")]
public class WarningService : ApplicationServiceBase
{
private readonly IWarningRuleRepository _warningRuleRepository;
private readonly ICompleteWarningHandler _completeWarningHandler;
private readonly ITransportWarningHandler _transportWarningHandler;
private readonly IReturnWarningHandler _returnWarningHandler;
private readonly AppNameDictCache _appNameDictCache;
public WarningService(
IWarningRuleRepository warningRuleRepository,
ICompleteWarningHandler completeWarningHandler,
ITransportWarningHandler transportWarningHandler,
IReturnWarningHandler returnWarningHandler,
AppNameDictCache appNameDictCache
)
{
_warningRuleRepository = warningRuleRepository;
_completeWarningHandler = completeWarningHandler;
_transportWarningHandler = transportWarningHandler;
_returnWarningHandler = returnWarningHandler;
_appNameDictCache = appNameDictCache;
}
/// <summary>
/// 获取预警规则列表
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("GetWarningRule")]
public async Task<List<WarningRuleDto>> GetWarningRule(GetWarningRuleInput input)
{
var result = await _warningRuleRepository.GetWarningRuleListAsync(input.AppKey, input.Customer, input.KpiName, input.IsEnabled, input.WarningType);
if (result.Count == 0)
return new List<WarningRuleDto>();
// 应用中文名字典
var appKeys = result.Select(x => x.AppKey).ToHashSet();
var appDict = await _appNameDictCache.SafeGetBatchAsync(appKeys);
return result.Select(x => new WarningRuleDto
{
Id = x.Id,
AppKey = x.AppKey,
AppName = appDict.GetValueOrDefault(x.AppKey, GlobalConst.EmptyString),
Customer = x.Customer,
Name = x.Name,
IsEnabled = x.IsEnabled,
WarningType = x.WarningType,
Version = x.Version,
CreateTime = x.CreateTime,
UpdateTime = x.UpdateTime,
}).ToList();
}
/// <summary>
/// 修改预警主体启用状态
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
/// <exception cref="BusinessException"></exception>
[HttpPost("UpdateEnabledStatus")]
public async Task<IdBaseDto> UpdateEnabledStatus(UpdateEnabledStatusInput input)
{
var rule = await _warningRuleRepository.GetWarningRuleByIdAsync(input.WarningId, (WarningRuleType) input.WarningType);
if (rule == null)
{
throw new BusinessException(message: $"预警规则主体不存在,WarningId=[{input.WarningId}]");
}
if (rule.IsEnabled == input.IsEnabled)
{
throw new BusinessException(message: $"预警规则主体启用状态未发生变化,不允许重复操作");
}
// 如果设置为启用则检查同一个应用同一客户下有多个启用的规则
if (input.IsEnabled && await _warningRuleRepository.CheckEnableDuplicateAsync(rule.Id, rule.AppKey, rule.Customer, (WarningRuleType) rule.WarningType))
{
throw new BusinessException(message: $"同一【应用+客户+类型】下不能有多个启用的预警规则主体,AppKey=[{rule.AppKey},Customer=[{rule.Customer}]]");
}
// 更新规则主体状态
rule.IsEnabled = input.IsEnabled;
await _warningRuleRepository.UpsertReturnIdAsync(rule);
// 预警规则主体状态变化处理
await HandleRuleEnabledChangedAsync(rule);
return new IdBaseDto
{
Id = rule.Id
};
}
/// <summary>
/// 新增/修改预警规则主体
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("UpsertWarningRule")]
public async Task<IdBaseDto> UpsertWarningRule(UpsertWarningRuleInput input)
{
WarningRuleEntity? ruleEntity = null;
// 当为修改操作时,则要求被修改的主体必须存在
if (input.Id != 0)
{
ruleEntity = await _warningRuleRepository.GetWarningRuleByIdAsync(input.Id, (WarningRuleType) input.WarningType);
if (ruleEntity == null)
throw new BusinessException(message: $"WarningId=[{input.Id}],WarningType=[{input.WarningType}]预警规则主体不存在,不允许修改");
}
// 要求 AppKey+Customer+Name+WarningType 联合不能重复
if (await _warningRuleRepository.CheckNameDuplicateAsync(input.Id, input.AppKey, input.Customer, input.Name, (WarningRuleType) input.WarningType))
{
throw new BusinessException(message: $"AppKey:[{input.AppKey}],Customer:[{input.Customer}]," +
$"WarningType:[{input.WarningType.ToString()}]已存在名称为[{input.Name}]规则,不允许重复创建/修改");
}
// 如果创建/修改的主体为启用状态,则要求通过检查
if (input.IsEnabled && await _warningRuleRepository.CheckEnableDuplicateAsync(input.Id, input.AppKey, input.Customer, (WarningRuleType) input.WarningType))
{
throw new BusinessException(message: $"同一【应用+客户+预警类型】下不能有多个启用的预警规则主体,不允许创建/修改");
}
var enabledChanged = false;
if (ruleEntity == null)
{
ruleEntity = new WarningRuleEntity(
input.Id,
input.Name,
input.AppKey,
input.Customer,
input.IsEnabled,
input.WarningType
);
}
else
{
enabledChanged = ruleEntity.IsEnabled != input.IsEnabled;
ruleEntity.Name = input.Name;
ruleEntity.AppKey = input.AppKey;
ruleEntity.Customer = input.Customer;
ruleEntity.IsEnabled = input.IsEnabled;
ruleEntity.WarningType = input.WarningType;
}
var id = await _warningRuleRepository.UpsertReturnIdAsync(ruleEntity);
// 当为修改操作时,进行
if (enabledChanged)
await HandleRuleEnabledChangedAsync(ruleEntity);
return new IdBaseDto
{
Id = id
};
}
private async Task HandleRuleEnabledChangedAsync(WarningRuleEntity rule)
{
// 只处理预警主体关闭的情况
if (rule.IsEnabled) return;
await (rule.WarningType switch
{
(int) WarningRuleType.运输时效预警 => _transportWarningHandler.WarningRuleToDisabledHandleAsync(rule),
(int) WarningRuleType.回传时效预警 => _returnWarningHandler.WarningRuleToDisabledHandleAsync(rule),
(int) WarningRuleType.完整预警 => _completeWarningHandler.WarningRuleToDisabledHandleAsync(rule),
_ => throw new BusinessException(message: $"不支持的预警类型,WarningType=[{rule.WarningType}]")
});
}
}