修改了触发器的问题

This commit is contained in:
2025-09-14 19:58:18 +08:00
parent 8e039e657a
commit ebf67b98fa
16 changed files with 144 additions and 56 deletions

View File

@@ -11,12 +11,12 @@ namespace DMS.Application.DTOs.Triggers
/// <summary>
/// 触发器唯一标识符
/// </summary>
public Guid Id { get; set; }
public int Id { get; set; }
/// <summary>
/// 关联的变量 ID
/// </summary>
public Guid VariableId { get; set; }
public int VariableId { get; set; }
/// <summary>
/// 触发器是否处于激活状态

View File

@@ -1,6 +1,8 @@
using AutoMapper;
using DMS.Core.Models;
using DMS.Application.DTOs;
using DMS.Application.DTOs.Triggers;
using DMS.Core.Models.Triggers;
namespace DMS.Application.Profiles;
@@ -56,5 +58,6 @@ public class MappingProfile : Profile
CreateMap<EmailTemplate, EmailTemplateDto>().ReverseMap();
CreateMap<EmailLog, EmailLogDto>().ReverseMap();
CreateMap<TriggerDefinition, TriggerDefinitionDto>().ReverseMap();
}
}

View File

@@ -21,7 +21,7 @@ namespace DMS.Application.Services.Triggers
/// </summary>
/// <param name="id">触发器 ID</param>
/// <returns>触发器定义 DTO如果未找到则返回 null</returns>
Task<TriggerDefinitionDto?> GetTriggerByIdAsync(Guid id);
Task<TriggerDefinitionDto?> GetTriggerByIdAsync(int id);
/// <summary>
/// 创建一个新的触发器定义
@@ -36,20 +36,20 @@ namespace DMS.Application.Services.Triggers
/// <param name="id">要更新的触发器 ID</param>
/// <param name="triggerDto">包含更新信息的触发器定义 DTO</param>
/// <returns>更新后的触发器定义 DTO如果未找到则返回 null</returns>
Task<TriggerDefinitionDto?> UpdateTriggerAsync(Guid id, TriggerDefinitionDto triggerDto);
Task<TriggerDefinitionDto?> UpdateTriggerAsync(int id, TriggerDefinitionDto triggerDto);
/// <summary>
/// 删除一个触发器定义
/// </summary>
/// <param name="id">要删除的触发器 ID</param>
/// <returns>删除成功返回 true否则返回 false</returns>
Task<bool> DeleteTriggerAsync(Guid id);
Task<bool> DeleteTriggerAsync(int id);
/// <summary>
/// 获取与指定变量关联的所有触发器定义
/// </summary>
/// <param name="variableId">变量 ID</param>
/// <returns>该变量关联的触发器定义列表</returns>
Task<List<TriggerDefinitionDto>> GetTriggersForVariableAsync(Guid variableId);
Task<List<TriggerDefinitionDto>> GetTriggersForVariableAsync(int variableId);
}
}

View File

