namespace CloudGaming.Api.Admin.ApplicationServices.Systems;
///
/// 菜单服务
///
public class SysMenuService(
IServiceProvider serviceProvider,
IRepository sysFunctionRepository,
IRepository sysMenuFunctionRepository,
IRepository sysRoleMenuFunctionRepository,
IAccountService accountService)
: ApplicationService(serviceProvider)
{
private readonly AccountContext _accountInfo = accountService.GetAccountContext();
///
/// 获取列表数据
///
///
///
public override async Task FindListAsync(PagingSearchInput pagingSearchInput)
{
var query = (from sysMenu in Repository.Select
from sysMenuParent in Repository.Select.Where(w => w.Id == sysMenu.ParentId).DefaultIfEmpty()
select new { t1 = sysMenu, t2 = sysMenuParent })
.WhereIf(pagingSearchInput.Search?.ParentId == 0 || pagingSearchInput.Search?.ParentId == null,
w => w.t1.ParentId == null || w.t1.ParentId == 0)
.WhereIf(pagingSearchInput.Search?.ParentId != 0 && pagingSearchInput.Search?.ParentId != null,
w => w.t1.ParentId == pagingSearchInput.Search.ParentId)
.WhereIf(!string.IsNullOrWhiteSpace(pagingSearchInput.Search?.Name),
a => a.t1.Name.Contains(pagingSearchInput.Search.Name))
.OrderBy(w => w.t1.Number)
.Select(w => new
{
w.t1.Number,
w.t1.Name,
w.t1.Url,
父级菜单 = w.t2.Name,
w.t1.ComponentName,
w.t1.Router,
w.t1.Icon,
w.t1.Close,
w.t1.Show,
w.t1.LastModificationTime,
w.t1.CreationTime,
w.t1.Id,
})
;
var result = await Repository.AsPagingViewAsync(query, pagingSearchInput);
//覆盖值
result
.FormatValue(query, w => w.CreationTime, (oldValue) => oldValue.ToString("yyyy-MM-dd"))
.FormatValue(query, w => w.LastModificationTime, (oldValue) => oldValue?.ToString("yyyy-MM-dd"))
;
return result;
}
///
/// 根据id数组删除
///
///
///
public override async Task DeleteListAsync(List ids)
{
foreach (var item in ids)
{
//删除当前菜单及一下的子集菜单
var menu = await Repository.FindByIdAsync(item);
var menus = await Repository.ToListAsync(w =>
w.LevelCode == menu.LevelCode || w.LevelCode.StartsWith(menu.LevelCode + "."));
await Repository.DeleteAsync(menus);
//删除菜单关联表
await sysRoleMenuFunctionRepository.DeleteAsync(w => menus.Select(w => w.Id).Contains(w.MenuId));
await sysMenuFunctionRepository.DeleteAsync(w => menus.Select(w => w.Id).Contains(w.MenuId));
}
}
///
/// 查询表单数据
///
///
///
public override async Task> FindFormAsync(int id)
{
var res = new Dictionary();
var form = await Repository.FindByIdAsync(id);
var allFunctions = await sysFunctionRepository.Select
.OrderBy(w => w.Number)
.ToListAsync();
var menuFunctionList = await sysMenuFunctionRepository.Select
.Where(w => w.MenuId == id)
.OrderBy(w => w.Number)
.ToListAsync();
res[nameof(id)] = id == 0 ? "" : id;
res[nameof(form)] = form.NullSafe();
res[nameof(allFunctions)] = allFunctions;
res[nameof(menuFunctionList)] = menuFunctionList;
return res;
}
///
/// 保存数据
///
///
///
public override async Task SaveFormAsync(SysMenuFormDto form)
{
var model = form.Form;
var menuFunctionList = form.MenuFunctionList;
model = await Repository.InsertOrUpdateAsync(model);
#region 更新级别码
if (model.ParentId == null || model.ParentId == 0)
{
model.LevelCode = model.Id.ToString();
}
else
{
var parent = await Repository.FindByIdAsync(model.ParentId);
model.LevelCode = parent.LevelCode + "." + model.Id;
}
model = await Repository.InsertOrUpdateAsync(model);
#endregion
#region 处理菜单功能绑定表
await sysMenuFunctionRepository.DeleteAsync(w => w.MenuId == model.Id);
if (menuFunctionList.Count <= 0) return;
foreach (var item in menuFunctionList)
{
item.Id = item.Id == Guid.Empty ? Guid.Empty : item.Id;
item.MenuId = model.Id;
}
await sysMenuFunctionRepository.InsertRangeAsync(menuFunctionList);
#endregion
}
///
/// 获取所有的菜单
///
///
///
public async Task> GetAllAsync(SysMenu search)
{
var query = (from sysMenu in Repository.Select
from sysMenuParent in Repository.Select.Where(w => w.Id == sysMenu.ParentId).DefaultIfEmpty()
select new { t1 = sysMenu, t2 = sysMenuParent })
.WhereIf(!string.IsNullOrWhiteSpace(search?.Name), a => a.t1.Name.Contains(search.Name))
.OrderBy(w => w.t1.Number)
.Select(w => new SysMenuDto
{
Id = w.t1.Id,
Number = w.t1.Number,
Name = w.t1.Name,
Url = w.t1.Url,
ParentName = w.t2.Name,
ParentId = w.t1.ParentId,
JumpUrl = w.t1.JumpUrl,
ComponentName = w.t1.ComponentName,
Router = w.t1.Router,
Icon = w.t1.Icon,
Close = w.t1.Close,
Show = w.t1.Show,
KeepAlive = w.t1.KeepAlive,
State = w.t1.State,
LevelCode = w.t1.LevelCode,
LastModificationTime = w.t1.LastModificationTime,
CreationTime = w.t1.CreationTime,
})
;
return await query.ToListAsync();
}
#region 创建系统左侧菜单
///
/// 根据角色ID 获取菜单
///
///
public async Task> GetMenusByCurrentRoleAsync()
{
var sysMenuAllList = await Repository.Select
.Where(w => w.State)
.OrderBy(w => w.Number)
.ToListAsync();
if (_accountInfo.IsAdministrator) return sysMenuAllList;
var sysMenuList = await (
from t1 in sysRoleMenuFunctionRepository.Select.Where(w =>
_accountInfo.SysRoles.Select(s => s.Id).Contains(w.RoleId))
from t2 in Repository.Select.Where(w => w.Id == t1.MenuId && w.State)
//.DefaultIfEmpty()
from t3 in sysMenuFunctionRepository.Select
.Where(w => w.Id == t1.MenuFunctionId &&
w.FunctionCode == PermissionFunctionConsts.Function_Display && t2.Id == w.MenuId)
//.DefaultIfEmpty()
select t2
)
.ToListAsync()
;
var newSysMenuList = new List();
foreach (var item in sysMenuList)
{
//检查 item 是否已经存在于新集合中
if (!newSysMenuList.Any(w => w.Id == item.Id))
newSysMenuList.Add(item);
CheckUpperLevel(sysMenuAllList, sysMenuList, newSysMenuList, item);
}
return newSysMenuList.OrderBy(w => w.Number).ToList();
}
private void CheckUpperLevel(List sysMenuAllList, List oldSysMenuList,
List newSysMenuList, SysMenu menu)
{
if (oldSysMenuList.Any(w => w.Id == menu.ParentId)) return;
var item = sysMenuAllList.Find(w => w.Id == menu.ParentId);
if (item == null) return;
//检查 item 是否已经存在于新集合中
if (!newSysMenuList.Any(w => w.Id == item.Id))
newSysMenuList.Add(item);
CheckUpperLevel(sysMenuAllList, oldSysMenuList, newSysMenuList, item);
}
///
/// 创建菜单
///
///
public List CreateMenus(List sysMenuList)
{
var result = new List();
foreach (var item in sysMenuList)
{
var menu = item.MapTo();
menu.JumpUrl = string.IsNullOrWhiteSpace(item.JumpUrl) ? item.Router : item.JumpUrl;
result.Add(menu);
}
return result;
}
#endregion 左侧菜单
///
/// 复制菜单
///
///
///
public async Task CopyMenuAsync(int id)
{
var menu = await this.Repository.FindByIdAsync(id);
var menuFunc = await sysMenuFunctionRepository.Select
.Where(w => w.MenuId == id)
.ToListAsync();
var newMenu = menu.CopyObject();
newMenu.Id = 0;
newMenu.Number++;
newMenu = await this.Repository.InsertAsync(newMenu);
#region 更新级别码
if (newMenu.ParentId == null || newMenu.ParentId == 0)
{
newMenu.LevelCode = newMenu.Id.ToString();
}
else
{
var parent = await this.Repository.FindByIdAsync(newMenu.ParentId);
newMenu.LevelCode = parent.LevelCode + "." + newMenu.Id;
}
#endregion
await this.Repository.UpdateAsync(newMenu);
var newMenuFuncList = menuFunc.CopyObject();
foreach (var item in newMenuFuncList)
{
item.Id = Guid.NewGuid();
item.MenuId = newMenu.Id;
}
return await sysMenuFunctionRepository.InsertRangeAsync(newMenuFuncList);
}
///
/// 获取菜单国际化json
///
///
public async Task> GetGlobalNameJsonAsync()
{
return await this.Repository.SelectNoTracking
.OrderBy(w => w.Id)
.ToDictionaryAsync(w => "menu." + w.Id, w => w.Name!);
}
}