using Dlhis.Entity.Sys.Entities; using Dlhis.Sys.Identity.Request; using Dlhis.Sys.Identity.Response; using Dlhis.User.Identity.Services; using Kurisu.AspNetCore.Scope; using Kurisu.AspNetCore.UnifyResultAndValidation; using Kurisu.AspNetCore.Utils.Extensions; using Kurisu.Core.DataAccess.Dto; using Kurisu.Core.User.Abstractions; using Kurisu.SqlSugar.Extensions; using Kurisu.SqlSugar.Services; namespace Dlhis.Sys.Identity.Services.Implements; public class RoleService : IRoleService { private readonly IConfiguration _configuration; private readonly ICurrentUser _currentUser; public RoleService(IConfiguration configuration, ICurrentUser currentUser) { _configuration = configuration; _currentUser = currentUser; } public async Task> GetPageAsync(RolePageRequest request) { return await Scoped.Request.Value.CreateAsync(async sp => { var dbContext = sp.GetService(); var identityConnection = _configuration.GetSection("SqlSugarOptions:IdentityConnectionString").Value; dbContext.Client.CurrentConnectionConfig.ConnectionString = identityConnection; return await dbContext.Queryable() .WhereIF(request.Search.IsPresent(), x => x.Name.Contains(request.Search) || x.NormalizedName.Contains(request.Search)) .Select() .ToPageListAsync(request); }); } public async Task GetMyRoleAsync() { var userId = _currentUser.GetIntSubjectId(); return await Scoped.Request.Value.CreateAsync(async sp => { var dbContext = sp.GetService(); var identityConnection = _configuration.GetSection("SqlSugarOptions:IdentityConnectionString").Value; dbContext.Client.CurrentConnectionConfig.ConnectionString = identityConnection; return await dbContext.Queryable() .LeftJoin((x, r) => x.RoleId == r.Id) .Where(x => x.UserId == userId) .Select((x, r) => new RoleRespone { Id = r.Id, Name = r.Name, NormalizedName = r.NormalizedName, }) .FirstAsync(); }); } public async Task SaveRoleAsync(RoleSaveRequest request) { await Scoped.Request.Value.CreateAsync(async sp => { var dbContext = sp.GetService(); var identityConnection = _configuration.GetSection("SqlSugarOptions:IdentityConnectionString").Value; dbContext.Client.CurrentConnectionConfig.ConnectionString = identityConnection; await CheckDuplicateNameAsync(dbContext, request); if (!request.Id.HasValue) { await dbContext.InsertAsync(new RoleEntity { Name = request.Name, NormalizedName = request.Name.ToUpperInvariant(), ConcurrencyStamp = Guid.NewGuid(), }); } else { var entity = await dbContext.Queryable().Where(x => x.Id == request.Id).FirstAsync(); entity.ThrowIfNull("角色不存在"); entity.Name = request.Name; entity.NormalizedName = request.Name.ToUpperInvariant(); entity.ConcurrencyStamp = Guid.NewGuid(); await dbContext.UpdateAsync(entity); } }); } private async Task CheckDuplicateNameAsync(IDbContext db, RoleSaveRequest request) { var exists = await db.Queryable() .WhereIF(request.Id.HasValue, x => x.Id != request.Id) .Where(x => x.Name == request.Name) .AnyAsync(); exists.ThrowIfTrue($"重复角色名称{request.Name}"); } public async Task AssignRoleAsync(AssignRoleRequest request) { var myUserId = _currentUser.GetIntSubjectId(); await Scoped.Request.Value.CreateAsync(async sp => { //不能被其他人修改 if (request.UserId == 1 && request.UserId != myUserId) { throw new UserFriendlyException("无法修改admin角色"); } var dbContext = sp.GetService(); var identityConnection = _configuration.GetSection("SqlSugarOptions:IdentityConnectionString").Value; dbContext.Client.CurrentConnectionConfig.ConnectionString = identityConnection; var existsUser = await dbContext.Queryable().Where(x => x.Id == request.UserId).AnyAsync(); existsUser.ThrowIfFalse("分配的用户不存在"); var userRole = await dbContext.Queryable().Where(x => x.UserId == request.UserId).FirstAsync(); if (request.RoleId.HasValue) { var existsRole = await dbContext.Queryable().Where(x => x.Id == request.RoleId).AnyAsync(); existsUser.ThrowIfFalse("分配的角色不存在"); if (userRole == null) { await dbContext.InsertAsync(new UserRoleEntity { UserId = request.UserId, RoleId = request.RoleId.Value, }); } else { await dbContext.Updateable() .SetColumns(x => x.RoleId == request.RoleId) .Where(x => x.UserId == request.UserId) .ExecuteCommandAsync(); } } else { await dbContext.DeleteReallyAsync(userRole); } }); } public async Task RemoveRoleAsync(RoleRemoveRequest request) { await Scoped.Request.Value.CreateAsync(async sp => { var dbContext = sp.GetService(); var identityConnection = _configuration.GetSection("SqlSugarOptions:IdentityConnectionString").Value; dbContext.Client.CurrentConnectionConfig.ConnectionString = identityConnection; var role = await dbContext.Queryable().FirstAsync(x => x.Id == request.Id); role.ThrowIfNull("角色不存在"); //删除角色,自动删除UserRoles对应关系 await dbContext.DeleteReallyAsync(role); }); } }