using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using FineAdmin.IService; using FineAdmin.Model; using FineAdmin.IRepository; namespace FineAdmin.Service { public class ModuleService : BaseService, IModuleService { public IModuleRepository ModuleRepository { get; set; } public IButtonService ButtonService { get; set; } public IRoleAuthorizeService RoleAuthorizeService { get; set; } public dynamic GetListByFilter(ModuleModel filter, PageInfo pageInfo) { throw new NotImplementedException(); } /// /// 获得菜单列表 /// /// /// public dynamic GetModuleList(int roleId) { IEnumerable allMenus = GetModuleListByRoleId(roleId); List treeList = new List(); var rootMenus = allMenus.Where(x => x.ParentId == 0).OrderBy(x => x.SortCode); foreach (var item in rootMenus) { var _tree = new Tree { id = item.Id, title = item.FullName, href = item.UrlAddress, fontFamily = item.FontFamily, icon = item.Icon }; GetModuleListByModuleId(treeList, allMenus, _tree, item.Id); treeList.Add(_tree); } var result = treeList; return result; } /// /// 根据一级菜单加载子菜单列表 /// /// /// /// /// private void GetModuleListByModuleId(List treeList, IEnumerable allMenus, Tree tree, int moduleId) { var childMenus = allMenus.Where(x => x.ParentId == moduleId).OrderBy(x => x.SortCode); if (childMenus != null && childMenus.Count() > 0) { List _children = new List(); foreach (var item in childMenus) { var _tree = new Tree { id = item.Id, title = item.FullName, href = item.UrlAddress, fontFamily = item.FontFamily, icon = item.Icon }; _children.Add(_tree); tree.children = _children; GetModuleListByModuleId(treeList, allMenus, _tree, item.Id); } } } /// /// 根据角色ID获取菜单列表 /// /// /// private IEnumerable GetModuleListByRoleId(int roleId) { string sql = @"SELECT m.* FROM dbsys_Module m"; var list = ModuleRepository.GetModuleListByRoleId(sql, roleId); return list; } /// /// Module treeSelect数据列表 /// public IEnumerable GetModuleTreeSelect() { IEnumerable moduleList = BaseRepository.GetAll("Id,FullName,ParentId", "ORDER BY SortCode ASC"); var rootModuleList = moduleList.Where(x => x.ParentId == 0).OrderBy(x => x.SortCode); List treeSelectList = new List(); foreach (var item in rootModuleList) { TreeSelect tree = new TreeSelect { id = item.Id, name = item.FullName, open = false }; GetModuleChildren(treeSelectList, moduleList, tree, item.Id); treeSelectList.Add(tree); } return treeSelectList; } /// /// 递归遍历treeSelectList /// private void GetModuleChildren(List treeSelectList, IEnumerable moduleList, TreeSelect tree, int id) { var childModuleList = moduleList.Where(x => x.ParentId == id).OrderBy(x => x.SortCode); if (childModuleList != null && childModuleList.Count() > 0) { List _children = new List(); foreach (var item in childModuleList) { TreeSelect _tree = new TreeSelect { id = item.Id, name = item.FullName, open = false }; _children.Add(_tree); tree.children = _children; GetModuleChildren(treeSelectList, moduleList, _tree, item.Id); } } } /// /// 获取所有菜单列表及可用按钮权限列表 /// /// 角色ID /// public IEnumerable GetModuleButtonList(int roleId) { string returnFields = "Id,ParentId,FullName,Icon,SortCode"; string orderby = "ORDER BY SortCode ASC"; IEnumerable list = GetAll(returnFields, orderby); foreach (var item in list) { item.ModuleButtonHtml = ButtonService.GetButtonListHtmlByRoleIdModuleId(roleId, item.Id); item.IsChecked = RoleAuthorizeService.GetListByRoleIdModuleId(roleId, item.Id).Count() > 0 ? true : false; } return list; } } }