using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using DMS.Application.DTOs; using DMS.Application.Interfaces; using DMS.WPF.Interfaces; using DMS.WPF.ViewModels.Dialogs; using System.Collections.ObjectModel; using Microsoft.Extensions.DependencyInjection; namespace DMS.WPF.ViewModels { /// /// 邮件管理视图模型 /// public partial class EmailManagementViewModel : ViewModelBase { private readonly IEmailAppService _emailAppService; private readonly IDialogService _dialogService; private readonly INotificationService _notificationService; [ObservableProperty] private ObservableCollection _emailAccounts = new(); [ObservableProperty] private EmailAccountDto? _selectedEmailAccount; [ObservableProperty] private ObservableCollection _emailTemplates = new(); [ObservableProperty] private EmailTemplateDto? _selectedEmailTemplate; public EmailManagementViewModel( IEmailAppService emailAppService, IDialogService dialogService, INotificationService notificationService) { _emailAppService = emailAppService; _dialogService = dialogService; _notificationService = notificationService; } /// /// 加载数据命令 /// [RelayCommand] private async Task LoadDataAsync() { try { // 加载邮件账户 var accounts = await _emailAppService.GetAllEmailAccountsAsync(); EmailAccounts = new ObservableCollection(accounts); // 加载邮件模板 var templates = await _emailAppService.GetAllEmailTemplatesAsync(); EmailTemplates = new ObservableCollection(templates); } catch (Exception ex) { _notificationService.ShowError("加载数据失败", ex); } } /// /// 添加邮件账户命令 /// [RelayCommand] private async Task AddEmailAccountAsync() { EmailAccountDialogViewModel viewModel = App.Current.Services.GetRequiredService(); var emailAccountDto = await _dialogService.ShowDialogAsync(viewModel); if (emailAccountDto==null) { return; } } /// /// 编辑邮件账户命令 /// [RelayCommand] private async Task EditEmailAccountAsync() { if (SelectedEmailAccount == null) { _notificationService.ShowWarn("请选择要编辑的邮件账户"); return; } EmailAccountDialogViewModel viewModel = App.Current.Services.GetRequiredService(); viewModel.SetEmailAccount(SelectedEmailAccount); var emailAccountDto = await _dialogService.ShowDialogAsync(viewModel); if (emailAccountDto==null) { return; } // var dialog = _dialogService.CreateDialog(); // dialog.SetEmailAccount(SelectedEmailAccount); // var result = await dialog.ShowAsync(); // if (result == true) // { // await LoadDataAsync(); // } } /// /// 删除邮件账户命令 /// [RelayCommand] private async Task DeleteEmailAccountAsync() { if (SelectedEmailAccount == null) { _notificationService.ShowWarn("请选择要删除的邮件账户"); return; } ConfirmDialogViewModel confirmDialogViewModel = new ConfirmDialogViewModel( "确认删除", $"确定要删除邮件账户 {SelectedEmailAccount.Name} 吗?", "删除"); var confirmResult = await _dialogService.ShowDialogAsync(confirmDialogViewModel); if (confirmResult == true) { try { await _emailAppService.DeleteEmailAccountAsync(SelectedEmailAccount.Id); _notificationService.ShowSuccess("删除成功"); await LoadDataAsync(); } catch (Exception ex) { _notificationService.ShowError("删除失败", ex); } } } /// /// 测试邮件账户连接命令 /// [RelayCommand] private async Task TestEmailAccountAsync() { if (SelectedEmailAccount == null) { _notificationService.ShowWarn("请选择要测试的邮件账户"); return; } try { var result = await _emailAppService.TestEmailAccountAsync(SelectedEmailAccount.Id); if (result) { _notificationService.ShowSuccess("连接测试成功"); } else { _notificationService.ShowWarn("连接测试失败"); } } catch (Exception ex) { _notificationService.ShowError("连接测试失败", ex); } } /// /// 添加邮件模板命令 /// [RelayCommand] private async Task AddEmailTemplateAsync() { // var dialog = _dialogService.CreateDialog(); // var result = await dialog.ShowAsync(); // if (result == true) // { // await LoadDataAsync(); // } } /// /// 编辑邮件模板命令 /// [RelayCommand] private async Task EditEmailTemplateAsync() { if (SelectedEmailTemplate == null) { _notificationService.ShowWarn("请选择要编辑的邮件模板"); return; } // var dialog = _dialogService.CreateDialog(); // dialog.SetEmailTemplate(SelectedEmailTemplate); // var result = await dialog.ShowAsync(); // if (result == true) // { // await LoadDataAsync(); // } } /// /// 删除邮件模板命令 /// [RelayCommand] private async Task DeleteEmailTemplateAsync() { if (SelectedEmailTemplate == null) { _notificationService.ShowWarn("请选择要删除的邮件模板"); return; } // var confirmResult = await _dialogService.ShowConfirmDialogAsync( // "确认删除", // $"确定要删除邮件模板 {SelectedEmailTemplate.Name} 吗?"); // if (confirmResult == true) // { // try // { // await _emailAppService.DeleteEmailTemplateAsync(SelectedEmailTemplate.Id); // _notificationService.ShowSuccess("删除成功"); // await LoadDataAsync(); // } // catch (Exception ex) // { // _notificationService.ShowError("删除失败", ex); // } // } } } }