From eb4bee8963945d4c9c3504d9c3537c1d80916da8 Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Tue, 23 Sep 2025 09:28:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=A7=A6=E5=8F=91=E5=99=A8?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E7=9A=84=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Management/ITriggerManagementService.cs | 2 +- DMS.Application/Services/DataLoaderService.cs | 1 + .../Management/TriggerManagementService.cs | 30 ++++++++++++++++--- .../Triggers/Impl/TriggerEvaluationService.cs | 1 + .../EmptyCollectionToVisibilityConverter.cs | 29 ++++++++++++++++++ DMS.WPF/Views/Dialogs/TriggerDialog.xaml | 3 +- 6 files changed, 60 insertions(+), 6 deletions(-) rename DMS.Application/{Services => Interfaces}/Management/ITriggerManagementService.cs (97%) create mode 100644 DMS.WPF/Converters/EmptyCollectionToVisibilityConverter.cs diff --git a/DMS.Application/Services/Management/ITriggerManagementService.cs b/DMS.Application/Interfaces/Management/ITriggerManagementService.cs similarity index 97% rename from DMS.Application/Services/Management/ITriggerManagementService.cs rename to DMS.Application/Interfaces/Management/ITriggerManagementService.cs index 4ababf4..7535cba 100644 --- a/DMS.Application/Services/Management/ITriggerManagementService.cs +++ b/DMS.Application/Interfaces/Management/ITriggerManagementService.cs @@ -1,6 +1,6 @@ using DMS.Application.DTOs; -namespace DMS.Application.Services.Management +namespace DMS.Application.Interfaces.Management { /// /// 触发器管理服务接口 (负责 CRUD 操作) diff --git a/DMS.Application/Services/DataLoaderService.cs b/DMS.Application/Services/DataLoaderService.cs index 6c934a2..b9b472e 100644 --- a/DMS.Application/Services/DataLoaderService.cs +++ b/DMS.Application/Services/DataLoaderService.cs @@ -5,6 +5,7 @@ using DMS.Core.Interfaces; using System.Collections.Concurrent; using DMS.Application.Events; using DMS.Application.Interfaces.Database; +using DMS.Application.Interfaces.Management; using DMS.Application.Services.Management; using DMS.Core.Models; using DMS.Core.Models.Triggers; diff --git a/DMS.Application/Services/Management/TriggerManagementService.cs b/DMS.Application/Services/Management/TriggerManagementService.cs index 15cedd1..fbe1b2f 100644 --- a/DMS.Application/Services/Management/TriggerManagementService.cs +++ b/DMS.Application/Services/Management/TriggerManagementService.cs @@ -1,5 +1,7 @@ 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; @@ -13,11 +15,13 @@ namespace DMS.Application.Services.Management { private readonly IRepositoryManager _repositoryManager; private readonly IMapper _mapper; + private readonly IAppDataStorageService _appDataStorageService; - public TriggerManagementService(IRepositoryManager repositoryManager, IMapper mapper) + public TriggerManagementService(IRepositoryManager repositoryManager, IMapper mapper, IAppDataStorageService appDataStorageService) { _repositoryManager = repositoryManager ?? throw new ArgumentNullException(nameof(repositoryManager)); _mapper = mapper ?? throw new ArgumentNullException(nameof(mapper)); + _appDataStorageService = appDataStorageService ?? throw new ArgumentNullException(nameof(appDataStorageService)); } /// @@ -55,7 +59,12 @@ namespace DMS.Application.Services.Management var createdTrigger = await _repositoryManager.Triggers.AddAsync(triggerEntity); // 4. 转换回 DTO 并返回 - return _mapper.Map(createdTrigger); + var result = _mapper.Map(createdTrigger); + + // 5. 同步更新AppDataStorageService中的Triggers字典 + _appDataStorageService.Triggers[result.Id] = result; + + return result; } /// @@ -81,7 +90,12 @@ namespace DMS.Application.Services.Management return null; // 5. 转换回 DTO 并返回 - return _mapper.Map(updatedTrigger); + var result = _mapper.Map(updatedTrigger); + + // 6. 同步更新AppDataStorageService中的Triggers字典 + _appDataStorageService.Triggers[result.Id] = result; + + return result; } /// @@ -89,7 +103,15 @@ namespace DMS.Application.Services.Management /// public async Task DeleteTriggerAsync(int id) { - return await _repositoryManager.Triggers.DeleteAsync(id); + var result = await _repositoryManager.Triggers.DeleteAsync(id); + + // 如果删除成功,也从AppDataStorageService中的Triggers字典中移除 + if (result) + { + _appDataStorageService.Triggers.TryRemove(id, out _); + } + + return result; } /// diff --git a/DMS.Application/Services/Triggers/Impl/TriggerEvaluationService.cs b/DMS.Application/Services/Triggers/Impl/TriggerEvaluationService.cs index 07a019a..0ce4a1d 100644 --- a/DMS.Application/Services/Triggers/Impl/TriggerEvaluationService.cs +++ b/DMS.Application/Services/Triggers/Impl/TriggerEvaluationService.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using DMS.Application.DTOs; +using DMS.Application.Interfaces.Management; using DMS.Application.Services.Management; // 明确指定 Timer 类型,避免歧义 using ThreadingTimer = System.Threading.Timer; diff --git a/DMS.WPF/Converters/EmptyCollectionToVisibilityConverter.cs b/DMS.WPF/Converters/EmptyCollectionToVisibilityConverter.cs new file mode 100644 index 0000000..aeee028 --- /dev/null +++ b/DMS.WPF/Converters/EmptyCollectionToVisibilityConverter.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections; +using System.Globalization; +using System.Windows; +using System.Windows.Data; + +namespace DMS.WPF.Converters +{ + /// + /// 当集合为空时返回Visibility.Visible,否则返回Visibility.Collapsed + /// + public class EmptyCollectionToVisibilityConverter : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + if (value is ICollection collection) + { + return collection.Count == 0 ? Visibility.Visible : Visibility.Collapsed; + } + + return Visibility.Visible; + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/DMS.WPF/Views/Dialogs/TriggerDialog.xaml b/DMS.WPF/Views/Dialogs/TriggerDialog.xaml index 5b9c19e..33e2326 100644 --- a/DMS.WPF/Views/Dialogs/TriggerDialog.xaml +++ b/DMS.WPF/Views/Dialogs/TriggerDialog.xaml @@ -26,6 +26,7 @@ + + Visibility="{Binding SelectedVariables, Converter={StaticResource EmptyCollectionToVisibilityConverter}}"/>