using System.Diagnostics; using iNKORE.UI.WPF.Modern.Common.IconKeys; using DMS.Extensions; using SqlSugar; using AutoMapper; using DMS.Infrastructure.Entities; using DMS.Core.Enums; using DMS.Helper; using DMS.Models; namespace DMS.Infrastructure.Repositories; public class MenuRepository { private readonly IMapper _mapper; public MenuRepository(IMapper mapper) { _mapper = mapper; } public async Task DeleteAsync(MenuBean menu) { using var db = DbContext.GetInstance(); return await DeleteAsync(menu, db); } public async Task DeleteAsync(MenuBean menu, SqlSugarClient db) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var childList = await db.Queryable() .ToChildListAsync(it => it.ParentId, menu.Id); var result = await db.Deleteable(childList) .ExecuteCommandAsync(); stopwatch.Stop(); NlogHelper.Info($"删除菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } public async Task> GetMenuTreesAsync() { 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(_mapper.Map(dbMenu)); stopwatch.Stop(); NlogHelper.Info($"获取菜单树耗时:{stopwatch.ElapsedMilliseconds}ms"); return menuTree; } } public async Task AddAsync(MenuBean menu) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); using var db = DbContext.GetInstance(); var result = await AddAsync(menu, db); stopwatch.Stop(); NlogHelper.Info($"添加菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } /// /// 添加菜单,支持事务 /// /// /// /// public async Task AddAsync(MenuBean menu, SqlSugarClient db) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var result = await db.Insertable(_mapper.Map(menu)) .ExecuteCommandAsync(); stopwatch.Stop(); NlogHelper.Info($"添加菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } public async Task AddVarTableMenuAsync(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(); // NlogHelper.Info($"添加变量表菜单 '{addVarTable.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return addTableRes; } /// /// 添加设备菜单 /// /// /// /// /// public async Task AddAsync(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(_mapper.Map(menu)) .ExecuteReturnIdentityAsync(); stopwatch.Stop(); NlogHelper.Info($"添加设备菜单 '{device.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return addDeviceMenuId; } /// /// 编辑菜单 /// /// /// public async Task UpdateAsync(MenuBean menu) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); using (var db = DbContext.GetInstance()) { var result = await UpdateAsync(menu, db); stopwatch.Stop(); NlogHelper.Info($"编辑菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } } /// /// 编辑菜单,支持事务 /// /// /// public async Task UpdateAsync(MenuBean menu, SqlSugarClient db) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var result = await db.Updateable(_mapper.Map(menu)) .ExecuteCommandAsync(); stopwatch.Stop(); NlogHelper.Info($"编辑菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } public async Task GetMenuByDataIdAsync(int dataId, MenuType menuType) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); using (var db = DbContext.GetInstance()) { var result = await db.Queryable() .FirstAsync(m => m.DataId == dataId && m.Type == menuType); stopwatch.Stop(); NlogHelper.Info($"根据DataId '{dataId}' 和 MenuType '{menuType}' 获取菜单耗时:{stopwatch.ElapsedMilliseconds}ms"); return _mapper.Map(result); } } public async Task GetMainMenuByNameAsync(string name) { using var db = DbContext.GetInstance(); var dbMenu= await db.Queryable().FirstAsync(m => m.Name == name && m.Type == MenuType.MainMenu); return _mapper.Map(dbMenu); } }