2025-07-03 13:53:29 +08:00
|
|
|
using System.Diagnostics;
|
2025-06-26 22:40:20 +08:00
|
|
|
using iNKORE.UI.WPF.Modern.Common.IconKeys;
|
2025-06-24 20:48:38 +08:00
|
|
|
using PMSWPF.Data.Entities;
|
2025-06-26 22:40:20 +08:00
|
|
|
using PMSWPF.Enums;
|
2025-06-25 22:33:57 +08:00
|
|
|
using PMSWPF.Extensions;
|
2025-07-06 19:51:53 +08:00
|
|
|
using PMSWPF.Helper;
|
2025-06-25 22:33:57 +08:00
|
|
|
using PMSWPF.Models;
|
2025-07-02 22:07:16 +08:00
|
|
|
using SqlSugar;
|
2025-06-24 20:48:38 +08:00
|
|
|
|
2025-07-06 19:51:53 +08:00
|
|
|
using PMSWPF.Helper;
|
|
|
|
|
|
2025-06-24 20:48:38 +08:00
|
|
|
namespace PMSWPF.Data.Repositories;
|
|
|
|
|
|
2025-06-26 19:36:27 +08:00
|
|
|
public class MenuRepository
|
2025-06-24 20:48:38 +08:00
|
|
|
{
|
2025-07-04 18:33:48 +08:00
|
|
|
|
2025-06-26 19:36:27 +08:00
|
|
|
public MenuRepository()
|
2025-06-24 20:48:38 +08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-01 21:34:20 +08:00
|
|
|
public async Task<int> DeleteMenu(MenuBean menu)
|
|
|
|
|
{
|
2025-07-03 13:53:29 +08:00
|
|
|
Stopwatch stopwatch = new Stopwatch();
|
|
|
|
|
stopwatch.Start();
|
2025-07-01 21:34:20 +08:00
|
|
|
using (var db = DbContext.GetInstance())
|
|
|
|
|
{
|
2025-07-04 18:33:48 +08:00
|
|
|
return await DeleteMenu(menu, db);
|
2025-07-01 21:34:20 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-04 18:33:48 +08:00
|
|
|
public async Task<int> DeleteMenu(MenuBean menu, SqlSugarClient db)
|
|
|
|
|
{
|
2025-07-13 18:05:31 +08:00
|
|
|
|
2025-07-04 18:33:48 +08:00
|
|
|
Stopwatch stopwatch = new Stopwatch();
|
|
|
|
|
stopwatch.Start();
|
|
|
|
|
var childList = await db.Queryable<DbMenu>()
|
|
|
|
|
.ToChildListAsync(it => it.ParentId, menu.Id);
|
|
|
|
|
var result = await db.Deleteable<DbMenu>(childList)
|
|
|
|
|
.ExecuteCommandAsync();
|
|
|
|
|
stopwatch.Stop();
|
2025-07-06 19:51:53 +08:00
|
|
|
NlogHelper.Info($"删除菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
2025-07-04 18:33:48 +08:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-01 21:34:20 +08:00
|
|
|
public async Task<List<MenuBean>> GetMenuTrees()
|
2025-06-24 20:48:38 +08:00
|
|
|
{
|
2025-07-03 13:53:29 +08:00
|
|
|
Stopwatch stopwatch = new Stopwatch();
|
|
|
|
|
stopwatch.Start();
|
2025-06-30 22:03:49 +08:00
|
|
|
using (var db = DbContext.GetInstance())
|
2025-06-30 20:11:21 +08:00
|
|
|
{
|
2025-06-30 22:03:49 +08:00
|
|
|
List<MenuBean> menuTree = new();
|
2025-07-04 18:33:48 +08:00
|
|
|
var dbMenuTree = await db.Queryable<DbMenu>()
|
|
|
|
|
.ToTreeAsync(dm => dm.Items, dm => dm.ParentId, 0);
|
2025-07-01 21:34:20 +08:00
|
|
|
|
2025-06-30 22:03:49 +08:00
|
|
|
foreach (var dbMenu in dbMenuTree)
|
|
|
|
|
menuTree.Add(dbMenu.CopyTo<MenuBean>());
|
2025-07-03 13:53:29 +08:00
|
|
|
stopwatch.Stop();
|
2025-07-06 19:51:53 +08:00
|
|
|
NlogHelper.Info($"获取菜单树耗时:{stopwatch.ElapsedMilliseconds}ms");
|
2025-06-30 22:03:49 +08:00
|
|
|
return menuTree;
|
2025-06-30 20:11:21 +08:00
|
|
|
}
|
2025-06-24 20:48:38 +08:00
|
|
|
}
|
2025-06-26 19:36:27 +08:00
|
|
|
|
2025-06-30 22:03:49 +08:00
|
|
|
|
2025-07-02 12:01:20 +08:00
|
|
|
public async Task<int> Add(MenuBean menu)
|
2025-06-24 20:48:38 +08:00
|
|
|
{
|
2025-07-03 13:53:29 +08:00
|
|
|
Stopwatch stopwatch = new Stopwatch();
|
|
|
|
|
stopwatch.Start();
|
2025-07-04 18:33:48 +08:00
|
|
|
using var db = DbContext.GetInstance();
|
|
|
|
|
var result = await Add(menu, db);
|
|
|
|
|
stopwatch.Stop();
|
2025-07-06 19:51:53 +08:00
|
|
|
NlogHelper.Info($"添加菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
2025-07-04 18:33:48 +08:00
|
|
|
return result;
|
2025-06-24 20:48:38 +08:00
|
|
|
}
|
2025-07-04 18:33:48 +08:00
|
|
|
|
|
|
|
|
|
2025-07-02 22:07:16 +08:00
|
|
|
/// <summary>
|
2025-07-04 18:33:48 +08:00
|
|
|
/// 添加菜单,支持事务
|
2025-07-02 22:07:16 +08:00
|
|
|
/// </summary>
|
2025-07-04 18:33:48 +08:00
|
|
|
/// <param name="menu"></param>
|
2025-07-02 22:07:16 +08:00
|
|
|
/// <param name="db"></param>
|
|
|
|
|
/// <returns></returns>
|
2025-07-04 18:33:48 +08:00
|
|
|
public async Task<int> Add(MenuBean menu, SqlSugarClient db)
|
2025-06-26 19:36:27 +08:00
|
|
|
{
|
2025-07-03 13:53:29 +08:00
|
|
|
Stopwatch stopwatch = new Stopwatch();
|
|
|
|
|
stopwatch.Start();
|
2025-07-04 18:33:48 +08:00
|
|
|
var result = await db.Insertable<DbMenu>(menu.CopyTo<DbMenu>())
|
|
|
|
|
.ExecuteCommandAsync();
|
2025-07-03 13:53:29 +08:00
|
|
|
stopwatch.Stop();
|
2025-07-06 19:51:53 +08:00
|
|
|
NlogHelper.Info($"添加菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
2025-07-04 18:33:48 +08:00
|
|
|
return result;
|
2025-07-02 22:07:16 +08:00
|
|
|
}
|
2025-06-30 20:11:21 +08:00
|
|
|
|
2025-07-02 22:07:16 +08:00
|
|
|
|
|
|
|
|
public async Task<int> AddVarTableMenu(DbDevice dbDevice, int parentMenuId, SqlSugarClient db)
|
|
|
|
|
{
|
2025-07-03 13:53:29 +08:00
|
|
|
Stopwatch stopwatch = new Stopwatch();
|
|
|
|
|
stopwatch.Start();
|
2025-07-02 22:07:16 +08:00
|
|
|
var addVarTable = new MenuBean()
|
2025-07-04 18:33:48 +08:00
|
|
|
{
|
|
|
|
|
Name = "添加变量表",
|
|
|
|
|
Icon = SegoeFluentIcons.Add.Glyph,
|
|
|
|
|
Type = MenuType.AddVariableTableMenu,
|
|
|
|
|
ParentId = parentMenuId,
|
|
|
|
|
DataId = dbDevice.Id
|
|
|
|
|
};
|
|
|
|
|
var addTableRes = await db.Insertable<DbMenu>(addVarTable)
|
|
|
|
|
.ExecuteCommandAsync();
|
2025-07-03 13:53:29 +08:00
|
|
|
stopwatch.Stop();
|
2025-07-06 19:51:53 +08:00
|
|
|
// NlogHelper.Info($"添加变量表菜单 '{addVarTable.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
2025-07-02 22:07:16 +08:00
|
|
|
return addTableRes;
|
|
|
|
|
}
|
2025-06-26 22:40:20 +08:00
|
|
|
|
2025-07-04 18:33:48 +08:00
|
|
|
|
2025-07-02 22:07:16 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 添加设备菜单
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="device"></param>
|
|
|
|
|
/// <param name="db"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
/// <exception cref="InvalidOperationException"></exception>
|
2025-07-04 18:33:48 +08:00
|
|
|
public async Task<int> Add(DbDevice device, SqlSugarClient db)
|
2025-07-02 22:07:16 +08:00
|
|
|
{
|
2025-07-03 13:53:29 +08:00
|
|
|
Stopwatch stopwatch = new Stopwatch();
|
|
|
|
|
stopwatch.Start();
|
2025-07-04 18:33:48 +08:00
|
|
|
var deviceMainMenu = await db.Queryable<DbMenu>()
|
|
|
|
|
.FirstAsync(m => m.Name == "设备");
|
2025-07-02 22:07:16 +08:00
|
|
|
if (deviceMainMenu == null)
|
|
|
|
|
throw new InvalidOperationException("没有找到设备菜单!!");
|
2025-06-30 22:03:49 +08:00
|
|
|
|
2025-07-02 22:07:16 +08:00
|
|
|
// 添加菜单项
|
|
|
|
|
MenuBean menu = new MenuBean()
|
2025-07-04 18:33:48 +08:00
|
|
|
{
|
|
|
|
|
Name = device.Name,
|
|
|
|
|
Type = MenuType.DeviceMenu,
|
|
|
|
|
DataId = device.Id,
|
|
|
|
|
Icon = SegoeFluentIcons.Devices4.Glyph,
|
|
|
|
|
};
|
2025-07-02 22:07:16 +08:00
|
|
|
menu.ParentId = deviceMainMenu.Id;
|
|
|
|
|
var addDeviceMenuId = await db.Insertable<DbMenu>(menu.CopyTo<DbMenu>())
|
2025-07-04 18:33:48 +08:00
|
|
|
.ExecuteReturnIdentityAsync();
|
2025-07-03 13:53:29 +08:00
|
|
|
stopwatch.Stop();
|
2025-07-06 19:51:53 +08:00
|
|
|
NlogHelper.Info($"添加设备菜单 '{device.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
2025-07-02 22:07:16 +08:00
|
|
|
return addDeviceMenuId;
|
2025-06-26 19:36:27 +08:00
|
|
|
}
|
2025-07-01 21:34:20 +08:00
|
|
|
|
2025-07-04 18:33:48 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 编辑菜单
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="menu"></param>
|
|
|
|
|
/// <returns></returns>
|
2025-07-01 21:34:20 +08:00
|
|
|
public async Task<int> Edit(MenuBean menu)
|
|
|
|
|
{
|
2025-07-03 13:53:29 +08:00
|
|
|
Stopwatch stopwatch = new Stopwatch();
|
|
|
|
|
stopwatch.Start();
|
2025-07-01 21:34:20 +08:00
|
|
|
using (var db = DbContext.GetInstance())
|
|
|
|
|
{
|
2025-07-04 18:33:48 +08:00
|
|
|
var result = await Edit(menu, db);
|
2025-07-03 13:53:29 +08:00
|
|
|
stopwatch.Stop();
|
2025-07-06 19:51:53 +08:00
|
|
|
NlogHelper.Info($"编辑菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
2025-07-03 13:53:29 +08:00
|
|
|
return result;
|
2025-07-01 21:34:20 +08:00
|
|
|
}
|
|
|
|
|
}
|
2025-07-04 18:33:48 +08:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 编辑菜单,支持事务
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="menu"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<int> Edit(MenuBean menu, SqlSugarClient db)
|
|
|
|
|
{
|
|
|
|
|
Stopwatch stopwatch = new Stopwatch();
|
|
|
|
|
stopwatch.Start();
|
|
|
|
|
var result = await db.Updateable<DbMenu>(menu.CopyTo<DbMenu>())
|
|
|
|
|
.ExecuteCommandAsync();
|
|
|
|
|
stopwatch.Stop();
|
2025-07-06 19:51:53 +08:00
|
|
|
NlogHelper.Info($"编辑菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
2025-07-04 18:33:48 +08:00
|
|
|
return result;
|
|
|
|
|
}
|
2025-07-05 18:35:40 +08:00
|
|
|
|
|
|
|
|
public async Task<MenuBean?> GetMenuByDataId(int dataId, MenuType menuType)
|
|
|
|
|
{
|
|
|
|
|
Stopwatch stopwatch = new Stopwatch();
|
|
|
|
|
stopwatch.Start();
|
|
|
|
|
using (var db = DbContext.GetInstance())
|
|
|
|
|
{
|
|
|
|
|
var result = await db.Queryable<DbMenu>()
|
|
|
|
|
.FirstAsync(m => m.DataId == dataId && m.Type == menuType);
|
|
|
|
|
stopwatch.Stop();
|
2025-07-06 19:51:53 +08:00
|
|
|
NlogHelper.Info($"根据DataId '{dataId}' 和 MenuType '{menuType}' 获取菜单耗时:{stopwatch.ElapsedMilliseconds}ms");
|
2025-07-05 18:35:40 +08:00
|
|
|
return result?.CopyTo<MenuBean>();
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-05 21:49:41 +08:00
|
|
|
|
|
|
|
|
public async Task<MenuBean> GetMainMenuByName(string name)
|
|
|
|
|
{
|
|
|
|
|
using var db = DbContext.GetInstance();
|
|
|
|
|
var dbMenu= await db.Queryable<DbMenu>().FirstAsync(m => m.Name == name && m.Type == MenuType.MainMenu);
|
|
|
|
|
return dbMenu.CopyTo<MenuBean>();
|
|
|
|
|
}
|
2025-06-30 20:11:21 +08:00
|
|
|
}
|