From edd4798e09d5242af1738ec9991a77048fd2ada3 Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Mon, 30 Jun 2025 13:06:51 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=99Menu=E8=8F=9C=E5=8D=95=E7=BB=91?= =?UTF-8?q?=E5=AE=9A=E5=AF=B9=E5=BA=94=E7=9A=84=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.xaml.cs | 5 +- Data/Repositories/DeviceRepository.cs | 5 +- Data/Repositories/VariableTableRepository.cs | 53 +++++++++++++ Enums/LoadTypes.cs | 3 +- Helper/MessageHelper.cs | 34 ++++++++ Models/VariableTable.cs | 1 + Services/DataServices.cs | 82 +++++++++++++++----- ViewModels/DevicesViewModel.cs | 11 +-- ViewModels/MainViewModel.cs | 7 +- Views/MainView.xaml.cs | 11 ++- 10 files changed, 177 insertions(+), 35 deletions(-) create mode 100644 Data/Repositories/VariableTableRepository.cs create mode 100644 Helper/MessageHelper.cs diff --git a/App.xaml.cs b/App.xaml.cs index 95a6f74..ffc61e8 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -69,7 +69,10 @@ public partial class App : Application InitMenu().Await((e) => { NotificationHelper.ShowMessage($"初始化主菜单失败:{e.Message}"); - }, () => { }); + }, () => + { + MessageHelper.SendLoadMessage(LoadTypes.Menu); + }); MainWindow = Services.GetRequiredService(); MainWindow.Show(); diff --git a/Data/Repositories/DeviceRepository.cs b/Data/Repositories/DeviceRepository.cs index 51fbdc6..60d666a 100644 --- a/Data/Repositories/DeviceRepository.cs +++ b/Data/Repositories/DeviceRepository.cs @@ -14,7 +14,7 @@ public class DeviceRepository { _db = DbContext.GetInstance(); } - public async Task Add(Device device) + public async Task Add(Device device) { var exist = await _db.Queryable().Where(d => d.Name == device.Name).FirstAsync(); if (exist != null) @@ -28,7 +28,8 @@ public class DeviceRepository dbVariableTable.Description = "默认变量表"; dbVariableTable.ProtocolType = dbDevice.ProtocolType; dbDevice.VariableTables.Add(dbVariableTable); - return await _db.InsertNav(dbDevice).Include(d => d.VariableTables).ExecuteCommandAsync(); + var addDbDevice= await _db.InsertNav(dbDevice).Include(d => d.VariableTables).ExecuteReturnEntityAsync(); + return addDbDevice.CopyTo(); } public async Task> GetAll() diff --git a/Data/Repositories/VariableTableRepository.cs b/Data/Repositories/VariableTableRepository.cs new file mode 100644 index 0000000..6721c48 --- /dev/null +++ b/Data/Repositories/VariableTableRepository.cs @@ -0,0 +1,53 @@ +using PMSWPF.Data.Entities; +using PMSWPF.Extensions; +using PMSWPF.Models; +using SqlSugar; + +namespace PMSWPF.Data.Repositories; + +public class VariableTableRepository +{ + private SqlSugarClient _db; + public VariableTableRepository() + { + _db = DbContext.GetInstance(); + } + // public async Task Add(VariableTable variableTable) + // { + // var exist = await _db.Queryable().Where(d => d.Name == device.Name).FirstAsync(); + // if (exist != null) + // throw new InvalidOperationException("设备名称已经存在。"); + // var dbDevice = new DbDevice(); + // device.CopyTo(dbDevice); + // dbDevice.VariableTables = new List(); + // // 添加默认变量表 + // var dbVariableTable = new DbVariableTable(); + // dbVariableTable.Name = "默认变量表"; + // dbVariableTable.Description = "默认变量表"; + // dbVariableTable.ProtocolType = dbDevice.ProtocolType; + // dbDevice.VariableTables.Add(dbVariableTable); + // var addDbDevice= await _db.InsertNav(dbDevice).Include(d => d.VariableTables).ExecuteReturnEntityAsync(); + // return addDbDevice.CopyTo(); + // } + + public async Task> GetAll() + { + var dbVariableTables = await _db.Queryable().ToListAsync(); + var variableTables = new List(); + + foreach (var dbVariableTable in dbVariableTables) + variableTables.Add(dbVariableTable.CopyTo()); + + return variableTables; + } + + public async Task GetById(int id) + { + return await _db.Queryable().FirstAsync(p => p.Id == id); + } + + public async Task DeleteById(int id) + { + return await _db.Deleteable(new DbVariableTable { Id = id }).ExecuteCommandAsync(); + } +} \ No newline at end of file diff --git a/Enums/LoadTypes.cs b/Enums/LoadTypes.cs index e23ccf6..f5078b0 100644 --- a/Enums/LoadTypes.cs +++ b/Enums/LoadTypes.cs @@ -4,5 +4,6 @@ public enum LoadTypes { Devices, Menu, - Mqtt + Mqtt, + All } \ No newline at end of file diff --git a/Helper/MessageHelper.cs b/Helper/MessageHelper.cs new file mode 100644 index 0000000..512f7ca --- /dev/null +++ b/Helper/MessageHelper.cs @@ -0,0 +1,34 @@ +using CommunityToolkit.Mvvm.Messaging; +using PMSWPF.Enums; +using PMSWPF.Message; +using PMSWPF.ViewModels; + +namespace PMSWPF.Helper; + +public class MessageHelper +{ + public static void Send(T message) where T : class + { + WeakReferenceMessenger.Default.Send(message); + } + /// + /// 发送加载消息 + /// + /// 加载的类型,如菜单 + public static void SendLoadMessage(LoadTypes loadType) + { + WeakReferenceMessenger.Default.Send(new LoadMessage(loadType)); + } + /// + /// 发送导航消息 + /// + /// 导航View的ViewModel + /// 带的参数 + public static void SendNavgatorMessage(ViewModelBase vm,Object param=null) + { + WeakReferenceMessenger.Default.Send(new NavgatorMessage(vm) + { + Parameters = param + }); + } +} \ No newline at end of file diff --git a/Models/VariableTable.cs b/Models/VariableTable.cs index ad2e151..ce5ed1a 100644 --- a/Models/VariableTable.cs +++ b/Models/VariableTable.cs @@ -9,4 +9,5 @@ public class VariableTable public string Description { get; set; } public ProtocolType ProtocolType { get; set; } public List DataVariables { get; set; } + public Device? Device { get; set; } } \ No newline at end of file diff --git a/Services/DataServices.cs b/Services/DataServices.cs index 4220d3d..8e400ca 100644 --- a/Services/DataServices.cs +++ b/Services/DataServices.cs @@ -10,13 +10,12 @@ using PMSWPF.Models; namespace PMSWPF.Services; -public partial class DataServices:ObservableRecipient,IRecipient +public partial class DataServices : ObservableRecipient, IRecipient { private readonly ILogger _logger; - [ObservableProperty] - private List _devices = new List(); - [ObservableProperty] - private List menuBeans = new List(); + [ObservableProperty] private List _devices = new List(); + [ObservableProperty] private List _variableTables = new (); + [ObservableProperty] private List menuBeans = new List(); private readonly DeviceRepository _deviceRepository; private readonly MenuRepository _menuRepository; @@ -24,53 +23,100 @@ public partial class DataServices:ObservableRecipient,IRecipient public event Action> OnMenuListChanged; - partial void OnDevicesChanged(List devices) + partial void OnDevicesChanged(List devices) { OnDeviceListChanged?.Invoke(devices); + FillMenuData(MenuBeans); } partial void OnMenuBeansChanged(List menuBeans) { OnMenuListChanged?.Invoke(menuBeans); } - - public DataServices(ILogger logger ) + + public DataServices(ILogger logger) { _logger = logger; IsActive = true; - _deviceRepository = new DeviceRepository(); - _menuRepository = new MenuRepository(); + _deviceRepository = new DeviceRepository(); + _menuRepository = new MenuRepository(); + } + /// + /// 给Menu菜单的Data填充数据 + /// + /// + private void FillMenuData(List menuBeans) + { + if (menuBeans == null || menuBeans.Count == 0) + return; + + foreach (MenuBean menuBean in menuBeans) + { + switch (menuBean.Type) + { + case MenuType.MainMenu: + break; + case MenuType.DeviceMenu: + menuBean.Data= Devices.FirstOrDefault(d => d.Id == menuBean.DataId); + break; + case MenuType.VariableTableMenu: + menuBean.Data= FindVarTableForDevice(menuBean.DataId); + // menuBean.Data= Devices.FirstOrDefault(d => d.Id == menuBean.DataId); + break; + case MenuType.AddVariableTableMenu: + break; + } + if (menuBean.Items!=null && menuBean.Items.Count>0) + { + FillMenuData(menuBean.Items); + } + } + } + + private VariableTable FindVarTableForDevice(int vtableId) + { + VariableTable varTable = null; + foreach (var device in _devices) + { + varTable= device.VariableTables.FirstOrDefault(v => v.Id == vtableId); + return varTable; + } + return varTable; + } + + public async void Receive(LoadMessage message) { - if (!(message.Value is LoadTypes)) throw new ArgumentException($"接受到的加载类型错误:{message.Value}"); try { - - switch ((LoadTypes)message.Value ) + switch ((LoadTypes)message.Value) { + case LoadTypes.All: + Devices = await _deviceRepository.GetAll(); + MenuBeans = await _menuRepository.GetMenu(); + break; case LoadTypes.Devices: - Devices= await _deviceRepository.GetAll(); + Devices = await _deviceRepository.GetAll(); break; case LoadTypes.Menu: - MenuBeans= await _menuRepository.GetMenu(); + MenuBeans = await _menuRepository.GetMenu(); break; - } } catch (Exception e) { NotificationHelper.ShowMessage($"加载数据出现了错误:{e.Message}"); _logger.LogError($"加载数据出现了错误:{e.Message}"); - } - } + + } \ No newline at end of file diff --git a/ViewModels/DevicesViewModel.cs b/ViewModels/DevicesViewModel.cs index 50d75c0..c882411 100644 --- a/ViewModels/DevicesViewModel.cs +++ b/ViewModels/DevicesViewModel.cs @@ -33,8 +33,8 @@ public partial class DevicesViewModel : ViewModelBase _logger = logger; _dialogService = dialogService; _dataServices = dataServices; - - WeakReferenceMessenger.Default.Send(new LoadMessage(LoadTypes.Devices)); + + MessageHelper.SendLoadMessage(LoadTypes.Devices); _dataServices.OnDeviceListChanged += (devices) => { Devices = new ObservableCollection(devices); }; } @@ -51,7 +51,8 @@ public partial class DevicesViewModel : ViewModelBase device = await _dialogService.ShowAddDeviceDialog(); if (device != null) { - if (await _deviceRepository.Add(device)) + device= await _deviceRepository.Add(device); + if (device!=null) { var msg = $"添加设备成功:{device.Name}"; _logger.LogInformation(msg); @@ -60,7 +61,7 @@ public partial class DevicesViewModel : ViewModelBase if (addMenuRes) { // 通知更新菜单 - WeakReferenceMessenger.Default.Send(new UpdateMenuMessage(0)); + MessageHelper.SendLoadMessage(LoadTypes.Menu); NotificationHelper.ShowMessage(msg, NotificationType.Success); } else @@ -82,7 +83,7 @@ public partial class DevicesViewModel : ViewModelBase { var msg = $"添加设备失败:{e.Message}"; _logger.LogError(msg); - NotificationHelper.ShowMessage(msg, NotificationType.Success); + NotificationHelper.ShowMessage(msg, NotificationType.Error); } } diff --git a/ViewModels/MainViewModel.cs b/ViewModels/MainViewModel.cs index d797810..0f6aaca 100644 --- a/ViewModels/MainViewModel.cs +++ b/ViewModels/MainViewModel.cs @@ -5,6 +5,7 @@ using CommunityToolkit.Mvvm.Messaging; using PMSWPF.Data.Entities; using PMSWPF.Data.Repositories; using PMSWPF.Enums; +using PMSWPF.Helper; using PMSWPF.Message; using PMSWPF.Models; using PMSWPF.Services; @@ -28,10 +29,12 @@ public partial class MainViewModel : ViewModelBase _dataServices = dataServices; _navgatorServices.OnViewModelChanged += () => { CurrentViewModel = _navgatorServices.CurrentViewModel; }; + CurrentViewModel = new HomeViewModel(); CurrentViewModel.OnLoaded(); - - WeakReferenceMessenger.Default.Send(new LoadMessage(LoadTypes.Menu)); + // 发送消息加载数据 + MessageHelper.SendLoadMessage(LoadTypes.All); + // 当菜单加载成功后,在前台显示菜单 dataServices.OnMenuListChanged += (menus) => { Menus = new ObservableCollection(menus); diff --git a/Views/MainView.xaml.cs b/Views/MainView.xaml.cs index 96375e7..f8a7535 100644 --- a/Views/MainView.xaml.cs +++ b/Views/MainView.xaml.cs @@ -4,6 +4,7 @@ using CommunityToolkit.Mvvm.Messaging; using iNKORE.UI.WPF.Modern.Controls; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using PMSWPF.Helper; using PMSWPF.Message; using PMSWPF.Models; using PMSWPF.ViewModels; @@ -58,9 +59,8 @@ public partial class MainView : Window _logger.LogInformation("导航到到设备页面"); break; } - - var nm = new NavgatorMessage(navgateVM); - WeakReferenceMessenger.Default.Send(nm); + + MessageHelper.SendNavgatorMessage(navgateVM); } private async void MainView_OnLoaded(object sender, RoutedEventArgs e) @@ -95,9 +95,8 @@ public partial class MainView : Window _logger.LogInformation("导航到到设备页面"); break; } - - var nm = new NavgatorMessage(navgateVM,parameter); - WeakReferenceMessenger.Default.Send(nm); + + MessageHelper.SendNavgatorMessage(navgateVM,parameter); } private void NavigationView_OnSelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)