diff --git a/Src/Dlhis.Sys/Identity/Controllers/UserTenantsController.cs b/Src/Dlhis.Sys/Identity/Controllers/UserTenantsController.cs new file mode 100644 index 00000000..60559162 --- /dev/null +++ b/Src/Dlhis.Sys/Identity/Controllers/UserTenantsController.cs @@ -0,0 +1,42 @@ +using Dlhis.Sys.Identity.Request; +using Dlhis.Sys.Identity.Response; +using Dlhis.Sys.Identity.Services; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace Dlhis.Sys.Identity.Controllers; + +[Authorize(Roles = "admin")] +[Route("api/[controller]")] +[ApiController] +public class UserTenantsController : ControllerBase +{ + private readonly IUserTenantService _userTenantService; + + public UserTenantsController(IUserTenantService userTenantService) + { + _userTenantService = userTenantService; + } + + + /// + /// 获取用户的基地配置 + /// + /// + /// + [HttpGet] + public async Task> GetUserTenantsByIdAsync(int userId) + { + return await _userTenantService.GetUserTenantsByIdAsync(userId); + } + + /// + /// 保存 + /// + /// + [HttpPost("save")] + public async Task SaveUserTenantsAsync(UserTenantSaveRequest request) + { + await _userTenantService.SaveUserTenantsAsync(request); + } +} \ No newline at end of file diff --git a/Src/Dlhis.Sys/Identity/Request/UserTenantSaveRequest.cs b/Src/Dlhis.Sys/Identity/Request/UserTenantSaveRequest.cs new file mode 100644 index 00000000..2df4d8c5 --- /dev/null +++ b/Src/Dlhis.Sys/Identity/Request/UserTenantSaveRequest.cs @@ -0,0 +1,9 @@ +namespace Dlhis.Sys.Identity.Request +{ + public class UserTenantSaveRequest + { + public int UserId { get; set; } + + public List TenantCodes { get; set; } = new(); + } +} \ No newline at end of file diff --git a/Src/Dlhis.Sys/Identity/Response/UserTenantResponse.cs b/Src/Dlhis.Sys/Identity/Response/UserTenantResponse.cs new file mode 100644 index 00000000..3b39d8d7 --- /dev/null +++ b/Src/Dlhis.Sys/Identity/Response/UserTenantResponse.cs @@ -0,0 +1,6 @@ +namespace Dlhis.Sys.Identity.Response; + +public class UserTenantResponse +{ + public string Code { get; set; } +} \ No newline at end of file diff --git a/Src/Dlhis.Sys/Identity/Services/IUserTenantService.cs b/Src/Dlhis.Sys/Identity/Services/IUserTenantService.cs new file mode 100644 index 00000000..6cb1dc21 --- /dev/null +++ b/Src/Dlhis.Sys/Identity/Services/IUserTenantService.cs @@ -0,0 +1,11 @@ +using Dlhis.Sys.Identity.Request; +using Dlhis.Sys.Identity.Response; + +namespace Dlhis.Sys.Identity.Services; + +public interface IUserTenantService : IScopeDependency +{ + Task> GetUserTenantsByIdAsync(int id); + + Task SaveUserTenantsAsync(UserTenantSaveRequest request); +} \ No newline at end of file diff --git a/Src/Dlhis.Sys/Identity/Services/Implements/UserTenantService.cs b/Src/Dlhis.Sys/Identity/Services/Implements/UserTenantService.cs new file mode 100644 index 00000000..6dbaffe5 --- /dev/null +++ b/Src/Dlhis.Sys/Identity/Services/Implements/UserTenantService.cs @@ -0,0 +1,40 @@ +using Dlhis.Entity.Sys.Entities; +using Dlhis.Sys.Identity.Request; +using Dlhis.Sys.Identity.Response; +using Kurisu.SqlSugar.Services; + +namespace Dlhis.Sys.Identity.Services.Implements; + +public class UserTenantService : IUserTenantService +{ + private readonly IDbContext _dbContext; + + public UserTenantService(IDbContext dbContext) + { + _dbContext = dbContext.ChangeDb("identity"); + } + + public async Task> GetUserTenantsByIdAsync(int id) + { + return await _dbContext.Queryable() + .Where(x => x.UserId == id) + .Select(x => new UserTenantResponse + { + Code = x.TenantCode + }) + .ToListAsync(); + } + + public async Task SaveUserTenantsAsync(UserTenantSaveRequest request) + { + var userTenants = await _dbContext.Queryable().Where(x => x.UserId == request.UserId).ToListAsync(); + await _dbContext.DeleteReallyAsync(userTenants); + + await _dbContext.InsertAsync(request.TenantCodes.Select(x => new UserTenantEntity + { + UserId = request.UserId, + TenantCode = x + }) + .ToList()); + } +} \ No newline at end of file