完成编辑设备和删除设备

This commit is contained in:
2025-07-01 21:34:20 +08:00
parent 0110ba9492
commit 663e4fda0c
23 changed files with 504 additions and 235 deletions

View File

@@ -4,11 +4,11 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using PMSWPF.Data.Repositories;
using PMSWPF.Enums;
using PMSWPF.Extensions;
using PMSWPF.Helper;
using PMSWPF.Message;
using PMSWPF.Models;
using PMSWPF.ViewModels;
using SqlSugar;
namespace PMSWPF.Services;
@@ -17,29 +17,29 @@ public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
private readonly ILogger<DataServices> _logger;
[ObservableProperty] private List<Device> _devices;
[ObservableProperty] private List<VariableTable> _variableTables;
[ObservableProperty] private List<MenuBean> menuBeans;
[ObservableProperty] private List<MenuBean> menuTrees;
private readonly DeviceRepository _deviceRepository;
private readonly MenuRepository _menuRepository;
public event Action<List<Device>> OnDeviceListChanged;
public event Action<List<MenuBean>> OnMenuListChanged;
public event Action<List<MenuBean>> OnMenuTreeListChanged;
partial void OnDevicesChanged(List<Device> devices)
{
OnDeviceListChanged?.Invoke(devices);
if (menuBeans != null && Devices != null)
if (menuTrees != null && Devices != null)
{
FillMenuData(MenuBeans, Devices);
FillMenuData(MenuTrees, Devices);
}
}
partial void OnMenuBeansChanged(List<MenuBean> menuBeans)
partial void OnMenuTreesChanged(List<MenuBean> MenuTrees)
{
OnMenuListChanged?.Invoke(menuBeans);
if (MenuBeans != null && Devices != null)
OnMenuTreeListChanged?.Invoke(MenuTrees);
if (MenuTrees != null && Devices != null)
{
FillMenuData(MenuBeans, Devices);
FillMenuData(MenuTrees, Devices);
}
}
@@ -56,18 +56,18 @@ public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
/// <summary>
/// 给Menu菜单的Data填充数据
/// </summary>
/// <param name="menuBeans"></param>
private void FillMenuData(List<MenuBean> menuBeans, List<Device> devices)
/// <param name="MenuTrees"></param>
private void FillMenuData(List<MenuBean> MenuTrees, List<Device> devices)
{
if (menuBeans == null || menuBeans.Count == 0)
if (MenuTrees == null || MenuTrees.Count == 0)
return;
foreach (MenuBean menuBean in menuBeans)
foreach (MenuBean menuBean in MenuTrees)
{
switch (menuBean.Type)
{
case MenuType.MainMenu:
menuBean.ViewModel = GetMainViewModel(menuBean.Name);
menuBean.ViewModel =DataServicesHelper.GetMainViewModel(menuBean.Name);
break;
case MenuType.DeviceMenu:
menuBean.ViewModel = App.Current.Services.GetRequiredService<DeviceDetailViewModel>();
@@ -90,26 +90,20 @@ public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
}
}
private ViewModelBase GetMainViewModel(string name)
{
ViewModelBase navgateVM = App.Current.Services.GetRequiredService<HomeViewModel>();
switch (name)
{
case "主页":
navgateVM = App.Current.Services.GetRequiredService<HomeViewModel>();
break;
case "设备":
navgateVM = App.Current.Services.GetRequiredService<DevicesViewModel>();
break;
case "数据转换":
navgateVM = App.Current.Services.GetRequiredService<DataTransformViewModel>();
break;
case "设置":
navgateVM = App.Current.Services.GetRequiredService<SettingViewModel>();
break;
}
return navgateVM;
/// <summary>
/// 查找设备所对应的菜单对象
/// </summary>
/// <param name="device"></param>
/// <returns></returns>
public async Task<int> UpdateMenuForDevice(Device device)
{
var menu= DataServicesHelper.FindMenusForDevice(device, MenuTrees);
if (menu != null)
return await _menuRepository.Edit(menu);
return 0;
}
/// <summary>
@@ -164,10 +158,22 @@ public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
private async Task LoadMenus()
{
MenuBeans = await _menuRepository.GetMenu();
foreach (MenuBean menu in MenuBeans)
MenuTrees = await _menuRepository.GetMenuTrees();
foreach (MenuBean menu in MenuTrees)
{
MenuHelper.MenuAddParent(menu);
}
}
public async Task<int> DeleteMenuForDevice(Device device)
{
var menu= DataServicesHelper.FindMenusForDevice(device, MenuTrees);
if (menu != null)
{
return await _menuRepository.DeleteMenu(menu);
}
return 0;
}
}

View File

@@ -1,8 +1,4 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Messaging;
using HandyControl.Tools.Extension;
using iNKORE.UI.WPF.Modern.Controls;
using PMSWPF.Message;
using iNKORE.UI.WPF.Modern.Controls;
using PMSWPF.Models;
using PMSWPF.ViewModels.Dialogs;
using PMSWPF.Views.Dialogs;
@@ -19,7 +15,15 @@ public class DialogService :IDialogService
public async Task<Device> ShowAddDeviceDialog()
{
var device = new Device();
var dialog = new DeviceDialog(device);
DeviceDialogViewModel vm = new DeviceDialogViewModel(device);
vm.Title = "添加设备";
vm.PrimaryButContent = "添加设备";
return await ShowConentDialog(vm,device);
}
private static async Task<Device> ShowConentDialog(DeviceDialogViewModel viewModel,Device device)
{
var dialog = new DeviceDialog(viewModel);
var res = await dialog.ShowAsync();
if (res == ContentDialogResult.Primary)
{
@@ -28,6 +32,31 @@ public class DialogService :IDialogService
return null;
}
public async Task<Device> ShowEditDeviceDialog(Device device)
{
DeviceDialogViewModel vm = new DeviceDialogViewModel(device);
vm.Title = "编辑设备";
vm.PrimaryButContent = "编辑设备";
return await ShowConentDialog(vm,device);
}
public async Task<bool> ShowConfrimeDialog(string title, string message,string buttonText="确认")
{
ConfrimDialogViewModel vm = new ConfrimDialogViewModel();
vm.Title = title;
vm.Message = message;
vm.PrimaryButtonText = buttonText;
var dialog = new ConfirmDialog(vm);
var res = await dialog.ShowAsync();
if (res == ContentDialogResult.Primary)
{
return true;
}
return false;
}
public void ShowMessageDialog(string title, string message)
{
MessageBox.Show(message);

View File

@@ -5,6 +5,9 @@ namespace PMSWPF.Services;
public interface IDialogService
{
Task<Device> ShowAddDeviceDialog();
Task<Device> ShowEditDeviceDialog(Device device);
Task<bool> ShowConfrimeDialog(string title, string message,string buttonText="确认");
void ShowMessageDialog(string title, string message);
}