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 递归 /// /// 根据父节点ID递归生成树 /// /// /// /// public List GetTreeList(Guid parentId, List allMenuList) { //树节点集合 每个节点包含一个菜单项 和一个子菜单集合 List treeList = new List(); // 根据父菜单节点获取子菜单节点 并且进行排序 List 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 GetMenuIds(Guid parentId, List allMenuList) //{ // var menuIdList = new List(); // // 根据单节点获取子节点 并且进行排序 // List 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 GetTreeNotDisabled() //{ // var allMenuList = _menuFunctionRepository.GetAll().Where(t => t.Status == 1).ProjectTo(_mapper.ConfigurationProvider).ToList(); // return GetTreeList(Guid.Empty, allMenuList); //} //所有的菜单树 包括禁用的 public List GetTreeAll() { var allMenuList = _menuFunctionRepository.GetAll().ProjectTo(_mapper.ConfigurationProvider).ToList(); return GetTreeList(Guid.Empty, allMenuList); } public List 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 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 GetTreeList(Guid parentId, List allMenuList) { List TreeList = new List(); List 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 GetSelectTreeList(Guid parentId, List allMenuList) { List TreeList = new List(); List 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; } /// /// 添加菜单 /// /// /// /// 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(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(menuUpdateModel); if (!string.IsNullOrWhiteSpace(updateModel.FunctionName)) { updateModel.IsFunction = true; } _menuFunctionRepository.Update(updateModel); var success = _menuFunctionRepository.SaveChanges(); return ResponseOutput.Result(success); } } /// /// 删除菜单 /// /// /// 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 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); } } }