修改使用TriggerDataService模式

This commit is contained in:
2025-09-23 06:51:29 +08:00
parent 87942a45c8
commit 2d457ae248
20 changed files with 335 additions and 86 deletions

View File

@@ -2,6 +2,7 @@ using System.Collections.Concurrent;
using DMS.Application.DTOs; using DMS.Application.DTOs;
using DMS.Application.Interfaces.Management; using DMS.Application.Interfaces.Management;
using DMS.Application.Services; using DMS.Application.Services;
using DMS.Application.Services.Management;
using DMS.Core.Models; using DMS.Core.Models;
namespace DMS.Application.Interfaces; namespace DMS.Application.Interfaces;
@@ -17,5 +18,6 @@ public interface IAppDataCenterService
IVariableManagementService VariableManagementService { get; set; } IVariableManagementService VariableManagementService { get; set; }
IVariableTableManagementService VariableTableManagementService { get; set; } IVariableTableManagementService VariableTableManagementService { get; set; }
IDeviceManagementService DeviceManagementService { get; set; } IDeviceManagementService DeviceManagementService { get; set; }
ITriggerManagementService TriggerManagementService { get; set; } // 添加触发器管理服务
IDataLoaderService DataLoaderService { get; set; } IDataLoaderService DataLoaderService { get; set; }
} }

View File

@@ -12,6 +12,7 @@ using System;
using System.Linq; using System.Linq;
using DMS.Application.Events; using DMS.Application.Events;
using DMS.Application.Interfaces.Management; using DMS.Application.Interfaces.Management;
using DMS.Application.Services.Management;
using DMS.Core.Events; using DMS.Core.Events;
namespace DMS.Application.Services; namespace DMS.Application.Services;
@@ -77,7 +78,8 @@ public class AppDataCenterService : IAppDataCenterService
IVariableManagementService variableManagementService, IVariableManagementService variableManagementService,
IMenuManagementService menuManagementService, IMenuManagementService menuManagementService,
IMqttManagementService mqttManagementService, IMqttManagementService mqttManagementService,
ILogManagementService logManagementService ILogManagementService logManagementService,
ITriggerManagementService triggerManagementService // 添加触发器管理服务
) )
{ {
_repositoryManager = repositoryManager; _repositoryManager = repositoryManager;
@@ -91,6 +93,7 @@ public class AppDataCenterService : IAppDataCenterService
MenuManagementService = menuManagementService; MenuManagementService = menuManagementService;
MqttManagementService = mqttManagementService; MqttManagementService = mqttManagementService;
LogManagementService = logManagementService; LogManagementService = logManagementService;
TriggerManagementService = triggerManagementService; // 初始化触发器管理服务
} }
public ILogManagementService LogManagementService { get; set; } public ILogManagementService LogManagementService { get; set; }
@@ -105,5 +108,7 @@ public class AppDataCenterService : IAppDataCenterService
public IDeviceManagementService DeviceManagementService { get; set; } public IDeviceManagementService DeviceManagementService { get; set; }
public ITriggerManagementService TriggerManagementService { get; set; } // 添加触发器管理服务
public IDataLoaderService DataLoaderService { get; set; } public IDataLoaderService DataLoaderService { get; set; }
} }

View File

@@ -5,6 +5,7 @@ using DMS.Core.Interfaces;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using DMS.Application.Events; using DMS.Application.Events;
using DMS.Application.Interfaces.Database; using DMS.Application.Interfaces.Database;
using DMS.Application.Services.Management;
using DMS.Core.Models; using DMS.Core.Models;
using DMS.Core.Models.Triggers; using DMS.Core.Models.Triggers;
using DMS.Application.Services.Triggers; using DMS.Application.Services.Triggers;

View File

