Files
DMS/DMS.Application/Services/Management/TriggerManagementService.cs
David P.G 740688d575 refactor:将DataLoaderService中的LoadAll*Async方法移到对应的管理服务中
- 将LoadAllDevicesAsync方法移到DeviceManagementService
 - 将LoadAllVariableTablesAsync方法移到VariableTableManagementService
 - 将LoadAllVariablesAsync方法移到VariableManagementService
 - 将LoadAllMenusAsync方法移到MenuManagementService
 - 将LoadAllMqttServersAsync方法移到MqttManagementService
 - 将LoadAllNlogsAsync方法移到LogManagementService
 - 更新DataLoaderService以使用管理服务提供的方法
 - 修改IDataLoaderService接口以移除这些方法
 - 保持与LoadAllTriggersAsync相同的模式
 - 遵循单一职责原则,提高代码一致性" (提交修改并添加描述性的提交信息)
2025-10-18 17:59:21 +08:00

172 lines
6.4 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 AutoMapper;
using DMS.Application.DTOs;
using DMS.Application.Interfaces;
using DMS.Application.Interfaces.Management;
using DMS.Application.Services.Triggers;
using DMS.Core.Interfaces;
using DMS.Core.Models.Triggers;
namespace DMS.Application.Services.Management
{
/// <summary>
/// 触发器管理服务实现
/// </summary>
public class TriggerManagementService : ITriggerManagementService
{
private readonly IAppStorageService _appStorageService;
private readonly IRepositoryManager _repositoryManager;
private readonly IMapper _mapper;
public TriggerManagementService(IAppStorageService appStorageService,IRepositoryManager repositoryManager, IMapper mapper)
{
_appStorageService = appStorageService;
_repositoryManager = repositoryManager;
_mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
}
/// <summary>
/// 获取所有触发器定义
/// </summary>
public List<TriggerDefinition> GetAllTriggersAsync()
{
var triggers = _appStorageService.Triggers.Values.ToList();
return _mapper.Map<List<TriggerDefinition>>(triggers);
}
/// <summary>
/// 根据 ID 获取触发器定义
/// </summary>
public async Task<TriggerDefinition?> GetTriggerByIdAsync(int id)
{
_appStorageService.Triggers.TryGetValue(id, out var trigger);
return trigger;
}
/// <summary>
/// 创建一个新的触发器定义
/// </summary>
public async Task<TriggerDefinition> CreateTriggerAsync(TriggerDefinition triggerDto)
{
// 1. 验证 DTO (可以在应用层或领域层做)
ValidateTriggerDto(triggerDto);
// 2. 转换 DTO 到实体
var triggerEntity = _mapper.Map<TriggerDefinition>(triggerDto);
triggerEntity.CreatedAt = DateTime.UtcNow;
triggerEntity.UpdatedAt = DateTime.UtcNow;
// 3. 调用仓储保存实体
var createdTrigger = await _repositoryManager.Triggers.AddAsync(triggerEntity);
// 4. 转换回 DTO 并返回
var result = _mapper.Map<TriggerDefinition>(createdTrigger);
// 5. 同步更新AppDataStorageService中的Triggers字典
_appStorageService.Triggers[result.Id] = result;
return result;
}
/// <summary>
/// 更新一个已存在的触发器定义
/// </summary>
public async Task<TriggerDefinition?> UpdateTriggerAsync(int id, TriggerDefinition triggerDto)
{
// 1. 获取现有实体
var existingTrigger = await _repositoryManager.Triggers.GetByIdAsync(id);
if (existingTrigger == null)
return null;
// 2. 验证 DTO
ValidateTriggerDto(triggerDto);
// 3. 将 DTO 映射到现有实体 (排除不可变字段如 Id, CreatedAt)
_mapper.Map(triggerDto, existingTrigger, opts => opts.Items["IgnoreIdAndCreatedAt"] = true);
existingTrigger.UpdatedAt = DateTime.UtcNow;
// 4. 调用仓储更新实体
var updatedTrigger = await _repositoryManager.Triggers.UpdateAsync(existingTrigger);
if (updatedTrigger == null)
return null;
// 5. 转换回 DTO 并返回
var result = _mapper.Map<TriggerDefinition>(updatedTrigger);
// 6. 同步更新AppDataStorageService中的Triggers字典
_appStorageService.Triggers[result.Id] = result;
return result;
}
/// <summary>
/// 删除一个触发器定义
/// </summary>
public async Task<bool> DeleteTriggerAsync(int id)
{
// var result = await _repositoryManager.Triggers.DeleteAsync(id);
//
// // 如果删除成功也从AppDataStorageService中的Triggers字典中移除
// if (result)
// {
// _appStorageService.Triggers.TryRemove(id, out _);
// }
//
// return result;
return false;
}
/// <summary>
/// 获取与指定变量关联的所有触发器定义
/// </summary>
public async Task<List<TriggerDefinition>> GetTriggersForVariableAsync(int variableId)
{
// var triggers = await _repositoryManager.Triggers.GetByVariableIdAsync(variableId);
// return _mapper.Map<List<TriggerDefinition>>(triggers);
return null;
}
/// <summary>
/// 异步加载所有触发器数据
/// </summary>
public async Task LoadAllTriggersAsync()
{
_appStorageService.Triggers.Clear();
var triggerDefinitions = await _repositoryManager.Triggers.GetAllAsync();
foreach (var triggerDefinition in triggerDefinitions)
{
_appStorageService.Triggers.TryAdd(triggerDefinition.Id, triggerDefinition);
}
}
/// <summary>
/// 内部方法:验证 TriggerDefinition 的有效性
/// </summary>
private void ValidateTriggerDto(TriggerDefinition dto)
{
// 检查是否至少关联了一个变量
if (dto.Variables == null || !dto.Variables.Any())
throw new ArgumentException("触发器必须至少关联一个变量。");
// 添加必要的验证逻辑
switch (dto.Condition)
{
case ConditionType.GreaterThan:
case ConditionType.LessThan:
case ConditionType.EqualTo:
case ConditionType.NotEqualTo:
if (!dto.Threshold.HasValue)
throw new ArgumentException($"{dto.Condition} requires Threshold.");
break;
case ConditionType.InRange:
case ConditionType.OutOfRange:
if (!dto.LowerBound.HasValue || !dto.UpperBound.HasValue)
throw new ArgumentException($"{dto.Condition} requires LowerBound and UpperBound.");
if (dto.LowerBound > dto.UpperBound)
throw new ArgumentException("LowerBound must be less than or equal to UpperBound.");
break;
}
}
}
}