2
3 - 从Trigger、DbTriggerDefinition和TriggerItem类中移除了所有条件相关的属性(Condition, Threshold, LowerBound, UpperBound)
4 - 删除了ConditionType枚举,简化了触发器逻辑
5 - 为触发器添加了Name字段,在核心模型、数据库实体和视图模型中都添加了该属性
6 - 删除了TriggerDialog界面中的变量选择和搜索功能
7 - 从TriggerDialog界面中删除了触发条件相关的UI元素
8 - 更新了TriggerDialogViewModel,移除了条件相关的验证和业务逻辑
9 - 更新了TriggersViewModel,移除了条件的初始化设置
10 - 更新了AutoMapper配置文件,增加TriggerItem与Trigger之间的映射
11 - 在TriggerEvaluationService中移除了条件判断逻辑,现在激活的触发器会直接执行动作
12 - 更新了App.xaml,移除了对已删除枚举的引用
13 - 修改了保存验证逻辑,确保触发器名称不能为空
168 lines
6.1 KiB
C#
168 lines
6.1 KiB
C#
using System.Collections.ObjectModel;
|
||
using System.ComponentModel;
|
||
using System.Text.Json;
|
||
using System.Windows;
|
||
using CommunityToolkit.Mvvm.ComponentModel;
|
||
using CommunityToolkit.Mvvm.Input;
|
||
using DMS.Application.DTOs;
|
||
using DMS.Application.Interfaces;
|
||
using DMS.Application.Interfaces.Database;
|
||
using DMS.Core.Interfaces;
|
||
using DMS.Core.Models.Triggers;
|
||
using DMS.WPF.Interfaces;
|
||
using DMS.WPF.ItemViewModel;
|
||
|
||
namespace DMS.WPF.ViewModels.Dialogs
|
||
{
|
||
/// <summary>
|
||
/// 触发器编辑器视图模型
|
||
/// </summary>
|
||
public partial class TriggerDialogViewModel : DialogViewModelBase<TriggerItem?>
|
||
{
|
||
|
||
private readonly IDialogService _dialogService;
|
||
private readonly IDataStorageService _dataStorageService;
|
||
private readonly INotificationService _notificationService;
|
||
|
||
[ObservableProperty]
|
||
private TriggerItem _trigger = new();
|
||
|
||
// Properties for easier binding in XAML for SendEmail action config
|
||
[ObservableProperty]
|
||
private string _emailRecipients = "";
|
||
|
||
[ObservableProperty]
|
||
private string _emailSubjectTemplate = "";
|
||
|
||
[ObservableProperty]
|
||
private string _emailBodyTemplate = "";
|
||
|
||
public TriggerDialogViewModel(
|
||
IDialogService dialogService,
|
||
IDataStorageService dataStorageService,
|
||
INotificationService notificationService)
|
||
{
|
||
_dialogService = dialogService ?? throw new ArgumentNullException(nameof(dialogService));
|
||
_dataStorageService = dataStorageService;
|
||
_notificationService = notificationService ?? throw new ArgumentNullException(nameof(notificationService));
|
||
}
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// 初始化视图模型(传入待编辑的触发器)
|
||
/// </summary>
|
||
/// <param name="parameter">待编辑的触发器 DTO</param>
|
||
public async Task OnInitializedAsync(object? parameter)
|
||
{
|
||
if (parameter is TriggerItem triggerItemViewModel)
|
||
{
|
||
Trigger = triggerItemViewModel;
|
||
Title = Trigger.Id == default(int) ? "新建触发器" : "编辑触发器";
|
||
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)
|
||
{
|
||
_notificationService.ShowWarn($"无法解析邮件配置: {ex.Message}");
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// 保存按钮点击命令
|
||
/// </summary>
|
||
[RelayCommand]
|
||
private async Task SaveAsync()
|
||
{
|
||
if (string.IsNullOrWhiteSpace(Trigger.Name))
|
||
{
|
||
_notificationService.ShowWarn("请输入触发器名称");
|
||
return;
|
||
}
|
||
|
||
if (string.IsNullOrWhiteSpace(Trigger.Description))
|
||
{
|
||
_notificationService.ShowWarn("请输入触发器描述");
|
||
return;
|
||
}
|
||
|
||
|
||
|
||
// Prepare action configuration based on selected action type
|
||
if (Trigger.Action == ActionType.SendEmail)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(EmailRecipients))
|
||
{
|
||
_notificationService.ShowWarn("请输入至少一个收件人邮箱地址");
|
||
return;
|
||
}
|
||
|
||
if (string.IsNullOrWhiteSpace(EmailSubjectTemplate))
|
||
{
|
||
_notificationService.ShowWarn("请输入邮件主题模板");
|
||
return;
|
||
}
|
||
|
||
if (string.IsNullOrWhiteSpace(EmailBodyTemplate))
|
||
{
|
||
_notificationService.ShowWarn("请输入邮件内容模板");
|
||
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
|
||
Trigger.UpdatedAt = DateTime.UtcNow;
|
||
if (Trigger.Id == default(int))
|
||
{
|
||
Trigger.CreatedAt = DateTime.UtcNow;
|
||
Trigger.Id = 0; // 对于自增ID,设置为0让数据库自动生成
|
||
}
|
||
|
||
// Close dialog with the updated trigger DTO
|
||
await Close(Trigger);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 取消按钮点击命令
|
||
/// </summary>
|
||
[RelayCommand]
|
||
private async Task CancelAsync()
|
||
{
|
||
await Close(null); // Return null to indicate cancellation
|
||
}
|
||
}
|
||
} |