//
//
//
using ERP.Core.Constant;
using ERP.Core.Dto;
using ERP.Core.Entities;
using ERP.Core.Interface;
using ERP.Core.Repositorys;
using ERP.Framework.Security;
using ERP.Framework.Utils;
namespace ERP.Core.Services
{
public class SysMenuService : ISysMenuService
{
private readonly SysMenuRepository _sysMenuRepository;
///
///
///
///
public SysMenuService(SysMenuRepository sysMenuRepository)
{
_sysMenuRepository = sysMenuRepository;
}
///
/// 获取
///
///
public List SelectMenuTreeByUserId(long userId)
{
List result;
if (LoginHelper.IsAdmin())
{
result = _sysMenuRepository.List();
}
else
{
result = _sysMenuRepository.SelectMenuListByUserId(userId);
}
return TreeUtil.Build(result);
}
public List BuildMenuTree(List list, string parentPath)
{
var routers = new List();
foreach (var menu in list)
{
var router = new Router()
{
Hidden = menu.Visible,
Name = menu.Path,
Path = GetRoutePath(menu, parentPath),
Component = GetComponent(menu),
Meta = new Meta()
{
Icon = menu.Icon,
Title = menu.MenuName,
//IsCache = menu.ca
}
};
var cMenus = menu.Children;
if (cMenus != null && menu.MenuType == Enums.MenuTypeEnum.Catalog)
{
router.Children = BuildMenuTree(cMenus, router.Path);
}
else if (IsMenuFrame(menu))
{
var childrenList = new List();
var children = new Router()
{
Path = menu.Path,
Component = menu.Component,
Name = menu.Path,
Meta = new Meta()
{
Icon = menu.Icon,
Title = menu.MenuName
}
};
childrenList.Add(children);
router.Children = childrenList;
}
routers.Add(router);
}
return routers;
}
private string GetRoutePath(SysMenu menu, string parentPath)
{
string routerPath;
if (menu.ParentId == 0 && menu.MenuType == Enums.MenuTypeEnum.Catalog)
{
routerPath = "/" + menu.Path;
}
else if (IsMenuFrame(menu))
{
routerPath = "/";
}
else
{
routerPath = parentPath + "/" + menu.Path;
}
return routerPath;
}
///
/// 是否为一级菜单
///
///
///
private bool IsMenuFrame(SysMenu menu)
{
return menu.ParentId == 0 && menu.MenuType == Enums.MenuTypeEnum.Menu;
}
///
/// 是否为中间组件
///
///
///
private bool IsParentView(SysMenu menu)
{
return menu.ParentId != 0 && menu.MenuType == Enums.MenuTypeEnum.Catalog;
}
///
/// 获取组件值
///
///
///
private string GetComponent(SysMenu menu)
{
var component = CoreConstant.LAYOUT;
if (!menu.Component.IsNullOrEmpty() && !IsMenuFrame(menu))
{
component = menu.Component;
}
else if (menu.Component.IsNullOrEmpty() && IsParentView(menu))
{
component = CoreConstant.PARENT_VIEW;
}
return component;
}
}
}