@@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using DMS.Application.DTOs; using DMS.Application.DTOs;
namespace DMS.Application.Services.Triggers namespace DMS.Application.Services.Management
{ {
/// <summary> /// <summary>
/// 触发器管理服务接口 (负责 CRUD 操作) /// 触发器管理服务接口 (负责 CRUD 操作)

View File

@@ -1,13 +1,10 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AutoMapper; using AutoMapper;
using DMS.Application.DTOs; using DMS.Application.DTOs;
using DMS.Application.Services.Triggers; using DMS.Application.Services.Triggers;
using DMS.Core.Interfaces; using DMS.Core.Interfaces;
using DMS.Core.Models.Triggers; using DMS.Core.Models.Triggers;
namespace DMS.Application.Services.Triggers.Impl namespace DMS.Application.Services.Management
{ {
/// <summary> /// <summary>
/// 触发器管理服务实现 /// 触发器管理服务实现

View File

@@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using DMS.Application.DTOs; using DMS.Application.DTOs;
using DMS.Application.Services.Management;
// 明确指定 Timer 类型,避免歧义 // 明确指定 Timer 类型,避免歧义
using ThreadingTimer = System.Threading.Timer; using ThreadingTimer = System.Threading.Timer;
using TimersTimer = System.Timers.Timer; using TimersTimer = System.Timers.Timer;

View File

@@ -32,7 +32,6 @@ using DMS.WPF.Services;
using DMS.WPF.ViewModels; using DMS.WPF.ViewModels;
using DMS.WPF.ViewModels.Dialogs; using DMS.WPF.ViewModels.Dialogs;
using DMS.WPF.ViewModels.Items; using DMS.WPF.ViewModels.Items;
using DMS.WPF.ViewModels.Triggers;
using DMS.WPF.Views; using DMS.WPF.Views;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
@@ -304,6 +303,7 @@ public partial class App : System.Windows.Application
services.AddSingleton<IMenuDataService, MenuDataService>(); services.AddSingleton<IMenuDataService, MenuDataService>();
services.AddSingleton<IMqttDataService, MqttDataService>(); services.AddSingleton<IMqttDataService, MqttDataService>();
services.AddSingleton<ILogDataService, LogDataService>(); services.AddSingleton<ILogDataService, LogDataService>();
services.AddSingleton<ITriggerDataService, TriggerDataService>(); // 注册触发器数据服务
services.AddSingleton<IDataEventService, DataEventService>(); services.AddSingleton<IDataEventService, DataEventService>();
services.AddSingleton<IDataStorageService, DataStorageService>(); services.AddSingleton<IDataStorageService, DataStorageService>();
services.AddSingleton<IEmailDataService, EmailDataService>(); services.AddSingleton<IEmailDataService, EmailDataService>();

View File

@@ -41,4 +41,9 @@ public interface IDataStorageService
/// 日志列表。 /// 日志列表。
/// </summary> /// </summary>
ObservableCollection<NlogItemViewModel> Nlogs { get; set; } ObservableCollection<NlogItemViewModel> Nlogs { get; set; }
/// <summary>
/// 触发器列表。
/// </summary>
ObservableDictionary<int, TriggerItemViewModel> Triggers { get; set; }
} }

View File

@@ -0,0 +1,31 @@
using System.Collections.ObjectModel;
using DMS.Application.DTOs;
using DMS.WPF.ViewModels.Items;
namespace DMS.WPF.Interfaces;
/// <summary>
/// 触发器数据服务接口。
/// </summary>
public interface ITriggerDataService
{
/// <summary>
/// 加载所有触发器数据。
/// </summary>
void LoadAllTriggers();
/// <summary>
/// 添加触发器。
/// </summary>
Task<TriggerItemViewModel> AddTrigger(TriggerItemViewModel dto);
/// <summary>
/// 删除触发器。
/// </summary>
Task<bool> DeleteTrigger(TriggerItemViewModel trigger);
/// <summary>
/// 更新触发器。
/// </summary>
Task<bool> UpdateTrigger(TriggerItemViewModel trigger);
}

View File

@@ -36,4 +36,8 @@ public interface IWPFDataService
/// </summary> /// </summary>
ILogDataService LogDataService { get; } ILogDataService LogDataService { get; }
/// <summary>
/// 触发器数据服务。
/// </summary>
ITriggerDataService TriggerDataService { get; }
} }

View File

