2025-09-22 22:58:51 +08:00
|
|
|
|
using System.Collections.ObjectModel;
|
2025-09-22 23:33:34 +08:00
|
|
|
|
using System.ComponentModel;
|
2025-09-14 16:16:10 +08:00
|
|
|
|
using System.Text.Json;
|
2025-09-14 19:13:40 +08:00
|
|
|
|
using System.Windows;
|
2025-09-14 16:16:10 +08:00
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
|
|
|
using CommunityToolkit.Mvvm.Input;
|
2025-09-14 19:13:40 +08:00
|
|
|
|
using DMS.Application.DTOs;
|
|
|
|
|
|
using DMS.Application.Interfaces;
|
2025-09-16 12:29:09 +08:00
|
|
|
|
using DMS.Application.Interfaces.Database;
|
2025-09-22 22:58:51 +08:00
|
|
|
|
using DMS.Core.Interfaces;
|
2025-09-14 16:16:10 +08:00
|
|
|
|
using DMS.Core.Models.Triggers;
|
|
|
|
|
|
using DMS.WPF.Interfaces;
|
2025-10-06 18:17:56 +08:00
|
|
|
|
using DMS.WPF.ItemViewModel;
|
2025-09-14 16:16:10 +08:00
|
|
|
|
|
2025-09-14 19:13:40 +08:00
|
|
|
|
namespace DMS.WPF.ViewModels.Dialogs
|
2025-09-14 16:16:10 +08:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 触发器编辑器视图模型
|
|
|
|
|
|
/// </summary>
|
2025-10-06 18:17:56 +08:00
|
|
|
|
public partial class TriggerDialogViewModel : DialogViewModelBase<TriggerItem?>
|
2025-09-14 16:16:10 +08:00
|
|
|
|
{
|
2025-10-18 18:55:08 +08:00
|
|
|
|
|
2025-09-14 16:16:10 +08:00
|
|
|
|
private readonly IDialogService _dialogService;
|
2025-09-22 22:58:51 +08:00
|
|
|
|
private readonly IDataStorageService _dataStorageService;
|
2025-09-14 16:16:10 +08:00
|
|
|
|
private readonly INotificationService _notificationService;
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
2025-10-06 18:17:56 +08:00
|
|
|
|
private TriggerItem _trigger = new();
|
2025-09-14 16:16:10 +08:00
|
|
|
|
|
|
|
|
|
|
// Properties for easier binding in XAML for SendEmail action config
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
private string _emailRecipients = "";
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
private string _emailSubjectTemplate = "";
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
private string _emailBodyTemplate = "";
|
|
|
|
|
|
|
2025-09-14 19:13:40 +08:00
|
|
|
|
public TriggerDialogViewModel(
|
2025-09-14 16:16:10 +08:00
|
|
|
|
IDialogService dialogService,
|
2025-09-22 22:58:51 +08:00
|
|
|
|
IDataStorageService dataStorageService,
|
2025-09-14 16:16:10 +08:00
|
|
|
|
INotificationService notificationService)
|
|
|
|
|
|
{
|
|
|
|
|
|
_dialogService = dialogService ?? throw new ArgumentNullException(nameof(dialogService));
|
2025-09-22 22:58:51 +08:00
|
|
|
|
_dataStorageService = dataStorageService;
|
2025-09-14 16:16:10 +08:00
|
|
|
|
_notificationService = notificationService ?? throw new ArgumentNullException(nameof(notificationService));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-18 18:55:08 +08:00
|
|
|
|
|
2025-09-22 22:58:51 +08:00
|
|
|
|
|
2025-09-14 16:16:10 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 初始化视图模型(传入待编辑的触发器)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="parameter">待编辑的触发器 DTO</param>
|
2025-09-14 19:13:40 +08:00
|
|
|
|
public async Task OnInitializedAsync(object? parameter)
|
2025-09-14 16:16:10 +08:00
|
|
|
|
{
|
2025-10-06 18:17:56 +08:00
|
|
|
|
if (parameter is TriggerItem triggerItemViewModel)
|
2025-09-14 16:16:10 +08:00
|
|
|
|
{
|
2025-09-23 06:51:29 +08:00
|
|
|
|
Trigger = triggerItemViewModel;
|
2025-09-14 19:58:18 +08:00
|
|
|
|
Title = Trigger.Id == default(int) ? "新建触发器" : "编辑触发器";
|
2025-09-14 16:16:10 +08:00
|
|
|
|
PrimaryButText = "保存";
|
|
|
|
|
|
|
|
|
|
|
|
// Parse action configuration if it's SendEmail
|
|
|
|
|
|
if (Trigger.Action == ActionType.SendEmail && !string.IsNullOrEmpty(Trigger.ActionConfigurationJson))
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var config = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(Trigger.ActionConfigurationJson);
|
|
|
|
|
|
if (config != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (config.TryGetValue("Recipients", out var recipientsElement))
|
|
|
|
|
|
{
|
|
|
|
|
|
var recipients = recipientsElement.Deserialize<List<string>>();
|
|
|
|
|
|
EmailRecipients = string.Join(";", recipients ?? new List<string>());
|
|
|
|
|
|
}
|
|
|
|
|
|
EmailSubjectTemplate = config.TryGetValue("SubjectTemplate", out var subjectElement) ? subjectElement.GetString() ?? "" : "";
|
|
|
|
|
|
EmailBodyTemplate = config.TryGetValue("BodyTemplate", out var bodyElement) ? bodyElement.GetString() ?? "" : "";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2025-09-14 19:13:40 +08:00
|
|
|
|
_notificationService.ShowWarn($"无法解析邮件配置: {ex.Message}");
|
2025-09-14 16:16:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-18 18:55:08 +08:00
|
|
|
|
|
2025-09-14 16:16:10 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 保存按钮点击命令
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[RelayCommand]
|
|
|
|
|
|
private async Task SaveAsync()
|
|
|
|
|
|
{
|
2025-10-18 18:55:08 +08:00
|
|
|
|
if (string.IsNullOrWhiteSpace(Trigger.Name))
|
2025-09-14 16:16:10 +08:00
|
|
|
|
{
|
2025-10-18 18:55:08 +08:00
|
|
|
|
_notificationService.ShowWarn("请输入触发器名称");
|
2025-09-14 16:16:10 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(Trigger.Description))
|
|
|
|
|
|
{
|
2025-09-14 19:13:40 +08:00
|
|
|
|
_notificationService.ShowWarn("请输入触发器描述");
|
2025-09-14 16:16:10 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-22 22:58:51 +08:00
|
|
|
|
|
2025-09-14 16:16:10 +08:00
|
|
|
|
|
|
|
|
|
|
// Prepare action configuration based on selected action type
|
|
|
|
|
|
if (Trigger.Action == ActionType.SendEmail)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(EmailRecipients))
|
|
|
|
|
|
{
|
2025-09-14 19:13:40 +08:00
|
|
|
|
_notificationService.ShowWarn("请输入至少一个收件人邮箱地址");
|
2025-09-14 16:16:10 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(EmailSubjectTemplate))
|
|
|
|
|
|
{
|
2025-09-14 19:13:40 +08:00
|
|
|
|
_notificationService.ShowWarn("请输入邮件主题模板");
|
2025-09-14 16:16:10 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(EmailBodyTemplate))
|
|
|
|
|
|
{
|
2025-09-14 19:13:40 +08:00
|
|
|
|
_notificationService.ShowWarn("请输入邮件内容模板");
|
2025-09-14 16:16:10 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var recipientList = new List<string>(EmailRecipients.Split(';', StringSplitOptions.RemoveEmptyEntries));
|
|
|
|
|
|
var configDict = new Dictionary<string, object>
|
|
|
|
|
|
{
|
|
|
|
|
|
{ "Recipients", recipientList },
|
|
|
|
|
|
{ "SubjectTemplate", EmailSubjectTemplate },
|
|
|
|
|
|
{ "BodyTemplate", EmailBodyTemplate }
|
|
|
|
|
|
};
|
|
|
|
|
|
Trigger.ActionConfigurationJson = JsonSerializer.Serialize(configDict);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// For other actions, leave ActionConfigurationJson as is or set to default "{}"
|
|
|
|
|
|
Trigger.ActionConfigurationJson ??= "{}";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Set timestamps
|
2025-10-19 20:34:20 +08:00
|
|
|
|
Trigger.UpdatedAt = DateTime.Now;
|
2025-09-14 19:58:18 +08:00
|
|
|
|
if (Trigger.Id == default(int))
|
2025-09-14 16:16:10 +08:00
|
|
|
|
{
|
2025-10-19 20:34:20 +08:00
|
|
|
|
Trigger.CreatedAt = DateTime.Now;
|
2025-09-14 19:58:18 +08:00
|
|
|
|
Trigger.Id = 0; // 对于自增ID,设置为0让数据库自动生成
|
2025-09-14 16:16:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Close dialog with the updated trigger DTO
|
2025-09-14 19:13:40 +08:00
|
|
|
|
await Close(Trigger);
|
2025-09-14 16:16:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 取消按钮点击命令
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[RelayCommand]
|
|
|
|
|
|
private async Task CancelAsync()
|
|
|
|
|
|
{
|
2025-09-14 19:13:40 +08:00
|
|
|
|
await Close(null); // Return null to indicate cancellation
|
2025-09-14 16:16:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|