318 lines
13 KiB
C#
318 lines
13 KiB
C#
using AutoMapper;
|
|
using AutoMapper.QueryableExtensions;
|
|
using IRaCIS.Application.Interfaces;
|
|
using IRaCIS.Application.ViewModels;
|
|
using IRaCIS.Core.Application.Contracts.RequestAndResponse;
|
|
using IRaCIS.Core.Domain.Interfaces;
|
|
using IRaCIS.Core.Domain.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace IRaCIS.Application.Services
|
|
{
|
|
public class MenuService : IMenuService
|
|
{
|
|
private readonly IMenuFunctionRepository _menuFunctionRepository;
|
|
private readonly IRoleMenuFunctionRepository _roleMenuFunctionRepository;
|
|
private readonly IMapper _mapper;
|
|
|
|
public MenuService(IMenuFunctionRepository menuRepository, IRoleMenuFunctionRepository roleMenuRepository, IMapper mapper)
|
|
{
|
|
_menuFunctionRepository = menuRepository;
|
|
_roleMenuFunctionRepository = roleMenuRepository;
|
|
_mapper = mapper;
|
|
}
|
|
|
|
#region byzhouhang 递归
|
|
|
|
/// <summary>
|
|
/// 根据父节点ID递归生成树
|
|
/// </summary>
|
|
/// <param name="parentId"></param>
|
|
/// <param name="allMenuList"></param>
|
|
/// <returns></returns>
|
|
public List<MenuTreeNode> GetTreeList(Guid parentId, List<MenuFunctionDTO> allMenuList)
|
|
{
|
|
//树节点集合 每个节点包含一个菜单项 和一个子菜单集合
|
|
List<MenuTreeNode> treeList = new List<MenuTreeNode>();
|
|
|
|
// 根据父菜单节点获取子菜单节点 并且进行排序
|
|
List<MenuFunctionDTO> menuList = allMenuList.Where(x => x.ParentId == parentId).OrderBy(t => t.ShowOrder).ToList();
|
|
|
|
foreach (var menuItem in menuList)
|
|
{
|
|
MenuTreeNode treeItem = new MenuTreeNode()
|
|
{
|
|
Id = menuItem.Id,
|
|
ParentId = menuItem.ParentId,
|
|
RouteName = menuItem.RouteName,
|
|
MenuName = menuItem.MenuName,
|
|
Path = menuItem.Path,
|
|
Hidden = menuItem.Hidden,
|
|
Component = menuItem.Component,
|
|
FunctionName = menuItem.FunctionName,
|
|
IsFunction = menuItem.IsFunction,
|
|
MetaActiveMenu = menuItem.MetaActiveMenu,
|
|
MetaBreadcrumb = menuItem.MetaBreadcrumb,
|
|
MetaIcon = menuItem.MetaIcon,
|
|
MetaTitle = menuItem.MetaTitle,
|
|
Redirect = menuItem.Redirect,
|
|
ShowOrder = menuItem.ShowOrder,
|
|
Note = menuItem.Note,
|
|
Status = menuItem.Status,
|
|
Children = GetTreeList(menuItem.Id, allMenuList)
|
|
};
|
|
|
|
treeList.Add(treeItem);
|
|
}
|
|
return treeList;
|
|
}
|
|
|
|
|
|
///// <summary>
|
|
///// 递归获取某父节点下所有的子节点
|
|
///// </summary>
|
|
///// <param name="parentId"></param>
|
|
///// <param name="allMenuList"></param>
|
|
///// <returns></returns>
|
|
//private List<Guid> GetMenuIds(Guid parentId, List<MenuFunctionViewModel> allMenuList)
|
|
//{
|
|
// var menuIdList = new List<Guid>();
|
|
|
|
// // 根据单节点获取子节点 并且进行排序
|
|
// List<MenuFunctionViewModel> menuList = allMenuList.Where(x => x.ParentId == parentId).OrderBy(t => t.ShowOrder).ToList();
|
|
|
|
// foreach (var menuItem in menuList)
|
|
// {
|
|
// menuIdList.Add(menuItem.Id);
|
|
|
|
// menuIdList.AddRange(GetMenuIds(menuItem.Id, allMenuList));
|
|
// }
|
|
|
|
// return menuIdList;
|
|
//}
|
|
|
|
#endregion
|
|
|
|
////获取菜单树 没有被禁用的
|
|
//public List<MenuTreeNode> GetTreeNotDisabled()
|
|
//{
|
|
// var allMenuList = _menuFunctionRepository.GetAll().Where(t => t.Status == 1).ProjectTo<MenuFunctionViewModel>(_mapper.ConfigurationProvider).ToList();
|
|
|
|
// return GetTreeList(Guid.Empty, allMenuList);
|
|
//}
|
|
|
|
//所有的菜单树 包括禁用的
|
|
public List<MenuTreeNode> GetTreeAll()
|
|
{
|
|
var allMenuList = _menuFunctionRepository.GetAll().ProjectTo<MenuFunctionDTO>(_mapper.ConfigurationProvider).ToList();
|
|
|
|
return GetTreeList(Guid.Empty, allMenuList);
|
|
}
|
|
|
|
public List<MenuTreeNodeSelect> GetMenuFunctionIsSelectByRoleId(Guid roleId)
|
|
{
|
|
var menuFuncQuery = from function in _menuFunctionRepository.Find(u => !u.IsFunction)
|
|
join roleMenuFunctionItem in _roleMenuFunctionRepository.GetAll()
|
|
.Where(t => t.RoleId == roleId)
|
|
on function.Id equals roleMenuFunctionItem.MenuFunctionId into t
|
|
from roleFunction in t.DefaultIfEmpty()
|
|
select new MenuTreeNodeSelect()
|
|
{
|
|
IsSelect = roleFunction != null,
|
|
ShowOrder = function.ShowOrder,
|
|
MenuName = function.MenuName,
|
|
Note = function.Note,
|
|
FunctionName = function.FunctionName,
|
|
ParentId = function.ParentId,
|
|
Id = function.Id,
|
|
};
|
|
var list = menuFuncQuery.ToList();
|
|
return GetSelectTreeList(Guid.Empty, list);
|
|
}
|
|
|
|
public List<MenuTreeNodeSelect> GetFunctionIsSelectByRoleId(Guid roleId, Guid parentId)
|
|
{
|
|
var menuFuncQuery = from function in _menuFunctionRepository.Find(u => u.IsFunction &&
|
|
u.ParentId == parentId)
|
|
join roleMenuFunctionItem in _roleMenuFunctionRepository.GetAll()
|
|
.Where(t => t.RoleId == roleId)
|
|
on function.Id equals roleMenuFunctionItem.MenuFunctionId into t
|
|
from roleFunction in t.DefaultIfEmpty()
|
|
select new MenuTreeNodeSelect()
|
|
{
|
|
IsSelect = roleFunction != null,
|
|
ShowOrder = function.ShowOrder,
|
|
MenuName = function.MenuName,
|
|
Note = function.Note,
|
|
FunctionName = function.FunctionName,
|
|
ParentId = function.ParentId,
|
|
Id = function.Id,
|
|
};
|
|
return menuFuncQuery.ToList();
|
|
//return GetSelectTreeList(Guid.Empty, list);
|
|
}
|
|
|
|
private List<MenuTreeNode> GetTreeList(Guid parentId, List<MenuTreeNode> allMenuList)
|
|
{
|
|
List<MenuTreeNode> TreeList = new List<MenuTreeNode>();
|
|
List<MenuTreeNode> menuList = allMenuList.Where(x => x.ParentId == parentId).OrderBy(t => t.ShowOrder).ToList();
|
|
|
|
foreach (var menuItem in menuList)
|
|
{
|
|
MenuTreeNode treeItem = new MenuTreeNode()
|
|
{
|
|
Id = menuItem.Id,
|
|
ParentId = menuItem.ParentId,
|
|
RouteName = menuItem.RouteName,
|
|
MenuName = menuItem.MenuName,
|
|
Path = menuItem.Path,
|
|
Hidden = menuItem.Hidden,
|
|
Component = menuItem.Component,
|
|
FunctionName = menuItem.FunctionName,
|
|
IsFunction = menuItem.IsFunction,
|
|
MetaActiveMenu = menuItem.MetaActiveMenu,
|
|
MetaBreadcrumb = menuItem.MetaBreadcrumb,
|
|
MetaIcon = menuItem.MetaIcon,
|
|
MetaTitle = menuItem.MetaTitle,
|
|
Redirect = menuItem.Redirect,
|
|
ShowOrder = menuItem.ShowOrder,
|
|
Note = menuItem.Note,
|
|
Status = menuItem.Status,
|
|
Children = GetTreeList(menuItem.Id, allMenuList)
|
|
};
|
|
|
|
TreeList.Add(treeItem);
|
|
}
|
|
return TreeList;
|
|
}
|
|
|
|
|
|
private List<MenuTreeNodeSelect> GetSelectTreeList(Guid parentId, List<MenuTreeNodeSelect> allMenuList)
|
|
{
|
|
List<MenuTreeNodeSelect> TreeList = new List<MenuTreeNodeSelect>();
|
|
List<MenuTreeNodeSelect> menuList = allMenuList.Where(x => x.ParentId == parentId).OrderBy(t => t.ShowOrder).ToList();
|
|
|
|
foreach (var menuItem in menuList)
|
|
{
|
|
MenuTreeNodeSelect treeItem = new MenuTreeNodeSelect()
|
|
{
|
|
Id = menuItem.Id,
|
|
ParentId = menuItem.ParentId,
|
|
RouteName = menuItem.RouteName,
|
|
MenuName = menuItem.MenuName,
|
|
Note = menuItem.Note,
|
|
IsSelect = menuItem.IsSelect,
|
|
ShowOrder = menuItem.ShowOrder,
|
|
Children = GetSelectTreeList(menuItem.Id, allMenuList)
|
|
};
|
|
|
|
TreeList.Add(treeItem);
|
|
}
|
|
return TreeList;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 添加菜单
|
|
/// </summary>
|
|
/// <param name="menuAddOrUpdateModel"></param>
|
|
/// <param name="userId"></param>
|
|
/// <returns></returns>
|
|
public IResponseOutput AddOrUpdateMenu(MenuFunctionCommand menuAddOrUpdateModel, Guid userId)
|
|
{
|
|
if (menuAddOrUpdateModel.Id == null || menuAddOrUpdateModel.Id == Guid.Empty)
|
|
{
|
|
bool exist = _menuFunctionRepository.IsExist(u => u.ParentId == menuAddOrUpdateModel.ParentId && u.MenuName == menuAddOrUpdateModel.MenuName);
|
|
if (exist)
|
|
{
|
|
return ResponseOutput.NotOk("该父节点已经存在同名的节点");
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(menuAddOrUpdateModel.FunctionName))
|
|
{
|
|
menuAddOrUpdateModel.IsFunction = true;
|
|
}
|
|
|
|
var result = _menuFunctionRepository.Add(_mapper.Map<MenuFunction>(menuAddOrUpdateModel));
|
|
|
|
if (_menuFunctionRepository.SaveChanges())
|
|
{
|
|
return ResponseOutput.Ok(result.Id.ToString());
|
|
}
|
|
return ResponseOutput.NotOk();
|
|
}
|
|
else
|
|
{
|
|
var menuUpdateModel = menuAddOrUpdateModel;
|
|
bool exist = _menuFunctionRepository.IsExist(u => u.ParentId == menuUpdateModel.ParentId &&
|
|
u.MenuName == menuUpdateModel.MenuName && u.Id != menuUpdateModel.Id);
|
|
if (exist)
|
|
{
|
|
return ResponseOutput.NotOk("该父节点已经存在同名的节点");
|
|
}
|
|
var updateModel = _mapper.Map<MenuFunction>(menuUpdateModel);
|
|
|
|
if (!string.IsNullOrWhiteSpace(updateModel.FunctionName))
|
|
{
|
|
updateModel.IsFunction = true;
|
|
}
|
|
_menuFunctionRepository.Update(updateModel);
|
|
|
|
var success = _menuFunctionRepository.SaveChanges();
|
|
return ResponseOutput.Result(success);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除菜单
|
|
/// </summary>
|
|
/// <param name="menuId"></param>
|
|
/// <returns></returns>
|
|
public IResponseOutput DeleteMenuFunction(Guid menuId)
|
|
{
|
|
var success = _menuFunctionRepository.Delete(u => u.Id == menuId || u.ParentId == menuId);
|
|
return ResponseOutput.Result(success);
|
|
}
|
|
|
|
public IResponseOutput UpdateRoleMenuSelect(Guid roleId, List<Guid> menuIds)
|
|
{
|
|
var deleteSuccess =
|
|
_roleMenuFunctionRepository.Delete(t => t.RoleId == roleId && !t.IsFunction);
|
|
foreach (var item in menuIds)
|
|
{
|
|
_roleMenuFunctionRepository.Add(new RoleMenuFunction()
|
|
{
|
|
MenuFunctionId = item,
|
|
RoleId = roleId,
|
|
IsFunction = false
|
|
});
|
|
}
|
|
var addSuccess = _roleMenuFunctionRepository.SaveChanges();
|
|
|
|
return ResponseOutput.Result(deleteSuccess || addSuccess);
|
|
}
|
|
|
|
public IResponseOutput UpdateRoleFunctionSelect(FunctionSelectDTO function)
|
|
{
|
|
var success = false;
|
|
if (function.IsSelect)
|
|
{
|
|
_roleMenuFunctionRepository.Add(new RoleMenuFunction()
|
|
{
|
|
MenuFunctionId = function.FunctionId,
|
|
RoleId = function.RoleId,
|
|
IsFunction = true
|
|
});
|
|
success = _roleMenuFunctionRepository.SaveChanges();
|
|
}
|
|
else
|
|
{
|
|
success = _roleMenuFunctionRepository.Delete(u => u.MenuFunctionId == function.FunctionId && function.RoleId == u.RoleId);
|
|
}
|
|
|
|
return ResponseOutput.Result(success);
|
|
}
|
|
}
|
|
}
|