修复了一些Bug

This commit is contained in:
2025-07-17 09:45:58 +08:00
parent 28c2754d53
commit d8e46e4707
5 changed files with 187 additions and 39 deletions

View File

@@ -133,7 +133,7 @@ public partial class App : Application
services.AddSingleton<DataTransformViewModel>(); services.AddSingleton<DataTransformViewModel>();
services.AddSingleton<SettingViewModel>(); services.AddSingleton<SettingViewModel>();
services.AddSingleton<DataTransformViewModel>(); services.AddSingleton<DataTransformViewModel>();
services.AddTransient<VariableTableViewModel>(); services.AddSingleton<VariableTableViewModel>();
services.AddScoped<MqttServerDetailViewModel>(); services.AddScoped<MqttServerDetailViewModel>();
services.AddSingleton<DeviceDetailViewModel>(); services.AddSingleton<DeviceDetailViewModel>();
services.AddScoped<MqttsViewModel>(); services.AddScoped<MqttsViewModel>();

View File

@@ -6,7 +6,7 @@ namespace PMSWPF.Models;
/// <summary> /// <summary>
/// 表示变量数据信息。 /// 表示变量数据信息。
/// </summary> /// </summary>
public partial class VariableData : ObservableObject public partial class VariableData : ObservableObject, IEquatable<VariableData>
{ {
/// <summary> /// <summary>
/// 变量唯一标识符。 /// 变量唯一标识符。
@@ -181,4 +181,70 @@ public partial class VariableData : ObservableObject
{ {
IsModified = true; IsModified = true;
} }
public override bool Equals(object? obj)
{
return Equals(obj as VariableData);
}
public bool Equals(VariableData? other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
// Compare all relevant properties for equality
return Name == other.Name &&
NodeId == other.NodeId &&
S7Address == other.S7Address &&
OpcUaEndpointUrl == other.OpcUaEndpointUrl &&
OpcUaNodeId == other.OpcUaNodeId &&
Description == other.Description &&
ProtocolType == other.ProtocolType &&
SignalType == other.SignalType &&
DataType == other.DataType &&
DataValue == other.DataValue &&
DisplayValue == other.DisplayValue &&
IsActive == other.IsActive &&
IsSave == other.IsSave &&
IsAlarm == other.IsAlarm &&
PollLevelType == other.PollLevelType &&
OpcUaUpdateType == other.OpcUaUpdateType &&
IsDeleted == other.IsDeleted &&
AlarmMax == other.AlarmMax &&
AlarmMin == other.AlarmMin &&
Converstion == other.Converstion &&
SaveRange == other.SaveRange &&
CreateTime == other.CreateTime &&
VariableTableId == other.VariableTableId;
}
public override int GetHashCode()
{
// Combine hash codes of all relevant properties
var hash = new HashCode();
hash.Add(Name);
hash.Add(NodeId);
hash.Add(S7Address);
hash.Add(OpcUaEndpointUrl);
hash.Add(OpcUaNodeId);
hash.Add(Description);
hash.Add(ProtocolType);
hash.Add(SignalType);
hash.Add(DataType);
hash.Add(DataValue);
hash.Add(DisplayValue);
hash.Add(IsActive);
hash.Add(IsSave);
hash.Add(IsAlarm);
hash.Add(PollLevelType);
hash.Add(OpcUaUpdateType);
hash.Add(IsDeleted);
hash.Add(AlarmMax);
hash.Add(AlarmMin);
hash.Add(Converstion);
hash.Add(SaveRange);
hash.Add(CreateTime);
hash.Add(VariableTableId);
return hash.ToHashCode();
}
} }

View File

@@ -230,4 +230,11 @@ public class DialogService :IDialogService
} }
return null; return null;
} }
public async Task ShowImportResultDialog(List<string> importedVariables, List<string> existingVariables)
{
var vm = new ImportResultDialogViewModel(importedVariables, existingVariables);
var dialog = new ImportResultDialog(vm);
await dialog.ShowAsync();
}
} }

View File

@@ -26,4 +26,5 @@ public interface IDialogService
Task<List<VariableData>> ShowOpcUaImportDialog(string endpointUrl); Task<List<VariableData>> ShowOpcUaImportDialog(string endpointUrl);
Task<OpcUaUpdateType?> ShowOpcUaUpdateTypeDialog(); Task<OpcUaUpdateType?> ShowOpcUaUpdateTypeDialog();
Task<bool?> ShowIsActiveDialog(bool currentIsActive); Task<bool?> ShowIsActiveDialog(bool currentIsActive);
Task ShowImportResultDialog(List<string> importedVariables, List<string> existingVariables);
} }

View File