@@ -23,7 +23,7 @@ namespace DMS.Application.Services.Triggers.Impl
private readonly ITriggerActionExecutor _actionExecutor;
private readonly ILogger<TriggerEvaluationService> _logger; // 使用标准日志接口
// 为每个触发器存储抑制定时器
private readonly ConcurrentDictionary<Guid, ThreadingTimer> _suppressionTimers = new();
private readonly ConcurrentDictionary<int, ThreadingTimer> _suppressionTimers = new();
public TriggerEvaluationService(
ITriggerManagementService triggerManagementService,

View File

@@ -35,7 +35,7 @@ namespace DMS.Application.Services.Triggers.Impl
/// <summary>
/// 根据 ID 获取触发器定义
/// </summary>
public async Task<TriggerDefinitionDto?> GetTriggerByIdAsync(Guid id)
public async Task<TriggerDefinitionDto?> GetTriggerByIdAsync(int id)
{
var trigger = await _repositoryManager.Triggers.GetByIdAsync(id);
return trigger != null ? _mapper.Map<TriggerDefinitionDto>(trigger) : null;
@@ -64,7 +64,7 @@ namespace DMS.Application.Services.Triggers.Impl
/// <summary>
/// 更新一个已存在的触发器定义
/// </summary>
public async Task<TriggerDefinitionDto?> UpdateTriggerAsync(Guid id, TriggerDefinitionDto triggerDto)
public async Task<TriggerDefinitionDto?> UpdateTriggerAsync(int id, TriggerDefinitionDto triggerDto)
{
// 1. 获取现有实体
var existingTrigger = await _repositoryManager.Triggers.GetByIdAsync(id);
@@ -90,7 +90,7 @@ namespace DMS.Application.Services.Triggers.Impl
/// <summary>
/// 删除一个触发器定义
/// </summary>
public async Task<bool> DeleteTriggerAsync(Guid id)
public async Task<bool> DeleteTriggerAsync(int id)
{
return await _repositoryManager.Triggers.DeleteAsync(id);
}
@@ -98,7 +98,7 @@ namespace DMS.Application.Services.Triggers.Impl
/// <summary>
/// 获取与指定变量关联的所有触发器定义
/// </summary>
public async Task<List<TriggerDefinitionDto>> GetTriggersForVariableAsync(Guid variableId)
public async Task<List<TriggerDefinitionDto>> GetTriggersForVariableAsync(int variableId)
{
var triggers = await _repositoryManager.Triggers.GetByVariableIdAsync(variableId);
return _mapper.Map<List<TriggerDefinitionDto>>(triggers);

View File

@@ -83,23 +83,33 @@ public class VariableTableManagementService : IVariableTableManagementService
return;
DeviceDto deviceDto = null;
if (_appDataStorageService.Devices.TryGetValue(variableTableDto.DeviceId, out var device))
if (_appDataStorageService.Devices != null &&
_appDataStorageService.Devices.TryGetValue(variableTableDto.DeviceId, out var device))
{
deviceDto = device;
// 确保VariableTables不为null
device.VariableTables ??= new List<VariableTableDto>();
if (device.VariableTables == null)
device.VariableTables = new List<VariableTableDto>();
device.VariableTables.Add(variableTableDto);
// 确保Device属性不为null
if (variableTableDto != null)
variableTableDto.Device = device;
}
// 确保_variableTables和variableTableDto不为null
if (_variableTables != null && variableTableDto != null)
{
if (_variableTables.TryAdd(variableTableDto.Id, variableTableDto))
{
OnVariableTableChanged?.Invoke(this,new VariableTableChangedEventArgs(
OnVariableTableChanged?.Invoke(this, new VariableTableChangedEventArgs(
DataChangeType.Added,
variableTableDto,
deviceDto));
}
}
}
/// <summary>
/// 在内存中更新变量表

View File

@@ -21,7 +21,7 @@ namespace DMS.Core.Interfaces.Repositories.Triggers
/// </summary>
/// <param name="id">触发器 ID</param>
/// <returns>触发器定义实体,如果未找到则返回 null</returns>
Task<TriggerDefinition?> GetByIdAsync(Guid id);
Task<TriggerDefinition?> GetByIdAsync(int id);
/// <summary>
/// 添加一个新的触发器定义
@@ -42,13 +42,13 @@ namespace DMS.Core.Interfaces.Repositories.Triggers
/// </summary>
/// <param name="id">要删除的触发器 ID</param>
/// <returns>删除成功返回 true否则返回 false</returns>
Task<bool> DeleteAsync(Guid id);
Task<bool> DeleteAsync(int id);
/// <summary>
/// 获取与指定变量关联的所有触发器定义
/// </summary>
/// <param name="variableId">变量 ID</param>
/// <returns>该变量关联的触发器定义实体列表</returns>
Task<IEnumerable<TriggerDefinition>> GetByVariableIdAsync(Guid variableId);
Task<IEnumerable<TriggerDefinition>> GetByVariableIdAsync(int variableId);
}
}

View File

@@ -34,12 +34,12 @@ namespace DMS.Core.Models.Triggers
/// <summary>
/// 触发器唯一标识符
/// </summary>
public Guid Id { get; set; } = Guid.NewGuid();
public int Id { get; set; }
/// <summary>
/// 关联的变量 ID
/// </summary>
public Guid VariableId { get; set; }
public int VariableId { get; set; }
/// <summary>
/// 触发器是否处于激活状态

View File

@@ -14,13 +14,13 @@ public class DbTriggerDefinition
/// <summary>
/// 触发器唯一标识符,主键。
/// </summary>
[SugarColumn(IsPrimaryKey = true)]
public Guid Id { get; set; }
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; }
/// <summary>
/// 关联的变量 ID。
/// </summary>
public Guid VariableId { get; set; }
public int VariableId { get; set; }
/// <summary>
/// 触发器是否处于激活状态。

View File

@@ -100,10 +100,12 @@ public abstract class BaseRepository<TEntity>
return entities;
}
/// <summary>
/// 异步根据主键 ID 获取单个实体。
/// 异步根据主键 ID (int类型) 获取单个实体。
/// </summary>
/// <param name="id">实体的主键 ID。</param>
/// <param name="id">实体的主键 ID (int类型)。</param>
/// <returns>返回找到的实体,如果未找到则返回 null。</returns>
public virtual async Task<TEntity> GetByIdAsync(int id)
{

View File

@@ -40,7 +40,7 @@ namespace DMS.Infrastructure.Repositories
/// <summary>
/// 根据 ID 获取触发器定义
/// </summary>
public async Task<TriggerDefinition?> GetByIdAsync(Guid id)
public async Task<TriggerDefinition?> GetByIdAsync(int id)
{
var dbTrigger = await base.GetByIdAsync(id);
return _mapper.Map<TriggerDefinition>(dbTrigger);
@@ -67,7 +67,7 @@ namespace DMS.Infrastructure.Repositories
/// <summary>
/// 删除一个触发器定义
/// </summary>
public async Task<bool> DeleteAsync(Guid id)
public async Task<bool> DeleteAsync(int id)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
@@ -82,7 +82,7 @@ namespace DMS.Infrastructure.Repositories
/// <summary>
/// 获取与指定变量关联的所有触发器定义
/// </summary>
public async Task<IEnumerable<TriggerDefinition>> GetByVariableIdAsync(Guid variableId)
public async Task<IEnumerable<TriggerDefinition>> GetByVariableIdAsync(int variableId)
{
var stopwatch = new Stopwatch();
stopwatch.Start();

View File

@@ -85,42 +85,67 @@ public class DeviceDataService : IDeviceDataService
/// </summary>
public async Task<CreateDeviceWithDetailsDto> AddDevice(CreateDeviceWithDetailsDto dto)
{
// 添加null检查
if (dto == null || _appDataCenterService == null || _appDataCenterService.DeviceManagementService == null)
return null;
var addDto = await _appDataCenterService.DeviceManagementService.CreateDeviceWithDetailsAsync(dto);
//更新当前界面
_dataStorageService.Devices.Add(_mapper.Map<DeviceItemViewModel>(addDto.Device));
_menuDataService.AddMenuItem(_mapper.Map<MenuItemViewModel>(addDto.DeviceMenu));
// 添加null检查
if (addDto.VariableTable != null)
if (_dataStorageService != null && addDto != null && addDto.Device != null)
{
//更新当前界面
_dataStorageService.Devices.Add(_mapper.Map<DeviceItemViewModel>(addDto.Device));
}
// 添加null检查
if (_menuDataService != null && addDto != null && addDto.DeviceMenu != null)
{
_menuDataService.AddMenuItem(_mapper.Map<MenuItemViewModel>(addDto.DeviceMenu));
}
// 添加null检查
if (addDto != null && addDto.VariableTable != null)
{
await _variableDataService.AddVariableTable(addDto.VariableTable);
}
// 添加null检查
if (addDto.VariableTableMenu != null)
if (_menuDataService != null && addDto != null && addDto.VariableTableMenu != null)
{
_menuDataService.AddMenuItem(_mapper.Map<MenuItemViewModel>(addDto.VariableTableMenu));
}
// 添加null检查
if (_appDataCenterService.DeviceManagementService != null && addDto != null && addDto.Device != null)
{
//更新数据中心
_appDataCenterService.DeviceManagementService.AddDeviceToMemory(addDto.Device);
}
// 添加null检查
if (addDto.VariableTable != null)
if (_appDataCenterService.VariableTableManagementService != null && addDto != null && addDto.VariableTable != null)
{
_appDataCenterService.VariableTableManagementService.AddVariableTableToMemory(addDto.VariableTable);
}
// 添加null检查
if (_appDataCenterService.MenuManagementService != null && addDto != null && addDto.DeviceMenu != null)
{
_appDataCenterService.MenuManagementService.AddMenuToMemory(addDto.DeviceMenu);
}
// 添加null检查
if (addDto.VariableTableMenu != null)
if (_appDataCenterService.MenuManagementService != null && addDto != null && addDto.VariableTableMenu != null)
{
_appDataCenterService.MenuManagementService.AddMenuToMemory(addDto.VariableTableMenu);
}
// 添加null检查
if (_menuDataService != null)
{
_menuDataService.BuildMenuTrees();
}
return addDto;
}

View File

@@ -51,19 +51,38 @@ public class VariableDataService : IVariableDataService
public async Task<bool> AddVariableTable(VariableTableDto variableTableDto,
MenuBeanDto menuDto = null, bool isAddDb = false)
{
// 添加null检查
if (variableTableDto == null)
return false;
// 添加_appDataCenterService和_variableTableManagementService的null检查
if (_appDataCenterService == null || _appDataCenterService.VariableTableManagementService == null)
return false;
if (isAddDb && menuDto != null)
{
// 添加null检查
if (_appDataCenterService.VariableTableManagementService == null)
return false;
CreateVariableTableWithMenuDto createDto = new CreateVariableTableWithMenuDto();
createDto.VariableTable = variableTableDto;
createDto.DeviceId = variableTableDto.DeviceId;
createDto.Menu = menuDto;
// 添加null检查
if (_appDataCenterService.VariableTableManagementService == null)
return false;
var resDto = await _appDataCenterService.VariableTableManagementService.CreateVariableTableAsync(createDto);
// 添加null检查
if (resDto != null && resDto.VariableTable != null && variableTableDto != null)
_mapper.Map(resDto.VariableTable, variableTableDto);
}
// 添加null检查
if (_appDataCenterService.VariableTableManagementService != null && variableTableDto != null)
_appDataCenterService.VariableTableManagementService.AddVariableTableToMemory(variableTableDto);
return true;

View File

@@ -89,7 +89,16 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable
CreateDeviceWithDetailsDto dto = new CreateDeviceWithDetailsDto();
// 添加null检查
if (_mapper != null)
{
dto.Device = _mapper.Map<DeviceDto>(device);
}
else
{
_notificationService?.ShowError("映射服务未初始化");
return;
}
dto.DeviceMenu = new MenuBeanDto()
@@ -98,6 +107,7 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable
Icon = SegoeFluentIcons.Devices2.Glyph,
TargetViewKey = "DeviceDetailView"
};
if (device.IsAddDefVarTable)
{
dto.VariableTable = new VariableTableDto()
@@ -116,16 +126,36 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable
// 添加设备
// 添加null检查
if (_wpfDataService != null && _wpfDataService.DeviceDataService != null)
{
var addDto = await _wpfDataService.DeviceDataService.AddDevice(dto);
// 添加null检查
if (addDto != null && addDto.Device != null && _notificationService != null)
{
_notificationService.ShowSuccess($"设备添加成功:{addDto.Device.Name}");
}
else if (_notificationService != null)
{
_notificationService.ShowError("设备添加失败");
}
}
else if (_notificationService != null)
{
_notificationService.ShowError("数据服务未初始化");
}
}
catch (Exception e)
{
Console.WriteLine(e);
// 添加null检查
if (_notificationService != null)
{
_notificationService.ShowError($"添加设备的过程中发生错误:{e.Message}", e);
}
}
}
/// <summary>
/// 删除设备命令。

View File

@@ -54,7 +54,7 @@ namespace DMS.WPF.ViewModels.Dialogs
if (parameter is TriggerDefinitionDto triggerDto)
{
Trigger = triggerDto;
Title = Trigger.Id == Guid.Empty ? "新建触发器" : "编辑触发器";
Title = Trigger.Id == default(int) ? "新建触发器" : "编辑触发器";
PrimaryButText = "保存";
// Load available variables for selection dropdown
@@ -109,7 +109,7 @@ namespace DMS.WPF.ViewModels.Dialogs
private async Task SaveAsync()
{
// Basic validation
if (Trigger.VariableId == Guid.Empty)
if (Trigger.VariableId == default(int))
{
_notificationService.ShowWarn("请选择关联的变量");
return;
@@ -187,10 +187,10 @@ namespace DMS.WPF.ViewModels.Dialogs
// Set timestamps
Trigger.UpdatedAt = DateTime.UtcNow;
if (Trigger.Id == Guid.Empty)
if (Trigger.Id == default(int))
{
Trigger.CreatedAt = DateTime.UtcNow;
Trigger.Id = Guid.NewGuid();
Trigger.Id = 0; // 对于自增ID设置为0让数据库自动生成
}
// Close dialog with the updated trigger DTO

View File

@@ -63,7 +63,6 @@ namespace DMS.WPF.ViewModels.Triggers
{
var newTrigger = new TriggerDefinitionDto
{
Id = Guid.NewGuid(),
IsActive = true,
Condition = Core.Models.Triggers.ConditionType.GreaterThan,
Action = Core.Models.Triggers.ActionType.SendEmail,