完成编辑变量对话框的更改
This commit is contained in:
@@ -13,6 +13,8 @@ namespace DMS.WPF.Profiles
|
||||
.ReverseMap();
|
||||
CreateMap<Variable, VariableItemViewModel>()
|
||||
.ReverseMap();
|
||||
CreateMap<VariableItemViewModel, VariableItemViewModel>();
|
||||
|
||||
|
||||
CreateMap<MenuBeanDto, MenuItemViewModel>()
|
||||
.ForMember(dest => dest.Children, opt => opt.Ignore())
|
||||
|
||||
@@ -3,6 +3,7 @@ using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using DMS.Application.DTOs;
|
||||
using DMS.Application.Interfaces;
|
||||
using DMS.Core.Enums;
|
||||
using DMS.Helper;
|
||||
using DMS.WPF.Services;
|
||||
using DMS.WPF.ViewModels.Items;
|
||||
@@ -17,6 +18,9 @@ public partial class VariableDialogViewModel : DialogViewModelBase<VariableItemV
|
||||
[ObservableProperty]
|
||||
private string _errorMessage = string.Empty;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool _isAddModel = true;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool _hasError;
|
||||
private readonly DataServices _dataServices;
|
||||
@@ -75,10 +79,20 @@ public partial class VariableDialogViewModel : DialogViewModelBase<VariableItemV
|
||||
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 (Variable.Name == existVariable.Name)
|
||||
{
|
||||
ErrorMessage = $"变量名称:{existVariable.Name}已经存在。";
|
||||
|
||||
@@ -252,36 +252,56 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable
|
||||
/// </summary>
|
||||
/// <param name="variableTable">当前操作的变量表,用于更新其内部的变量数据。</param>
|
||||
[RelayCommand]
|
||||
private async void UpdateVariable(VariableTable variableTable)
|
||||
private async void UpdateVariable()
|
||||
{
|
||||
// try
|
||||
// {
|
||||
// // 显示编辑变量数据的对话框,并传入当前选中的变量数据
|
||||
// var varData = await _dialogService.ShowEditVarDataDialog(SelectedVariable);
|
||||
//
|
||||
// // 如果用户取消或对话框未返回数据,则直接返回
|
||||
// if (varData == null)
|
||||
// return;
|
||||
//
|
||||
// // 设置变量数据的所属变量表ID
|
||||
// varData.VariableTableId = variableTable.Id;
|
||||
//
|
||||
// // 更新数据库中的变量数据
|
||||
// await _varDataRepository.UpdateAsync(varData);
|
||||
//
|
||||
// // 更新当前页面显示的数据:找到原数据在集合中的索引并替换
|
||||
// var index = variableTable.Variables.IndexOf(SelectedVariable);
|
||||
// if (index >= 0 && index < variableTable.Variables.Count)
|
||||
// variableTable.Variables[index] = varData; // 替换为编辑后的数据
|
||||
//
|
||||
// // 显示成功通知
|
||||
// NotificationHelper.ShowSuccess($"编辑变量成功:{varData?.Name}");
|
||||
// }
|
||||
// catch (Exception e)
|
||||
// {
|
||||
// // 捕获并显示错误通知
|
||||
// NotificationHelper.ShowError($"编辑变量的过程中发生了不可预期的错误:{e.Message}", e);
|
||||
// }
|
||||
try
|
||||
{
|
||||
// 检查是否有变量被选中
|
||||
if (SelectedVariable == null)
|
||||
{
|
||||
NotificationHelper.ShowInfo("请选择要编辑的变量");
|
||||
return;
|
||||
}
|
||||
|
||||
// 显示编辑变量数据的对话框,并传入当前选中的变量数据
|
||||
VariableDialogViewModel variableDialogViewModel = App.Current.Services.GetRequiredService<VariableDialogViewModel>();
|
||||
variableDialogViewModel.Title = "编辑变量";
|
||||
variableDialogViewModel.PrimaryButText = "保存修改";
|
||||
variableDialogViewModel.IsAddModel = false;
|
||||
// 创建一个副本用于编辑,避免直接修改原数据
|
||||
var variableToEdit = new VariableItemViewModel();
|
||||
_mapper.Map(SelectedVariable, variableToEdit);
|
||||
variableDialogViewModel.Variable = variableToEdit;
|
||||
|
||||
var editedVariable = await _dialogService.ShowDialogAsync(variableDialogViewModel);
|
||||
|
||||
// 如果用户取消或对话框未返回数据,则直接返回
|
||||
if (editedVariable == null)
|
||||
return;
|
||||
|
||||
// 更新时间戳
|
||||
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>
|
||||
@@ -492,15 +512,15 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable
|
||||
try
|
||||
{
|
||||
// 显示添加变量数据的对话框
|
||||
VariableDialogViewModel variableDialogViewModel=App.Current.Services.GetRequiredService<VariableDialogViewModel>();
|
||||
VariableItemViewModel variableItem=new VariableItemViewModel();
|
||||
variableItem.Protocol=CurrentVariableTable.Protocol;
|
||||
VariableDialogViewModel variableDialogViewModel = App.Current.Services.GetRequiredService<VariableDialogViewModel>();
|
||||
VariableItemViewModel variableItem = new VariableItemViewModel();
|
||||
variableItem.Protocol = CurrentVariableTable.Protocol;
|
||||
variableDialogViewModel.Title = "添加变量";
|
||||
variableDialogViewModel.PrimaryButText = "添加变量";
|
||||
variableDialogViewModel.Variable = variableItem;
|
||||
|
||||
var variableItemViewModel = await _dialogService.ShowDialogAsync(variableDialogViewModel);
|
||||
|
||||
|
||||
// 如果用户取消或对话框未返回数据,则直接返回
|
||||
if (variableItemViewModel == null)
|
||||
return;
|
||||
@@ -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);
|
||||
// // 更新当前页面显示的数据:将新变量添加到集合中
|
||||
_variableItemList.Add(variableItemViewModel);
|
||||
_dataServices.AddVariable(variableItemViewModel);
|
||||
//
|
||||
// // 显示成功通知
|
||||
NotificationHelper.ShowSuccess($"添加变量成功:{variableItemViewModel.Name}");
|
||||
NotificationHelper.ShowSuccess($"添加变量成功:{variableItemViewModel.Name}");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
||||
@@ -82,6 +82,7 @@
|
||||
Grid.Column="2"
|
||||
MinWidth="150"
|
||||
hc:InfoElement.Title="S7地址:"
|
||||
IsEnabled="{Binding IsAddModel}"
|
||||
Text="{Binding Variable.S7Address, UpdateSourceTrigger=PropertyChanged}">
|
||||
<hc:TextBox.Style>
|
||||
<Style BasedOn="{StaticResource {x:Type hc:TextBox}}" TargetType="hc:TextBox">
|
||||
@@ -101,6 +102,7 @@
|
||||
Grid.Column="2"
|
||||
MinWidth="150"
|
||||
hc:InfoElement.Title="OpcUa节点ID:"
|
||||
IsEnabled="{Binding IsAddModel}"
|
||||
Text="{Binding Variable.OpcUaNodeId, UpdateSourceTrigger=PropertyChanged}">
|
||||
<hc:TextBox.Style>
|
||||
<Style BasedOn="{StaticResource {x:Type hc:TextBox}}" TargetType="hc:TextBox">
|
||||
@@ -162,6 +164,7 @@
|
||||
Grid.Column="2"
|
||||
Margin="0,15,0,0"
|
||||
hc:InfoElement.Title="数据类型:"
|
||||
IsEnabled="{Binding IsAddModel}"
|
||||
ItemsSource="{Binding Source={StaticResource CSharpDataType}}"
|
||||
SelectedItem="{Binding Variable.CSharpDataType, UpdateSourceTrigger=PropertyChanged}">
|
||||
<ComboBox.ItemTemplate>
|
||||
|
||||
@@ -9,6 +9,7 @@ public partial class VariableDialog
|
||||
{
|
||||
private const int ContentAreaMaxWidth = 1200;
|
||||
private const int ContentAreaMaxHeight = 900;
|
||||
|
||||
|
||||
public VariableDialog()
|
||||
{
|
||||
|
||||
@@ -53,7 +53,7 @@
|
||||
</ui:AppBarButton>
|
||||
|
||||
<ui:AppBarButton
|
||||
Command="{Binding EditVarDataCommand}"
|
||||
Command="{Binding UpdateVariableCommand}"
|
||||
CommandParameter="{Binding VariableTable}"
|
||||
Label="编辑变量">
|
||||
<ui:AppBarButton.Icon>
|
||||
|
||||
Reference in New Issue
Block a user