2025-08-24 14:42:31 +08:00
|
|
|
using AutoMapper;
|
2025-08-23 16:01:30 +08:00
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
|
|
using CommunityToolkit.Mvvm.Input;
|
2025-08-24 14:42:31 +08:00
|
|
|
using DMS.Application.DTOs;
|
|
|
|
|
using DMS.Application.Interfaces;
|
2025-09-16 12:29:09 +08:00
|
|
|
using DMS.Application.Interfaces.Database;
|
2025-08-24 17:01:33 +08:00
|
|
|
using DMS.Core.Enums;
|
2025-09-09 13:35:16 +08:00
|
|
|
using DMS.WPF.Interfaces;
|
2025-08-24 14:42:31 +08:00
|
|
|
using DMS.WPF.Services;
|
2025-10-06 18:17:56 +08:00
|
|
|
using DMS.WPF.ItemViewModel;
|
2025-08-23 16:01:30 +08:00
|
|
|
|
|
|
|
|
namespace DMS.WPF.ViewModels.Dialogs;
|
|
|
|
|
|
2025-10-06 18:17:56 +08:00
|
|
|
public partial class VariableDialogViewModel : DialogViewModelBase<VariableItem>
|
2025-08-23 16:01:30 +08:00
|
|
|
{
|
|
|
|
|
[ObservableProperty]
|
2025-10-06 18:17:56 +08:00
|
|
|
private VariableItem _variable;
|
2025-08-23 16:01:30 +08:00
|
|
|
|
2025-08-24 14:42:31 +08:00
|
|
|
[ObservableProperty]
|
|
|
|
|
private string _errorMessage = string.Empty;
|
|
|
|
|
|
2025-08-24 17:01:33 +08:00
|
|
|
[ObservableProperty]
|
|
|
|
|
private bool _isAddModel = true;
|
|
|
|
|
|
2025-08-24 14:42:31 +08:00
|
|
|
[ObservableProperty]
|
|
|
|
|
private bool _hasError;
|
2025-09-09 13:35:16 +08:00
|
|
|
|
|
|
|
|
private readonly IWPFDataService _wpfDataService;
|
|
|
|
|
private readonly IDataStorageService _dataStorageService;
|
2025-08-24 14:42:31 +08:00
|
|
|
private readonly IVariableAppService _variableAppService;
|
|
|
|
|
private readonly IMapper _mapper;
|
|
|
|
|
|
2025-09-09 13:35:16 +08:00
|
|
|
public VariableDialogViewModel(IWPFDataService wpfDataService,IDataStorageService dataStorageService, IVariableAppService variableAppService, IMapper mapper)
|
2025-08-23 16:01:30 +08:00
|
|
|
{
|
2025-08-24 14:42:31 +08:00
|
|
|
|
2025-10-06 18:17:56 +08:00
|
|
|
Variable = new VariableItem();
|
2025-08-24 14:42:31 +08:00
|
|
|
|
2025-09-09 13:35:16 +08:00
|
|
|
_wpfDataService = wpfDataService;
|
|
|
|
|
_dataStorageService = dataStorageService;
|
2025-08-24 14:42:31 +08:00
|
|
|
this._variableAppService = variableAppService;
|
|
|
|
|
this._mapper = mapper;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<bool> PrimaryButtonAsync()
|
|
|
|
|
{
|
|
|
|
|
// 清除之前的错误信息
|
|
|
|
|
ErrorMessage = string.Empty;
|
|
|
|
|
HasError = false;
|
|
|
|
|
// 验证输入
|
|
|
|
|
bool isValid = ValidateInputAsync();
|
|
|
|
|
if (isValid)
|
2025-08-23 16:01:30 +08:00
|
|
|
{
|
2025-08-24 14:42:31 +08:00
|
|
|
Close(Variable);
|
2025-08-23 16:01:30 +08:00
|
|
|
}
|
2025-08-24 14:42:31 +08:00
|
|
|
|
|
|
|
|
return isValid;
|
2025-08-23 16:01:30 +08:00
|
|
|
}
|
2025-08-24 14:42:31 +08:00
|
|
|
|
|
|
|
|
private bool ValidateInputAsync()
|
2025-08-23 16:01:30 +08:00
|
|
|
{
|
2025-08-24 14:42:31 +08:00
|
|
|
// 检查变量名称是否为空
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
//检查变量是否存在
|
2025-09-16 13:05:37 +08:00
|
|
|
var existVariables = _dataStorageService.Variables.Where(v => v.Value.Name == Variable.Name || (v.Value.Protocol == ProtocolType.S7 && v.Value.S7Address == Variable.S7Address) || (v.Value.Protocol == ProtocolType.OpcUa && v.Value.OpcUaNodeId == Variable.OpcUaNodeId)).Select(v=>v.Value).ToList();
|
2025-10-06 18:17:56 +08:00
|
|
|
VariableItem existVariable = null;
|
2025-08-24 17:01:33 +08:00
|
|
|
if (IsAddModel)
|
|
|
|
|
{
|
|
|
|
|
existVariable = existVariables.FirstOrDefault();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
existVariable = existVariables.FirstOrDefault(v => v.Id != Variable.Id);
|
2025-09-16 13:05:37 +08:00
|
|
|
|
2025-08-24 17:01:33 +08:00
|
|
|
}
|
|
|
|
|
|
2025-08-24 14:42:31 +08:00
|
|
|
if (existVariable != null)
|
|
|
|
|
{
|
2025-08-24 17:01:33 +08:00
|
|
|
|
2025-08-24 14:42:31 +08:00
|
|
|
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;
|
2025-08-23 16:01:30 +08:00
|
|
|
}
|
2025-08-24 14:42:31 +08:00
|
|
|
|
2025-08-23 16:01:30 +08:00
|
|
|
[RelayCommand]
|
|
|
|
|
private void CancleButton()
|
|
|
|
|
{
|
|
|
|
|
Close(null);
|
|
|
|
|
}
|
|
|
|
|
}
|