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;
|
|
|
|
|
using PMSWPF.Models;
|
2025-07-02 22:07:16 +08:00
|
|
|
using SqlSugar;
|
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-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)
|
|
|
|
|
{
|
|
|
|
|
using (var db = DbContext.GetInstance())
|
|
|
|
|
{
|
|
|
|
|
var childList = await db.Queryable<DbMenu>().ToChildListAsync(it => it.ParentId, menu.Id);
|
|
|
|
|
return await db.Deleteable<DbMenu>(childList).ExecuteCommandAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<List<MenuBean>> GetMenuTrees()
|
2025-06-24 20:48:38 +08:00
|
|
|
{
|
2025-06-25 22:33:57 +08:00
|
|
|
// //无主键用法新:5.1.4.110
|
|
|
|
|
// db.Queryable<Tree>().ToTree(it=>it.Child,it=>it.ParentId,0,it=>it.Id)//+4重载
|
2025-06-30 22:03:49 +08:00
|
|
|
// List<DbMenu> dbMenuList = await _db.Queryable<DbMenu>().ToListAsync();
|
2025-06-30 20:11:21 +08:00
|
|
|
|
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();
|
|
|
|
|
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>());
|
|
|
|
|
|
|
|
|
|
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-06-30 22:03:49 +08:00
|
|
|
using (var db = DbContext.GetInstance())
|
|
|
|
|
{
|
|
|
|
|
return await db.Insertable<DbMenu>(menu.CopyTo<DbMenu>()).ExecuteCommandAsync();
|
|
|
|
|
}
|
2025-06-24 20:48:38 +08:00
|
|
|
}
|
2025-07-02 22:07:16 +08:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 添加默认变量表的菜单
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="device"></param>
|
|
|
|
|
/// <param name="addDeviceMenuId"></param>
|
|
|
|
|
/// <param name="db"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
/// <exception cref="InvalidOperationException"></exception>
|
|
|
|
|
public async Task<int> AddDeviceDefTableMenu(Device device, int parentMenuId,int varTableId, SqlSugarClient db)
|
2025-06-26 19:36:27 +08:00
|
|
|
{
|
2025-07-02 22:07:16 +08:00
|
|
|
var defVarTableMenu = new MenuBean()
|
2025-06-29 12:37:35 +08:00
|
|
|
{
|
2025-07-02 22:07:16 +08:00
|
|
|
Name = "默认变量表",
|
|
|
|
|
Icon = SegoeFluentIcons.Tablet.Glyph,
|
|
|
|
|
Type = MenuType.VariableTableMenu,
|
|
|
|
|
ParentId = parentMenuId,
|
|
|
|
|
DataId = varTableId
|
|
|
|
|
};
|
|
|
|
|
var defTableRes = await db.Insertable<DbMenu>(defVarTableMenu).ExecuteCommandAsync();
|
|
|
|
|
return defTableRes;
|
|
|
|
|
}
|
2025-06-30 20:11:21 +08:00
|
|
|
|
2025-07-02 22:07:16 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 给设备添加默认变量表菜单
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="device"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
/// <exception cref="InvalidOperationException"></exception>
|
|
|
|
|
// public async Task<bool> 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<DbMenu>().Where(m => m.Id == parentMenuId).ExecuteCommandAsync();
|
|
|
|
|
// // throw new InvalidOperationException("添加默认变量表时发生了错误!!");
|
|
|
|
|
// // }
|
|
|
|
|
//
|
|
|
|
|
// await db.CommitTranAsync();
|
|
|
|
|
// return true;
|
|
|
|
|
// }
|
|
|
|
|
// catch (Exception e)
|
|
|
|
|
// {
|
|
|
|
|
// await db.RollbackTranAsync();
|
|
|
|
|
// }
|
|
|
|
|
// finally
|
|
|
|
|
// {
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
public async Task<int> AddVarTableMenu(DbDevice dbDevice, int parentMenuId, SqlSugarClient db)
|
|
|
|
|
{
|
|
|
|
|
var addVarTable = new MenuBean()
|
|
|
|
|
{
|
|
|
|
|
Name = "添加变量表",
|
|
|
|
|
Icon = SegoeFluentIcons.Add.Glyph,
|
|
|
|
|
Type = MenuType.AddVariableTableMenu,
|
|
|
|
|
ParentId = parentMenuId,
|
|
|
|
|
DataId = dbDevice.Id
|
|
|
|
|
};
|
|
|
|
|
var addTableRes = await db.Insertable<DbMenu>(addVarTable).ExecuteCommandAsync();
|
|
|
|
|
return addTableRes;
|
|
|
|
|
}
|
2025-06-26 22:40:20 +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>
|
|
|
|
|
public async Task<int> AddDeviceMenu(DbDevice device, SqlSugarClient db)
|
|
|
|
|
{
|
|
|
|
|
var deviceMainMenu = await db.Queryable<DbMenu>().FirstAsync(m => m.Name == "设备");
|
|
|
|
|
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()
|
|
|
|
|
{
|
|
|
|
|
Name = device.Name,
|
|
|
|
|
Type = MenuType.DeviceMenu,
|
|
|
|
|
DataId = device.Id,
|
|
|
|
|
Icon = SegoeFluentIcons.Devices4.Glyph,
|
|
|
|
|
};
|
|
|
|
|
menu.ParentId = deviceMainMenu.Id;
|
|
|
|
|
var addDeviceMenuId = await db.Insertable<DbMenu>(menu.CopyTo<DbMenu>())
|
|
|
|
|
.ExecuteReturnIdentityAsync();
|
|
|
|
|
return addDeviceMenuId;
|
2025-06-26 19:36:27 +08:00
|
|
|
}
|
2025-07-01 21:34:20 +08:00
|
|
|
|
|
|
|
|
public async Task<int> Edit(MenuBean menu)
|
|
|
|
|
{
|
|
|
|
|
using (var db = DbContext.GetInstance())
|
|
|
|
|
{
|
|
|
|
|
return await db.Updateable<DbMenu>(menu.CopyTo<DbMenu>()).ExecuteCommandAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-06-30 20:11:21 +08:00
|
|
|
}
|