From 58c934064065c82f1a7f86d6d33b3b3c8db0c65f Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Sat, 13 Sep 2025 19:39:18 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=A7=E7=BB=AD=E5=AE=8C=E6=88=90=E9=82=AE?= =?UTF-8?q?=E4=BB=B6=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DMS.WPF/App.xaml.cs | 1 + DMS.WPF/Interfaces/IEmailDataService.cs | 60 +++++ DMS.WPF/Services/DialogService.cs | 3 +- DMS.WPF/Services/EmailDataService.cs | 241 ++++++++++++++++++ .../Dialogs/EmailTemplateDialogViewModel.cs | 7 +- .../ViewModels/EmailManagementViewModel.cs | 188 ++++++++++---- 6 files changed, 449 insertions(+), 51 deletions(-) create mode 100644 DMS.WPF/Interfaces/IEmailDataService.cs create mode 100644 DMS.WPF/Services/EmailDataService.cs diff --git a/DMS.WPF/App.xaml.cs b/DMS.WPF/App.xaml.cs index e205a05..ec741d1 100644 --- a/DMS.WPF/App.xaml.cs +++ b/DMS.WPF/App.xaml.cs @@ -269,6 +269,7 @@ public partial class App : System.Windows.Application services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); + services.AddSingleton(); // 注册主数据服务 services.AddSingleton(); diff --git a/DMS.WPF/Interfaces/IEmailDataService.cs b/DMS.WPF/Interfaces/IEmailDataService.cs new file mode 100644 index 0000000..b7849cb --- /dev/null +++ b/DMS.WPF/Interfaces/IEmailDataService.cs @@ -0,0 +1,60 @@ +using System.Collections.ObjectModel; +using DMS.Application.DTOs; + +namespace DMS.WPF.Interfaces; + +/// +/// 邮件数据服务接口。 +/// +public interface IEmailDataService +{ + /// + /// 邮件账户列表。 + /// + ObservableCollection EmailAccounts { get; set; } + + /// + /// 邮件模板列表。 + /// + ObservableCollection EmailTemplates { get; set; } + + /// + /// 加载所有邮件数据。 + /// + void LoadAllEmailData(); + + /// + /// 添加邮件账户。 + /// + Task AddEmailAccountAsync(CreateEmailAccountRequest request); + + /// + /// 更新邮件账户。 + /// + Task UpdateEmailAccountAsync(int id, CreateEmailAccountRequest request); + + /// + /// 删除邮件账户。 + /// + Task DeleteEmailAccountAsync(int id); + + /// + /// 测试邮件账户连接。 + /// + Task TestEmailAccountAsync(int id); + + /// + /// 添加邮件模板。 + /// + Task AddEmailTemplateAsync(EmailTemplateDto template); + + /// + /// 更新邮件模板。 + /// + Task UpdateEmailTemplateAsync(int id, EmailTemplateDto template); + + /// + /// 删除邮件模板。 + /// + Task DeleteEmailTemplateAsync(int id); +} \ No newline at end of file diff --git a/DMS.WPF/Services/DialogService.cs b/DMS.WPF/Services/DialogService.cs index 0d70457..f425004 100644 --- a/DMS.WPF/Services/DialogService.cs +++ b/DMS.WPF/Services/DialogService.cs @@ -28,7 +28,8 @@ namespace DMS.WPF.Services { typeof(MqttAliasBatchEditDialogViewModel), typeof(MqttAliasBatchEditDialog) }, { typeof(HistorySettingsDialogViewModel), typeof(HistorySettingsDialog) }, { typeof(AlarmSettingsDialogViewModel), typeof(AlarmSettingsDialog) }, - { typeof(EmailAccountDialogViewModel), typeof(EmailAccountDialog) } + { typeof(EmailAccountDialogViewModel), typeof(EmailAccountDialog) }, + { typeof(EmailTemplateDialogViewModel), typeof(EmailTemplateDialog) } // Add other mappings here // ... other dialogs }; diff --git a/DMS.WPF/Services/EmailDataService.cs b/DMS.WPF/Services/EmailDataService.cs new file mode 100644 index 0000000..d9febd2 --- /dev/null +++ b/DMS.WPF/Services/EmailDataService.cs @@ -0,0 +1,241 @@ +using System.Collections.ObjectModel; +using System.Windows.Threading; +using AutoMapper; +using CommunityToolkit.Mvvm.ComponentModel; +using DMS.Application.DTOs; +using DMS.Application.Interfaces; +using DMS.WPF.Interfaces; + +namespace DMS.WPF.Services; + +/// +/// 邮件数据服务类,负责管理邮件相关的数据和操作。 +/// +public class EmailDataService : IEmailDataService +{ + private readonly IMapper _mapper; + private readonly IEmailAppService _emailAppService; + private readonly INotificationService _notificationService; + private readonly Dispatcher _uiDispatcher; + + /// + /// 邮件账户列表。 + /// + public ObservableCollection EmailAccounts { get; set; } = new(); + + /// + /// 邮件模板列表。 + /// + public ObservableCollection EmailTemplates { get; set; } = new(); + + /// + /// EmailDataService类的构造函数。 + /// + /// AutoMapper 实例。 + /// 邮件应用服务实例。 + /// 通知服务实例。 + public EmailDataService(IMapper mapper, IEmailAppService emailAppService, INotificationService notificationService) + { + _mapper = mapper; + _emailAppService = emailAppService; + _notificationService = notificationService; + _uiDispatcher = Dispatcher.CurrentDispatcher; + } + + /// + /// 加载所有邮件数据。 + /// + public void LoadAllEmailData() + { + _ = LoadAllEmailDataAsync(); + } + + /// + /// 异步加载所有邮件数据。 + /// + private async Task LoadAllEmailDataAsync() + { + try + { + // 加载邮件账户 + var accounts = await _emailAppService.GetAllEmailAccountsAsync(); + _uiDispatcher.Invoke(() => + { + EmailAccounts.Clear(); + foreach (var account in accounts) + { + EmailAccounts.Add(account); + } + }); + + // 加载邮件模板 + var templates = await _emailAppService.GetAllEmailTemplatesAsync(); + _uiDispatcher.Invoke(() => + { + EmailTemplates.Clear(); + foreach (var template in templates) + { + EmailTemplates.Add(template); + } + }); + } + catch (Exception ex) + { + _notificationService.ShowError("加载邮件数据失败", ex); + } + } + + /// + /// 添加邮件账户。 + /// + public async Task AddEmailAccountAsync(CreateEmailAccountRequest request) + { + var emailAccount = await _emailAppService.CreateEmailAccountAsync(request); + _uiDispatcher.Invoke(() => + { + EmailAccounts.Add(emailAccount); + }); + return emailAccount; + } + + /// + /// 更新邮件账户。 + /// + public async Task UpdateEmailAccountAsync(int id, CreateEmailAccountRequest request) + { + try + { + var emailAccount = await _emailAppService.UpdateEmailAccountAsync(id, request); + _uiDispatcher.Invoke(() => + { + var existingAccount = EmailAccounts.FirstOrDefault(a => a.Id == id); + if (existingAccount != null) + { + // 更新现有账户的信息 + var index = EmailAccounts.IndexOf(existingAccount); + EmailAccounts[index] = emailAccount; + } + }); + return true; + } + catch (Exception ex) + { + _notificationService.ShowError("更新邮件账户失败", ex); + return false; + } + } + + /// + /// 删除邮件账户。 + /// + public async Task DeleteEmailAccountAsync(int id) + { + try + { + var result = await _emailAppService.DeleteEmailAccountAsync(id); + if (result) + { + _uiDispatcher.Invoke(() => + { + var accountToRemove = EmailAccounts.FirstOrDefault(a => a.Id == id); + if (accountToRemove != null) + { + EmailAccounts.Remove(accountToRemove); + } + }); + return true; + } + return false; + } + catch (Exception ex) + { + _notificationService.ShowError("删除邮件账户失败", ex); + return false; + } + } + + /// + /// 测试邮件账户连接。 + /// + public async Task TestEmailAccountAsync(int id) + { + try + { + return await _emailAppService.TestEmailAccountAsync(id); + } + catch (Exception ex) + { + _notificationService.ShowError("测试邮件账户连接失败", ex); + return false; + } + } + + /// + /// 添加邮件模板。 + /// + public async Task AddEmailTemplateAsync(EmailTemplateDto template) + { + var emailTemplate = await _emailAppService.CreateEmailTemplateAsync(template); + _uiDispatcher.Invoke(() => + { + EmailTemplates.Add(emailTemplate); + }); + return emailTemplate; + } + + /// + /// 更新邮件模板。 + /// + public async Task UpdateEmailTemplateAsync(int id, EmailTemplateDto template) + { + try + { + var emailTemplate = await _emailAppService.UpdateEmailTemplateAsync(id, template); + _uiDispatcher.Invoke(() => + { + var existingTemplate = EmailTemplates.FirstOrDefault(t => t.Id == id); + if (existingTemplate != null) + { + // 更新现有模板的信息 + var index = EmailTemplates.IndexOf(existingTemplate); + EmailTemplates[index] = emailTemplate; + } + }); + return true; + } + catch (Exception ex) + { + _notificationService.ShowError("更新邮件模板失败", ex); + return false; + } + } + + /// + /// 删除邮件模板。 + /// + public async Task DeleteEmailTemplateAsync(int id) + { + try + { + var result = await _emailAppService.DeleteEmailTemplateAsync(id); + if (result) + { + _uiDispatcher.Invoke(() => + { + var templateToRemove = EmailTemplates.FirstOrDefault(t => t.Id == id); + if (templateToRemove != null) + { + EmailTemplates.Remove(templateToRemove); + } + }); + return true; + } + return false; + } + catch (Exception ex) + { + _notificationService.ShowError("删除邮件模板失败", ex); + return false; + } + } +} \ No newline at end of file diff --git a/DMS.WPF/ViewModels/Dialogs/EmailTemplateDialogViewModel.cs b/DMS.WPF/ViewModels/Dialogs/EmailTemplateDialogViewModel.cs index 86d73f3..3f61fd6 100644 --- a/DMS.WPF/ViewModels/Dialogs/EmailTemplateDialogViewModel.cs +++ b/DMS.WPF/ViewModels/Dialogs/EmailTemplateDialogViewModel.cs @@ -86,19 +86,20 @@ namespace DMS.WPF.ViewModels.Dialogs IsActive = IsActive }; + EmailTemplateDto resultTemplate; if (_isEditMode) { - await _emailAppService.UpdateEmailTemplateAsync(_templateId, template); + resultTemplate = await _emailAppService.UpdateEmailTemplateAsync(_templateId, template); _notificationService.ShowSuccess("邮件模板更新成功"); } else { - await _emailAppService.CreateEmailTemplateAsync(template); + resultTemplate = await _emailAppService.CreateEmailTemplateAsync(template); _notificationService.ShowSuccess("邮件模板创建成功"); } CloseRequested?.Invoke(true); - await Close(new EmailTemplateDto()); + await Close(resultTemplate); } catch (Exception ex) { diff --git a/DMS.WPF/ViewModels/EmailManagementViewModel.cs b/DMS.WPF/ViewModels/EmailManagementViewModel.cs index 8d009f5..11fe60b 100644 --- a/DMS.WPF/ViewModels/EmailManagementViewModel.cs +++ b/DMS.WPF/ViewModels/EmailManagementViewModel.cs @@ -15,6 +15,7 @@ namespace DMS.WPF.ViewModels public partial class EmailManagementViewModel : ViewModelBase { private readonly IEmailAppService _emailAppService; + private readonly IEmailDataService _emailDataService; private readonly IDialogService _dialogService; private readonly INotificationService _notificationService; @@ -32,12 +33,18 @@ namespace DMS.WPF.ViewModels public EmailManagementViewModel( IEmailAppService emailAppService, + IEmailDataService emailDataService, IDialogService dialogService, INotificationService notificationService) { _emailAppService = emailAppService; + _emailDataService = emailDataService; _dialogService = dialogService; _notificationService = notificationService; + + // 绑定数据集合 + _emailAccounts = _emailDataService.EmailAccounts; + _emailTemplates = _emailDataService.EmailTemplates; } /// @@ -48,13 +55,11 @@ namespace DMS.WPF.ViewModels { try { - // 加载邮件账户 - var accounts = await _emailAppService.GetAllEmailAccountsAsync(); - EmailAccounts = new ObservableCollection(accounts); - - // 加载邮件模板 - var templates = await _emailAppService.GetAllEmailTemplatesAsync(); - EmailTemplates = new ObservableCollection(templates); + // 使用EmailDataService加载所有邮件数据 + _emailDataService.LoadAllEmailData(); + + // 等待一段时间确保数据加载完成 + await Task.Delay(100); } catch (Exception ex) { @@ -75,6 +80,30 @@ namespace DMS.WPF.ViewModels return; } + try + { + var request = new CreateEmailAccountRequest + { + Name = emailAccountDto.Name, + EmailAddress = emailAccountDto.EmailAddress, + SmtpServer = emailAccountDto.SmtpServer, + SmtpPort = emailAccountDto.SmtpPort, + EnableSsl = emailAccountDto.EnableSsl, + Username = emailAccountDto.Username, + Password = emailAccountDto.Password, + ImapServer = emailAccountDto.ImapServer, + ImapPort = emailAccountDto.ImapPort, + IsDefault = emailAccountDto.IsDefault, + IsActive = emailAccountDto.IsActive + }; + + await _emailDataService.AddEmailAccountAsync(request); + _notificationService.ShowSuccess("添加邮件账户成功"); + } + catch (Exception ex) + { + _notificationService.ShowError("添加邮件账户失败", ex); + } } /// @@ -96,13 +125,37 @@ namespace DMS.WPF.ViewModels return; } - // var dialog = _dialogService.CreateDialog(); - // dialog.SetEmailAccount(SelectedEmailAccount); - // var result = await dialog.ShowAsync(); - // if (result == true) - // { - // await LoadDataAsync(); - // } + try + { + var request = new CreateEmailAccountRequest + { + Name = emailAccountDto.Name, + EmailAddress = emailAccountDto.EmailAddress, + SmtpServer = emailAccountDto.SmtpServer, + SmtpPort = emailAccountDto.SmtpPort, + EnableSsl = emailAccountDto.EnableSsl, + Username = emailAccountDto.Username, + Password = emailAccountDto.Password, + ImapServer = emailAccountDto.ImapServer, + ImapPort = emailAccountDto.ImapPort, + IsDefault = emailAccountDto.IsDefault, + IsActive = emailAccountDto.IsActive + }; + + var result = await _emailDataService.UpdateEmailAccountAsync(SelectedEmailAccount.Id, request); + if (result) + { + _notificationService.ShowSuccess("更新邮件账户成功"); + } + else + { + _notificationService.ShowError("更新邮件账户失败"); + } + } + catch (Exception ex) + { + _notificationService.ShowError("更新邮件账户失败", ex); + } } /// @@ -127,9 +180,15 @@ namespace DMS.WPF.ViewModels { try { - await _emailAppService.DeleteEmailAccountAsync(SelectedEmailAccount.Id); - _notificationService.ShowSuccess("删除成功"); - await LoadDataAsync(); + var result = await _emailDataService.DeleteEmailAccountAsync(SelectedEmailAccount.Id); + if (result) + { + _notificationService.ShowSuccess("删除成功"); + } + else + { + _notificationService.ShowError("删除失败"); + } } catch (Exception ex) { @@ -152,7 +211,7 @@ namespace DMS.WPF.ViewModels try { - var result = await _emailAppService.TestEmailAccountAsync(SelectedEmailAccount.Id); + var result = await _emailDataService.TestEmailAccountAsync(SelectedEmailAccount.Id); if (result) { _notificationService.ShowSuccess("连接测试成功"); @@ -174,12 +233,22 @@ namespace DMS.WPF.ViewModels [RelayCommand] private async Task AddEmailTemplateAsync() { - // var dialog = _dialogService.CreateDialog(); - // var result = await dialog.ShowAsync(); - // if (result == true) - // { - // await LoadDataAsync(); - // } + var viewModel = App.Current.Services.GetRequiredService(); + var emailTemplateDto = await _dialogService.ShowDialogAsync(viewModel); + if (emailTemplateDto == null) + { + return; + } + + try + { + var result = await _emailDataService.AddEmailTemplateAsync(emailTemplateDto); + _notificationService.ShowSuccess("添加邮件模板成功"); + } + catch (Exception ex) + { + _notificationService.ShowError("添加邮件模板失败", ex); + } } /// @@ -194,13 +263,30 @@ namespace DMS.WPF.ViewModels return; } - // var dialog = _dialogService.CreateDialog(); - // dialog.SetEmailTemplate(SelectedEmailTemplate); - // var result = await dialog.ShowAsync(); - // if (result == true) - // { - // await LoadDataAsync(); - // } + var viewModel = App.Current.Services.GetRequiredService(); + viewModel.SetEmailTemplate(SelectedEmailTemplate); + var emailTemplateDto = await _dialogService.ShowDialogAsync(viewModel); + if (emailTemplateDto == null) + { + return; + } + + try + { + var result = await _emailDataService.UpdateEmailTemplateAsync(SelectedEmailTemplate.Id, emailTemplateDto); + if (result) + { + _notificationService.ShowSuccess("更新邮件模板成功"); + } + else + { + _notificationService.ShowError("更新邮件模板失败"); + } + } + catch (Exception ex) + { + _notificationService.ShowError("更新邮件模板失败", ex); + } } /// @@ -215,23 +301,31 @@ namespace DMS.WPF.ViewModels return; } - // var confirmResult = await _dialogService.ShowConfirmDialogAsync( - // "确认删除", - // $"确定要删除邮件模板 {SelectedEmailTemplate.Name} 吗?"); + var confirmDialogViewModel = new ConfirmDialogViewModel( + "确认删除", + $"确定要删除邮件模板 {SelectedEmailTemplate.Name} 吗?", "删除"); - // if (confirmResult == true) - // { - // try - // { - // await _emailAppService.DeleteEmailTemplateAsync(SelectedEmailTemplate.Id); - // _notificationService.ShowSuccess("删除成功"); - // await LoadDataAsync(); - // } - // catch (Exception ex) - // { - // _notificationService.ShowError("删除失败", ex); - // } - // } + var confirmResult = await _dialogService.ShowDialogAsync(confirmDialogViewModel); + + if (confirmResult == true) + { + try + { + var result = await _emailDataService.DeleteEmailTemplateAsync(SelectedEmailTemplate.Id); + if (result) + { + _notificationService.ShowSuccess("删除成功"); + } + else + { + _notificationService.ShowError("删除失败"); + } + } + catch (Exception ex) + { + _notificationService.ShowError("删除失败", ex); + } + } } } } \ No newline at end of file