using Edu.AOP.CustomerAttribute; using Edu.Common.Enum; using Edu.Model.ViewModel.System; using Edu.Repository.System; using System; using System.Collections.Generic; using System.Linq; using VT.FW.DB; namespace Edu.Module.System { /// /// 角色处理类 /// public class RoleModule { /// /// 角色仓储层对象 /// private readonly RB_RoleRepository roleRepository = new RB_RoleRepository(); /// /// 角色菜单权限仓储层对象 /// private readonly RB_Role_PermissionRepository rolePermissionRepository = new RB_Role_PermissionRepository(); /// /// 角色功能权限表仓储层对象 /// private readonly RB_Role_FunctionPermissionRepository roleFunctionPermissionRepository = new RB_Role_FunctionPermissionRepository(); /// /// 菜单功能权限仓储层对象 /// private readonly RB_Menu_FunctionRepository functionRepository = new RB_Menu_FunctionRepository(); /// /// 系统菜单处理对象 /// private readonly MenuModule menuModule = new MenuModule(); /// /// 获取角色列表 /// /// /// public List GetRoleListModule(RB_Role_ViewModel query) { return roleRepository.GetRoleListRepository(query); } /// /// 获取角色分页列表 /// /// /// /// /// /// public List GetRolePageListModule(int pageIndex, int pageSize, out long rowsCount, RB_Role_ViewModel query) { return roleRepository.GetRolePageListRepository(pageIndex, pageSize, out rowsCount, query); } /// /// 新增修改角色 /// /// /// [TransactionCallHandler] public virtual bool SetRoleModule(RB_Role_ViewModel model) { bool flag; if (model.RoleId > 0) { Dictionary fileds = new Dictionary() { {nameof(RB_Role_ViewModel.RoleName),model.RoleName.Trim() }, {nameof(RB_Role_ViewModel.RoleIntro),model.RoleIntro }, {nameof(RB_Role_ViewModel.Status),model.Status }, }; flag = roleRepository.Update(fileds, new WhereHelper(nameof(RB_Role_ViewModel.RoleId), model.RoleId)); } else { var newId = roleRepository.Insert(model); model.RoleId = newId; flag = newId > 0; } rolePermissionRepository.DeleteOne(new WhereHelper(nameof(RB_Role_Permission_ViewModel.Role_Id), model.RoleId)); //添加角色权限 if (model.RolePermissionList != null && model.RolePermissionList.Count > 0) { foreach (var roleItem in model.RolePermissionList) { roleItem.Role_Id = model.RoleId; flag= rolePermissionRepository.Insert(roleItem) > 0; } } //添加角色功能权限 roleFunctionPermissionRepository.DeleteOne(new WhereHelper(nameof(RB_Role_FunctionPermission_ViewModel.Role_Id), model.RoleId)); if (model.RoleFunctionList != null && model.RoleFunctionList.Count > 0) { foreach (var roleItem in model.RoleFunctionList) { roleItem.Role_Id = model.RoleId; flag = roleFunctionPermissionRepository.Insert(roleItem) > 0; } } return flag; } /// /// 获取角色实体 /// /// /// public RB_Role_ViewModel GetRoleModule(object RoleId) { return roleRepository.GetEntity(RoleId); } /// /// 预览角色权限 /// /// 角色编号 /// public object ViewRolePermissionModule(int RoleId) { var roleModel = GetRoleModule(RoleId); //菜单列表 var menuList = menuModule.GetTreeMenuModule(new RB_Menu_ViewModel(), roleId: RoleId); //获取功能权限列表 var actionList = functionRepository.GetMenuFunctionListRepository(new RB_Menu_Function_ViewModel() { }, isQueryNormal: true, roleId: RoleId); if (menuList != null && menuList.Count > 0) { foreach (var item in menuList) { item.MenuType = 1; if (item.SubList != null && item.SubList.Count > 0) { foreach (var subItem in item.SubList) { subItem.MenuType = 1; if (subItem.SubList != null && subItem.SubList.Count > 0) { foreach (var childItem in subItem.SubList) { childItem.MenuType = 1; childItem.SubList = new List(); var subActionList = actionList?.Where(qitem => qitem.MenuId == childItem.MenuId)?.ToList() ?? new List(); if (subActionList != null && subActionList.Count > 0) { foreach (var actionItem in actionList) { var actionModel = new MenuTree_ViewModel() { MenuName = actionItem?.FunctionName + "【F】", ParentId = childItem.MenuId, MenuIcon = "", MenuType = 2, MenuUrl = "", MenuId = actionItem.Id, SubList = new List() }; childItem.SubList.Add(actionModel); } } } } } } } } var obj = new { RoleName = roleModel?.RoleName ?? "", menuList }; return obj; } /// /// 修改角色状态 /// /// /// /// public bool SetRoleStatusModule(object RoleId, int Status) { Dictionary fileds = new Dictionary() { {nameof(RB_Role_ViewModel.Status),Status}, }; bool flag = roleRepository.Update(fileds, new WhereHelper(nameof(RB_Role_ViewModel.RoleId), RoleId)); return flag; } /// /// 获取角色权限信息 /// /// /// public List GetRolePermissionModule(int roleId) { //菜单列表 var menuList = menuModule.GetTreeMenuModule(new RB_Menu_ViewModel()); //获取功能权限列表 var actionList = functionRepository.GetMenuFunctionListRepository(new RB_Menu_Function_ViewModel() { }, isQueryNormal: true); //角色菜单权限列表 List rolePermissionList = new List(); //角色功能权限列表 List roleFunctionList = new List(); if (roleId > 0) { rolePermissionList = GetRolePermissionListModule(new RB_Role_Permission_ViewModel() { Role_Id = roleId }); roleFunctionList = GetRoleFunctionPermissionListModule(new RB_Role_FunctionPermission_ViewModel() { Role_Id = roleId }); } if (menuList != null && menuList.Count > 0) { foreach (var item in menuList) { item.IsChecked = rolePermissionList?.Where(qitem => qitem.Menu_Id == item.MenuId)?.Count() > 0; if (item.SubList != null && item.SubList.Count > 0) { foreach (var subItem in item.SubList) { subItem.IsChecked = rolePermissionList?.Where(qitem => qitem.Menu_Id == subItem.MenuId)?.Count() > 0; var secondActionList = actionList?.Where(qitem => qitem.MenuId == subItem.MenuId)?.ToList() ?? new List(); if (secondActionList != null && secondActionList.Count > 0) { foreach (var aItem in secondActionList) { var actionModel = new MenuTree_ViewModel() { MenuName = aItem?.FunctionName + "【F】", ParentId = item.MenuId, MenuIcon = "", MenuType = 2, MenuUrl = "", MenuId = aItem.Id, SubList = new List() }; actionModel.IsChecked = roleFunctionList?.Where(qitem => qitem.Action_Id == aItem.Id)?.Count() > 0; subItem.SubList.Add(actionModel); } } if (subItem.SubList != null && subItem.SubList.Count > 0) { foreach (var childItem in subItem.SubList.Where(qitem=>qitem.MenuType==1)) { childItem.IsChecked = rolePermissionList?.Where(qitem => qitem.Menu_Id == childItem.MenuId)?.Count() > 0; childItem.SubList = new List(); var subActionList = actionList?.Where(qitem => qitem.MenuId == childItem.MenuId)?.ToList() ?? new List(); if (subActionList != null && subActionList.Count > 0) { foreach (var actionItem in subActionList) { var actionModel = new MenuTree_ViewModel() { MenuName = actionItem?.FunctionName+"【F】", ParentId = childItem.MenuId, MenuIcon = "", MenuType = 2, MenuUrl = "", MenuId = actionItem.Id, SubList = new List() }; actionModel.IsChecked = roleFunctionList?.Where(qitem => qitem.Action_Id == actionItem.Id)?.Count() > 0; childItem.SubList.Add(actionModel); } } } } } } } } return menuList; } /// /// 新增修改[角色菜单、角色功能]权限 /// /// 菜单权限列表 /// 功能权限列表 /// [TransactionCallHandler] public bool SetRolePermissionModule(List roleMenuList, List roleFunctionList) { bool flag = false; //菜单权限 if (roleMenuList != null && roleMenuList.Count > 0) { rolePermissionRepository.DeleteOne(new WhereHelper(nameof(RB_Role_Permission_ViewModel.Role_Id), roleMenuList[0].Role_Id)); foreach (var item in roleMenuList) { item.ID = 0; flag = rolePermissionRepository.Insert(item) > 0; } } //功能权限 if (roleFunctionList != null && roleFunctionList.Count > 0) { roleFunctionPermissionRepository.DeleteOne(new WhereHelper(nameof(RB_Role_FunctionPermission_ViewModel.Role_Id), roleFunctionList[0].Role_Id)); foreach (var item in roleFunctionList) { item.ID = 0; flag = roleFunctionPermissionRepository.Insert(item) > 0; } } return flag; } /// /// 获取角色菜单权限列表 /// /// /// public List GetRolePermissionListModule(RB_Role_Permission_ViewModel query) { return rolePermissionRepository.GetRolePermissionListRepository(query); } /// /// 获取角色功能菜单列表 /// /// /// public List GetRoleFunctionPermissionListModule(RB_Role_FunctionPermission_ViewModel query) { return roleFunctionPermissionRepository.GetRoleFunctionPermissionListRepository(query); } } }