using Attribute; using Base; using Dto; using Infrastructure; using Model; using Model.Base; using Repository; using Service; using System.Collections; namespace Services { /// /// 角色 /// [AppService(ServiceType = typeof(ISysRoleService), ServiceLifetime = LifeTime.Transient)] public class SysRoleService : BaseService, ISysRoleService { private ISysUserRoleService SysUserRoleService; private ISysDeptService DeptService; private ISysRoleMenuService RoleMenuService; public SysRoleService( ISysUserRoleService sysUserRoleService, ISysDeptService deptService, ISysRoleMenuService roleMenuService) { SysUserRoleService = sysUserRoleService; DeptService = deptService; RoleMenuService = roleMenuService; } /// /// 根据条件分页查询角色数据 /// /// 角色信息 /// 分页信息 /// 角色数据集合信息 public PagedInfo SelectRoleList(SysRole sysRole, PagerInfo pager) { var exp = Expressionable.Create(); exp.And(role => role.DelFlag == "0"); exp.AndIF(!string.IsNullOrEmpty(sysRole.roleName), role => role.roleName.Contains(sysRole.roleName)); exp.AndIF(!string.IsNullOrEmpty(sysRole.roleCode), role => role.roleCode == sysRole.roleCode); var query = Queryable() .Where(exp.ToExpression()) .OrderBy(x => x.roleId) .Select((role) => new SysRole { userNum = SqlFunc.Subqueryable().Where(f => f.RoleId == role.roleId).Count() }, true); return query.ToPage(pager); } /// /// 查询所有角色 /// /// public List SelectRoleAll() { return Queryable() .Where(role => role.DelFlag == "0") .OrderBy(role => role.roleId) .ToList(); } /// /// 根据用户查询 /// /// /// public List SelectRolePermissionByUserId(long userId) { return Queryable() .Where(role => role.DelFlag == "0") .Where(it => SqlFunc.Subqueryable().Where(s => s.UserId == userId).Any()) .OrderBy(role => role.roleId) .ToList(); } /// /// 通过角色ID查询角色 /// /// 角色ID /// 角色对象信息 public SysRole SelectRoleById(long roleId) { return GetId(roleId); } /// /// 批量删除角色信息 /// /// 需要删除的角色ID /// public int DeleteRoleByRoleId(long[] roleIds) { foreach (var item in roleIds) { CheckRoleAllowed(new SysRole(item)); SysRole role = SelectRoleById(item); if (SysUserRoleService.CountUserRoleByRoleId(item) > 0) { throw new CustomException($"{role.roleName}已分配,不能删除"); } } return Delete(roleIds); } /// /// 更改角色权限状态 /// /// /// public int UpdateRoleStatus(SysRole roleDto) { return Update(roleDto, it => new { it.DelFlag }, f => f.roleId == roleDto.roleId); } /// /// 校验角色权限是否唯一 /// /// 角色信息 /// public string CheckRoleKeyUnique(SysRole sysRole) { long roleId = 0 == sysRole.roleId ? -1L : sysRole.roleId; SysRole info = GetFirst(it => it.roleCode == sysRole.roleCode); if (info != null && info.roleId != roleId) { return UserConstants.NOT_UNIQUE; } return UserConstants.UNIQUE; } /// /// 校验角色是否允许操作 /// /// public void CheckRoleAllowed(SysRole role) { if (IsRoleAdmin(role.roleId)) { throw new CustomException("不允许操作超级管理员角色"); } } /// /// 新增保存角色信息 /// /// 角色信息 /// public long InsertRole(SysRole sysRole) { sysRole.CreateTime = DateTime.Now; sysRole.roleId = InsertReturnBigIdentity(sysRole); //插入角色部门数据 DeptService.InsertRoleDepts(sysRole); return sysRole.roleId; } /// /// 通过角色ID删除角色和菜单关联 /// /// 角色ID /// public int DeleteRoleMenuByRoleId(long roleId) { return RoleMenuService.DeleteRoleMenuByRoleId(roleId); } /// /// 修改数据权限信息 /// /// /// public bool AuthDataScope(SysRoleDto sysRoleDto) { return UseTran2(() => { //删除角色菜单 //DeleteRoleMenuByRoleId(sysRoleDto.roleId); //InsertRoleMenu(sysRoleDto); var oldMenus = SelectUserRoleMenus(sysRoleDto.roleId); var newMenus = sysRoleDto.menuIds; //并集菜单 var arr_c = oldMenus.Intersect(newMenus).ToArray(); //获取减量菜单 var delMenuIds = oldMenus.Where(c => !arr_c.Contains(c)).ToArray(); //获取增量 var addMenuIds = newMenus.Where(c => !arr_c.Contains(c)).ToArray(); RoleMenuService.DeleteRoleMenuByRoleIdMenuIds(sysRoleDto.roleId, delMenuIds); sysRoleDto.menuIds = addMenuIds.ToList(); sysRoleDto.delMenuIds= delMenuIds.ToList(); InsertRoleMenu(sysRoleDto); Console.WriteLine($"减少了{delMenuIds.Length},增加了{addMenuIds.Length}菜单"); }); } #region Service /// /// 批量新增角色菜单信息 /// /// /// public int InsertRoleMenu(SysRoleDto sysRoleDto) { int rows = 1; // 新增用户与角色管理 List sysRoleMenus = new(); foreach (var item in sysRoleDto.menuIds) { SysRoleMenu rm = new() { MenuId = item, RoleId = sysRoleDto.roleId, }; sysRoleMenus.Add(rm); } //添加角色菜单 if (sysRoleMenus.Count > 0) { rows = RoleMenuService.AddRoleMenu(sysRoleMenus); } return rows; } /// /// 判断是否是管理员 /// /// /// public bool IsAdmin(long userid) { List roles = SelectUserRoleKeys(userid); return ((IList)roles).Contains(GlobalConstant.AdminRole); } /// /// 判断是否是管理员 /// /// /// public bool IsRoleAdmin(long roleid) { var roleInfo = GetFirst(x => x.roleId == roleid) ?? new SysRole(); return roleInfo.roleCode == GlobalConstant.AdminRole; } /// /// 获取角色菜单id集合 /// /// /// public List SelectUserRoleMenus(long roleId) { var list = RoleMenuService.SelectRoleMenuByRoleId(roleId); return list.Select(x => x.MenuId).Distinct().ToList(); } /// /// 根据用户所有角色获取菜单 /// /// /// public List SelectRoleMenuByRoleIds(long[] roleIds) { return RoleMenuService.SelectRoleMenuByRoleIds(roleIds) .Select(x => x.MenuId) .Distinct().ToList(); } /// /// 获取用户角色列表 /// /// /// public List SelectUserRoleListByUserId(long userId) { return Context.Queryable() .LeftJoin((ur, r) => ur.RoleId == r.roleId) .Where((ur, r) => ur.UserId == userId && r.roleId > 0) .WithCache(60 * 10) .Select((ur, r) => r) .ToList(); } /// /// 获取用户权限集合 /// /// /// public List SelectUserRoles(long userId) { var list = SelectUserRoleListByUserId(userId).Where(f => f.DelFlag == "0"); return list.Select(x => x.roleId).ToList(); } /// /// 获取用户权限字符串集合 /// /// /// public List SelectUserRoleKeys(long userId) { var list = SelectUserRoleListByUserId(userId); return list.Select(x => x.roleCode).ToList(); } /// /// 获取用户所有角色名 /// /// /// public List SelectUserRoleNames(long userId) { var list = SelectUserRoleListByUserId(userId); return list.Select(x => x.roleName).ToList(); } #endregion /// /// 修改保存角色信息 /// /// 角色信息 /// public int UpdateRole(SysRole sysRole) { sysRole.UpdateTime = DateTime.Now; var result = UseTran(() => { //修改角色信息 UpdateSysRole(sysRole); //删除角色与部门管理 // DeptService.DeleteRoleDeptByRoleId(sysRole.roleId); // //插入角色部门数据 // DeptService.InsertRoleDepts(sysRole); }); return result.IsSuccess ? 1 : 0; } /// /// 修改用户角色 /// /// /// public int UpdateSysRole(SysRole sysRole) { var db = Context; sysRole.UpdateTime = db.GetDate(); return db.Updateable() .SetColumns(it => it.UpdateTime == sysRole.UpdateTime) .SetColumns(it => it.roleDesc == sysRole.roleDesc) .SetColumns(it => it.UpdateBy == sysRole.UpdateBy) .SetColumnsIF(!string.IsNullOrEmpty(sysRole.roleName), it => it.roleName == sysRole.roleName) .SetColumnsIF(!string.IsNullOrEmpty(sysRole.roleCode), it => it.roleCode == sysRole.roleCode) .Where(it => it.roleId == sysRole.roleId) .ExecuteCommand(); } } }