修复了添加设备不更新的问题和一些已知的问题
This commit is contained in:
@@ -22,5 +22,4 @@ public class DbMenu
|
|||||||
[SugarColumn(IsIgnore = true)]
|
[SugarColumn(IsIgnore = true)]
|
||||||
public List<DbMenu> Items { get; set; }
|
public List<DbMenu> Items { get; set; }
|
||||||
|
|
||||||
public DbMenu? Parent { get; set; }
|
|
||||||
}
|
}
|
||||||
@@ -13,57 +13,45 @@ namespace PMSWPF.Data.Repositories;
|
|||||||
|
|
||||||
public class MenuRepository
|
public class MenuRepository
|
||||||
{
|
{
|
||||||
private readonly SqlSugarClient _db;
|
|
||||||
|
|
||||||
public MenuRepository()
|
public MenuRepository()
|
||||||
{
|
{
|
||||||
_db = DbContext.GetInstance();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<List<MenuBean>> GetMenu()
|
public async Task<List<MenuBean>> GetMenu()
|
||||||
{
|
{
|
||||||
// //无主键用法新:5.1.4.110
|
// //无主键用法新:5.1.4.110
|
||||||
// db.Queryable<Tree>().ToTree(it=>it.Child,it=>it.ParentId,0,it=>it.Id)//+4重载
|
// db.Queryable<Tree>().ToTree(it=>it.Child,it=>it.ParentId,0,it=>it.Id)//+4重载
|
||||||
List<DbMenu> dbMenuList = await _db.Queryable<DbMenu>().ToListAsync();
|
// List<DbMenu> dbMenuList = await _db.Queryable<DbMenu>().ToListAsync();
|
||||||
|
|
||||||
|
using (var db = DbContext.GetInstance())
|
||||||
|
{
|
||||||
List<MenuBean> menuTree = new();
|
List<MenuBean> menuTree = new();
|
||||||
var dbMenuTree = await _db.Queryable<DbMenu>().ToTreeAsync(dm => dm.Items, dm => dm.ParentId, 0);
|
var dbMenuTree = await db.Queryable<DbMenu>().ToTreeAsync(dm => dm.Items, dm => dm.ParentId, 0);
|
||||||
foreach (var dbMenu in dbMenuTree)
|
foreach (var dbMenu in dbMenuTree)
|
||||||
{
|
{
|
||||||
AddParent(dbMenu);
|
|
||||||
menuTree.Add(dbMenu.CopyTo<MenuBean>());
|
menuTree.Add(dbMenu.CopyTo<MenuBean>());
|
||||||
}
|
}
|
||||||
|
|
||||||
return menuTree;
|
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)
|
|
||||||
{
|
|
||||||
AddParent(item);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<int> AddMenu(MenuBean menu)
|
public async Task<int> AddMenu(MenuBean menu)
|
||||||
{
|
{
|
||||||
return await _db.Insertable<DbMenu>(menu.CopyTo<DbMenu>()).ExecuteCommandAsync();
|
using (var db = DbContext.GetInstance())
|
||||||
|
{
|
||||||
|
return await db.Insertable<DbMenu>(menu.CopyTo<DbMenu>()).ExecuteCommandAsync();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public async Task<bool> AddDeviceMenu(Device device)
|
public async Task<bool> AddDeviceMenu(Device device)
|
||||||
|
{
|
||||||
|
using (var db = DbContext.GetInstance())
|
||||||
{
|
{
|
||||||
bool result = false;
|
bool result = false;
|
||||||
var deviceMainMenu = await _db.Queryable<DbMenu>().FirstAsync(m => m.Name == "设备");
|
var deviceMainMenu = await db.Queryable<DbMenu>().FirstAsync(m => m.Name == "设备");
|
||||||
if (deviceMainMenu == null)
|
if (deviceMainMenu == null)
|
||||||
throw new InvalidOperationException("没有找到设备菜单!!");
|
throw new InvalidOperationException("没有找到设备菜单!!");
|
||||||
|
|
||||||
@@ -77,12 +65,12 @@ public class MenuRepository
|
|||||||
};
|
};
|
||||||
|
|
||||||
menu.ParentId = deviceMainMenu.Id;
|
menu.ParentId = deviceMainMenu.Id;
|
||||||
var addDeviceMenuId = await _db.Insertable<DbMenu>(menu.CopyTo<DbMenu>())
|
var addDeviceMenuId = await db.Insertable<DbMenu>(menu.CopyTo<DbMenu>())
|
||||||
.ExecuteReturnIdentityAsync();
|
.ExecuteReturnIdentityAsync();
|
||||||
if (addDeviceMenuId == 0)
|
if (addDeviceMenuId == 0)
|
||||||
throw new InvalidOperationException($"{menu.Name},设备菜单添加失败!!");
|
throw new InvalidOperationException($"{menu.Name},设备菜单添加失败!!");
|
||||||
|
|
||||||
var defVarTable = await _db.Queryable<DbVariableTable>()
|
var defVarTable = await db.Queryable<DbVariableTable>()
|
||||||
.FirstAsync(v => v.DeviceId == device.Id && v.Name == "默认变量表");
|
.FirstAsync(v => v.DeviceId == device.Id && v.Name == "默认变量表");
|
||||||
if (defVarTable == null)
|
if (defVarTable == null)
|
||||||
throw new InvalidOperationException($"没有找到{device.Name}的默认变量表。");
|
throw new InvalidOperationException($"没有找到{device.Name}的默认变量表。");
|
||||||
@@ -101,16 +89,17 @@ public class MenuRepository
|
|||||||
Type = MenuType.AddVariableTableMenu,
|
Type = MenuType.AddVariableTableMenu,
|
||||||
ParentId = addDeviceMenuId,
|
ParentId = addDeviceMenuId,
|
||||||
};
|
};
|
||||||
var defTableRes = await _db.Insertable<DbMenu>(defVarTableMenu).ExecuteCommandAsync();
|
var defTableRes = await db.Insertable<DbMenu>(defVarTableMenu).ExecuteCommandAsync();
|
||||||
var addTableRes = await _db.Insertable<DbMenu>(addVarTable).ExecuteCommandAsync();
|
var addTableRes = await db.Insertable<DbMenu>(addVarTable).ExecuteCommandAsync();
|
||||||
if ((addTableRes + defTableRes) != 2)
|
if ((addTableRes + defTableRes) != 2)
|
||||||
{
|
{
|
||||||
// 如果出错删除原来添加的设备菜单
|
// 如果出错删除原来添加的设备菜单
|
||||||
await _db.Deleteable<DbMenu>().Where(m => m.Id == addDeviceMenuId).ExecuteCommandAsync();
|
await db.Deleteable<DbMenu>().Where(m => m.Id == addDeviceMenuId).ExecuteCommandAsync();
|
||||||
throw new InvalidOperationException("添加默认变量表时发生了错误!!");
|
throw new InvalidOperationException("添加默认变量表时发生了错误!!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -82,7 +82,10 @@ public static class ObjectExtensions
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
var sObj = sourceProperty.GetValue(tsource);
|
||||||
|
var tObj = targetProperty.GetValue(ttarget);
|
||||||
|
// if (sObj == null && tObj == null)
|
||||||
|
// CopyTo(sObj,tObj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
20
Helper/MenuHelper.cs
Normal file
20
Helper/MenuHelper.cs
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,7 +7,7 @@ namespace PMSWPF.Models;
|
|||||||
|
|
||||||
public partial class Device : ObservableObject
|
public partial class Device : ObservableObject
|
||||||
{
|
{
|
||||||
[ObservableProperty] private string description;
|
[ObservableProperty]private string description;
|
||||||
|
|
||||||
[ObservableProperty] private int id;
|
[ObservableProperty] private int id;
|
||||||
|
|
||||||
@@ -24,4 +24,6 @@ public partial class Device : ObservableObject
|
|||||||
|
|
||||||
public List<VariableTable>? VariableTables { get; set; }
|
public List<VariableTable>? VariableTables { get; set; }
|
||||||
public ProtocolType ProtocolType { get; set; }
|
public ProtocolType ProtocolType { get; set; }
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -15,9 +15,9 @@ namespace PMSWPF.Services;
|
|||||||
public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
|
public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
|
||||||
{
|
{
|
||||||
private readonly ILogger<DataServices> _logger;
|
private readonly ILogger<DataServices> _logger;
|
||||||
[ObservableProperty] private List<Device> _devices = new List<Device>();
|
[ObservableProperty] private List<Device> _devices;
|
||||||
[ObservableProperty] private List<VariableTable> _variableTables = new ();
|
[ObservableProperty] private List<VariableTable> _variableTables;
|
||||||
[ObservableProperty] private List<MenuBean> menuBeans = new List<MenuBean>();
|
[ObservableProperty] private List<MenuBean> menuBeans;
|
||||||
private readonly DeviceRepository _deviceRepository;
|
private readonly DeviceRepository _deviceRepository;
|
||||||
private readonly MenuRepository _menuRepository;
|
private readonly MenuRepository _menuRepository;
|
||||||
|
|
||||||
@@ -28,12 +28,21 @@ public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
|
|||||||
partial void OnDevicesChanged(List<Device> devices)
|
partial void OnDevicesChanged(List<Device> devices)
|
||||||
{
|
{
|
||||||
OnDeviceListChanged?.Invoke(devices);
|
OnDeviceListChanged?.Invoke(devices);
|
||||||
FillMenuData(MenuBeans);
|
if (menuBeans!=null && Devices!=null)
|
||||||
|
{
|
||||||
|
FillMenuData(MenuBeans,Devices);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
partial void OnMenuBeansChanged(List<MenuBean> menuBeans)
|
partial void OnMenuBeansChanged(List<MenuBean> menuBeans)
|
||||||
{
|
{
|
||||||
OnMenuListChanged?.Invoke(menuBeans);
|
OnMenuListChanged?.Invoke(menuBeans);
|
||||||
|
if (MenuBeans!=null && Devices!=null)
|
||||||
|
{
|
||||||
|
FillMenuData(MenuBeans,Devices);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -43,17 +52,14 @@ public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
|
|||||||
IsActive = true;
|
IsActive = true;
|
||||||
_deviceRepository = new DeviceRepository();
|
_deviceRepository = new DeviceRepository();
|
||||||
_menuRepository = new MenuRepository();
|
_menuRepository = new MenuRepository();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 给Menu菜单的Data填充数据
|
/// 给Menu菜单的Data填充数据
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="menuBeans"></param>
|
/// <param name="menuBeans"></param>
|
||||||
private void FillMenuData(List<MenuBean> menuBeans)
|
private void FillMenuData(List<MenuBean> menuBeans,List<Device> devices)
|
||||||
{
|
{
|
||||||
if (menuBeans == null || menuBeans.Count == 0)
|
if (menuBeans == null || menuBeans.Count == 0)
|
||||||
return;
|
return;
|
||||||
@@ -63,23 +69,25 @@ public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
|
|||||||
switch (menuBean.Type)
|
switch (menuBean.Type)
|
||||||
{
|
{
|
||||||
case MenuType.MainMenu:
|
case MenuType.MainMenu:
|
||||||
menuBean.ViewModel= GetMainViewModel(menuBean.Name);
|
menuBean.ViewModel = GetMainViewModel(menuBean.Name);
|
||||||
break;
|
break;
|
||||||
case MenuType.DeviceMenu:
|
case MenuType.DeviceMenu:
|
||||||
menuBean.ViewModel = App.Current.Services.GetRequiredService<DeviceDetailViewModel>();
|
menuBean.ViewModel = App.Current.Services.GetRequiredService<DeviceDetailViewModel>();
|
||||||
menuBean.Data= Devices.FirstOrDefault(d => d.Id == menuBean.DataId);
|
menuBean.Data = devices.FirstOrDefault(d => d.Id == menuBean.DataId);
|
||||||
break;
|
break;
|
||||||
case MenuType.VariableTableMenu:
|
case MenuType.VariableTableMenu:
|
||||||
var varTableVM = App.Current.Services.GetRequiredService<VariableTableViewModel>();
|
var varTableVM = App.Current.Services.GetRequiredService<VariableTableViewModel>();
|
||||||
varTableVM.VariableTable = FindVarTableForDevice(menuBean.DataId);
|
varTableVM.VariableTable = FindVarTableForDevice(menuBean.DataId);
|
||||||
menuBean.ViewModel = varTableVM;
|
menuBean.ViewModel = varTableVM;
|
||||||
|
menuBean.Data = varTableVM.VariableTable;
|
||||||
break;
|
break;
|
||||||
case MenuType.AddVariableTableMenu:
|
case MenuType.AddVariableTableMenu:
|
||||||
break;
|
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<LoadMessage>
|
|||||||
navgateVM = App.Current.Services.GetRequiredService<SettingViewModel>();
|
navgateVM = App.Current.Services.GetRequiredService<SettingViewModel>();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return navgateVM;
|
|
||||||
|
|
||||||
|
return navgateVM;
|
||||||
}
|
}
|
||||||
|
|
||||||
private VariableTable FindVarTableForDevice(int vtableId)
|
private VariableTable FindVarTableForDevice(int vtableId)
|
||||||
@@ -111,9 +119,11 @@ public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
|
|||||||
VariableTable varTable = null;
|
VariableTable varTable = null;
|
||||||
foreach (var device in _devices)
|
foreach (var device in _devices)
|
||||||
{
|
{
|
||||||
varTable= device.VariableTables.FirstOrDefault(v => v.Id == vtableId);
|
varTable = device.VariableTables.FirstOrDefault(v => v.Id == vtableId);
|
||||||
|
if (varTable!=null)
|
||||||
return varTable;
|
return varTable;
|
||||||
}
|
}
|
||||||
|
|
||||||
return varTable;
|
return varTable;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -128,22 +138,29 @@ public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
|
|||||||
{
|
{
|
||||||
case LoadTypes.All:
|
case LoadTypes.All:
|
||||||
Devices = await _deviceRepository.GetAll();
|
Devices = await _deviceRepository.GetAll();
|
||||||
MenuBeans = await _menuRepository.GetMenu();
|
await LoadMenus();
|
||||||
break;
|
break;
|
||||||
case LoadTypes.Devices:
|
case LoadTypes.Devices:
|
||||||
Devices = await _deviceRepository.GetAll();
|
Devices = await _deviceRepository.GetAll();
|
||||||
break;
|
break;
|
||||||
case LoadTypes.Menu:
|
case LoadTypes.Menu:
|
||||||
MenuBeans = await _menuRepository.GetMenu();
|
await LoadMenus();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
NotificationHelper.ShowMessage($"加载数据出现了错误:{e.Message}");
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -62,6 +62,7 @@ public partial class DevicesViewModel : ViewModelBase
|
|||||||
{
|
{
|
||||||
// 通知更新菜单
|
// 通知更新菜单
|
||||||
MessageHelper.SendLoadMessage(LoadTypes.Menu);
|
MessageHelper.SendLoadMessage(LoadTypes.Menu);
|
||||||
|
MessageHelper.SendLoadMessage(LoadTypes.Devices);
|
||||||
NotificationHelper.ShowMessage(msg, NotificationType.Success);
|
NotificationHelper.ShowMessage(msg, NotificationType.Success);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Windows.Controls;
|
using System.Windows.Controls;
|
||||||
using iNKORE.UI.WPF.Modern.Controls;
|
using iNKORE.UI.WPF.Modern.Controls;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using PMSWPF.Services;
|
||||||
|
using PMSWPF.ViewModels;
|
||||||
|
|
||||||
namespace PMSWPF.Views;
|
namespace PMSWPF.Views;
|
||||||
|
|
||||||
@@ -9,6 +12,7 @@ public partial class DevicesView : UserControl
|
|||||||
public DevicesView()
|
public DevicesView()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
|
DataContext=App.Current.Services.GetRequiredService<DevicesViewModel>();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void BasicGridView_ItemClick(object sender, ItemClickEventArgs e)
|
private void BasicGridView_ItemClick(object sender, ItemClickEventArgs e)
|
||||||
|
|||||||
@@ -68,34 +68,34 @@ public partial class MainView : Window
|
|||||||
_viewModel.OnLoaded();
|
_viewModel.OnLoaded();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void NavigationView_OnItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args)
|
// private void NavigationView_OnItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args)
|
||||||
{
|
// {
|
||||||
ViewModelBase navgateVM = App.Current.Services.GetRequiredService<HomeViewModel>();
|
// ViewModelBase navgateVM = App.Current.Services.GetRequiredService<HomeViewModel>();
|
||||||
|
//
|
||||||
switch (args.InvokedItem)
|
// switch (args.InvokedItem)
|
||||||
{
|
// {
|
||||||
case "主页":
|
// case "主页":
|
||||||
// mainViewModel.NavgateTo<HomeViewModel>();
|
// // mainViewModel.NavgateTo<HomeViewModel>();
|
||||||
navgateVM = App.Current.Services.GetRequiredService<HomeViewModel>();
|
// navgateVM = App.Current.Services.GetRequiredService<HomeViewModel>();
|
||||||
_logger.LogInformation("导航到到主页面");
|
// _logger.LogInformation("导航到到主页面");
|
||||||
break;
|
// break;
|
||||||
case "设备":
|
// case "设备":
|
||||||
navgateVM = App.Current.Services.GetRequiredService<DevicesViewModel>();
|
// navgateVM = App.Current.Services.GetRequiredService<DevicesViewModel>();
|
||||||
// mainViewModel.NavgateTo<DevicesViewModel>();
|
// // mainViewModel.NavgateTo<DevicesViewModel>();
|
||||||
_logger.LogInformation("导航到到设备页面");
|
// _logger.LogInformation("导航到到设备页面");
|
||||||
break;
|
// break;
|
||||||
case "数据转换":
|
// case "数据转换":
|
||||||
navgateVM = App.Current.Services.GetRequiredService<DataTransformViewModel>();
|
// navgateVM = App.Current.Services.GetRequiredService<DataTransformViewModel>();
|
||||||
// mainViewModel.NavgateTo<DataTransformViewModel>();
|
// // mainViewModel.NavgateTo<DataTransformViewModel>();
|
||||||
_logger.LogInformation("导航到到数据转换页面");
|
// _logger.LogInformation("导航到到数据转换页面");
|
||||||
break;
|
// break;
|
||||||
case "设置":
|
// case "设置":
|
||||||
// mainViewModel.NavgateTo<SettingViewModel>();
|
// // mainViewModel.NavgateTo<SettingViewModel>();
|
||||||
navgateVM = App.Current.Services.GetRequiredService<SettingViewModel>();
|
// navgateVM = App.Current.Services.GetRequiredService<SettingViewModel>();
|
||||||
_logger.LogInformation("导航到到设备页面");
|
// _logger.LogInformation("导航到到设备页面");
|
||||||
break;
|
// break;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
MessageHelper.SendNavgatorMessage(navgateVM);
|
// MessageHelper.SendNavgatorMessage(navgateVM);
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user