实现了设备详情页面,并添加了在设备界面点击对应的设备直接跳转到设备详情页面
This commit is contained in:
@@ -1,6 +1,151 @@
|
||||
namespace PMSWPF.ViewModels;
|
||||
using System.Collections.ObjectModel;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using PMSWPF.Data.Repositories;
|
||||
using PMSWPF.Helper;
|
||||
using PMSWPF.Models;
|
||||
using PMSWPF.Services;
|
||||
|
||||
public class DeviceDetailViewModel:ViewModelBase
|
||||
namespace PMSWPF.ViewModels;
|
||||
|
||||
public partial class DeviceDetailViewModel : ViewModelBase
|
||||
{
|
||||
private readonly IDialogService _dialogService;
|
||||
private readonly VarTableRepository _varTableRepository;
|
||||
|
||||
[ObservableProperty]
|
||||
private Device _currentDevice;
|
||||
|
||||
[ObservableProperty]
|
||||
private VariableTable _selectedVariableTable;
|
||||
|
||||
public DeviceDetailViewModel(IDialogService dialogService, VarTableRepository varTableRepository)
|
||||
{
|
||||
_dialogService = dialogService;
|
||||
_varTableRepository = varTableRepository;
|
||||
}
|
||||
|
||||
public override void OnLoaded()
|
||||
{
|
||||
// Ensure VariableTables is an ObservableCollection for UI binding
|
||||
// if (_curentDevice.VariableTables != null &&
|
||||
// !(_curentDevice.VariableTables is ObservableCollection<VariableTable>))
|
||||
// {
|
||||
// _curentDevice.VariableTables = new ObservableCollection<VariableTable>(_curentDevice.VariableTables);
|
||||
// }
|
||||
// else if (_curentDevice.VariableTables == null)
|
||||
// {
|
||||
// _curentDevice.VariableTables = new ObservableCollection<VariableTable>();
|
||||
// }
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private async Task AddVariableTable()
|
||||
{
|
||||
try
|
||||
{
|
||||
var newVarTable = await _dialogService.ShowAddVarTableDialog();
|
||||
if (newVarTable == null) return;
|
||||
|
||||
newVarTable.DeviceId = CurrentDevice.Id;
|
||||
newVarTable = await _varTableRepository.AddAsync(newVarTable);
|
||||
if (newVarTable != null)
|
||||
{
|
||||
CurrentDevice.VariableTables.Add(newVarTable);
|
||||
NotificationHelper.ShowSuccess($"变量表 {newVarTable.Name} 添加成功。");
|
||||
}
|
||||
else
|
||||
{
|
||||
NotificationHelper.ShowError($"变量表 {newVarTable.Name} 添加失败。");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
NotificationHelper.ShowError($"添加变量表时发生错误: {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private async Task EditVariableTable()
|
||||
{
|
||||
if (SelectedVariableTable == null)
|
||||
{
|
||||
NotificationHelper.ShowInfo("请选择要编辑的变量表。");
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var editedVarTable = await _dialogService.ShowEditVarTableDialog(SelectedVariableTable);
|
||||
if (editedVarTable == null) return;
|
||||
|
||||
// The dialog already updated the SelectedVariableTable if it returned a non-null value
|
||||
// So we just need to save it to the database
|
||||
var result = await _varTableRepository.UpdateAsync(SelectedVariableTable);
|
||||
if (result > 0)
|
||||
{
|
||||
NotificationHelper.ShowSuccess($"变量表 {SelectedVariableTable.Name} 编辑成功。");
|
||||
}
|
||||
else
|
||||
{
|
||||
NotificationHelper.ShowError($"变量表 {SelectedVariableTable.Name} 编辑失败。");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
NotificationHelper.ShowError($"编辑变量表时发生错误: {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private async Task DeleteVariableTable()
|
||||
{
|
||||
if (SelectedVariableTable == null)
|
||||
{
|
||||
NotificationHelper.ShowInfo("请选择要删除的变量表。");
|
||||
return;
|
||||
}
|
||||
|
||||
var confirm = await _dialogService.ShowConfrimeDialog(
|
||||
"删除确认",
|
||||
$"确定要删除变量表 \"{SelectedVariableTable.Name}\" 吗?\n\n此操作将同时删除该变量表下的所有变量数据,且无法恢复!",
|
||||
"删除");
|
||||
|
||||
if (!confirm) return;
|
||||
|
||||
try
|
||||
{
|
||||
var result = await _varTableRepository.DeleteAsync(SelectedVariableTable);
|
||||
if (result > 0)
|
||||
{
|
||||
CurrentDevice.VariableTables.Remove(SelectedVariableTable);
|
||||
NotificationHelper.ShowSuccess($"变量表 {SelectedVariableTable.Name} 删除成功。");
|
||||
}
|
||||
else
|
||||
{
|
||||
NotificationHelper.ShowError($"变量表 {SelectedVariableTable.Name} 删除失败。");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
NotificationHelper.ShowError($"删除变量表时发生错误: {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
// Placeholder for EditDeviceCommand and DeleteDeviceCommand if they are needed here
|
||||
[RelayCommand]
|
||||
private async Task EditDevice()
|
||||
{
|
||||
// Implement device editing logic, similar to AddDeviceCommand but for existing device
|
||||
NotificationHelper.ShowInfo("编辑设备功能待实现。");
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private async Task DeleteDevice()
|
||||
{
|
||||
// Implement device deletion logic
|
||||
NotificationHelper.ShowInfo("删除设备功能待实现。");
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PMSWPF.Data;
|
||||
using PMSWPF.Data.Repositories;
|
||||
@@ -160,4 +161,13 @@ public partial class DevicesViewModel : ViewModelBase
|
||||
NotificationHelper.ShowError($"编辑设备的过程中发生错误:{e.Message}", e);
|
||||
}
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
public void NavigateToDetail()
|
||||
{
|
||||
if (SelectedDevice == null) return;
|
||||
var deviceDetailVm = App.Current.Services.GetRequiredService<DeviceDetailViewModel>();
|
||||
deviceDetailVm.CurrentDevice = SelectedDevice;
|
||||
MessageHelper.SendNavgatorMessage(deviceDetailVm);
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ using PMSWPF.Helper;
|
||||
using PMSWPF.Models;
|
||||
using PMSWPF.Services;
|
||||
using PMSWPF.Views;
|
||||
|
||||
// Add this using directive
|
||||
// Add this using directive
|
||||
|
||||
@@ -49,7 +50,8 @@ public partial class MainViewModel : ViewModelBase
|
||||
/// <param name="dialogService">对话框服务。</param>
|
||||
/// <param name="logger">日志记录器。</param>
|
||||
public MainViewModel(NavgatorServices navgatorServices, DataServices dataServices, IDialogService dialogService,
|
||||
ILogger<MainViewModel> logger,VarTableRepository varTableRepository,MenuRepository menuRepository)
|
||||
ILogger<MainViewModel> logger, VarTableRepository varTableRepository,
|
||||
MenuRepository menuRepository)
|
||||
{
|
||||
_navgatorServices = navgatorServices;
|
||||
_dataServices = dataServices;
|
||||
@@ -65,7 +67,7 @@ public partial class MainViewModel : ViewModelBase
|
||||
// 发送消息加载数据
|
||||
MessageHelper.SendLoadMessage(LoadTypes.All);
|
||||
// 当菜单加载成功后,在前台显示菜单
|
||||
dataServices.OnMenuTreeListChanged += ( menus) => { Menus = new ObservableCollection<MenuBean>(menus); };
|
||||
dataServices.OnMenuTreeListChanged += (menus) => { Menus = new ObservableCollection<MenuBean>(menus); };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -124,22 +126,22 @@ public partial class MainViewModel : ViewModelBase
|
||||
// 假设 _varTableRepository.AddAsync 返回一个布尔值表示成功,或者一个表示 ID 的整数
|
||||
// 这里为了演示,我们假设它返回新添加的ID,如果失败则返回0
|
||||
await db.BeginTranAsync();
|
||||
var addVarTable = await _varTableRepository.Add(varTable,db);
|
||||
var addVarTable = await _varTableRepository.Add(varTable, db);
|
||||
|
||||
// 5. 添加变量表菜单
|
||||
MenuBean newMenu = new MenuBean
|
||||
{
|
||||
Icon = SegoeFluentIcons.Tablet.Glyph,
|
||||
Name = varTable.Name,
|
||||
DataId = addVarTable.Id, // 使用实际添加的ID
|
||||
Type = MenuType.VariableTableMenu,
|
||||
ParentId = menu.Parent.Id
|
||||
};
|
||||
{
|
||||
Icon = SegoeFluentIcons.Tablet.Glyph,
|
||||
Name = varTable.Name,
|
||||
DataId = addVarTable.Id, // 使用实际添加的ID
|
||||
Type = MenuType.VariableTableMenu,
|
||||
ParentId = menu.Parent.Id
|
||||
};
|
||||
|
||||
var addMenuRes = await _menuRepository.Add(newMenu,db);
|
||||
var addMenuRes = await _menuRepository.Add(newMenu, db);
|
||||
if (addMenuRes > 0)
|
||||
{
|
||||
await db.CommitTranAsync();
|
||||
await db.CommitTranAsync();
|
||||
// 变量表和菜单都添加成功
|
||||
MessageHelper.SendLoadMessage(LoadTypes.Menu);
|
||||
MessageHelper.SendLoadMessage(LoadTypes.Devices);
|
||||
@@ -153,7 +155,7 @@ public partial class MainViewModel : ViewModelBase
|
||||
NotificationHelper.ShowError($"变量表:{varTable.Name},添加菜单失败");
|
||||
_logger.LogError($"变量表:{varTable.Name},添加菜单失败");
|
||||
// 考虑:如果菜单添加失败,是否需要删除之前添加的变量表?
|
||||
// 例如:await _varTableRepository.Delete(addVarTableId);
|
||||
// 例如:await _varTableRepository.DeleteAsync(addVarTableId);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
@@ -179,8 +181,11 @@ public partial class MainViewModel : ViewModelBase
|
||||
break;
|
||||
// 导航到设备下面的菜单
|
||||
case MenuType.DeviceMenu:
|
||||
menu.ViewModel = App.Current.Services.GetRequiredService<DeviceDetailViewModel>();
|
||||
menu.Data = _dataServices.Devices.FirstOrDefault(d => d.Id == menu.DataId);
|
||||
var deviceDetailVm = App.Current.Services.GetRequiredService<DeviceDetailViewModel>();
|
||||
var currentDevice = _dataServices.Devices.FirstOrDefault(d => d.Id == menu.DataId);
|
||||
deviceDetailVm.CurrentDevice = currentDevice;
|
||||
menu.ViewModel = deviceDetailVm;
|
||||
menu.Data = currentDevice;
|
||||
break;
|
||||
// 导航到变量表菜单
|
||||
case MenuType.VariableTableMenu:
|
||||
@@ -201,10 +206,9 @@ public partial class MainViewModel : ViewModelBase
|
||||
// 导航到Mqtt服务器
|
||||
case MenuType.MqttMenu:
|
||||
var mqttVM = App.Current.Services.GetRequiredService<MqttServerDetailViewModel>();
|
||||
mqttVM.CurrentMqtt=_dataServices.Mqtts.FirstOrDefault(d=>d.Id == menu.DataId);
|
||||
mqttVM.CurrentMqtt = _dataServices.Mqtts.FirstOrDefault(d => d.Id == menu.DataId);
|
||||
menu.ViewModel = mqttVM;
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -442,15 +442,15 @@ partial class VariableTableViewModel : ViewModelBase
|
||||
/// </summary>
|
||||
private async Task RefreshDataView()
|
||||
{
|
||||
// 更新界面显示的数据:重新从数据库加载所有变量数据
|
||||
VariableTable.DataVariables = await _varDataRepository.GetByVariableTableId(VariableTable.Id);
|
||||
DataVariables.Clear();
|
||||
foreach (var item in VariableTable.DataVariables)
|
||||
{
|
||||
DataVariables.Add(item);
|
||||
}
|
||||
|
||||
VariableDataView.Refresh();
|
||||
// // 更新界面显示的数据:重新从数据库加载所有变量数据
|
||||
// VariableTable.DataVariables = await _varDataRepository.GetByVariableTableId(VariableTable.Id);
|
||||
// DataVariables.Clear();
|
||||
// foreach (var item in VariableTable.DataVariables)
|
||||
// {
|
||||
// DataVariables.Add(item);
|
||||
// }
|
||||
//
|
||||
// VariableDataView.Refresh();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -723,7 +723,7 @@ partial class VariableTableViewModel : ViewModelBase
|
||||
public async Task OnIsActiveChanged(bool active)
|
||||
{
|
||||
// 更新数据库中变量表的激活状态
|
||||
var res = await _varTableRepository.Edit(VariableTable);
|
||||
var res = await _varTableRepository.UpdateAsync(VariableTable);
|
||||
if (res > 0)
|
||||
{
|
||||
// 根据激活状态显示成功通知
|
||||
|
||||
Reference in New Issue
Block a user