From 72d244031429f8986e4cfdf232912b9ee1bbafab Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Sat, 18 Oct 2025 18:55:08 +0800 Subject: [PATCH] =?UTF-8?q?=201=20feat:=20=E9=87=8D=E6=9E=84=E8=A7=A6?= =?UTF-8?q?=E5=8F=91=E5=99=A8=E8=AE=BE=E8=AE=A1=EF=BC=8C=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E8=A7=A6=E5=8F=91=E6=9D=A1=E4=BB=B6=E5=B9=B6=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=90=8D=E7=A7=B0=E5=AD=97=E6=AE=B5=20=20=20=20=202=20=20=20?= =?UTF-8?q?=20=203=20-=20=E4=BB=8ETrigger=E3=80=81DbTriggerDefinition?= =?UTF-8?q?=E5=92=8CTriggerItem=E7=B1=BB=E4=B8=AD=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E4=BA=86=E6=89=80=E6=9C=89=E6=9D=A1=E4=BB=B6=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E7=9A=84=E5=B1=9E=E6=80=A7=EF=BC=88Condition,=20Threshold,=20L?= =?UTF-8?q?owerBound,=20UpperBound=EF=BC=89=20=20=20=20=204=20-=20?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E4=BA=86ConditionType=E6=9E=9A=E4=B8=BE?= =?UTF-8?q?=EF=BC=8C=E7=AE=80=E5=8C=96=E4=BA=86=E8=A7=A6=E5=8F=91=E5=99=A8?= =?UTF-8?q?=E9=80=BB=E8=BE=91=20=20=20=20=205=20-=20=E4=B8=BA=E8=A7=A6?= =?UTF-8?q?=E5=8F=91=E5=99=A8=E6=B7=BB=E5=8A=A0=E4=BA=86Name=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=EF=BC=8C=E5=9C=A8=E6=A0=B8=E5=BF=83=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=E3=80=81=E6=95=B0=E6=8D=AE=E5=BA=93=E5=AE=9E=E4=BD=93=E5=92=8C?= =?UTF-8?q?=E8=A7=86=E5=9B=BE=E6=A8=A1=E5=9E=8B=E4=B8=AD=E9=83=BD=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E4=BA=86=E8=AF=A5=E5=B1=9E=E6=80=A7=20=20=20=20=206?= =?UTF-8?q?=20-=20=E5=88=A0=E9=99=A4=E4=BA=86TriggerDialog=E7=95=8C?= =?UTF-8?q?=E9=9D=A2=E4=B8=AD=E7=9A=84=E5=8F=98=E9=87=8F=E9=80=89=E6=8B=A9?= =?UTF-8?q?=E5=92=8C=E6=90=9C=E7=B4=A2=E5=8A=9F=E8=83=BD=20=20=20=20=207?= =?UTF-8?q?=20-=20=E4=BB=8ETriggerDialog=E7=95=8C=E9=9D=A2=E4=B8=AD?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E4=BA=86=E8=A7=A6=E5=8F=91=E6=9D=A1=E4=BB=B6?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E7=9A=84UI=E5=85=83=E7=B4=A0=20=20=20=20=208?= =?UTF-8?q?=20-=20=E6=9B=B4=E6=96=B0=E4=BA=86TriggerDialogViewModel?= =?UTF-8?q?=EF=BC=8C=E7=A7=BB=E9=99=A4=E4=BA=86=E6=9D=A1=E4=BB=B6=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E7=9A=84=E9=AA=8C=E8=AF=81=E5=92=8C=E4=B8=9A=E5=8A=A1?= =?UTF-8?q?=E9=80=BB=E8=BE=91=20=20=20=20=209=20-=20=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E4=BA=86TriggersViewModel=EF=BC=8C=E7=A7=BB=E9=99=A4=E4=BA=86?= =?UTF-8?q?=E6=9D=A1=E4=BB=B6=E7=9A=84=E5=88=9D=E5=A7=8B=E5=8C=96=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=20=20=20=2010=20-=20=E6=9B=B4=E6=96=B0=E4=BA=86AutoMa?= =?UTF-8?q?pper=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=EF=BC=8C=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0TriggerItem=E4=B8=8ETrigger=E4=B9=8B=E9=97=B4=E7=9A=84?= =?UTF-8?q?=E6=98=A0=E5=B0=84=20=20=20=2011=20-=20=E5=9C=A8TriggerEvaluati?= =?UTF-8?q?onService=E4=B8=AD=E7=A7=BB=E9=99=A4=E4=BA=86=E6=9D=A1=E4=BB=B6?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E9=80=BB=E8=BE=91=EF=BC=8C=E7=8E=B0=E5=9C=A8?= =?UTF-8?q?=E6=BF=80=E6=B4=BB=E7=9A=84=E8=A7=A6=E5=8F=91=E5=99=A8=E4=BC=9A?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E6=89=A7=E8=A1=8C=E5=8A=A8=E4=BD=9C=20=20=20?= =?UTF-8?q?=2012=20-=20=E6=9B=B4=E6=96=B0=E4=BA=86App.xaml=EF=BC=8C?= =?UTF-8?q?=E7=A7=BB=E9=99=A4=E4=BA=86=E5=AF=B9=E5=B7=B2=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E6=9E=9A=E4=B8=BE=E7=9A=84=E5=BC=95=E7=94=A8=20=20=20=2013=20-?= =?UTF-8?q?=20=E4=BF=AE=E6=94=B9=E4=BA=86=E4=BF=9D=E5=AD=98=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E9=80=BB=E8=BE=91=EF=BC=8C=E7=A1=AE=E4=BF=9D=E8=A7=A6?= =?UTF-8?q?=E5=8F=91=E5=99=A8=E5=90=8D=E7=A7=B0=E4=B8=8D=E8=83=BD=E4=B8=BA?= =?UTF-8?q?=E7=A9=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Management/TriggerManagementService.cs | 19 +- .../Triggers/Impl/TriggerActionExecutor.cs | 69 +------ .../Triggers/Impl/TriggerEvaluationService.cs | 27 +-- DMS.Core/Models/Triggers/Trigger.cs | 41 +--- .../Entities/DbTriggerDefinition.cs | 32 +-- DMS.Infrastructure/Profiles/MappingProfile.cs | 1 + DMS.WPF/App.xaml | 6 - DMS.WPF/ItemViewModel/TriggerItem.cs | 30 +-- DMS.WPF/Profiles/MappingProfile.cs | 5 + .../Dialogs/TriggerDialogViewModel.cs | 152 +------------- DMS.WPF/ViewModels/TriggersViewModel.cs | 1 - DMS.WPF/Views/Dialogs/TriggerDialog.xaml | 189 +----------------- DMS.WPF/Views/Dialogs/TriggerDialog.xaml.cs | 24 +-- 13 files changed, 44 insertions(+), 552 deletions(-) diff --git a/DMS.Application/Services/Management/TriggerManagementService.cs b/DMS.Application/Services/Management/TriggerManagementService.cs index a6f6234..6ccd17b 100644 --- a/DMS.Application/Services/Management/TriggerManagementService.cs +++ b/DMS.Application/Services/Management/TriggerManagementService.cs @@ -49,7 +49,7 @@ namespace DMS.Application.Services.Management public async Task CreateTriggerAsync(Trigger triggerDto) { // 1. 验证 DTO (可以在应用层或领域层做) - ValidateTriggerDto(triggerDto); + // ValidateTriggerDto(triggerDto); // 2. 转换 DTO 到实体 var triggerEntity = _mapper.Map(triggerDto); @@ -150,23 +150,6 @@ namespace DMS.Application.Services.Management 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; - } } } } \ No newline at end of file diff --git a/DMS.Application/Services/Triggers/Impl/TriggerActionExecutor.cs b/DMS.Application/Services/Triggers/Impl/TriggerActionExecutor.cs index 118e80c..dd43ac3 100644 --- a/DMS.Application/Services/Triggers/Impl/TriggerActionExecutor.cs +++ b/DMS.Application/Services/Triggers/Impl/TriggerActionExecutor.cs @@ -38,7 +38,7 @@ namespace DMS.Application.Services.Triggers.Impl switch (context.Trigger.Action) { case ActionType.SendEmail: - await ExecuteSendEmail(context); + // await ExecuteSendEmail(context); break; case ActionType.ActivateAlarm: _logger.LogWarning("Action 'ActivateAlarm' is not implemented yet."); @@ -61,72 +61,5 @@ namespace DMS.Application.Services.Triggers.Impl } } - #region 私有执行方法 - - private async Task ExecuteSendEmail(TriggerContext context) - { - if (_emailService == null) - { - _logger.LogWarning("Email service is not configured, skipping SendEmail action for trigger '{TriggerId}'.", context.Trigger.Id); - return; - } - - var config = JsonSerializer.Deserialize>(context.Trigger.ActionConfigurationJson); - if (config == null || - !config.TryGetValue("Recipients", out var recipientsElement) || - !config.TryGetValue("SubjectTemplate", out var subjectTemplateElement) || - !config.TryGetValue("BodyTemplate", out var bodyTemplateElement)) - { - _logger.LogError("Invalid configuration for SendEmail action for trigger '{TriggerId}'.", context.Trigger.Id); - return; - } - - var recipients = recipientsElement.Deserialize>(); - var subjectTemplate = subjectTemplateElement.GetString(); - var bodyTemplate = bodyTemplateElement.GetString(); - - if (recipients == null || string.IsNullOrEmpty(subjectTemplate) || string.IsNullOrEmpty(bodyTemplate)) - { - _logger.LogError("Missing required fields in SendEmail configuration for trigger '{TriggerId}'.", context.Trigger.Id); - return; - } - - // Simple token replacement - in practice, use a templating engine like Scriban, RazorLight etc. - // Note: This assumes context.Variable and context.CurrentValue have Name properties/values. - // You might need to adjust the token names and values based on your actual Variable structure. - var subject = subjectTemplate - .Replace("{VariableName}", context.Variable?.Name ?? "Unknown") - .Replace("{CurrentValue}", context.CurrentValue?.ToString() ?? "N/A") - .Replace("{Threshold}", context.Trigger.Threshold?.ToString() ?? "N/A") - .Replace("{LowerBound}", context.Trigger.LowerBound?.ToString() ?? "N/A") - .Replace("{UpperBound}", context.Trigger.UpperBound?.ToString() ?? "N/A"); - - var body = bodyTemplate - .Replace("{VariableName}", context.Variable?.Name ?? "Unknown") - .Replace("{CurrentValue}", context.CurrentValue?.ToString() ?? "N/A") - .Replace("{Threshold}", context.Trigger.Threshold?.ToString() ?? "N/A") - .Replace("{LowerBound}", context.Trigger.LowerBound?.ToString() ?? "N/A") - .Replace("{UpperBound}", context.Trigger.UpperBound?.ToString() ?? "N/A") - .Replace("{Timestamp}", DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss")); - - // await _emailService.SendEmailAsync(recipients, subject, body); - } - - private async Task ExecuteActivateAlarm(TriggerContext context) - { - var alarmId = $"trigger_{context.Trigger.Id}_{context.Variable.Id}"; - var message = $"Trigger '{context.Trigger.Description}' activated for variable '{context.Variable.Name}' with value '{context.CurrentValue}'."; - // 假设 INotificationService 有 RaiseAlarmAsync 方法 - // await _notificationService.RaiseAlarmAsync(alarmId, message); - } - - private async Task ExecuteWriteToLog(TriggerContext context) - { - var message = $"Trigger '{context.Trigger.Description}' activated for variable '{context.Variable.Name}' with value '{context.CurrentValue}'."; - // 假设 ILoggingService 有 LogTriggerAsync 方法 - // await _loggingService.LogTriggerAsync(message); - } - - #endregion } } \ No newline at end of file diff --git a/DMS.Application/Services/Triggers/Impl/TriggerEvaluationService.cs b/DMS.Application/Services/Triggers/Impl/TriggerEvaluationService.cs index c6f8983..aa02d76 100644 --- a/DMS.Application/Services/Triggers/Impl/TriggerEvaluationService.cs +++ b/DMS.Application/Services/Triggers/Impl/TriggerEvaluationService.cs @@ -109,31 +109,10 @@ namespace DMS.Application.Services.Triggers.Impl return false; // Cannot evaluate null } - // Attempt conversion from object to double - adjust parsing logic as needed for your data types - if (!double.TryParse(currentValueObj.ToString(), out double currentValue)) - { - _logger.LogWarning("Could not parse current value '{CurrentValue}' to double for trigger evaluation (trigger ID: {TriggerId}).", currentValueObj, trigger.Id); - return false; - } + // 由于移除了条件,所有激活的触发器都会被触发 + _logger.LogInformation("Trigger activated for trigger ID: {TriggerId}", trigger.Id); - bool result = trigger.Condition switch - { - ConditionType.GreaterThan => currentValue > trigger.Threshold, - ConditionType.LessThan => currentValue < trigger.Threshold, - ConditionType.EqualTo => Math.Abs(currentValue - trigger.Threshold.GetValueOrDefault()) < double.Epsilon, - ConditionType.NotEqualTo => Math.Abs(currentValue - trigger.Threshold.GetValueOrDefault()) >= double.Epsilon, - ConditionType.InRange => currentValue >= trigger.LowerBound && currentValue <= trigger.UpperBound, - ConditionType.OutOfRange => currentValue < trigger.LowerBound || currentValue > trigger.UpperBound, - _ => false - }; - - if(result) - { - _logger.LogInformation("Trigger condition met: Variable value {CurrentValue} satisfies {Condition} for trigger ID: {TriggerId}", - currentValue, trigger.Condition, trigger.Id); - } - - return result; + return true; } /// diff --git a/DMS.Core/Models/Triggers/Trigger.cs b/DMS.Core/Models/Triggers/Trigger.cs index e917520..d62fc64 100644 --- a/DMS.Core/Models/Triggers/Trigger.cs +++ b/DMS.Core/Models/Triggers/Trigger.cs @@ -3,19 +3,6 @@ using System.Collections.Generic; namespace DMS.Core.Models.Triggers { - /// - /// 触发器条件类型枚举 - /// - public enum ConditionType - { - GreaterThan, - LessThan, - EqualTo, - NotEqualTo, - InRange, // 值在 LowerBound 和 UpperBound 之间 (包含边界) - OutOfRange // 值低于 LowerBound 或高于 UpperBound - } - /// /// 触发器动作类型枚举 /// @@ -37,6 +24,11 @@ namespace DMS.Core.Models.Triggers /// public int Id { get; set; } + /// + /// 触发器名称 + /// + public string Name { get; set; } = ""; + /// /// 关联的变量列表 /// @@ -47,28 +39,6 @@ namespace DMS.Core.Models.Triggers /// public bool IsActive { get; set; } = true; - // --- 条件部分 --- - - /// - /// 触发条件类型 - /// - public ConditionType Condition { get; set; } - - /// - /// 阈值 (用于 GreaterThan, LessThan, EqualTo, NotEqualTo) - /// - public double? Threshold { get; set; } - - /// - /// 下限 (用于 InRange, OutOfRange) - /// - public double? LowerBound { get; set; } - - /// - /// 上限 (用于 InRange, OutOfRange) - /// - public double? UpperBound { get; set; } - // --- 动作部分 --- /// @@ -93,6 +63,7 @@ namespace DMS.Core.Models.Triggers /// public DateTime? LastTriggeredAt { get; set; } + /// /// 触发器描述 /// diff --git a/DMS.Infrastructure/Entities/DbTriggerDefinition.cs b/DMS.Infrastructure/Entities/DbTriggerDefinition.cs index 0aed3b2..b5b468e 100644 --- a/DMS.Infrastructure/Entities/DbTriggerDefinition.cs +++ b/DMS.Infrastructure/Entities/DbTriggerDefinition.cs @@ -18,37 +18,17 @@ public class DbTriggerDefinition [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] public int Id { get; set; } + /// + /// 触发器名称 + /// + [SugarColumn(Length = 200, IsNullable = true)] + public string Name { get; set; } = ""; + /// /// 触发器是否处于激活状态。 /// public bool IsActive { get; set; } = true; - // --- 条件部分 --- - - /// - /// 触发条件类型。 - /// - [SugarColumn(ColumnDataType = "varchar(20)", SqlParameterDbType = typeof(EnumToStringConvert))] - public ConditionType Condition { get; set; } - - /// - /// 阈值 (用于 GreaterThan, LessThan, EqualTo, NotEqualTo)。 - /// - [SugarColumn(IsNullable = true)] - public double? Threshold { get; set; } - - /// - /// 下限 (用于 InRange, OutOfRange)。 - /// - [SugarColumn(IsNullable = true)] - public double? LowerBound { get; set; } - - /// - /// 上限 (用于 InRange, OutOfRange)。 - /// - [SugarColumn(IsNullable = true)] - public double? UpperBound { get; set; } - // --- 动作部分 --- /// diff --git a/DMS.Infrastructure/Profiles/MappingProfile.cs b/DMS.Infrastructure/Profiles/MappingProfile.cs index ce7aa6a..0e8892c 100644 --- a/DMS.Infrastructure/Profiles/MappingProfile.cs +++ b/DMS.Infrastructure/Profiles/MappingProfile.cs @@ -46,6 +46,7 @@ public class MappingProfile : Profile // --- 触发器映射 --- CreateMap() + .ForMember(dest => dest.Variables, opt => opt.Ignore()) // 忽略Variables属性映射,因为可能需要特殊处理 .ReverseMap(); } } diff --git a/DMS.WPF/App.xaml b/DMS.WPF/App.xaml index 53958a4..cba873f 100644 --- a/DMS.WPF/App.xaml +++ b/DMS.WPF/App.xaml @@ -38,12 +38,6 @@ /Assets/Fonts/materialdesignicons-webfont.ttf#Material Design Icons - - - - - - diff --git a/DMS.WPF/ItemViewModel/TriggerItem.cs b/DMS.WPF/ItemViewModel/TriggerItem.cs index adf57b9..f567735 100644 --- a/DMS.WPF/ItemViewModel/TriggerItem.cs +++ b/DMS.WPF/ItemViewModel/TriggerItem.cs @@ -16,6 +16,12 @@ namespace DMS.WPF.ItemViewModel [ObservableProperty] private int _id; + /// + /// 触发器名称 + /// + [ObservableProperty] + private string _name = string.Empty; + /// /// 触发器描述 /// @@ -28,30 +34,6 @@ namespace DMS.WPF.ItemViewModel [ObservableProperty] private bool _isActive; - /// - /// 触发条件类型 - /// - [ObservableProperty] - private ConditionType _condition; - - /// - /// 阈值 (用于 GreaterThan, LessThan, EqualTo, NotEqualTo) - /// - [ObservableProperty] - private double? _threshold; - - /// - /// 下限 (用于 InRange, OutOfRange) - /// - [ObservableProperty] - private double? _lowerBound; - - /// - /// 上限 (用于 InRange, OutOfRange) - /// - [ObservableProperty] - private double? _upperBound; - /// /// 动作类型 /// diff --git a/DMS.WPF/Profiles/MappingProfile.cs b/DMS.WPF/Profiles/MappingProfile.cs index 39145bc..82d1157 100644 --- a/DMS.WPF/Profiles/MappingProfile.cs +++ b/DMS.WPF/Profiles/MappingProfile.cs @@ -38,6 +38,11 @@ namespace DMS.WPF.Profiles CreateMap() .ReverseMap(); CreateMap().ReverseMap(); + + // 添加触发器相关映射 + CreateMap() + .ForMember(dest => dest.Variables, opt => opt.Ignore()) // 忽略Variables属性,因为这个通常在业务逻辑中处理 + .ReverseMap(); } } } diff --git a/DMS.WPF/ViewModels/Dialogs/TriggerDialogViewModel.cs b/DMS.WPF/ViewModels/Dialogs/TriggerDialogViewModel.cs index e4ca98f..7ecad9a 100644 --- a/DMS.WPF/ViewModels/Dialogs/TriggerDialogViewModel.cs +++ b/DMS.WPF/ViewModels/Dialogs/TriggerDialogViewModel.cs @@ -19,26 +19,14 @@ namespace DMS.WPF.ViewModels.Dialogs /// public partial class TriggerDialogViewModel : DialogViewModelBase { - private readonly IVariableAppService _variableAppService; // To populate variable selection dropdown + private readonly IDialogService _dialogService; private readonly IDataStorageService _dataStorageService; private readonly INotificationService _notificationService; - [ObservableProperty] - private string _searchText = ""; - [ObservableProperty] private TriggerItem _trigger = new(); - [ObservableProperty] - private List _availableVariables = new(); - - [ObservableProperty] - private ObservableCollection _selectedVariables = new(); - - [ObservableProperty] - private ObservableCollection _filteredVariables = new(); - // Properties for easier binding in XAML for SendEmail action config [ObservableProperty] private string _emailRecipients = ""; @@ -50,78 +38,16 @@ namespace DMS.WPF.ViewModels.Dialogs private string _emailBodyTemplate = ""; public TriggerDialogViewModel( - IVariableAppService variableAppService, IDialogService dialogService, IDataStorageService dataStorageService, INotificationService notificationService) { - _variableAppService = variableAppService ?? throw new ArgumentNullException(nameof(variableAppService)); _dialogService = dialogService ?? throw new ArgumentNullException(nameof(dialogService)); _dataStorageService = dataStorageService; _notificationService = notificationService ?? throw new ArgumentNullException(nameof(notificationService)); } - partial void OnSearchTextChanged(string searchText) - { - UpdateFilteredVariables(); - } - - private void UpdateFilteredVariables() - { - FilteredVariables.Clear(); - - // 如果没有搜索文本,显示所有可用变量 - if (string.IsNullOrEmpty(SearchText)) - { - foreach (var variable in AvailableVariables) - { - // 只显示未被选中的变量 - if (!SelectedVariables.Contains(variable)) - { - FilteredVariables.Add(variable); - } - } - } - else - { - // 根据搜索文本过滤变量 - foreach (var variable in AvailableVariables) - { - // 只显示未被选中的变量且名称包含搜索文本的变量 - if (!SelectedVariables.Contains(variable) && - variable.Name.Contains(SearchText, StringComparison.OrdinalIgnoreCase)) - { - FilteredVariables.Add(variable); - } - } - } - } - - /// - /// 将变量添加到选中列表 - /// - /// 要添加的变量 - public void AddVariable(VariableItem variable) - { - if (!SelectedVariables.Contains(variable)) - { - SelectedVariables.Add(variable); - UpdateFilteredVariables(); - } - } - - /// - /// 从选中列表中移除变量 - /// - /// 要移除的变量 - public void RemoveVariable(VariableItem variable) - { - if (SelectedVariables.Contains(variable)) - { - SelectedVariables.Remove(variable); - UpdateFilteredVariables(); - } - } + /// /// 初始化视图模型(传入待编辑的触发器) @@ -134,25 +60,6 @@ namespace DMS.WPF.ViewModels.Dialogs Trigger = triggerItemViewModel; Title = Trigger.Id == default(int) ? "新建触发器" : "编辑触发器"; PrimaryButText = "保存"; - - // Load available variables for selection dropdown - await LoadVariablesAsync(); - - // Load selected variables - if (Trigger.VariableIds != null && Trigger.VariableIds.Any()) - { - foreach (var variableId in Trigger.VariableIds) - { - var variable = AvailableVariables.FirstOrDefault(v => v.Id == variableId); - if (variable != null) - { - SelectedVariables.Add(variable); - } - } - } - - // 初始化过滤后的变量列表 - UpdateFilteredVariables(); // Parse action configuration if it's SendEmail if (Trigger.Action == ActionType.SendEmail && !string.IsNullOrEmpty(Trigger.ActionConfigurationJson)) @@ -179,23 +86,7 @@ namespace DMS.WPF.ViewModels.Dialogs } } - /// - /// 加载所有可用的变量 - /// - private async Task LoadVariablesAsync() - { - try - { - // 使用数据存储服务中的变量列表 - AvailableVariables = new List(_dataStorageService.Variables.Select(kvp => kvp.Value)); - UpdateFilteredVariables(); - } - catch (Exception ex) - { - _notificationService.ShowError($"加载变量列表失败: {ex.Message}"); - AvailableVariables = new List(); - } - } + /// /// 保存按钮点击命令 @@ -203,10 +94,9 @@ namespace DMS.WPF.ViewModels.Dialogs [RelayCommand] private async Task SaveAsync() { - // Basic validation - if (SelectedVariables == null || !SelectedVariables.Any()) + if (string.IsNullOrWhiteSpace(Trigger.Name)) { - _notificationService.ShowWarn("请至少选择一个关联的变量"); + _notificationService.ShowWarn("请输入触发器名称"); return; } @@ -216,39 +106,7 @@ namespace DMS.WPF.ViewModels.Dialogs return; } - // 设置选中的变量ID - foreach (var selectedVariable in SelectedVariables) - { - Trigger.VariableIds.Add(selectedVariable.Id); - } - // Validate condition-specific fields - switch (Trigger.Condition) - { - case ConditionType.GreaterThan: - case ConditionType.LessThan: - case ConditionType.EqualTo: - case ConditionType.NotEqualTo: - if (!Trigger.Threshold.HasValue) - { - _notificationService.ShowWarn($"{Trigger.Condition} 条件需要设置阈值"); - return; - } - break; - case ConditionType.InRange: - case ConditionType.OutOfRange: - if (!Trigger.LowerBound.HasValue || !Trigger.UpperBound.HasValue) - { - _notificationService.ShowWarn($"{Trigger.Condition} 条件需要设置下限和上限"); - return; - } - if (Trigger.LowerBound > Trigger.UpperBound) - { - _notificationService.ShowWarn("下限必须小于或等于上限"); - return; - } - break; - } // Prepare action configuration based on selected action type if (Trigger.Action == ActionType.SendEmail) diff --git a/DMS.WPF/ViewModels/TriggersViewModel.cs b/DMS.WPF/ViewModels/TriggersViewModel.cs index ae11ce1..9013883 100644 --- a/DMS.WPF/ViewModels/TriggersViewModel.cs +++ b/DMS.WPF/ViewModels/TriggersViewModel.cs @@ -57,7 +57,6 @@ namespace DMS.WPF.ViewModels var newTrigger = new TriggerItem() { IsActive = true, - Condition = Core.Models.Triggers.ConditionType.GreaterThan, Action = Core.Models.Triggers.ActionType.SendEmail, Description = "新建触发器", CreatedAt = DateTime.UtcNow, diff --git a/DMS.WPF/Views/Dialogs/TriggerDialog.xaml b/DMS.WPF/Views/Dialogs/TriggerDialog.xaml index 5d9eac9..00a3467 100644 --- a/DMS.WPF/Views/Dialogs/TriggerDialog.xaml +++ b/DMS.WPF/Views/Dialogs/TriggerDialog.xaml @@ -27,8 +27,6 @@ - @@ -39,6 +37,15 @@ + + + - private void VariableListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e) - { - if (VariableListBox.SelectedItem is VariableItem selectedVariable) - { - var viewModel = DataContext as ViewModels.Dialogs.TriggerDialogViewModel; - viewModel?.AddVariable(selectedVariable); - } - } - - /// - /// 处理移除变量按钮点击事件 - /// - private void RemoveVariableButton_Click(object sender, System.Windows.RoutedEventArgs e) - { - if (sender is System.Windows.Controls.Button button && button.Tag is VariableItem variable) - { - var viewModel = DataContext as ViewModels.Dialogs.TriggerDialogViewModel; - viewModel?.RemoveVariable(variable); - } - } + } } \ No newline at end of file