2025-07-01 11:11:02 +08:00
|
|
|
using System.Collections.ObjectModel;
|
2025-06-30 14:19:22 +08:00
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
2025-07-03 12:55:00 +08:00
|
|
|
using CommunityToolkit.Mvvm.Input;
|
|
|
|
|
using NLog;
|
2025-07-02 18:33:08 +08:00
|
|
|
using PMSWPF.Data.Repositories;
|
|
|
|
|
using PMSWPF.Enums;
|
|
|
|
|
using PMSWPF.Helper;
|
2025-06-30 14:19:22 +08:00
|
|
|
using PMSWPF.Models;
|
2025-07-03 12:55:00 +08:00
|
|
|
using PMSWPF.Services;
|
|
|
|
|
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
2025-06-30 14:19:22 +08:00
|
|
|
|
2025-06-23 17:01:06 +08:00
|
|
|
namespace PMSWPF.ViewModels;
|
|
|
|
|
|
2025-06-30 14:19:22 +08:00
|
|
|
partial class VariableTableViewModel : ViewModelBase
|
2025-06-23 17:01:06 +08:00
|
|
|
{
|
2025-07-03 12:55:00 +08:00
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
private readonly IDialogService _dialogService;
|
|
|
|
|
|
2025-07-02 18:33:08 +08:00
|
|
|
// private readonly ILogger<VariableTableViewModel> _logger;
|
2025-07-03 12:55:00 +08:00
|
|
|
[ObservableProperty]
|
|
|
|
|
private VariableTable variableTable;
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
private ObservableCollection<VariableData> _dataVariables;
|
2025-07-02 18:33:08 +08:00
|
|
|
|
2025-07-03 15:29:07 +08:00
|
|
|
[ObservableProperty]
|
|
|
|
|
private VariableData _selectedVariableData;
|
|
|
|
|
|
2025-07-02 18:33:08 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 是否是第一次加载,防止ToggleSwitch第一次加载触发改变事件
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool IsLoadCompletion { get; set; } = false;
|
|
|
|
|
|
|
|
|
|
private readonly VarTableRepository _varTableRepository;
|
2025-07-03 12:55:00 +08:00
|
|
|
private readonly VarDataRepository _varDataRepository;
|
2025-07-02 18:33:08 +08:00
|
|
|
|
2025-07-03 12:55:00 +08:00
|
|
|
|
|
|
|
|
public VariableTableViewModel(IDialogService dialogService)
|
2025-07-02 18:33:08 +08:00
|
|
|
{
|
2025-07-03 12:55:00 +08:00
|
|
|
_dialogService = dialogService;
|
2025-07-02 18:33:08 +08:00
|
|
|
IsLoadCompletion = false;
|
|
|
|
|
// _logger = logger;
|
|
|
|
|
_varTableRepository = new VarTableRepository();
|
2025-07-03 12:55:00 +08:00
|
|
|
_varDataRepository = new VarDataRepository();
|
2025-07-02 18:33:08 +08:00
|
|
|
}
|
2025-07-03 12:55:00 +08:00
|
|
|
|
2025-07-02 18:33:08 +08:00
|
|
|
|
2025-06-23 17:01:06 +08:00
|
|
|
public override void OnLoaded()
|
|
|
|
|
{
|
2025-07-02 18:33:08 +08:00
|
|
|
if (VariableTable.DataVariables != null)
|
2025-07-01 11:11:02 +08:00
|
|
|
{
|
2025-07-02 22:07:16 +08:00
|
|
|
DataVariables = new ObservableCollection<VariableData>(VariableTable.DataVariables);
|
2025-07-01 11:11:02 +08:00
|
|
|
}
|
2025-07-03 12:55:00 +08:00
|
|
|
|
2025-07-03 15:03:36 +08:00
|
|
|
|
2025-07-02 18:33:08 +08:00
|
|
|
IsLoadCompletion = true;
|
2025-06-23 17:01:06 +08:00
|
|
|
}
|
2025-07-02 18:33:08 +08:00
|
|
|
|
2025-07-03 15:29:07 +08:00
|
|
|
[RelayCommand]
|
|
|
|
|
public async void EditVarData(VariableTable variableTable)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// // 1. 显示添加设备对话框
|
|
|
|
|
var varData = await _dialogService.ShowEditVarDataDialog(SelectedVariableData);
|
|
|
|
|
// // 如果用户取消或对话框未返回设备,则直接返回
|
|
|
|
|
if (varData == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
varData.VariableTableId = variableTable.Id;
|
|
|
|
|
// 更新数据库
|
|
|
|
|
await _varDataRepository.UpdateAsync(varData);
|
|
|
|
|
// 更新当前页面的
|
|
|
|
|
var index = variableTable.DataVariables.IndexOf(SelectedVariableData);
|
|
|
|
|
// 更新变量表中的
|
|
|
|
|
if (index >= 0 && index < variableTable.DataVariables.Count)
|
|
|
|
|
variableTable.DataVariables[index] = varData;
|
|
|
|
|
NotificationHelper.ShowMessage($"编辑变量成功:{varData?.Name}", NotificationType.Success);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
string msg = $"编辑变量的过程中发生了不可预期的错误:";
|
|
|
|
|
Logger.Error(msg + e);
|
|
|
|
|
NotificationHelper.ShowMessage(msg + e.Message, NotificationType.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-07-03 12:55:00 +08:00
|
|
|
[RelayCommand]
|
|
|
|
|
public async void AddVarData(VariableTable variableTable)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// // 1. 显示添加设备对话框
|
|
|
|
|
var varData = await _dialogService.ShowAddVarDataDialog();
|
|
|
|
|
// // 如果用户取消或对话框未返回设备,则直接返回
|
|
|
|
|
if (varData == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
varData.VariableTableId = variableTable.Id;
|
|
|
|
|
var addVarData = await _varDataRepository.AddAsync(varData);
|
2025-07-03 15:03:36 +08:00
|
|
|
DataVariables?.Add(addVarData);
|
|
|
|
|
variableTable.DataVariables?.Add(addVarData);
|
2025-07-03 12:55:00 +08:00
|
|
|
var msg = addVarData.Id > 0 ? $"添加变量成功:{varData?.Name}" : $"添加变量成功:{varData.Name}";
|
|
|
|
|
var type = addVarData.Id > 0 ? NotificationType.Success : NotificationType.Error;
|
|
|
|
|
NotificationHelper.ShowMessage(msg, type);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
string msg = $"添加变量的过程中发生了不可预期的错误:";
|
|
|
|
|
Logger.Error(msg + e);
|
|
|
|
|
NotificationHelper.ShowMessage(msg + e.Message, NotificationType.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-07-02 18:33:08 +08:00
|
|
|
public async Task OnIsActiveChanged(bool active)
|
|
|
|
|
{
|
|
|
|
|
var res = await _varTableRepository.Edit(VariableTable);
|
|
|
|
|
if (res > 0)
|
|
|
|
|
{
|
|
|
|
|
var statusMessage = active ? "已启用" : "已停用";
|
|
|
|
|
NotificationHelper.ShowMessage($"变量表:{VariableTable.Name},{statusMessage}", NotificationType.Success);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
NotificationHelper.ShowMessage($"变量表:{VariableTable.Name},状态修改失败,状态:{active}", NotificationType.Error);
|
|
|
|
|
// _logger.LogInformation($"变量表:{VariableTable.Name},状态修改失败,状态:{active}");
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-06-23 17:01:06 +08:00
|
|
|
}
|