using Infrastructure.Attribute;
using System.Collections.Generic;
using System.Linq;
using ARW.Model.System.Dto;
using ARW.Model.System;
using ARW.Model.System.Vo;
using ARW.Repository.System;
using ARW.Service.System.IService;
using ARW.Common;
using Infrastructure.Extensions;
namespace ARW.Service
{
///
/// 菜单
///
[AppService(ServiceType = typeof(ISysMenuService), ServiceLifetime = LifeTime.Transient)]
public class SysMenuService : BaseService, ISysMenuService
{
public SysMenuRepository MenuRepository;
public ISysRoleService SysRoleService;
public SysMenuService(
SysMenuRepository menuRepository,
ISysRoleService sysRoleService)
{
MenuRepository = menuRepository;
SysRoleService = sysRoleService;
}
///
/// 获取所有菜单数(菜单管理)
///
///
public List SelectTreeMenuList(MenuQueryDto menu, long userId)
{
List menuList;
if (SysRoleService.IsAdmin(userId))
{
menuList = MenuRepository.SelectTreeMenuList(menu);
}
else
{
var userRoles = SysRoleService.SelectUserRoles(userId);
menuList = MenuRepository.SelectTreeMenuListByRoles(menu, userRoles);
}
return menuList;
}
///
/// 获取所有菜单列表
///
///
public List SelectMenuList(MenuQueryDto menu, long userId)
{
List menuList;
if (SysRoleService.IsAdmin(userId))
{
menuList = MenuRepository.SelectMenuList(menu);
}
else
{
var userRoles = SysRoleService.SelectUserRoles(userId);
menuList = MenuRepository.SelectMenuListByRoles(menu, userRoles);
}
return menuList;
}
///
/// 获取菜单详情
///
///
///
public SysMenu GetMenuByMenuId(int menuId)
{
return MenuRepository.SelectMenuById(menuId);
}
///
/// 根据菜单id获取菜单列表
///
///
///
public List GetMenusByMenuId(int menuId)
{
var list = MenuRepository.GetList(f => f.parentId == menuId).OrderBy(f => f.orderNum).ToList();
Context.ThenMapper(list, item =>
{
item.SubNum = Context.Queryable().SetContext(x => x.parentId, () => item.MenuId, item).Count;
});
return list;
}
///
/// 添加菜单
///
///
///
public int AddMenu(SysMenu menu)
{
return MenuRepository.AddMenu(menu);
}
///
/// 编辑菜单
///
///
///
public int EditMenu(SysMenu menu)
{
menu.icon = string.IsNullOrEmpty(menu.icon) ? "" : menu.icon;
return MenuRepository.EditMenu(menu);
}
///
/// 删除菜单管理信息
///
///
///
public int DeleteMenuById(int menuId)
{
return MenuRepository.DeleteMenuById(menuId);
}
///
/// 校验菜单名称是否唯一
///
///
///
public string CheckMenuNameUnique(SysMenu menu)
{
long menuId = menu.MenuId == 0 ? -1 : menu.MenuId;
SysMenu info = MenuRepository.CheckMenuNameUnique(menu);
//if (info != null && menuId != info.menuId && menu.menuName.Equals(info.menuName))
//{
// return UserConstants.NOT_UNIQUE;
//}
if (info != null && info.MenuId != menu.MenuId)
{
return UserConstants.NOT_UNIQUE;
}
return UserConstants.UNIQUE;
}
///
/// 菜单排序
///
///
///
public int ChangeSortMenu(MenuDto menuDto)
{
return MenuRepository.ChangeSortMenu(menuDto);
}
///
/// 是否存在菜单子节点
///
///
///
public bool HasChildByMenuId(long menuId)
{
return MenuRepository.HasChildByMenuId(menuId) > 0;
}
///
/// 获取子级菜单
///
///
///
public List SelectChildByMenuId(long menuId)
{
return MenuRepository.GetList(it => it.parentId == menuId);
}
///
/// 获取左边导航栏菜单树
///
///
///
public List SelectMenuTreeByUserId(long userId)
{
MenuQueryDto dto = new() { Status = "0", MenuTypeIds = "M,C" };
if (SysRoleService.IsAdmin(userId))
{
return MenuRepository.SelectTreeMenuList(dto);
}
else
{
List roleIds = SysRoleService.SelectUserRoles(userId);
return MenuRepository.SelectTreeMenuListByRoles(dto, roleIds);
}
}
///
/// 查询菜单权限
///
///
///
public List SelectMenuPermsListByUserId(long userId)
{
return MenuRepository.SelectMenuPermsByUserId(userId);
}
///
/// 查询精确到按钮的操作权限
///
///
///
public List SelectMenuPermsByUserId(long userId)
{
var menuList = SelectMenuPermsListByUserId(userId).Where(f => !string.IsNullOrEmpty(f.perms));
return menuList.Select(x => x.perms).Distinct().ToList();
}
#region RoleMenu
///
/// 查询菜单使用数量
///
///
///
public bool CheckMenuExistRole(long menuId)
{
return MenuRepository.CheckMenuExistRole(menuId) > 0;
}
#endregion
#region 方法
/////
///// 根据父节点的ID获取所有子节点
/////
///// 分类表
///// 传入的父节点ID
/////
//public List GetChildPerms(List list, int parentId)
//{
// List returnList = new List();
// var data = list.FindAll(f => f.parentId == parentId);
// foreach (var item in data)
// {
// RecursionFn(list, item);
// returnList.Add(item);
// }
// return returnList;
//}
///
/// 递归列表
///
///
///
private void RecursionFn(List list, SysMenu t)
{
//得到子节点列表
List childList = GetChildList(list, t);
t.children = childList;
foreach (var item in childList)
{
if (GetChildList(list, item).Count() > 0)
{
RecursionFn(list, item);
}
}
}
///
/// 递归获取子菜单
///
/// 所有菜单
///
///
private List GetChildList(List list, SysMenu sysMenu)
{
return list.Where(p => p.parentId == sysMenu.MenuId).ToList();
}
///
/// 获取路由侧边栏,动态生成
///
///
///
public List BuildMenus(List menus)
{
List routers = new List();
foreach (var menu in menus)
{
RouterVo router = new()
{
Hidden = "1".Equals(menu.visible),
Name = GetRouteName(menu),
Path = GetRoutePath(menu),
Component = GetComponent(menu),
Meta = new Meta(menu.MenuName, menu.icon, "1".Equals(menu.isCache), menu.MenuNameKey, menu.path)
};
List cMenus = menu.children;
//是目录并且有子菜单
if (cMenus != null && cMenus.Count > 0 && (UserConstants.TYPE_DIR.Equals(menu.menuType)))
{
router.AlwaysShow = true;
router.Redirect = "noRedirect";
router.Children = BuildMenus(cMenus);
}
else if (IsMeunFrame(menu))
{
router.Meta = null;
List childrenList = new();
RouterVo children = new()
{
Path = menu.path,
Component = menu.component,
Name = string.IsNullOrEmpty(menu.path) ? "" : menu.path.ToLower(),
Meta = new Meta(menu.MenuName, menu.icon, "1".Equals(menu.isCache), menu.MenuNameKey, menu.path)
};
childrenList.Add(children);
router.Children = childrenList;
}
else if (menu.parentId == 0 && IsInnerLink(menu))
{
router.Meta = new Meta(menu.MenuName, menu.icon);
router.Path = "/";
List childrenList = new();
RouterVo children = new();
string routerPath = InnerLinkReplaceEach(menu.path);
children.Path = routerPath;
children.Component = UserConstants.INNER_ARWNK;
children.Name = routerPath.ToLower();
children.Meta = new Meta(menu.MenuName, menu.icon, menu.path);
childrenList.Add(children);
router.Children = childrenList;
}
routers.Add(router);
}
return routers;
}
///
/// 构建前端所需要下拉树结构
///
/// 菜单列表
/// 下拉树结构列表
public List BuildMenuTree(List menus)
{
List returnList = new List();
List tempList = menus.Select(f => f.MenuId).ToList();
foreach (var menu in menus)
{
// 如果是顶级节点, 遍历该父节点的所有子节点
if (!tempList.Contains(menu.parentId))
{
RecursionFn(menus, menu);
returnList.Add(menu);
}
}
if (!returnList.Any())
{
returnList = menus;
}
return returnList;
}
///
/// 构建前端所需要下拉树结构
///
///
///
public List BuildMenuTreeSelect(List menus)
{
List menuTrees = BuildMenuTree(menus);
List treeMenuVos = new List();
foreach (var item in menuTrees)
{
treeMenuVos.Add(new TreeSelectVo(item));
}
return treeMenuVos;
}
///
/// 获取路由名称
///
/// 菜单信息
/// 路由名称
public string GetRouteName(SysMenu menu)
{
string routerName = menu.path.ToLower();
// 非外链并且是一级目录(类型为目录)
if (IsMeunFrame(menu))
{
routerName = string.Empty;
}
return routerName;
}
///
/// 获取路由路径
///
/// 菜单信息
/// 路由地址
public string GetRoutePath(SysMenu menu)
{
string routerPath = menu.path;
// 内链打开外网方式
if (menu.parentId != 0 && IsInnerLink(menu))
{
routerPath = InnerLinkReplaceEach(routerPath);
}
// 非外链并且是一级目录(类型为目录)
if (0 == menu.parentId && UserConstants.TYPE_DIR.Equals(menu.menuType)
&& UserConstants.NO_FRAME.Equals(menu.isFrame))
{
routerPath = "/" + menu.path;
}
else if (IsMeunFrame(menu))// 非外链并且是一级目录(类型为菜单)
{
routerPath = "/";
}
return routerPath;
}
///
/// 获取组件名称
///
///
///
public string GetComponent(SysMenu menu)
{
string component = UserConstants.LAYOUT;
if (!string.IsNullOrEmpty(menu.component) && !IsMeunFrame(menu))
{
component = menu.component;
}
else if (menu.component.IsEmpty() && menu.parentId != 0 && IsInnerLink(menu))
{
component = UserConstants.INNER_ARWNK;
}
else if (string.IsNullOrEmpty(menu.component) && IsParentView(menu))
{
component = UserConstants.PARENT_VIEW;
}
return component;
}
///
/// 是否为菜单内部跳转
///
/// 菜单信息
///
public bool IsMeunFrame(SysMenu menu)
{
return menu.parentId == 0 && UserConstants.TYPE_MENU.Equals(menu.menuType)
&& menu.isFrame.Equals(UserConstants.NO_FRAME);
}
///
/// 是否为内链组件
///
/// 菜单信息
/// 结果
public bool IsInnerLink(SysMenu menu)
{
return menu.isFrame.Equals(UserConstants.NO_FRAME) && Tools.IsUrl(menu.path);
}
///
///
/// 是否为parent_view组件
///
/// 菜单信息
///
public bool IsParentView(SysMenu menu)
{
return menu.parentId != 0 && UserConstants.TYPE_DIR.Equals(menu.menuType);
}
///
/// 内链域名特殊字符替换
///
/// param >
/// < returns > returns >
public string InnerLinkReplaceEach(string path)
{
return path.IsNotEmpty() ? path.Replace(UserConstants.HTTP, "").Replace(UserConstants.HTTPS, "") : path;
}
#endregion
}
}