using Furion.DatabaseAccessor.Extensions; using YBEE.EQM.Core; namespace YBEE.EQM.Application; /// /// 角色服务 /// public class SysRoleService(IRepository roleRep, IRepository roleUserRep, ISysRoleMenuService roleMenuService, ICacheService cacheService) : ISysRoleService, ITransient { /// /// 添加角色 /// /// /// public async Task Add(AddSysRoleInput input) { var isExist = await roleRep.DetachedEntities.AnyAsync(u => u.Name == input.Name); if (isExist) { throw Oops.Oh(ErrorCode.E2004); } var role = input.Adapt(); role.RoleType = RoleType.NORMAL; await role.InsertNowAsync(); } /// /// 删除角色 /// /// /// public async Task Del(DeleteSysRoleInput input) { var sysRole = await roleRep.FirstOrDefaultAsync(u => u.Id == input.Id); if (sysRole.RoleType != RoleType.NORMAL) { throw Oops.Oh(ErrorCode.E8201); } await sysRole.DeleteNowAsync(); //级联删除该角色对应的用户-角色表关联信息 var userRoles = await roleUserRep.Where(u => u.SysRoleId == sysRole.Id).ToListAsync(); await roleUserRep.DeleteAsync(userRoles); //级联删除该角色对应的角色-菜单表关联信息 await roleMenuService.DeleteRoleMenuListByRoleId(sysRole.Id); } /// /// 更新角色 /// /// /// public async Task Update(UpdateSysRoleInput input) { var role = await roleRep.DetachedEntities.FirstOrDefaultAsync(u => u.Id == input.Id); if (role.RoleType != RoleType.NORMAL) { throw Oops.Oh(ErrorCode.E8202); } var isExist = await roleRep.DetachedEntities.AnyAsync(u => u.Name == input.Name && u.Id != input.Id); if (isExist) { throw Oops.Oh(ErrorCode.E2004); } var sysRole = input.Adapt(); await sysRole.UpdateExcludeNowAsync(new[] { nameof(sysRole.RoleDataScope) }); } ///// ///// 设置角色数据权限范围 ///// ///// //public async Task SetDataScope() //{ // // TODO: // // 清空缓存,DelByPatternAsync,CommonConst.CACHE_KEY_PERMISSION //} /// /// 获取当前用户角色数据权限范围 /// /// public async Task GetCurrentUserDataScope() { var roles = await cacheService.GetRoleDataScope(CurrentSysUserInfo.SysUserId); if (roles == null) { return new(); } RoleDataScope roleDataScope = new(); foreach (var role in roles) { roleDataScope.EducationStages.AddRange(role.RoleDataScope.EducationStages); roleDataScope.SysOrgIds.AddRange(role.RoleDataScope.SysOrgIds); roleDataScope.TwoWayTables.AddRange(role.RoleDataScope.TwoWayTables); } roleDataScope.EducationStages = roleDataScope.EducationStages.Distinct().ToList(); roleDataScope.SysOrgIds = roleDataScope.SysOrgIds.Distinct().ToList(); roleDataScope.TwoWayTables = roleDataScope.TwoWayTables.Distinct().ToList(); return roleDataScope; } /// /// 获取所有角色 /// /// public async Task> GetAllList() { return await roleRep.DetachedEntities.OrderBy(u => u.RoleType).ProjectToType().ToListAsync(); } /// /// 根据角色ID列表获取角色列表 /// /// /// public async Task> GetListByIds(List ids) { return await roleRep.DetachedEntities.Where(t => ids.Contains(t.Id)).ProjectToType().ToListAsync(); } }