@@ -45,6 +45,11 @@ public class DataStorageService : IDataStorageService
/// </summary> /// </summary>
public ObservableCollection<NlogItemViewModel> Nlogs { get; set; } public ObservableCollection<NlogItemViewModel> Nlogs { get; set; }
/// <summary>
/// 触发器列表。
/// </summary>
public ObservableDictionary<int, TriggerItemViewModel> Triggers { get; set; }
public DataStorageService() public DataStorageService()
{ {
Devices=new ObservableDictionary<int,DeviceItemViewModel>(); Devices=new ObservableDictionary<int,DeviceItemViewModel>();
@@ -54,6 +59,7 @@ public class DataStorageService : IDataStorageService
Menus=new ObservableCollection<MenuItemViewModel>(); Menus=new ObservableCollection<MenuItemViewModel>();
MenuTrees=new ObservableCollection<MenuItemViewModel>(); MenuTrees=new ObservableCollection<MenuItemViewModel>();
Nlogs=new ObservableCollection<NlogItemViewModel>(); Nlogs=new ObservableCollection<NlogItemViewModel>();
Triggers = new ObservableDictionary<int, TriggerItemViewModel>();
} }

View File

@@ -1,12 +1,6 @@
using DMS.WPF.Interfaces;
using DMS.WPF.ViewModels.Dialogs; using DMS.WPF.ViewModels.Dialogs;
using DMS.WPF.Views.Dialogs; using DMS.WPF.Views.Dialogs;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows;
using DMS.WPF.Interfaces;
using DMS.WPF.ViewModels.Triggers;
using DMS.WPF.Views;
using iNKORE.UI.WPF.Modern.Controls; using iNKORE.UI.WPF.Modern.Controls;
namespace DMS.WPF.Services namespace DMS.WPF.Services

View File

@@ -1,7 +1,6 @@
using DMS.WPF.Interfaces; using DMS.WPF.Interfaces;
using DMS.WPF.ViewModels; using DMS.WPF.ViewModels;
using DMS.WPF.ViewModels.Items; using DMS.WPF.ViewModels.Items;
using DMS.WPF.ViewModels.Triggers;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
namespace DMS.WPF.Services; namespace DMS.WPF.Services;

View File

@@ -0,0 +1,119 @@
using System.Windows.Threading;
using AutoMapper;
using DMS.Application.DTOs;
using DMS.Application.Interfaces;
using DMS.Core.Events;
using DMS.WPF.Interfaces;
using DMS.WPF.ViewModels.Items;
using Opc.Ua;
namespace DMS.WPF.Services;
/// <summary>
/// 触发器数据服务类,负责管理触发器相关的数据和操作。
/// </summary>
public class TriggerDataService : ITriggerDataService
{
private readonly IMapper _mapper;
private readonly IAppDataCenterService _appDataCenterService;
private readonly IAppDataStorageService _appDataStorageService;
private readonly IDataStorageService _dataStorageService;
private readonly IEventService _eventService;
private readonly INotificationService _notificationService;
private readonly Dispatcher _uiDispatcher;
/// <summary>
/// TriggerDataService类的构造函数。
/// </summary>
/// <param name="mapper">AutoMapper 实例。</param>
/// <param name="appDataCenterService">数据服务中心实例。</param>
/// <param name="appDataStorageService">应用数据存储服务实例。</param>
/// <param name="dataStorageService">数据存储服务实例。</param>
/// <param name="eventService">事件服务实例。</param>
/// <param name="notificationService">通知服务实例。</param>
public TriggerDataService(IMapper mapper, IAppDataCenterService appDataCenterService,
IAppDataStorageService appDataStorageService, IDataStorageService dataStorageService,
IEventService eventService, INotificationService notificationService)
{
_mapper = mapper;
_appDataCenterService = appDataCenterService;
_appDataStorageService = appDataStorageService;
_dataStorageService = dataStorageService;
_eventService = eventService;
_notificationService = notificationService;
_uiDispatcher = Dispatcher.CurrentDispatcher;
}
/// <summary>
/// 加载所有触发器数据。
/// </summary>
public void LoadAllTriggers()
{
foreach (var triggerDto in _appDataStorageService.Triggers.Values)
{
_dataStorageService.Triggers.Add(triggerDto.Id, _mapper.Map<TriggerItemViewModel>(triggerDto));
}
}
/// <summary>
/// 添加触发器。
/// </summary>
public async Task<TriggerItemViewModel> AddTrigger(TriggerItemViewModel dto)
{
// 添加null检查
if (dto == null)
return null;
var addDto
= await _appDataCenterService.TriggerManagementService.CreateTriggerAsync(
_mapper.Map<TriggerDefinitionDto>(dto));
// 添加null检查
if (addDto == null)
{
return null;
}
// 给界面添加触发器
var addItem = _mapper.Map<TriggerItemViewModel>(addDto);
_dataStorageService.Triggers.Add(addDto.Id, addItem);
return addItem;
}
/// <summary>
/// 删除触发器。
/// </summary>
public async Task<bool> DeleteTrigger(TriggerItemViewModel trigger)
{
// 从数据库删除触发器数据
if (!await _appDataCenterService.TriggerManagementService.DeleteTriggerAsync(trigger.Id))
{
return false;
}
// 从界面删除触发器
_dataStorageService.Triggers.Remove(trigger.Id);
return true;
}
/// <summary>
/// 更新触发器。
/// </summary>
public async Task<bool> UpdateTrigger(TriggerItemViewModel trigger)
{
if (!_appDataStorageService.Triggers.TryGetValue(trigger.Id, out var triggerDto))
{
return false;
}
_mapper.Map(trigger, triggerDto);
if (await _appDataCenterService.TriggerManagementService.UpdateTriggerAsync(trigger.Id, triggerDto) != null)
{
return true;
}
return false;
}
}

