diff --git a/Data/Entities/DbMenu.cs b/Data/Entities/DbMenu.cs index 2ac2386..3091cd7 100644 --- a/Data/Entities/DbMenu.cs +++ b/Data/Entities/DbMenu.cs @@ -21,6 +21,5 @@ public class DbMenu [SugarColumn(IsIgnore = true)] public List Items { get; set; } - - public DbMenu? Parent { get; set; } + } \ No newline at end of file diff --git a/Data/Repositories/MenuRepository.cs b/Data/Repositories/MenuRepository.cs index 091e206..32081c3 100644 --- a/Data/Repositories/MenuRepository.cs +++ b/Data/Repositories/MenuRepository.cs @@ -13,104 +13,93 @@ namespace PMSWPF.Data.Repositories; public class MenuRepository { - private readonly SqlSugarClient _db; - public MenuRepository() { - _db = DbContext.GetInstance(); } public async Task> GetMenu() { // //无主键用法新:5.1.4.110 // db.Queryable().ToTree(it=>it.Child,it=>it.ParentId,0,it=>it.Id)//+4重载 - List dbMenuList = await _db.Queryable().ToListAsync(); + // List dbMenuList = await _db.Queryable().ToListAsync(); - List menuTree = new(); - var dbMenuTree = await _db.Queryable().ToTreeAsync(dm => dm.Items, dm => dm.ParentId, 0); - foreach (var dbMenu in dbMenuTree) + using (var db = DbContext.GetInstance()) { - AddParent(dbMenu); - menuTree.Add(dbMenu.CopyTo()); - } - - return menuTree; - } - - private void AddParent(DbMenu dbMenu) - { - if (dbMenu.Items == null || dbMenu.Items.Count == 0) - return; - foreach (var item in dbMenu.Items) - { - item.Parent = dbMenu; - if (item.Items!=null && item.Items.Count>0) + List menuTree = new(); + var dbMenuTree = await db.Queryable().ToTreeAsync(dm => dm.Items, dm => dm.ParentId, 0); + foreach (var dbMenu in dbMenuTree) { - AddParent(item); + menuTree.Add(dbMenu.CopyTo()); } - + + return menuTree; } - - } + public async Task AddMenu(MenuBean menu) { - return await _db.Insertable(menu.CopyTo()).ExecuteCommandAsync(); + using (var db = DbContext.GetInstance()) + { + return await db.Insertable(menu.CopyTo()).ExecuteCommandAsync(); + } } public async Task AddDeviceMenu(Device device) { - bool result = false; - var deviceMainMenu = await _db.Queryable().FirstAsync(m => m.Name == "设备"); - if (deviceMainMenu == null) - throw new InvalidOperationException("没有找到设备菜单!!"); + using (var db = DbContext.GetInstance()) + { + bool result = false; + var deviceMainMenu = await db.Queryable().FirstAsync(m => m.Name == "设备"); + if (deviceMainMenu == null) + throw new InvalidOperationException("没有找到设备菜单!!"); - // 添加菜单项 - MenuBean menu = new MenuBean() - { - Name = device.Name, - Type = MenuType.DeviceMenu, - DataId = device.Id, - Icon = SegoeFluentIcons.Devices4.Glyph, - }; + // 添加菜单项 + MenuBean menu = new MenuBean() + { + Name = device.Name, + Type = MenuType.DeviceMenu, + DataId = device.Id, + Icon = SegoeFluentIcons.Devices4.Glyph, + }; - menu.ParentId = deviceMainMenu.Id; - var addDeviceMenuId = await _db.Insertable(menu.CopyTo()) - .ExecuteReturnIdentityAsync(); - if (addDeviceMenuId == 0) - throw new InvalidOperationException($"{menu.Name},设备菜单添加失败!!"); + menu.ParentId = deviceMainMenu.Id; + var addDeviceMenuId = await db.Insertable(menu.CopyTo()) + .ExecuteReturnIdentityAsync(); + if (addDeviceMenuId == 0) + throw new InvalidOperationException($"{menu.Name},设备菜单添加失败!!"); - var defVarTable = await _db.Queryable() - .FirstAsync(v => v.DeviceId == device.Id && v.Name == "默认变量表"); - if (defVarTable == null) - throw new InvalidOperationException($"没有找到{device.Name}的默认变量表。"); - var defVarTableMenu = new MenuBean() - { - Name = "默认变量表", - Icon = SegoeFluentIcons.Tablet.Glyph, - Type = MenuType.VariableTableMenu, - ParentId = addDeviceMenuId, - DataId = defVarTable.Id - }; - var addVarTable = new MenuBean() - { - Name = "添加变量表", - Icon = SegoeFluentIcons.Add.Glyph, - Type = MenuType.AddVariableTableMenu, - ParentId = addDeviceMenuId, - }; - var defTableRes = await _db.Insertable(defVarTableMenu).ExecuteCommandAsync(); - var addTableRes = await _db.Insertable(addVarTable).ExecuteCommandAsync(); - if ((addTableRes + defTableRes) != 2) - { - // 如果出错删除原来添加的设备菜单 - await _db.Deleteable().Where(m => m.Id == addDeviceMenuId).ExecuteCommandAsync(); - throw new InvalidOperationException("添加默认变量表时发生了错误!!"); + var defVarTable = await db.Queryable() + .FirstAsync(v => v.DeviceId == device.Id && v.Name == "默认变量表"); + if (defVarTable == null) + throw new InvalidOperationException($"没有找到{device.Name}的默认变量表。"); + var defVarTableMenu = new MenuBean() + { + Name = "默认变量表", + Icon = SegoeFluentIcons.Tablet.Glyph, + Type = MenuType.VariableTableMenu, + ParentId = addDeviceMenuId, + DataId = defVarTable.Id + }; + var addVarTable = new MenuBean() + { + Name = "添加变量表", + Icon = SegoeFluentIcons.Add.Glyph, + Type = MenuType.AddVariableTableMenu, + ParentId = addDeviceMenuId, + }; + var defTableRes = await db.Insertable(defVarTableMenu).ExecuteCommandAsync(); + var addTableRes = await db.Insertable(addVarTable).ExecuteCommandAsync(); + if ((addTableRes + defTableRes) != 2) + { + // 如果出错删除原来添加的设备菜单 + await db.Deleteable().Where(m => m.Id == addDeviceMenuId).ExecuteCommandAsync(); + throw new InvalidOperationException("添加默认变量表时发生了错误!!"); + } + + + return true; } - - - return true; } } \ No newline at end of file diff --git a/Extensions/ObjectExtensions.cs b/Extensions/ObjectExtensions.cs index 99a86e8..605c887 100644 --- a/Extensions/ObjectExtensions.cs +++ b/Extensions/ObjectExtensions.cs @@ -82,7 +82,10 @@ public static class ObjectExtensions } else { - + var sObj = sourceProperty.GetValue(tsource); + var tObj = targetProperty.GetValue(ttarget); + // if (sObj == null && tObj == null) + // CopyTo(sObj,tObj); } } } diff --git a/Helper/MenuHelper.cs b/Helper/MenuHelper.cs new file mode 100644 index 0000000..0947d45 --- /dev/null +++ b/Helper/MenuHelper.cs @@ -0,0 +1,20 @@ +using PMSWPF.Models; + +namespace PMSWPF.Helper; + +public class MenuHelper +{ + public static void MenuAddParent(MenuBean menu) + { + if (menu.Items==null || menu.Items.Count==0) + return; + foreach (MenuBean menuItem in menu.Items) + { + menuItem.Parent=menu; + if (menuItem.Items!= null && menuItem.Items.Count>0) + { + MenuAddParent(menuItem); + } + } + } +} \ No newline at end of file diff --git a/Models/Device.cs b/Models/Device.cs index 80e9aef..890f179 100644 --- a/Models/Device.cs +++ b/Models/Device.cs @@ -7,21 +7,23 @@ namespace PMSWPF.Models; public partial class Device : ObservableObject { - [ObservableProperty] private string description; - + [ObservableProperty]private string description; + [ObservableProperty] private int id; - + [ObservableProperty] private string ip; - + [ObservableProperty] private bool isActive = true; - + [ObservableProperty] private bool isRuning; - + [ObservableProperty] private string name; - + [SugarColumn(ColumnDataType = "varchar(20)", SqlParameterDbType = typeof(EnumToStringConvert))] public DeviceType DeviceType { get; set; } - + public List? VariableTables { get; set; } public ProtocolType ProtocolType { get; set; } + + } \ No newline at end of file diff --git a/Services/DataServices.cs b/Services/DataServices.cs index 74a4040..c915555 100644 --- a/Services/DataServices.cs +++ b/Services/DataServices.cs @@ -15,9 +15,9 @@ namespace PMSWPF.Services; public partial class DataServices : ObservableRecipient, IRecipient { private readonly ILogger _logger; - [ObservableProperty] private List _devices = new List(); - [ObservableProperty] private List _variableTables = new (); - [ObservableProperty] private List menuBeans = new List(); + [ObservableProperty] private List _devices; + [ObservableProperty] private List _variableTables; + [ObservableProperty] private List menuBeans; private readonly DeviceRepository _deviceRepository; private readonly MenuRepository _menuRepository; @@ -28,12 +28,21 @@ public partial class DataServices : ObservableRecipient, IRecipient partial void OnDevicesChanged(List devices) { OnDeviceListChanged?.Invoke(devices); - FillMenuData(MenuBeans); + if (menuBeans!=null && Devices!=null) + { + FillMenuData(MenuBeans,Devices); + } + } partial void OnMenuBeansChanged(List menuBeans) { OnMenuListChanged?.Invoke(menuBeans); + if (MenuBeans!=null && Devices!=null) + { + FillMenuData(MenuBeans,Devices); + } + } @@ -43,43 +52,42 @@ public partial class DataServices : ObservableRecipient, IRecipient IsActive = true; _deviceRepository = new DeviceRepository(); _menuRepository = new MenuRepository(); - - } - /// /// 给Menu菜单的Data填充数据 /// /// - private void FillMenuData(List menuBeans) + private void FillMenuData(List menuBeans,List devices) { if (menuBeans == null || menuBeans.Count == 0) return; - + foreach (MenuBean menuBean in menuBeans) { switch (menuBean.Type) - { + { case MenuType.MainMenu: - menuBean.ViewModel= GetMainViewModel(menuBean.Name); + menuBean.ViewModel = GetMainViewModel(menuBean.Name); break; case MenuType.DeviceMenu: menuBean.ViewModel = App.Current.Services.GetRequiredService(); - menuBean.Data= Devices.FirstOrDefault(d => d.Id == menuBean.DataId); + menuBean.Data = devices.FirstOrDefault(d => d.Id == menuBean.DataId); break; case MenuType.VariableTableMenu: var varTableVM = App.Current.Services.GetRequiredService(); varTableVM.VariableTable = FindVarTableForDevice(menuBean.DataId); menuBean.ViewModel = varTableVM; + menuBean.Data = varTableVM.VariableTable; break; case MenuType.AddVariableTableMenu: break; } - if (menuBean.Items!=null && menuBean.Items.Count>0) + + if (menuBean.Items != null && menuBean.Items.Count > 0) { - FillMenuData(menuBean.Items); + FillMenuData(menuBean.Items,devices); } } } @@ -102,8 +110,8 @@ public partial class DataServices : ObservableRecipient, IRecipient navgateVM = App.Current.Services.GetRequiredService(); break; } - return navgateVM; + return navgateVM; } private VariableTable FindVarTableForDevice(int vtableId) @@ -111,9 +119,11 @@ public partial class DataServices : ObservableRecipient, IRecipient VariableTable varTable = null; foreach (var device in _devices) { - varTable= device.VariableTables.FirstOrDefault(v => v.Id == vtableId); - return varTable; + varTable = device.VariableTables.FirstOrDefault(v => v.Id == vtableId); + if (varTable!=null) + return varTable; } + return varTable; } @@ -128,22 +138,29 @@ public partial class DataServices : ObservableRecipient, IRecipient { case LoadTypes.All: Devices = await _deviceRepository.GetAll(); - MenuBeans = await _menuRepository.GetMenu(); - break; + await LoadMenus(); + break; case LoadTypes.Devices: Devices = await _deviceRepository.GetAll(); break; case LoadTypes.Menu: - MenuBeans = await _menuRepository.GetMenu(); + await LoadMenus(); break; } } catch (Exception e) { NotificationHelper.ShowMessage($"加载数据出现了错误:{e.Message}"); - _logger.LogError($"加载数据出现了错误:{e.Message}"); + _logger.LogError($"加载数据出现了错误:{e}"); } } - + private async Task LoadMenus() + { + MenuBeans = await _menuRepository.GetMenu(); + foreach (MenuBean menu in MenuBeans) + { + MenuHelper.MenuAddParent(menu); + } + } } \ No newline at end of file diff --git a/ViewModels/DevicesViewModel.cs b/ViewModels/DevicesViewModel.cs index c882411..3e45f9c 100644 --- a/ViewModels/DevicesViewModel.cs +++ b/ViewModels/DevicesViewModel.cs @@ -62,6 +62,7 @@ public partial class DevicesViewModel : ViewModelBase { // 通知更新菜单 MessageHelper.SendLoadMessage(LoadTypes.Menu); + MessageHelper.SendLoadMessage(LoadTypes.Devices); NotificationHelper.ShowMessage(msg, NotificationType.Success); } else diff --git a/Views/DevicesView.xaml.cs b/Views/DevicesView.xaml.cs index 97ba583..4479b1d 100644 --- a/Views/DevicesView.xaml.cs +++ b/Views/DevicesView.xaml.cs @@ -1,6 +1,9 @@ using System.Windows; using System.Windows.Controls; using iNKORE.UI.WPF.Modern.Controls; +using Microsoft.Extensions.DependencyInjection; +using PMSWPF.Services; +using PMSWPF.ViewModels; namespace PMSWPF.Views; @@ -9,6 +12,7 @@ public partial class DevicesView : UserControl public DevicesView() { InitializeComponent(); + DataContext=App.Current.Services.GetRequiredService(); } private void BasicGridView_ItemClick(object sender, ItemClickEventArgs e) diff --git a/Views/MainView.xaml.cs b/Views/MainView.xaml.cs index 7b6133d..a8997d6 100644 --- a/Views/MainView.xaml.cs +++ b/Views/MainView.xaml.cs @@ -68,34 +68,34 @@ public partial class MainView : Window _viewModel.OnLoaded(); } - private void NavigationView_OnItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args) - { - ViewModelBase navgateVM = App.Current.Services.GetRequiredService(); - - switch (args.InvokedItem) - { - case "主页": - // mainViewModel.NavgateTo(); - navgateVM = App.Current.Services.GetRequiredService(); - _logger.LogInformation("导航到到主页面"); - break; - case "设备": - navgateVM = App.Current.Services.GetRequiredService(); - // mainViewModel.NavgateTo(); - _logger.LogInformation("导航到到设备页面"); - break; - case "数据转换": - navgateVM = App.Current.Services.GetRequiredService(); - // mainViewModel.NavgateTo(); - _logger.LogInformation("导航到到数据转换页面"); - break; - case "设置": - // mainViewModel.NavgateTo(); - navgateVM = App.Current.Services.GetRequiredService(); - _logger.LogInformation("导航到到设备页面"); - break; - } - - MessageHelper.SendNavgatorMessage(navgateVM); - } + // private void NavigationView_OnItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args) + // { + // ViewModelBase navgateVM = App.Current.Services.GetRequiredService(); + // + // switch (args.InvokedItem) + // { + // case "主页": + // // mainViewModel.NavgateTo(); + // navgateVM = App.Current.Services.GetRequiredService(); + // _logger.LogInformation("导航到到主页面"); + // break; + // case "设备": + // navgateVM = App.Current.Services.GetRequiredService(); + // // mainViewModel.NavgateTo(); + // _logger.LogInformation("导航到到设备页面"); + // break; + // case "数据转换": + // navgateVM = App.Current.Services.GetRequiredService(); + // // mainViewModel.NavgateTo(); + // _logger.LogInformation("导航到到数据转换页面"); + // break; + // case "设置": + // // mainViewModel.NavgateTo(); + // navgateVM = App.Current.Services.GetRequiredService(); + // _logger.LogInformation("导航到到设备页面"); + // break; + // } + // + // MessageHelper.SendNavgatorMessage(navgateVM); + // } } \ No newline at end of file