修复了添加设备不更新的问题和一些已知的问题

This commit is contained in:
2025-06-30 22:03:49 +08:00
parent a1de03a94e
commit 5e0c530187
9 changed files with 171 additions and 136 deletions

View File

@@ -22,5 +22,4 @@ public class DbMenu
[SugarColumn(IsIgnore = true)]
public List<DbMenu> Items { get; set; }
public DbMenu? Parent { get; set; }
}

View File

@@ -13,57 +13,45 @@ namespace PMSWPF.Data.Repositories;
public class MenuRepository
{
private readonly SqlSugarClient _db;
public MenuRepository()
{
_db = DbContext.GetInstance();
}
public async Task<List<MenuBean>> GetMenu()
{
// //无主键用法新:5.1.4.110
// 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();
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)
{
AddParent(dbMenu);
menuTree.Add(dbMenu.CopyTo<MenuBean>());
}
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)
{
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)
{
using (var db = DbContext.GetInstance())
{
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)
throw new InvalidOperationException("没有找到设备菜单!!");
@@ -77,12 +65,12 @@ public class MenuRepository
};
menu.ParentId = deviceMainMenu.Id;
var addDeviceMenuId = await _db.Insertable<DbMenu>(menu.CopyTo<DbMenu>())
var addDeviceMenuId = await db.Insertable<DbMenu>(menu.CopyTo<DbMenu>())
.ExecuteReturnIdentityAsync();
if (addDeviceMenuId == 0)
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 == "默认变量表");
if (defVarTable == null)
throw new InvalidOperationException($"没有找到{device.Name}的默认变量表。");
@@ -101,16 +89,17 @@ public class MenuRepository
Type = MenuType.AddVariableTableMenu,
ParentId = addDeviceMenuId,
};
var defTableRes = await _db.Insertable<DbMenu>(defVarTableMenu).ExecuteCommandAsync();
var addTableRes = await _db.Insertable<DbMenu>(addVarTable).ExecuteCommandAsync();
var defTableRes = await db.Insertable<DbMenu>(defVarTableMenu).ExecuteCommandAsync();
var addTableRes = await db.Insertable<DbMenu>(addVarTable).ExecuteCommandAsync();
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("添加默认变量表时发生了错误!!");
}
return true;
}
}
}

View File

@@ -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);
}
}
}

20
Helper/MenuHelper.cs Normal file
View 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);
}
}
}
}

View File

@@ -7,7 +7,7 @@ namespace PMSWPF.Models;
public partial class Device : ObservableObject
{
[ObservableProperty] private string description;
[ObservableProperty]private string description;
[ObservableProperty] private int id;
@@ -24,4 +24,6 @@ public partial class Device : ObservableObject
public List<VariableTable>? VariableTables { get; set; }
public ProtocolType ProtocolType { get; set; }
}

View File

@@ -15,9 +15,9 @@ namespace PMSWPF.Services;
public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
{
private readonly ILogger<DataServices> _logger;
[ObservableProperty] private List<Device> _devices = new List<Device>();
[ObservableProperty] private List<VariableTable> _variableTables = new ();
[ObservableProperty] private List<MenuBean> menuBeans = new List<MenuBean>();
[ObservableProperty] private List<Device> _devices;
[ObservableProperty] private List<VariableTable> _variableTables;
[ObservableProperty] private List<MenuBean> menuBeans;
private readonly DeviceRepository _deviceRepository;
private readonly MenuRepository _menuRepository;
@@ -28,12 +28,21 @@ public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
partial void OnDevicesChanged(List<Device> devices)
{
OnDeviceListChanged?.Invoke(devices);
FillMenuData(MenuBeans);
if (menuBeans!=null && Devices!=null)
{
FillMenuData(MenuBeans,Devices);
}
}
partial void OnMenuBeansChanged(List<MenuBean> 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;
_deviceRepository = new DeviceRepository();
_menuRepository = new MenuRepository();
}
/// <summary>
/// 给Menu菜单的Data填充数据
/// </summary>
/// <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)
return;
@@ -63,23 +69,25 @@ public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
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<DeviceDetailViewModel>();
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<VariableTableViewModel>();
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<LoadMessage>
navgateVM = App.Current.Services.GetRequiredService<SettingViewModel>();
break;
}
return navgateVM;
return navgateVM;
}
private VariableTable FindVarTableForDevice(int vtableId)
@@ -111,9 +119,11 @@ public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
VariableTable varTable = null;
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;
}
@@ -128,22 +138,29 @@ public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
{
case LoadTypes.All:
Devices = await _deviceRepository.GetAll();
MenuBeans = await _menuRepository.GetMenu();
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);
}
}
}

View File

@@ -62,6 +62,7 @@ public partial class DevicesViewModel : ViewModelBase
{
// 通知更新菜单
MessageHelper.SendLoadMessage(LoadTypes.Menu);
MessageHelper.SendLoadMessage(LoadTypes.Devices);
NotificationHelper.ShowMessage(msg, NotificationType.Success);
}
else

View File

@@ -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<DevicesViewModel>();
}
private void BasicGridView_ItemClick(object sender, ItemClickEventArgs e)

View File

@@ -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<HomeViewModel>();
switch (args.InvokedItem)
{
case "主页":
// mainViewModel.NavgateTo<HomeViewModel>();
navgateVM = App.Current.Services.GetRequiredService<HomeViewModel>();
_logger.LogInformation("导航到到主页面");
break;
case "设备":
navgateVM = App.Current.Services.GetRequiredService<DevicesViewModel>();
// mainViewModel.NavgateTo<DevicesViewModel>();
_logger.LogInformation("导航到到设备页面");
break;
case "数据转换":
navgateVM = App.Current.Services.GetRequiredService<DataTransformViewModel>();
// mainViewModel.NavgateTo<DataTransformViewModel>();
_logger.LogInformation("导航到到数据转换页面");
break;
case "设置":
// mainViewModel.NavgateTo<SettingViewModel>();
navgateVM = App.Current.Services.GetRequiredService<SettingViewModel>();
_logger.LogInformation("导航到到设备页面");
break;
}
MessageHelper.SendNavgatorMessage(navgateVM);
}
// private void NavigationView_OnItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args)
// {
// ViewModelBase navgateVM = App.Current.Services.GetRequiredService<HomeViewModel>();
//
// switch (args.InvokedItem)
// {
// case "主页":
// // mainViewModel.NavgateTo<HomeViewModel>();
// navgateVM = App.Current.Services.GetRequiredService<HomeViewModel>();
// _logger.LogInformation("导航到到主页面");
// break;
// case "设备":
// navgateVM = App.Current.Services.GetRequiredService<DevicesViewModel>();
// // mainViewModel.NavgateTo<DevicesViewModel>();
// _logger.LogInformation("导航到到设备页面");
// break;
// case "数据转换":
// navgateVM = App.Current.Services.GetRequiredService<DataTransformViewModel>();
// // mainViewModel.NavgateTo<DataTransformViewModel>();
// _logger.LogInformation("导航到到数据转换页面");
// break;
// case "设置":
// // mainViewModel.NavgateTo<SettingViewModel>();
// navgateVM = App.Current.Services.GetRequiredService<SettingViewModel>();
// _logger.LogInformation("导航到到设备页面");
// break;
// }
//
// MessageHelper.SendNavgatorMessage(navgateVM);
// }
}