完成编辑变量对话框的更改

This commit is contained in:
2025-08-24 17:01:33 +08:00
parent f9e094ffbd
commit c813fe63c3
6 changed files with 78 additions and 38 deletions

View File

@@ -13,6 +13,8 @@ namespace DMS.WPF.Profiles
.ReverseMap(); .ReverseMap();
CreateMap<Variable, VariableItemViewModel>() CreateMap<Variable, VariableItemViewModel>()
.ReverseMap(); .ReverseMap();
CreateMap<VariableItemViewModel, VariableItemViewModel>();
CreateMap<MenuBeanDto, MenuItemViewModel>() CreateMap<MenuBeanDto, MenuItemViewModel>()
.ForMember(dest => dest.Children, opt => opt.Ignore()) .ForMember(dest => dest.Children, opt => opt.Ignore())

View File

@@ -3,6 +3,7 @@ using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Input;
using DMS.Application.DTOs; using DMS.Application.DTOs;
using DMS.Application.Interfaces; using DMS.Application.Interfaces;
using DMS.Core.Enums;
using DMS.Helper; using DMS.Helper;
using DMS.WPF.Services; using DMS.WPF.Services;
using DMS.WPF.ViewModels.Items; using DMS.WPF.ViewModels.Items;
@@ -17,6 +18,9 @@ public partial class VariableDialogViewModel : DialogViewModelBase<VariableItemV
[ObservableProperty] [ObservableProperty]
private string _errorMessage = string.Empty; private string _errorMessage = string.Empty;
[ObservableProperty]
private bool _isAddModel = true;
[ObservableProperty] [ObservableProperty]
private bool _hasError; private bool _hasError;
private readonly DataServices _dataServices; private readonly DataServices _dataServices;
@@ -75,7 +79,17 @@ public partial class VariableDialogViewModel : DialogViewModelBase<VariableItemV
return false; return false;
} }
//检查变量是否存在 //检查变量是否存在
var existVariable = _dataServices.Variables.FirstOrDefault(v => v.Name == Variable.Name || v.S7Address == Variable.S7Address || v.OpcUaNodeId == Variable.OpcUaNodeId); var existVariables = _dataServices.Variables.Where(v => v.Name == Variable.Name || (v.Protocol == ProtocolType.S7 && v.S7Address == Variable.S7Address) || (v.Protocol == ProtocolType.OpcUa && v.OpcUaNodeId == Variable.OpcUaNodeId)).ToList();
VariableItemViewModel existVariable = null;
if (IsAddModel)
{
existVariable = existVariables.FirstOrDefault();
}
else
{
existVariable = existVariables.FirstOrDefault(v => v.Id != Variable.Id);
}
if (existVariable != null) if (existVariable != null)
{ {

View File

@@ -252,36 +252,56 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable
/// </summary> /// </summary>
/// <param name="variableTable">当前操作的变量表,用于更新其内部的变量数据。</param> /// <param name="variableTable">当前操作的变量表,用于更新其内部的变量数据。</param>
[RelayCommand] [RelayCommand]
private async void UpdateVariable(VariableTable variableTable) private async void UpdateVariable()
{ {
// try try
// { {
// // 显示编辑变量数据的对话框,并传入当前选中的变量数据 // 检查是否有变量被选中
// var varData = await _dialogService.ShowEditVarDataDialog(SelectedVariable); if (SelectedVariable == null)
// {
// // 如果用户取消或对话框未返回数据,则直接返回 NotificationHelper.ShowInfo("请选择要编辑的变量");
// if (varData == null) return;
// return; }
//
// // 设置变量数据的所属变量表ID // 显示编辑变量数据的对话框,并传入当前选中的变量数据
// varData.VariableTableId = variableTable.Id; VariableDialogViewModel variableDialogViewModel = App.Current.Services.GetRequiredService<VariableDialogViewModel>();
// variableDialogViewModel.Title = "编辑变量";
// // 更新数据库中的变量数据 variableDialogViewModel.PrimaryButText = "保存修改";
// await _varDataRepository.UpdateAsync(varData); variableDialogViewModel.IsAddModel = false;
// // 创建一个副本用于编辑,避免直接修改原数据
// // 更新当前页面显示的数据:找到原数据在集合中的索引并替换 var variableToEdit = new VariableItemViewModel();
// var index = variableTable.Variables.IndexOf(SelectedVariable); _mapper.Map(SelectedVariable, variableToEdit);
// if (index >= 0 && index < variableTable.Variables.Count) variableDialogViewModel.Variable = variableToEdit;
// variableTable.Variables[index] = varData; // 替换为编辑后的数据
// var editedVariable = await _dialogService.ShowDialogAsync(variableDialogViewModel);
// // 显示成功通知
// NotificationHelper.ShowSuccess($"编辑变量成功:{varData?.Name}"); // 如果用户取消或对话框未返回数据,则直接返回
// } if (editedVariable == null)
// catch (Exception e) return;
// {
// // 捕获并显示错误通知 // 更新时间戳
// NotificationHelper.ShowError($"编辑变量的过程中发生了不可预期的错误:{e.Message}", e); editedVariable.UpdatedAt = DateTime.Now;
// }
// 更新数据库中的变量数据
var updateResult = await _variableAppService.UpdateVariableAsync(_mapper.Map<VariableDto>(editedVariable));
if (updateResult > 0)
{
// 更新当前页面显示的数据:找到原数据在集合中的索引并替换
_mapper.Map(editedVariable, SelectedVariable); // 更新选中项的属性
// 显示成功通知
NotificationHelper.ShowSuccess($"编辑变量成功:{editedVariable.Name}");
}
else
{
NotificationHelper.ShowError("编辑变量失败");
}
}
catch (Exception e)
{
// 捕获并显示错误通知
NotificationHelper.ShowError($"编辑变量的过程中发生了不可预期的错误:{e.Message}", e);
}
} }
/// <summary> /// <summary>
@@ -492,9 +512,9 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable
try try
{ {
// 显示添加变量数据的对话框 // 显示添加变量数据的对话框
VariableDialogViewModel variableDialogViewModel=App.Current.Services.GetRequiredService<VariableDialogViewModel>(); VariableDialogViewModel variableDialogViewModel = App.Current.Services.GetRequiredService<VariableDialogViewModel>();
VariableItemViewModel variableItem=new VariableItemViewModel(); VariableItemViewModel variableItem = new VariableItemViewModel();
variableItem.Protocol=CurrentVariableTable.Protocol; variableItem.Protocol = CurrentVariableTable.Protocol;
variableDialogViewModel.Title = "添加变量"; variableDialogViewModel.Title = "添加变量";
variableDialogViewModel.PrimaryButText = "添加变量"; variableDialogViewModel.PrimaryButText = "添加变量";
variableDialogViewModel.Variable = variableItem; variableDialogViewModel.Variable = variableItem;
@@ -512,14 +532,14 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable
// // 添加变量数据到数据库 // // 添加变量数据到数据库
var addVariable= await _variableAppService.CreateVariableAsync(_mapper.Map<VariableDto>(variableItemViewModel)); var addVariable = await _variableAppService.CreateVariableAsync(_mapper.Map<VariableDto>(variableItemViewModel));
_mapper.Map(addVariable, variableItemViewModel); _mapper.Map(addVariable, variableItemViewModel);
// // 更新当前页面显示的数据:将新变量添加到集合中 // // 更新当前页面显示的数据:将新变量添加到集合中
_variableItemList.Add(variableItemViewModel); _variableItemList.Add(variableItemViewModel);
_dataServices.AddVariable(variableItemViewModel); _dataServices.AddVariable(variableItemViewModel);
// //
// // 显示成功通知 // // 显示成功通知
NotificationHelper.ShowSuccess($"添加变量成功:{variableItemViewModel.Name}"); NotificationHelper.ShowSuccess($"添加变量成功:{variableItemViewModel.Name}");
} }
catch (Exception e) catch (Exception e)
{ {

View File

@@ -82,6 +82,7 @@
Grid.Column="2" Grid.Column="2"
MinWidth="150" MinWidth="150"
hc:InfoElement.Title="S7地址:" hc:InfoElement.Title="S7地址:"
IsEnabled="{Binding IsAddModel}"
Text="{Binding Variable.S7Address, UpdateSourceTrigger=PropertyChanged}"> Text="{Binding Variable.S7Address, UpdateSourceTrigger=PropertyChanged}">
<hc:TextBox.Style> <hc:TextBox.Style>
<Style BasedOn="{StaticResource {x:Type hc:TextBox}}" TargetType="hc:TextBox"> <Style BasedOn="{StaticResource {x:Type hc:TextBox}}" TargetType="hc:TextBox">
@@ -101,6 +102,7 @@
Grid.Column="2" Grid.Column="2"
MinWidth="150" MinWidth="150"
hc:InfoElement.Title="OpcUa节点ID:" hc:InfoElement.Title="OpcUa节点ID:"
IsEnabled="{Binding IsAddModel}"
Text="{Binding Variable.OpcUaNodeId, UpdateSourceTrigger=PropertyChanged}"> Text="{Binding Variable.OpcUaNodeId, UpdateSourceTrigger=PropertyChanged}">
<hc:TextBox.Style> <hc:TextBox.Style>
<Style BasedOn="{StaticResource {x:Type hc:TextBox}}" TargetType="hc:TextBox"> <Style BasedOn="{StaticResource {x:Type hc:TextBox}}" TargetType="hc:TextBox">
@@ -162,6 +164,7 @@
Grid.Column="2" Grid.Column="2"
Margin="0,15,0,0" Margin="0,15,0,0"
hc:InfoElement.Title="数据类型:" hc:InfoElement.Title="数据类型:"
IsEnabled="{Binding IsAddModel}"
ItemsSource="{Binding Source={StaticResource CSharpDataType}}" ItemsSource="{Binding Source={StaticResource CSharpDataType}}"
SelectedItem="{Binding Variable.CSharpDataType, UpdateSourceTrigger=PropertyChanged}"> SelectedItem="{Binding Variable.CSharpDataType, UpdateSourceTrigger=PropertyChanged}">
<ComboBox.ItemTemplate> <ComboBox.ItemTemplate>

View File

@@ -10,6 +10,7 @@ public partial class VariableDialog
private const int ContentAreaMaxWidth = 1200; private const int ContentAreaMaxWidth = 1200;
private const int ContentAreaMaxHeight = 900; private const int ContentAreaMaxHeight = 900;
public VariableDialog() public VariableDialog()
{ {
InitializeComponent(); InitializeComponent();

View File

@@ -53,7 +53,7 @@
</ui:AppBarButton> </ui:AppBarButton>
<ui:AppBarButton <ui:AppBarButton
Command="{Binding EditVarDataCommand}" Command="{Binding UpdateVariableCommand}"
CommandParameter="{Binding VariableTable}" CommandParameter="{Binding VariableTable}"
Label="编辑变量"> Label="编辑变量">
<ui:AppBarButton.Icon> <ui:AppBarButton.Icon>