refactor:完成变量表的重构添加,删除,更新

This commit is contained in:
2025-10-23 16:25:22 +08:00
parent 54d040b45f
commit 125348486a
15 changed files with 329 additions and 419 deletions

View File

@@ -22,6 +22,7 @@ public partial class DeviceDetailViewModel : ViewModelBase
private readonly IDialogService _dialogService;
private readonly IViewDataService _viewDataService;
private readonly INavigationService _navigationService;
private readonly IVariableTableViewService _variableTableViewService;
private readonly IViewCenterService _wpfDataService;
[ObservableProperty]
@@ -38,57 +39,49 @@ public partial class DeviceDetailViewModel : ViewModelBase
public DeviceDetailViewModel(IMapper mapper, IDialogService dialogService, IViewDataService dataStorageService,
INavigationService navigationService,
IViewCenterService wpfDataService, INotificationService notificationService)
IVariableTableViewService variableTableViewService, INotificationService notificationService)
{
_mapper = mapper;
_dialogService = dialogService;
_viewDataService = dataStorageService;
_navigationService = navigationService;
_variableTableViewService = variableTableViewService;
_notificationService = notificationService;
_wpfDataService = wpfDataService;
}
[RelayCommand]
private async Task AddVariableTable()
private async Task AddVariableTable(DeviceItem deviceItem = null)
{
if (IsBusy) return;
IsBusy = true;
try
{
VariableTableDialogViewModel variableTableDialogViewModel = new VariableTableDialogViewModel()
{
PrimaryButText = "添加变量表"
};
// 1. 显示添加设备对话框
var VariableTableItem = await _dialogService.ShowDialogAsync(variableTableDialogViewModel);
// 如果用户取消或对话框未返回设备,则直接返回
if (VariableTableItem == null)
if (deviceItem is null)
{
return;
deviceItem = CurrentDevice;
}
VariableTableItem.DeviceId = CurrentDevice.Id;
var tableMenu = new MenuBean()
{
Header = VariableTableItem.Name,
Icon = SegoeFluentIcons.DataSense.Glyph,
TargetViewKey = nameof(VariableTableViewModel)
};
int addVarTableId = await _wpfDataService.VariableTableDataService.AddVariableTable(
_mapper.Map<VariableTable>(VariableTableItem),
tableMenu, true);
if (addVarTableId > 0)
VariableTableDialogViewModel variableTableDialogViewModel = new VariableTableDialogViewModel()
{
VariableTableItem.Id = addVarTableId;
VariableTableItem.Device = CurrentDevice;
CurrentDevice.VariableTables.Add(VariableTableItem);
_viewDataService.VariableTables.Add(VariableTableItem.Id, VariableTableItem);
_notificationService.ShowSuccess($"添加变量表成功:{VariableTableItem.Name}");
PrimaryButText = "添加变量表"
};
// 1. 显示添加设备对话框
var variableTableItem = await _dialogService.ShowDialogAsync(variableTableDialogViewModel);
// 如果用户取消或对话框未返回设备,则直接返回
if (variableTableItem is null) return;
variableTableItem.DeviceId = deviceItem.Id;
variableTableItem.Device = deviceItem;
variableTableItem.Protocol = deviceItem.Protocol;
var res = await _variableTableViewService.AddAsync(variableTableItem);
if (res)
{
_notificationService.ShowSuccess($"添加变量表成功:{variableTableItem.Name}");
}
else
{
_notificationService.ShowError($"添加变量表失败:{VariableTableItem.Name}");
_notificationService.ShowError($"添加变量表失败:{variableTableItem.Name}");
}
}
catch (Exception ex)
@@ -116,19 +109,16 @@ public partial class DeviceDetailViewModel : ViewModelBase
VariableTableDialogViewModel variableTableDialogViewModel
= new VariableTableDialogViewModel(SelectedVariableTable)
{
PrimaryButText = "编辑变量表"
};
{
PrimaryButText = "编辑变量表"
};
// 1. 显示变量表对话框
VariableTableItem variableTable
= await _dialogService.ShowDialogAsync(variableTableDialogViewModel);
// 如果用户取消或对话框未返回变量表,则直接返回
if (variableTable == null)
{
return;
}
if (variableTable is null) return;
if (await _wpfDataService.VariableDataService.UpdateVariableTable(variableTable))
if (await _variableTableViewService.UpdateAsync(variableTable))
{
_notificationService.ShowSuccess($"编辑变量表成功:{variableTable.Name}");
}
@@ -148,40 +138,35 @@ public partial class DeviceDetailViewModel : ViewModelBase
}
[RelayCommand]
private async Task DeleteVariableTable()
private async Task DeleteVariableTable(VariableTableItem variableTableItem=null)
{
if (IsBusy) return;
IsBusy = true;
try
{
if (SelectedVariableTable == null)
if (variableTableItem is null)
{
_notificationService.ShowError("你没有选择任何变量表,请选择变量表后再点击删除变量表");
return;
if (SelectedVariableTable is null)
{
_notificationService.ShowError("你没有选择任何变量表,请选择变量表后再点击删除变量表");
return;
}
variableTableItem = SelectedVariableTable;
}
string message = $"确认要删除变量表名为:{SelectedVariableTable.Name} \n\n此操作将同时删除该变量表下的所有变量数据且无法恢复";
string message = $"确认要删除变量表名为:{variableTableItem.Name} \n\n此操作将同时删除该变量表下的所有变量数据且无法恢复";
ConfirmDialogViewModel viewModel = new ConfirmDialogViewModel("删除变量表", message, "删除");
if (await _dialogService.ShowDialogAsync(viewModel))
bool confirmRes = await _dialogService.ShowDialogAsync(viewModel);
if (!confirmRes) return;
var tableName = variableTableItem.Name;
if (await _variableTableViewService.DeleteAsync(variableTableItem))
{
var tableName = SelectedVariableTable.Name;
if (await _wpfDataService.VariableDataService.DeleteVariableTable(SelectedVariableTable, true))
{
if (SelectedVariableTable.Device != null)
{
if (_viewDataService.Devices.TryGetValue(SelectedVariableTable.DeviceId ,out var device))
{
device.VariableTables.Remove(SelectedVariableTable);
}
_viewDataService.VariableTables.Remove(SelectedVariableTable.Id);
SelectedVariableTable.Device.VariableTables.Remove(SelectedVariableTable);
}
_notificationService.ShowSuccess($"变量表:{tableName},删除成功。");
}
else
{
_notificationService.ShowError($"变量表:{tableName},删除失败!!!");
}
_notificationService.ShowSuccess($"变量表:{tableName},删除成功。");
}
else
{
_notificationService.ShowError($"变量表:{tableName},删除失败!!!");
}
}
catch (Exception e)
@@ -207,9 +192,6 @@ public partial class DeviceDetailViewModel : ViewModelBase
public void NavigateToVariableTable()
{
if (SelectedVariableTable == null) return;
// var menu = _viewCenterService.Menus.FirstOrDefault(m => m.MenuType == MenuType.VariableTableMenu &&
// m.TargetId == SelectedVariableTable.Id);
// if (menu == null) return;
_navigationService.NavigateToAsync(
this,
new NavigationParameter(nameof(VariableTableViewModel), SelectedVariableTable.Id,