@@ -88,7 +88,8 @@ partial class VariableTableViewModel : ViewModelBase
/// <summary> /// <summary>
/// 用于在UI中显示和过滤变量数据的视图集合。 /// 用于在UI中显示和过滤变量数据的视图集合。
/// </summary> /// </summary>
public ICollectionView VariableDataView { get; private set; } [ObservableProperty]
private ICollectionView variableDataView;
/// <summary> /// <summary>
/// 指示视图是否已完成首次加载。 /// 指示视图是否已完成首次加载。
@@ -130,16 +131,19 @@ partial class VariableTableViewModel : ViewModelBase
/// 初始化服务、数据仓库和变量数据集合视图。 /// 初始化服务、数据仓库和变量数据集合视图。
/// </summary> /// </summary>
/// <param name="dialogService">对话服务接口的实例。</param> /// <param name="dialogService">对话服务接口的实例。</param>
private readonly DataServices _dataServices;
public VariableTableViewModel(IMapper mapper, IDialogService dialogService, VarTableRepository varTableRepository, public VariableTableViewModel(IMapper mapper, IDialogService dialogService, VarTableRepository varTableRepository,
VarDataRepository varDataRepository) VarDataRepository varDataRepository, DataServices dataServices)
{ {
_mapper = mapper; _mapper = mapper;
_dialogService = dialogService; _dialogService = dialogService;
_dataServices = dataServices;
IsLoadCompletion = false; // 初始设置为 false表示未完成加载 IsLoadCompletion = false; // 初始设置为 false表示未完成加载
_varTableRepository = varTableRepository; _varTableRepository = varTableRepository;
_varDataRepository = varDataRepository; _varDataRepository = varDataRepository;
_dataVariables = new ObservableCollection<VariableData>(); // 初始化集合 _dataVariables = new ObservableCollection<VariableData>(); // 初始化集合
VariableDataView = CollectionViewSource.GetDefaultView(_dataVariables); // 获取集合视图 VariableDataView = CollectionViewSource.GetDefaultView(DataVariables); // 获取集合视图
VariableDataView.Filter = FilterVariables; // 设置过滤方法 VariableDataView.Filter = FilterVariables; // 设置过滤方法
} }
@@ -202,13 +206,14 @@ partial class VariableTableViewModel : ViewModelBase
// 如果变量表包含数据变量,则进行初始化 // 如果变量表包含数据变量,则进行初始化
if (VariableTable.DataVariables != null) if (VariableTable.DataVariables != null)
{ {
// 将变量表中的数据变量复制到可观察集合中 // // 将变量表中的数据变量复制到可观察集合中
_dataVariables.Clear(); // 清空现有集合 DataVariables.Clear(); // 清空现有集合
foreach (var item in VariableTable.DataVariables) foreach (var item in VariableTable.DataVariables)
{ {
_dataVariables.Add(item); // 添加新项 DataVariables.Add(item); // 添加新项
} }
VariableDataView.Refresh(); // 刷新视图以应用过滤和排序 VariableDataView.Refresh(); // 刷新视图以应用过滤和排序
// 创建原始数据的深拷贝备份,用于在取消保存时还原 // 创建原始数据的深拷贝备份,用于在取消保存时还原
@@ -346,26 +351,53 @@ partial class VariableTableViewModel : ViewModelBase
// 显示处理中的对话框 // 显示处理中的对话框
processingDialog = _dialogService.ShowProcessingDialog("正在处理...", "正在导入变量,请稍等片刻...."); processingDialog = _dialogService.ShowProcessingDialog("正在处理...", "正在导入变量,请稍等片刻....");
// 为导入的每个变量设置创建时间和所属变量表ID List<VariableData> newVariables = new List<VariableData>();
List<string> importedVariableNames = new List<string>();
List<string> existingVariableNames = new List<string>();
foreach (var variableData in importVarDataList) foreach (var variableData in importVarDataList)
{ {
variableData.CreateTime = DateTime.Now; // 判断是否存在重复变量
variableData.IsActive = true; bool isDuplicate = _dataServices.AllVariables.Values.Any(existingVar =>
variableData.VariableTableId = VariableTable.Id; (existingVar.Name == variableData.Name) ||
(!string.IsNullOrEmpty(
variableData.NodeId) &&
existingVar.NodeId ==
variableData.NodeId) ||
(!string.IsNullOrEmpty(
variableData.S7Address) &&
existingVar.S7Address ==
variableData.S7Address)
);
if (isDuplicate)
{
existingVariableNames.Add(variableData.Name);
}
else
{
variableData.CreateTime = DateTime.Now;
variableData.IsActive = true;
variableData.VariableTableId = VariableTable.Id;
newVariables.Add(variableData);
importedVariableNames.Add(variableData.Name);
}
} }
// 批量插入变量数据到数据库 if (newVariables.Any())
var resVarDataCount = await _varDataRepository.AddAsync(importVarDataList); {
// 批量插入新变量数据到数据库
var resVarDataCount = await _varDataRepository.AddAsync(newVariables);
NlogHelper.Info($"成功导入变量:{resVarDataCount}个。");
}
// 更新界面显示的数据:重新从数据库加载所有变量数据 // 更新界面显示的数据:重新从数据库加载所有变量数据
await RefreshDataView(); await RefreshDataView();
processingDialog?.Hide(); // 隐藏处理中的对话框 processingDialog?.Hide(); // 隐藏处理中的对话框
// 显示成功通知并记录日志 // 显示导入结果对话框
string msgSuccess = $"成功导入变量:{resVarDataCount}个。"; await _dialogService.ShowImportResultDialog(importedVariableNames, existingVariableNames);
NlogHelper.Info(msgSuccess);
NotificationHelper.ShowSuccess(msgSuccess);
} }
catch (Exception e) catch (Exception e)
{ {
@@ -437,22 +469,64 @@ partial class VariableTableViewModel : ViewModelBase
} }
} }
/// <summary> // /// <summary>
/// 刷新数据列表 // /// 刷新数据列表
/// </summary> // /// </summary>
private async Task RefreshDataView() private async Task RefreshDataView()
{ {
// // 更新界面显示的数据:重新从数据库加载所有变量数据 // 更新界面显示的数据:重新从数据库加载所有变量数据
// VariableTable.DataVariables = await _varDataRepository.GetByVariableTableIdAsync(VariableTable.Id);
// DataVariables.Clear(); var varList = await _varDataRepository.GetByVariableTableIdAsync(VariableTable.Id);
// foreach (var item in VariableTable.DataVariables) // 处理删除
// { if (varList.Count < DataVariables.Count)
// DataVariables.AddAsync(item); {
// } for (int i = DataVariables.Count-1; i >=0; i--)
// {
// VariableDataView.Refresh(); bool isExist=false;
foreach (var variableData in varList)
{
if (variableData.Id==DataVariables[i].Id)
{
isExist=true;
}
}
if (!isExist)
{
DataVariables.RemoveAt(i);
}
}
}
// 处理修改和 添加
foreach (var newVariable in varList)
{
bool isExiset = false;
for (int i = 0; i < DataVariables.Count; i++)
{
var oldVariable = DataVariables[i];
// 判断是否存在
if (newVariable.Id == oldVariable.Id)
{
isExiset = true;
//判断是否相等
if (!oldVariable.Equals(newVariable))
{
DataVariables[i] = newVariable;
}
}
}
// 不存在则添加
if (!isExiset)
{
DataVariables.Add(newVariable);
}
}
VariableDataView.Refresh();
} }
//
/// <summary> /// <summary>
/// 添加新的变量数据。 /// 添加新的变量数据。
/// 此命令通常绑定到UI中的“添加”按钮。 /// 此命令通常绑定到UI中的“添加”按钮。
@@ -474,10 +548,15 @@ partial class VariableTableViewModel : ViewModelBase
varData.VariableTableId = variableTable.Id; varData.VariableTableId = variableTable.Id;
// 添加变量数据到数据库 // 添加变量数据到数据库
await _varDataRepository.AddAsync(varData); var resVarData = await _varDataRepository.AddAsync(varData);
if (resVarData == null)
{
NotificationHelper.ShowError($"添加变量失败了:{varData?.Name}");
return;
}
// 更新当前页面显示的数据:将新变量添加到集合中 // 更新当前页面显示的数据:将新变量添加到集合中
DataVariables.Add(varData); DataVariables.Add(resVarData);
// 显示成功通知 // 显示成功通知
NotificationHelper.ShowSuccess($"添加变量成功:{varData?.Name}"); NotificationHelper.ShowSuccess($"添加变量成功:{varData?.Name}");
@@ -697,14 +776,9 @@ partial class VariableTableViewModel : ViewModelBase
} }
await _varDataRepository.UpdateAsync(validVariables); await _varDataRepository.UpdateAsync(validVariables);
// 更新界面 // 更新界面
foreach (var variable in validVariables) await RefreshDataView();
{
var displayVar = DataVariables.FirstOrDefault(v => v.Id == variable.Id);
if (displayVar != null)
displayVar.IsActive = newIsActive;
}
NotificationHelper.ShowSuccess($"已成功将 {validVariables.Count} 个变量的启用状态修改为 {newIsActive}"); NotificationHelper.ShowSuccess($"已成功将 {validVariables.Count} 个变量的启用状态修改为 {newIsActive}");