View File

@@ -44,6 +44,11 @@ public class WPFDataService : IWPFDataService
/// </summary> /// </summary>
public ILogDataService LogDataService { get; } public ILogDataService LogDataService { get; }
/// <summary>
/// 触发器数据服务。
/// </summary>
public ITriggerDataService TriggerDataService { get; }
/// <summary> /// <summary>
/// WPFDataService 构造函数。 /// WPFDataService 构造函数。
/// </summary> /// </summary>
@@ -55,7 +60,8 @@ public class WPFDataService : IWPFDataService
IMenuDataService menuDataService, IMenuDataService menuDataService,
IMqttDataService mqttDataService, IMqttDataService mqttDataService,
ILogDataService logDataService, ILogDataService logDataService,
IVariableTableDataService variableTableDataService) IVariableTableDataService variableTableDataService,
ITriggerDataService triggerDataService)
{ {
_mapper = mapper; _mapper = mapper;
_appDataCenterService = appDataCenterService; _appDataCenterService = appDataCenterService;
@@ -65,5 +71,6 @@ public class WPFDataService : IWPFDataService
MqttDataService = mqttDataService; MqttDataService = mqttDataService;
LogDataService = logDataService; LogDataService = logDataService;
VariableTableDataService = variableTableDataService; VariableTableDataService = variableTableDataService;
TriggerDataService = triggerDataService;
} }
} }

View File

