using System.Collections.Generic; using System.Data; using System.Linq; using System.Threading.Tasks; using Znyc.Cloudcar.Admin.Commons.Core.Dtos; using Znyc.Cloudcar.Admin.Commons.Entitys; using Znyc.Cloudcar.Admin.Commons.Extensions; using Znyc.Cloudcar.Admin.Commons.Mapping; using Znyc.Cloudcar.Admin.Commons.Services; using Znyc.Cloudcar.Admin.Security.Dtos; using Znyc.Cloudcar.Admin.Security.Entitys; using Znyc.Cloudcar.Admin.Security.IRepositories; using Znyc.Cloudcar.Admin.Security.IServices; namespace Znyc.Cloudcar.Admin.Security.Services { /// /// 菜单 /// public class MenuService : BaseService, IMenuService { private readonly IAdminUserRepository _adminUserRepository; private readonly IMenuRepository _MenuRepository; private readonly IRoleAuthorizeRepository roleAuthorizeRepository; private readonly ISystemTypeRepository systemTypeRepository; /// /// /// /// /// /// /// public MenuService(IMenuRepository repository, IAdminUserRepository adminUserRepository, IRoleAuthorizeRepository _roleAuthorizeRepository, ISystemTypeRepository _systemTypeRepository ) : base(repository) { _MenuRepository = repository; _adminUserRepository = adminUserRepository; roleAuthorizeRepository = _roleAuthorizeRepository; systemTypeRepository = _systemTypeRepository; } /// /// 根据用户获取功能菜单 /// /// 用户ID /// public List GetMenuByUser(long userId) { List result = new List(); List allMenuls = new List(); List subMenuls = new List(); string where = "Layers=1"; IEnumerable allMenus = _MenuRepository.GetAllByIsNotDeleteAndEnabledMark(); allMenuls = allMenus.ToList(); if (userId.ToString() == string.Empty) //超级管理员 { return allMenuls; } AdminUserEntity user = _adminUserRepository.Get(userId); if (user == null) { return result; } long userRoles = user.RoleId; where = string.Format("ItemType = 1 and ObjectType = 1 and ObjectId='{0}'", userRoles); IEnumerable Menus = roleAuthorizeRepository.GetListWhere(where); foreach (RoleAuthorizeEntity item in Menus) { MenuEntity MenuEntity = allMenuls.Find(t => t.Id == item.ItemId); if (MenuEntity != null) { result.Add(MenuEntity); } } return result.OrderBy(t => t.SortCode).ToList(); } /// /// 获取功能菜单适用于Vue 树形列表 /// /// 子系统Id /// public async Task> GetAllMenuTreeTable(string systemTypeId) { string where = "1=1"; List reslist = new List(); if (!string.IsNullOrEmpty(systemTypeId)) { IEnumerable elist = await _MenuRepository.GetListWhereAsync("SystemTypeId='" + systemTypeId + "'"); List list = elist.OrderBy(t => t.SortCode).ToList(); List oneMenuList = list.FindAll(t => t.ParentId == 0); foreach (MenuEntity item in oneMenuList) { MenuTreeTableOutputDto menuTreeTableOutputDto = new MenuTreeTableOutputDto(); menuTreeTableOutputDto = item.MapTo(); menuTreeTableOutputDto.Children = GetSubMenus(list, item.Id).ToList(); reslist.Add(menuTreeTableOutputDto); } } else { IEnumerable listSystemType = await systemTypeRepository.GetListWhereAsync(where); foreach (SystemTypeEntity systemType in listSystemType) { MenuTreeTableOutputDto menuTreeTableOutputDto = new MenuTreeTableOutputDto { Id = systemType.Id, FullName = systemType.FullName, EnCode = systemType.EnCode, UrlAddress = systemType.Url, IsEnabled = systemType.IsEnabled, SystemTag = true }; IEnumerable elist = await _MenuRepository.GetListWhereAsync("SystemTypeId='" + systemType.Id + "'"); if (elist.Count() > 0) { List list = elist.OrderBy(t => t.SortCode).ToList(); menuTreeTableOutputDto.Children = GetSubMenus(list, 0).ToList(); } reslist.Add(menuTreeTableOutputDto); } } return reslist; } /// /// 根据角色ID字符串(逗号分开)和系统类型ID,获取对应的操作功能列表 /// /// 角色ID /// 系统类型ID /// 是否是菜单 /// public List GetFunctions(string roleIds, string typeID, bool isMenu = false) { return _MenuRepository.GetFunctions(roleIds, typeID, isMenu).ToList(); } /// /// 根据系统类型ID,获取对应的操作功能列表 /// /// 系统类型ID /// public List GetFunctions(string typeID) { return _MenuRepository.GetFunctions(typeID).ToList(); } /// /// 根据父级功能编码查询所有子集功能,主要用于页面操作按钮权限 /// /// 菜单功能编码 /// public async Task> GetListByParentEnCode(string enCode) { string where = string.Format("EnCode='{0}'", enCode); MenuEntity function = await repository.GetWhereAsync(where); where = string.Format("ParentId='{0}'", function.ParentId); IEnumerable list = await repository.GetAllByIsNotEnabledMarkAsync(where); return list.MapTo().ToList(); } /// /// 按条件批量删除 /// /// 主键Id集合 /// 事务对象 /// public CommonResult DeleteBatchWhere(DeletesInputDto idsInfo, IDbTransaction trans = null) { CommonResult result = new CommonResult(); string where = string.Empty; for (long i = 0; i < idsInfo.Ids.Length; i++) { if (idsInfo.Ids[0] != null) { @where = string.Format("ParentId='{0}'", idsInfo.Ids[0]); IEnumerable list = _MenuRepository.GetListWhere(@where); if (list.Count() > 0) { result.ErrMsg = "功能存在子集数据,不能删除"; return result; } } } where = "id in ('" + idsInfo.Ids.Join(",").Trim(',').Replace(",", "','") + "')"; bool bl = repository.DeleteBatchWhere(where); if (bl) { result.ErrCode = "0"; } return result; } /// /// 按条件批量删除 /// /// 主键Id集合 /// 事务对象 /// public async Task DeleteBatchWhereAsync(DeletesInputDto idsInfo, IDbTransaction trans = null) { CommonResult result = new CommonResult(); string where = string.Empty; for (long i = 0; i < idsInfo.Ids.Length; i++) { if (idsInfo.Ids[0].ToString().Length > 0) { @where = string.Format("ParentId='{0}'", idsInfo.Ids[0]); IEnumerable list = _MenuRepository.GetListWhere(@where); if (list.Count() > 0) { result.ErrMsg = "功能存在子集数据,不能删除"; return result; } } } where = "id in ('" + idsInfo.Ids.Join(",").Trim(',').Replace(",", "','") + "')"; bool bl = await repository.DeleteBatchWhereAsync(where); if (bl) { result.ErrCode = "0"; } return result; } /// /// 获取子菜单,递归调用 /// /// /// 父级Id /// private List GetSubMenus(List data, long ParentId) { List list = new List(); MenuTreeTableOutputDto menuTreeTableOutputDto = new MenuTreeTableOutputDto(); List ChilList = data.FindAll(t => t.ParentId == ParentId); foreach (MenuEntity entity in ChilList) { menuTreeTableOutputDto = entity.MapTo(); menuTreeTableOutputDto.Children = GetSubMenus(data, entity.Id).OrderBy(t => t.SortCode) .MapTo(); list.Add(menuTreeTableOutputDto); } return list; } } }