完成添加变量对话框验证
This commit is contained in:
@@ -13,9 +13,9 @@ public partial class MqttSelectionDialogViewModel : ObservableObject
|
||||
// [ObservableProperty]
|
||||
// private Mqtt? selectedMqtt;
|
||||
//
|
||||
// public MqttSelectionDialogViewModel(DataServices dataServices)
|
||||
// public MqttSelectionDialogViewModel(DataServices _dataServices)
|
||||
// {
|
||||
// Mqtts = new ObservableCollection<Mqtt>(dataServices.Mqtts);
|
||||
// Mqtts = new ObservableCollection<Mqtt>(_dataServices.Mqtts);
|
||||
// }
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
using AutoMapper;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using DMS.Application.DTOs;
|
||||
using DMS.Application.Interfaces;
|
||||
using DMS.Helper;
|
||||
using DMS.WPF.Services;
|
||||
using DMS.WPF.ViewModels.Items;
|
||||
|
||||
namespace DMS.WPF.ViewModels.Dialogs;
|
||||
@@ -9,26 +14,97 @@ public partial class VariableDialogViewModel : DialogViewModelBase<VariableItemV
|
||||
[ObservableProperty]
|
||||
private VariableItemViewModel _variable;
|
||||
|
||||
public VariableDialogViewModel(string title,string primaryButText,VariableItemViewModel variable=null)
|
||||
[ObservableProperty]
|
||||
private string _errorMessage = string.Empty;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool _hasError;
|
||||
private readonly DataServices _dataServices;
|
||||
private readonly IVariableAppService _variableAppService;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public VariableDialogViewModel(DataServices dataServices, IVariableAppService variableAppService, IMapper mapper)
|
||||
{
|
||||
if (variable==null)
|
||||
{
|
||||
Variable=new VariableItemViewModel();
|
||||
}
|
||||
else
|
||||
{
|
||||
Variable=variable;
|
||||
}
|
||||
|
||||
Title=title;
|
||||
PrimaryButText=primaryButText;
|
||||
|
||||
Variable = new VariableItemViewModel();
|
||||
|
||||
this._dataServices = dataServices;
|
||||
this._variableAppService = variableAppService;
|
||||
this._mapper = mapper;
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void PrimaryButton()
|
||||
|
||||
public async Task<bool> PrimaryButtonAsync()
|
||||
{
|
||||
Close(Variable);
|
||||
// 清除之前的错误信息
|
||||
ErrorMessage = string.Empty;
|
||||
HasError = false;
|
||||
// 验证输入
|
||||
bool isValid = ValidateInputAsync();
|
||||
if (isValid)
|
||||
{
|
||||
Close(Variable);
|
||||
}
|
||||
|
||||
return isValid;
|
||||
}
|
||||
|
||||
private bool ValidateInputAsync()
|
||||
{
|
||||
// 检查变量名称是否为空
|
||||
if (string.IsNullOrWhiteSpace(Variable.Name))
|
||||
{
|
||||
ErrorMessage = "变量名称不能为空";
|
||||
HasError = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
// 根据协议类型检查地址
|
||||
if (Variable.Protocol == Core.Enums.ProtocolType.S7 &&
|
||||
string.IsNullOrWhiteSpace(Variable.S7Address))
|
||||
{
|
||||
ErrorMessage = "S7协议必须填写S7地址";
|
||||
HasError = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Variable.Protocol == Core.Enums.ProtocolType.OpcUa &&
|
||||
string.IsNullOrWhiteSpace(Variable.OpcUaNodeId))
|
||||
{
|
||||
ErrorMessage = "OPC UA协议必须填写节点ID";
|
||||
HasError = true;
|
||||
return false;
|
||||
}
|
||||
//检查变量是否存在
|
||||
var existVariable = _dataServices.Variables.FirstOrDefault(v => v.Name == Variable.Name || v.S7Address == Variable.S7Address || v.OpcUaNodeId == Variable.OpcUaNodeId);
|
||||
if (existVariable != null)
|
||||
{
|
||||
|
||||
if (Variable.Name == existVariable.Name)
|
||||
{
|
||||
ErrorMessage = $"变量名称:{existVariable.Name}已经存在。";
|
||||
HasError = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Variable.Protocol == Core.Enums.ProtocolType.S7 && Variable.S7Address == existVariable.S7Address)
|
||||
{
|
||||
ErrorMessage = $"变量S7地址:{existVariable.S7Address}已经存在。";
|
||||
HasError = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Variable.Protocol == Core.Enums.ProtocolType.OpcUa && Variable.OpcUaNodeId == existVariable.OpcUaNodeId)
|
||||
{
|
||||
ErrorMessage = $"变量OpcUa节点ID:{existVariable.OpcUaNodeId}已经存在。";
|
||||
HasError = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void CancleButton()
|
||||
{
|
||||
|
||||
@@ -62,7 +62,7 @@ public partial class MainViewModel : ViewModelBase
|
||||
// 发送消息加载数据
|
||||
MessageHelper.SendLoadMessage(LoadTypes.All);
|
||||
// 当菜单加载成功后,在前台显示菜单
|
||||
// dataServices.OnMenuTreeListChanged += (menus) => { Menus = new ObservableCollection<MenuBean>(menus); };
|
||||
// _dataServices.OnMenuTreeListChanged += (menus) => { Menus = new ObservableCollection<MenuBean>(menus); };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -57,13 +57,13 @@ public partial class MqttsViewModel : ViewModelBase
|
||||
_dialogService = dialogService;
|
||||
_dataServices = dataServices;
|
||||
|
||||
// if (dataServices.Mqtts == null || dataServices.Mqtts.Count == 0)
|
||||
// if (_dataServices.Mqtts == null || _dataServices.Mqtts.Count == 0)
|
||||
// {
|
||||
// MessageHelper.SendLoadMessage(LoadTypes.Mqtts);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// Mqtts = new ObservableCollection<Mqtt>(dataServices.Mqtts);
|
||||
// Mqtts = new ObservableCollection<Mqtt>(_dataServices.Mqtts);
|
||||
// }
|
||||
//
|
||||
//
|
||||
|
||||
@@ -483,65 +483,29 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable
|
||||
//
|
||||
/// <summary>
|
||||
/// 添加新的变量数据。
|
||||
/// 此命令通常绑定到UI中的“添加”按钮。
|
||||
/// </summary>
|
||||
/// 此命令通常绑定到UI中的“添加”按钮。
|
||||
/// <param name="variableTable">当前操作的变量表,用于设置新变量的所属ID。</param>
|
||||
[RelayCommand]
|
||||
private async void AddVariable(VariableTable variableTable)
|
||||
private async void AddVariable()
|
||||
{
|
||||
try
|
||||
{
|
||||
// 显示添加变量数据的对话框
|
||||
VariableDialogViewModel variableDialogViewModel=new VariableDialogViewModel("添加变量","添加变量");
|
||||
|
||||
VariableDialogViewModel variableDialogViewModel=App.Current.Services.GetRequiredService<VariableDialogViewModel>();
|
||||
variableDialogViewModel.Title = "添加变量";
|
||||
variableDialogViewModel.PrimaryButText = "添加变量";
|
||||
|
||||
var variableItemViewModel = await _dialogService.ShowDialogAsync(variableDialogViewModel);
|
||||
|
||||
// 如果用户取消或对话框未返回数据,则直接返回
|
||||
if (variableItemViewModel == null)
|
||||
return;
|
||||
|
||||
|
||||
// // 设置新变量的所属变量表ID
|
||||
// varData.VariableTableId = variableTable.Id;
|
||||
//
|
||||
// // --- 重复性检查逻辑开始 ---
|
||||
// bool isDuplicate = false;
|
||||
// string duplicateReason = string.Empty;
|
||||
//
|
||||
// // 检查名称是否重复
|
||||
// if (Variables.Any(v => v.Name == varData.Name))
|
||||
// {
|
||||
// isDuplicate = true;
|
||||
// duplicateReason = $"名称 '{varData.Name}' 已存在。";
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// // 根据协议类型检查S7地址或NodeId是否重复
|
||||
// if (variableTable.ProtocolType == ProtocolType.S7)
|
||||
// {
|
||||
// if (!string.IsNullOrEmpty(varData.S7Address) &&
|
||||
// Variables.Any(v => v.S7Address == varData.S7Address))
|
||||
// {
|
||||
// isDuplicate = true;
|
||||
// duplicateReason = $"S7地址 '{varData.S7Address}' 已存在。";
|
||||
// }
|
||||
// }
|
||||
// else if (variableTable.ProtocolType == ProtocolType.OpcUA)
|
||||
// {
|
||||
// if (!string.IsNullOrEmpty(varData.NodeId) && Variables.Any(v => v.NodeId == varData.NodeId))
|
||||
// {
|
||||
// isDuplicate = true;
|
||||
// duplicateReason = $"OPC UA NodeId '{varData.NodeId}' 已存在。";
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// if (isDuplicate)
|
||||
// {
|
||||
// NotificationHelper.ShowError($"添加变量失败:{duplicateReason}");
|
||||
// return;
|
||||
// }
|
||||
// // --- 重复性检查逻辑结束 ---
|
||||
variableItemViewModel.VariableTableId = CurrentVariableTable.Id;
|
||||
|
||||
|
||||
//
|
||||
// // 添加变量数据到数据库
|
||||
// var resVarData = await _varDataRepository.AddAsync(varData);
|
||||
|
||||
Reference in New Issue
Block a user