@@ -17,7 +17,7 @@ namespace DMS.WPF.ViewModels.Dialogs
/// <summary> /// <summary>
/// 触发器编辑器视图模型 /// 触发器编辑器视图模型
/// </summary> /// </summary>
public partial class TriggerDialogViewModel : DialogViewModelBase<TriggerDefinitionDto?> public partial class TriggerDialogViewModel : DialogViewModelBase<TriggerItemViewModel?>
{ {
private readonly IVariableAppService _variableAppService; // To populate variable selection dropdown private readonly IVariableAppService _variableAppService; // To populate variable selection dropdown
private readonly IDialogService _dialogService; private readonly IDialogService _dialogService;
@@ -28,7 +28,7 @@ namespace DMS.WPF.ViewModels.Dialogs
private string _searchText = ""; private string _searchText = "";
[ObservableProperty] [ObservableProperty]
private TriggerDefinitionDto _trigger = new(); private TriggerItemViewModel _trigger = new();
[ObservableProperty] [ObservableProperty]
private List<VariableItemViewModel> _availableVariables = new(); private List<VariableItemViewModel> _availableVariables = new();
@@ -129,9 +129,9 @@ namespace DMS.WPF.ViewModels.Dialogs
/// <param name="parameter">待编辑的触发器 DTO</param> /// <param name="parameter">待编辑的触发器 DTO</param>
public async Task OnInitializedAsync(object? parameter) public async Task OnInitializedAsync(object? parameter)
{ {
if (parameter is TriggerDefinitionDto triggerDto) if (parameter is TriggerItemViewModel triggerItemViewModel)
{ {
Trigger = triggerDto; Trigger = triggerItemViewModel;
Title = Trigger.Id == default(int) ? "新建触发器" : "编辑触发器"; Title = Trigger.Id == default(int) ? "新建触发器" : "编辑触发器";
PrimaryButText = "保存"; PrimaryButText = "保存";
@@ -217,7 +217,10 @@ namespace DMS.WPF.ViewModels.Dialogs
} }
// 设置选中的变量ID // 设置选中的变量ID
Trigger.VariableIds = SelectedVariables.Select(v => v.Id).ToList(); foreach (var selectedVariable in SelectedVariables)
{
Trigger.VariableIds.Add(selectedVariable.Id);
}
// Validate condition-specific fields // Validate condition-specific fields
switch (Trigger.Condition) switch (Trigger.Condition)

View File

@@ -0,0 +1,96 @@
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using DMS.Application.DTOs;
using DMS.Core.Models.Triggers;
namespace DMS.WPF.ViewModels.Items
{
/// <summary>
/// 触发器项视图模型
/// </summary>
public partial class TriggerItemViewModel : ObservableObject
{
/// <summary>
/// 触发器唯一标识符
/// </summary>
[ObservableProperty]
private int _id;
/// <summary>
/// 触发器描述
/// </summary>
[ObservableProperty]
private string _description = string.Empty;
/// <summary>
/// 触发器是否处于激活状态
/// </summary>
[ObservableProperty]
private bool _isActive;
/// <summary>
/// 触发条件类型
/// </summary>
[ObservableProperty]
private ConditionType _condition;
/// <summary>
/// 阈值 (用于 GreaterThan, LessThan, EqualTo, NotEqualTo)
/// </summary>
[ObservableProperty]
private double? _threshold;
/// <summary>
/// 下限 (用于 InRange, OutOfRange)
/// </summary>
[ObservableProperty]
private double? _lowerBound;
/// <summary>
/// 上限 (用于 InRange, OutOfRange)
/// </summary>
[ObservableProperty]
private double? _upperBound;
/// <summary>
/// 动作类型
/// </summary>
[ObservableProperty]
private ActionType _action;
/// <summary>
/// 动作配置 JSON 字符串
/// </summary>
[ObservableProperty]
private string _actionConfigurationJson = string.Empty;
/// <summary>
/// 抑制持续时间
/// </summary>
[ObservableProperty]
private TimeSpan? _suppressionDuration;
/// <summary>
/// 上次触发的时间
/// </summary>
[ObservableProperty]
private DateTime? _lastTriggeredAt;
/// <summary>
/// 创建时间
/// </summary>
[ObservableProperty]
private DateTime _createdAt;
/// <summary>
/// 最后更新时间
/// </summary>
[ObservableProperty]
private DateTime _updatedAt;
/// <summary>
/// 关联的变量 ID 列表
/// </summary>
public ObservableCollection<int> VariableIds { get; } = new ObservableCollection<int>();
}
}

View File

@@ -1,59 +1,47 @@
using System;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Input;
using DMS.Application.DTOs; using DMS.Application.DTOs;
using DMS.Application.Services.Triggers;
using DMS.WPF.Interfaces; using DMS.WPF.Interfaces;
using DMS.WPF.Services;
using DMS.WPF.ViewModels.Dialogs; using DMS.WPF.ViewModels.Dialogs;
using DMS.WPF.ViewModels.Items;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using ObservableCollections;
namespace DMS.WPF.ViewModels.Triggers namespace DMS.WPF.ViewModels
{ {
/// <summary> /// <summary>
/// 触发器管理视图模型 /// 触发器管理视图模型
/// </summary> /// </summary>
public partial class TriggersViewModel : ViewModelBase public partial class TriggersViewModel : ViewModelBase
{ {
private readonly ITriggerManagementService _triggerManagementService; private readonly ITriggerDataService _triggerDataService;
private readonly IDataStorageService _dataStorageService;
private readonly IDialogService _dialogService; private readonly IDialogService _dialogService;
private readonly INotificationService _notificationService; private readonly INotificationService _notificationService;
[ObservableProperty] [ObservableProperty]
private ObservableCollection<TriggerDefinitionDto> _triggers = new(); private ObservableDictionary<int, TriggerItemViewModel> _triggers ;
[ObservableProperty] [ObservableProperty]
private TriggerDefinitionDto? _selectedTrigger; private TriggerItemViewModel? _selectedTrigger;
public TriggersViewModel( public TriggersViewModel(
ITriggerManagementService triggerManagementService, ITriggerDataService triggerDataService,
IDataStorageService dataStorageService,
IDialogService dialogService, IDialogService dialogService,
INotificationService notificationService) INotificationService notificationService)
{ {
_triggerManagementService = triggerManagementService ?? throw new ArgumentNullException(nameof(triggerManagementService)); _triggerDataService = triggerDataService ?? throw new ArgumentNullException(nameof(triggerDataService));
_dataStorageService = dataStorageService ?? throw new ArgumentNullException(nameof(dataStorageService));
_dialogService = dialogService ?? throw new ArgumentNullException(nameof(dialogService)); _dialogService = dialogService ?? throw new ArgumentNullException(nameof(dialogService));
_notificationService = notificationService ?? throw new ArgumentNullException(nameof(notificationService)); _notificationService = notificationService ?? throw new ArgumentNullException(nameof(notificationService));
// 初始化时加载触发器数据
Triggers=_dataStorageService.Triggers;
} }
/// <summary>
/// 加载所有触发器
/// </summary>
[RelayCommand]
private async Task LoadTriggersAsync()
{
try
{
var triggerList = await _triggerManagementService.GetAllTriggersAsync();
Triggers = new ObservableCollection<TriggerDefinitionDto>(triggerList);
}
catch (Exception ex)
{
_notificationService.ShowError($"加载触发器失败: {ex.Message}");
}
}
/// <summary> /// <summary>
/// 添加新触发器 /// 添加新触发器
@@ -61,14 +49,14 @@ namespace DMS.WPF.ViewModels.Triggers
[RelayCommand] [RelayCommand]
private async Task AddTriggerAsync() private async Task AddTriggerAsync()
{ {
var newTrigger = new TriggerDefinitionDto var newTrigger = new TriggerItemViewModel()
{ {
IsActive = true, IsActive = true,
Condition = Core.Models.Triggers.ConditionType.GreaterThan, Condition = Core.Models.Triggers.ConditionType.GreaterThan,
Action = Core.Models.Triggers.ActionType.SendEmail, Action = Core.Models.Triggers.ActionType.SendEmail,
Description = "新建触发器", Description = "新建触发器",
CreatedAt = DateTime.UtcNow, CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow UpdatedAt = DateTime.UtcNow
}; };
TriggerDialogViewModel viewModel = App.Current.Services.GetRequiredService<TriggerDialogViewModel>(); TriggerDialogViewModel viewModel = App.Current.Services.GetRequiredService<TriggerDialogViewModel>();
@@ -79,10 +67,18 @@ namespace DMS.WPF.ViewModels.Triggers
{ {
try try
{ {
var createdTrigger = await _triggerManagementService.CreateTriggerAsync(result); // 使用TriggerDataService添加触发器
Triggers.Add(createdTrigger); var createdTrigger = await _triggerDataService.AddTrigger(newTrigger);
SelectedTrigger = createdTrigger;
_notificationService.ShowSuccess("触发器创建成功"); if (createdTrigger != null )
{
// 触发器已添加到数据存储中,只需更新本地集合
_notificationService.ShowSuccess("触发器创建成功");
}
else
{
_notificationService.ShowError("触发器创建失败");
}
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -121,6 +117,7 @@ namespace DMS.WPF.ViewModels.Triggers
CreatedAt = SelectedTrigger.CreatedAt, CreatedAt = SelectedTrigger.CreatedAt,
UpdatedAt = SelectedTrigger.UpdatedAt UpdatedAt = SelectedTrigger.UpdatedAt
}; };
TriggerDialogViewModel viewModel = App.Current.Services.GetRequiredService<TriggerDialogViewModel>(); TriggerDialogViewModel viewModel = App.Current.Services.GetRequiredService<TriggerDialogViewModel>();
await viewModel.OnInitializedAsync(triggerToEdit); await viewModel.OnInitializedAsync(triggerToEdit);
@@ -129,20 +126,15 @@ namespace DMS.WPF.ViewModels.Triggers
{ {
try try
{ {
var updatedTrigger = await _triggerManagementService.UpdateTriggerAsync(result.Id, result); // 使用TriggerDataService更新触发器
if (updatedTrigger != null) var updatedTrigger = await _triggerDataService.UpdateTrigger(SelectedTrigger);
if (updatedTrigger)
{ {
var index = Triggers.IndexOf(SelectedTrigger);
if (index >= 0)
{
Triggers[index] = updatedTrigger;
}
SelectedTrigger = updatedTrigger;
_notificationService.ShowSuccess("触发器更新成功"); _notificationService.ShowSuccess("触发器更新成功");
} }
else else
{ {
_notificationService.ShowError("触发器更新失败,未找到对应记录"); _notificationService.ShowError("触发器更新失败");
} }
} }
catch (Exception ex) catch (Exception ex)
@@ -169,11 +161,10 @@ namespace DMS.WPF.ViewModels.Triggers
{ {
try try
{ {
var success = await _triggerManagementService.DeleteTriggerAsync(SelectedTrigger.Id); // 使用TriggerDataService删除触发器
var success = await _triggerDataService.DeleteTrigger(SelectedTrigger);
if (success) if (success)
{ {
Triggers.Remove(SelectedTrigger);
SelectedTrigger = Triggers.FirstOrDefault();
_notificationService.ShowSuccess("触发器删除成功"); _notificationService.ShowSuccess("触发器删除成功");
} }
else else
@@ -188,13 +179,5 @@ namespace DMS.WPF.ViewModels.Triggers
} }
} }
/// <summary>
/// 视图加载时执行的命令
/// </summary>
[RelayCommand]
private async Task OnLoadedAsync()
{
await LoadTriggersCommand.ExecuteAsync(null);
}
} }
} }

View File

@@ -12,7 +12,6 @@
xmlns:tb="http://hardcodet.net/taskbar" xmlns:tb="http://hardcodet.net/taskbar"
xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern" xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern"
xmlns:vm="clr-namespace:DMS.WPF.ViewModels" xmlns:vm="clr-namespace:DMS.WPF.ViewModels"
xmlns:vmt="clr-namespace:DMS.WPF.ViewModels.Triggers"
Title="设备管理系统" Title="设备管理系统"
Width="1080" Width="1080"
Height="800" Height="800"
@@ -114,7 +113,7 @@
<local:EmailManagementView /> <local:EmailManagementView />
</DataTemplate> </DataTemplate>
<!-- 触发器管理页 --> <!-- 触发器管理页 -->
<DataTemplate DataType="{x:Type vmt:TriggersViewModel}"> <DataTemplate DataType="{x:Type vm:TriggersViewModel}">
<local:TriggersView/> <local:TriggersView/>
</DataTemplate> </DataTemplate>
<!-- 设备详情页 --> <!-- 设备详情页 -->

View File

@@ -1,5 +1,5 @@
using System.Windows.Controls; using System.Windows.Controls;
using DMS.WPF.ViewModels.Triggers; using DMS.WPF.ViewModels;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
namespace DMS.WPF.Views namespace DMS.WPF.Views