feat 用户基地配置

This commit is contained in:
2024-04-28 11:11:04 +08:00
parent 2380dc9c79
commit 9bdd39acbe
5 changed files with 108 additions and 0 deletions

View File

@@ -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;
}
/// <summary>
/// 获取用户的基地配置
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
[HttpGet]
public async Task<List<UserTenantResponse>> GetUserTenantsByIdAsync(int userId)
{
return await _userTenantService.GetUserTenantsByIdAsync(userId);
}
/// <summary>
/// 保存
/// </summary>
/// <param name="request"></param>
[HttpPost("save")]
public async Task SaveUserTenantsAsync(UserTenantSaveRequest request)
{
await _userTenantService.SaveUserTenantsAsync(request);
}
}

View File

@@ -0,0 +1,9 @@
namespace Dlhis.Sys.Identity.Request
{
public class UserTenantSaveRequest
{
public int UserId { get; set; }
public List<string> TenantCodes { get; set; } = new();
}
}

View File

@@ -0,0 +1,6 @@
namespace Dlhis.Sys.Identity.Response;
public class UserTenantResponse
{
public string Code { get; set; }
}

View File

@@ -0,0 +1,11 @@
using Dlhis.Sys.Identity.Request;
using Dlhis.Sys.Identity.Response;
namespace Dlhis.Sys.Identity.Services;
public interface IUserTenantService : IScopeDependency
{
Task<List<UserTenantResponse>> GetUserTenantsByIdAsync(int id);
Task SaveUserTenantsAsync(UserTenantSaveRequest request);
}

View File

@@ -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<List<UserTenantResponse>> GetUserTenantsByIdAsync(int id)
{
return await _dbContext.Queryable<UserTenantEntity>()
.Where(x => x.UserId == id)
.Select(x => new UserTenantResponse
{
Code = x.TenantCode
})
.ToListAsync();
}
public async Task SaveUserTenantsAsync(UserTenantSaveRequest request)
{
var userTenants = await _dbContext.Queryable<UserTenantEntity>().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());
}
}