//----------SysMenu开始----------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Admin.Core.Common;
using Admin.Core.IRepository;
using Admin.Core.IRepository.ISys;
using Admin.Core.IService.ISys;
using Admin.Core.Model;
using Admin.Core.Model.Sys;
using AutoMapper;
using SqlSugar;
namespace Admin.Core.Service.Sys
{
///
/// 菜单权限表Service
///
public partial class SysMenuService : BaseServices, ISysMenuService
{
IBaseRepository dal;
private readonly ISysUserRoleRepository _sysUserRoleRepository;
private readonly ISysRoleMenuRepository _sysRoleMenuRepository;
IMapper _mapper;
public SysMenuService(IBaseRepository dal, ISysUserRoleRepository sysUserRoleRepository, ISysRoleMenuRepository sysRoleMenuRepository, IMapper mapper)
{
this.dal = dal;
BaseDal = dal;
_sysUserRoleRepository = sysUserRoleRepository;
_sysRoleMenuRepository = sysRoleMenuRepository;
_mapper = mapper;
}
///
/// 根据用户查询系统菜单列表
///
/// 用户ID
/// 查询参数
/// 菜单列表
public async Task> SelectMenuList(int userId, PageQuery pageQuery)
{
List menuList = new List();
if (!pageQuery.IsNotEmptyOrNull() || !pageQuery.Query.IsNotEmptyOrNull())
{
pageQuery = new PageQuery();
pageQuery.Query = new SysMenu();
}
Expression> whereExpression = x => true;
if (pageQuery.Query.MenuName.IsNotEmptyOrNull())
{
whereExpression = whereExpression.And(x => x.MenuName.Contains(pageQuery.Query.MenuName));
}
if (pageQuery.Query.Status.IsNotEmptyOrNull())
{
whereExpression = whereExpression.And(x => x.Status == pageQuery.Query.Status);
}
// 管理员显示所有菜单信息
if (!UserView.IsAdmin(userId))
{
var roles = await _sysUserRoleRepository.QueryAsync(x => x.UserID == userId);
var roleIds = roles.Select(x => x.RoleID).ToList();
var menus = await _sysRoleMenuRepository.QueryAsync(x => roleIds.Contains(x.RoleID));
var mendIds = menus.Select(x => x.MenuID).ToList();
whereExpression = whereExpression.And(x => mendIds.Contains(x.MenuID));
}
menuList = await dal.QueryAsync(whereExpression, "OrderNum asc");
return menuList;
}
///
/// 根据用户查询系统菜单列表
///
/// 用户ID
/// 菜单列表
public async Task> SelectMenuList(int userId)
{
Expression> whereExpression = x => x.Status == SysConst.ENABLE;
// 管理员显示所有菜单信息
if (!UserView.IsAdmin(userId))
{
var roles = await _sysUserRoleRepository.QueryAsync(x => x.UserID == userId);
var roleIds = roles.Select(x => x.RoleID).ToList();
var menus = await _sysRoleMenuRepository.QueryAsync(x => roleIds.Contains(x.RoleID));
var mendIds = menus.Select(x => x.MenuID).ToList();
whereExpression = whereExpression.And(x => mendIds.Contains(x.MenuID));
}
var menuList = await dal.QueryAsync(whereExpression, "OrderNum asc");
return menuList;
}
///
/// 根据用户ID查询权限
///
/// 用户ID
/// 权限列表
public async Task> SelectMenuPermsByUserId(int userId)
{
var data = await SelectMenuList(userId);
return data.Select(x => x.Perms).ToList();
}
///
/// 根据用户ID查询菜单
///
/// 用户名称
/// 菜单列表
public async Task> SelectMenuTreeByUserId(int userId)
{
var data = await SelectMenuList(userId);
var menus = _mapper.Map>(data);
return GetChildPerms(menus, 0);
}
///
/// 根据角色ID查询菜单树信息
///
/// 角色ID
/// 选中菜单列表
public async Task> SelectMenuListByRoleId(int roleId)
{
var roleMenus = await _sysRoleMenuRepository.QueryAsync(x => x.RoleID == roleId);
return roleMenus.Select(x => (int)x.MenuID).ToList();
}
///
/// 构建前端路由所需要的菜单
///
/// 菜单列表
/// 路由列表
public List BuildMenus(List menus)
{
List routers = new List();
foreach (var menu in menus)
{
RouterVo router = new RouterVo();
router.hidden = !(bool)menu.Visible;
router.name = GetRouteName(menu);
router.path = GetRouterPath(menu);
router.component = GetComponent(menu);
router.meta = new MetaVo()
{
title = menu.MenuName,
icon = menu.Icon,
noCache = !(bool)menu.IsCache,
link = string.Empty
};
List cMenus = menu.Children;
if (cMenus.Count > 0 && cMenus.Count > 0 && SysConst.TYPE_DIR.Equals(menu.MenuType))
{
router.alwaysShow = true;
router.redirect = "noRedirect";
router.children = BuildMenus(cMenus);
}
else if (IsMenuFrame(menu))
{
router.meta = null;
List childrenList = new List();
RouterVo children = new RouterVo();
children.path = menu.Path;
children.component = menu.Component;
children.name = StringHelper.Capitalize(menu.Path);
children.meta = new MetaVo
{
title = menu.MenuName,
icon = menu.Icon,
noCache = !(bool)menu.IsCache,
link = string.Empty
};
childrenList.Add(children);
router.children = childrenList;
}
else if (menu.ParentID.Value == 0 && IsInnerLink(menu))
{
router.meta = new MetaVo
{
title = menu.MenuName,
icon = menu.Icon,
noCache = false,
link = string.Empty
};
router.path = "/inner";
List childrenList = new List();
RouterVo children = new RouterVo();
string routerPath = menu.Path;
children.path = routerPath;
children.component = SysConst.INNER_LINK;
children.name = StringHelper.Capitalize(routerPath);
children.meta = new MetaVo
{
title = menu.MenuName,
icon = menu.Icon,
noCache = false,
link = menu.Path
};
childrenList.Add(children);
router.children = childrenList;
}
routers.Add(router);
}
return routers;
}
///
/// 构建前端所需要树结构
///
/// 菜单列表
/// 树结构列表
public List BuildMenuTree(List menus)
{
List returnList = new List();
List tempList = new List();
foreach (var dept in menus)
{
tempList.Add(dept.MenuID);
}
for (IEnumerator iterator = menus.GetEnumerator(); iterator.MoveNext();)
{
MenuView menu = iterator.Current;
// 如果是顶级节点, 遍历该父节点的所有子节点
if (!tempList.Contains((int)menu.ParentID))
{
RecursionFn(menus, menu);
returnList.Add(menu);
}
}
if (returnList.Count == 0)
{
returnList = menus;
}
return returnList;
}
///
/// 构建前端所需要下拉树结构
///
/// 菜单列表
/// 下拉树结构列表
public List BuildMenuTreeSelect(List menus)
{
var menuTreesList = _mapper.Map>(menus);
var menuTrees = BuildMenuTree(menuTreesList);
return _mapper.Map>(menuTrees);
}
///
/// 根据菜单ID查询信息
///
/// 菜单ID
/// 菜单信息
public async Task SelectMenuById(int menuId)
{
return await dal.QueryByIdAsync(menuId);
}
///
/// 是否存在菜单子节点
///
/// 菜单ID
/// 结果
public async Task HasChildByMenuId(int menuId)
{
var result = await dal.QueryAsync(x => x.ParentID == menuId);
return result.Count > 0;
}
///
/// 查询菜单使用数量
///
/// 菜单ID
/// 结果
public async Task CheckMenuExistRole(int menuId)
{
var roleMenus = await _sysRoleMenuRepository.QueryAsync(x => x.MenuID == menuId);
return roleMenus.Count;
}
///
/// 新增保存菜单信息
///
/// 菜单信息
/// 结果
public async Task InsertMenu(SysMenu menu)
{
menu.CreateTime = DateTime.Now;
menu.UpdateTime = DateTime.Now;
if (!menu.ParentID.IsNotEmptyOrNull())
{
menu.ParentID = 0;
}
return await dal.AddAsync(menu); ;
}
///
/// 修改保存菜单信息
///
/// 菜单信息
/// 结果
public async Task UpdateMenu(SysMenu menu)
{
var list = await dal.QueryAsync();
var updates = new List();
GetChilds(list, menu.MenuID, ref updates);
updates.Add(menu);
updates.ForEach(x =>
{
x.Status = menu.Status;
x.UpdateBy = menu.UpdateBy;
x.UpdateTime = DateTime.Now;
});
return await dal.UpdateAsync(updates);
}
///
/// 删除菜单管理信息
///
/// 菜单ID
/// 结果
public async Task DeleteMenuById(int menuId)
{
return await dal.DeleteByIdAsync(menuId);
}
///
/// 校验菜单名称是否唯一
///
/// 菜单信息
/// 结果
public async Task CheckMenuNameUnique(SysMenu menu)
{
var info = await dal.QueryAsync(x => x.MenuName == menu.MenuName && x.MenuID != menu.MenuID);
return info.Count == 0;
}
///
/// 获取路由名称
///
/// 菜单信息
/// 路由名称
public string GetRouteName(SysMenu menu)
{
string routerName = menu.Path;
// 非外链并且是一级目录(类型为目录)
if (IsMenuFrame(menu))
{
routerName = string.Empty;
}
if (routerName.IsNotEmptyOrNull())
{
routerName = routerName.Substring(0, 1).ToUpper() + routerName.Substring(1);
}
return routerName;
}
///
/// 获取路由地址
///
/// 菜单信息
/// 路由地址
public string GetRouterPath(SysMenu menu)
{
string routerPath = menu.Path;
// 内链打开外网方式
if (menu.ParentID.Value != 0 && IsInnerLink(menu))
{
}
// 非外链并且是一级目录(类型为目录)
if (0 == menu.ParentID.Value && SysConst.TYPE_DIR.Equals(menu.MenuType) && SysConst.NO_FRAME.Equals(menu.IsFrame))
{
routerPath = "/" + menu.Path;
}
// 非外链并且是一级目录(类型为菜单)
else if (IsMenuFrame(menu))
{
routerPath = "/";
}
return routerPath;
}
///
/// 获取组件信息
///
/// 菜单信息
/// 组件信息
public string GetComponent(SysMenu menu)
{
string component = SysConst.LAYOUT;
if (menu.Component.IsNotEmptyOrNull() && !IsMenuFrame(menu))
{
component = menu.Component;
}
else if (menu.ParentID.Value != 0 && IsInnerLink(menu))
{
component = SysConst.INNER_LINK;
}
else if (IsParentView(menu))
{
component = SysConst.PARENT_VIEW;
}
return component;
}
///
/// 是否为菜单内部跳转
///
/// 菜单信息
/// 结果
public bool IsMenuFrame(SysMenu menu)
{
return menu.ParentID.Value == 0 && SysConst.TYPE_MENU.Equals(menu.MenuType) && menu.IsFrame.Equals(SysConst.NO_FRAME);
}
///
/// 是否为内链组件
///
/// 菜单信息
/// 结果
public bool IsInnerLink(SysMenu menu)
{
return menu.IsFrame.Equals(SysConst.NO_FRAME) && StringHelper.IsUrl(menu.Path);
}
///
/// 是否为parent_view组件
///
/// 菜单信息
/// 结果
public bool IsParentView(SysMenu menu)
{
return menu.ParentID.Value != 0 && SysConst.TYPE_DIR.Equals(menu.MenuType);
}
///
/// 根据子节点的ID获取所有父节点
///
/// 分类表
/// 传入的子节点ID
/// String
public List GetParents(List list, int childrenId, ref List returnList)
{
foreach (var item in list)
{
if (item.MenuID == childrenId && item.ParentID > 0)
{
returnList.Add(item);
GetParents(list, (int)item.ParentID, ref returnList);
}
else if (item.MenuID == childrenId && item.ParentID == 0)
{
returnList.Add(item);
}
}
return returnList;
}
///
/// 根据父节点的ID获取所有子节点
///
/// 分类表
/// 传入的父节点ID
/// String
public List GetChilds(List list, int parentId, ref List returnList)
{
foreach (var item in list)
{
if (item.ParentID == parentId)
{
returnList.Add(item);
GetChilds(list, item.MenuID, ref returnList);
}
}
return returnList;
}
///
/// 根据父节点的ID获取所有子节点
///
/// 分类表
/// 传入的父节点ID
/// String
public List GetChildPerms(List list, int parentId)
{
List returnList = new List();
for (IEnumerator iterator = list.GetEnumerator(); iterator.MoveNext();)
{
MenuView t = iterator.Current;
// 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
if (t.ParentID == parentId)
{
RecursionFn(list, t);
returnList.Add(t);
}
}
return returnList;
}
///
/// 递归列表
///
///
///
private void RecursionFn(List list, MenuView t)
{
// 得到子节点列表
List childList = GetChildList(list, t);
t.Children = childList;
foreach (MenuView tChild in childList)
{
if (HasChild(list, tChild))
{
RecursionFn(list, tChild);
}
}
}
///
/// 得到子节点列表
///
private List GetChildList(List list, MenuView t)
{
List tlist = new List();
IEnumerator it = list.GetEnumerator();
while (it.MoveNext())
{
MenuView n = it.Current;
if (n.ParentID.Value == t.MenuID)
{
tlist.Add(n);
}
}
return tlist;
}
///
/// 判断是否有子节点
///
private bool HasChild(List list, MenuView t)
{
return GetChildList(list, t).Count > 0 ? true : false;
}
}
}
//----------SysMenu结束----------