using System.Diagnostics; using iNKORE.UI.WPF.Modern.Common.IconKeys; using NLog; using PMSWPF.Data.Entities; using PMSWPF.Enums; using PMSWPF.Extensions; using PMSWPF.Models; using SqlSugar; namespace PMSWPF.Data.Repositories; public class MenuRepository { private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); public MenuRepository() { } public async Task DeleteMenu(MenuBean menu) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); using (var db = DbContext.GetInstance()) { var childList = await db.Queryable().ToChildListAsync(it => it.ParentId, menu.Id); var result = await db.Deleteable(childList).ExecuteCommandAsync(); stopwatch.Stop(); // Assuming NLog is available and configured for MenuRepository // If not, you might need to add a Logger field similar to DeviceRepository // For now, I'll assume it's available or will be added. Logger.Info($"删除菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } } public async Task> GetMenuTrees() { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); using (var db = DbContext.GetInstance()) { List menuTree = new(); var dbMenuTree = await db.Queryable().ToTreeAsync(dm => dm.Items, dm => dm.ParentId, 0); foreach (var dbMenu in dbMenuTree) menuTree.Add(dbMenu.CopyTo()); stopwatch.Stop(); Logger.Info($"获取菜单树耗时:{stopwatch.ElapsedMilliseconds}ms"); return menuTree; } } public async Task Add(MenuBean menu) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); using (var db = DbContext.GetInstance()) { var result = await db.Insertable(menu.CopyTo()).ExecuteCommandAsync(); stopwatch.Stop(); Logger.Info($"添加菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } } /// /// 添加默认变量表的菜单 /// /// /// /// /// /// public async Task AddDeviceDefTableMenu(Device device, int parentMenuId,int varTableId, SqlSugarClient db) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var defVarTableMenu = new MenuBean() { Name = "默认变量表", Icon = SegoeFluentIcons.Tablet.Glyph, Type = MenuType.VariableTableMenu, ParentId = parentMenuId, DataId = varTableId }; var defTableRes = await db.Insertable(defVarTableMenu).ExecuteCommandAsync(); stopwatch.Stop(); Logger.Info($"添加默认变量表菜单 '{defVarTableMenu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return defTableRes; } /// /// 给设备添加默认变量表菜单 /// /// /// /// // public async Task AddDeviceDefVarTableMenu(Device device) // { // var db = DbContext.GetInstance(); // try // { // await db.BeginTranAsync(); // bool result = false; // var parentMenuId = await AddDeviceMenu(device, db); // var defTableRes = await AddDeviceDefTableMenu(device, parentMenuId, db); // var addTableRes = await AddVarTableMenu(parentMenuId, db); // // if ((addTableRes + defTableRes) != 2) // // { // // // 如果出错删除原来添加的设备菜单 // // await db.Deleteable().Where(m => m.Id == parentMenuId).ExecuteCommandAsync(); // // throw new InvalidOperationException("添加默认变量表时发生了错误!!"); // // } // // await db.CommitTranAsync(); // return true; // } // catch (Exception e) // { // await db.RollbackTranAsync(); // } // finally // { // } // } public async Task AddVarTableMenu(DbDevice dbDevice, int parentMenuId, SqlSugarClient db) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var addVarTable = new MenuBean() { Name = "添加变量表", Icon = SegoeFluentIcons.Add.Glyph, Type = MenuType.AddVariableTableMenu, ParentId = parentMenuId, DataId = dbDevice.Id }; var addTableRes = await db.Insertable(addVarTable).ExecuteCommandAsync(); stopwatch.Stop(); // Logger.Info($"添加变量表菜单 '{addVarTable.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return addTableRes; } /// /// 添加设备菜单 /// /// /// /// /// public async Task AddDeviceMenu(DbDevice device, SqlSugarClient db) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); 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, }; menu.ParentId = deviceMainMenu.Id; var addDeviceMenuId = await db.Insertable(menu.CopyTo()) .ExecuteReturnIdentityAsync(); stopwatch.Stop(); // Logger.Info($"添加设备菜单 '{device.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return addDeviceMenuId; } public async Task Edit(MenuBean menu) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); using (var db = DbContext.GetInstance()) { var result = await db.Updateable(menu.CopyTo()).ExecuteCommandAsync(); stopwatch.Stop(); // Logger.Info($"编辑菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } } }