feat 基地/租户tree

This commit is contained in:
2024-03-05 19:35:03 +08:00
parent 001f28c4c3
commit 778d100e72
10 changed files with 80 additions and 110 deletions

View File

@@ -1,4 +1,4 @@
using Dlhis.User.Api.Response; using Dlhis.Sys.Api.Response;
using Kurisu.Core.Result; using Kurisu.Core.Result;
using Kurisu.RemoteCall.Attributes; using Kurisu.RemoteCall.Attributes;
@@ -18,6 +18,11 @@ public interface ITenantApi
[Get("/api/tenants/list")] [Get("/api/tenants/list")]
Task<DefaultApiResult<List<TenantResponse>>> GetTenantsAsync(); Task<DefaultApiResult<List<TenantResponse>>> GetTenantsAsync();
[Auth]
[Get("/api/tenants/tree")]
Task<DefaultApiResult<List<TenantTreeResponse>>> GetTenantTreeAsync();
/// <summary> /// <summary>
/// 获取所有租户(内部使用) /// 获取所有租户(内部使用)
/// </summary> /// </summary>

View File

@@ -1,4 +1,4 @@
namespace Dlhis.User.Api.Response; namespace Dlhis.Sys.Api.Response;
public class TenantResponse public class TenantResponse
{ {

View File

@@ -0,0 +1,12 @@
using Newtonsoft.Json;
namespace Dlhis.Sys.Api.Response;
public class TenantTreeResponse : TenantResponse
{
/// <summary>
/// 下级
/// </summary>
[JsonProperty(Order = 99)]
public List<TenantTreeResponse> Next { get; set; }
}

View File

@@ -51,6 +51,16 @@ public class TenantsController : ControllerBase
return await _tenantService.GetListAsync(); return await _tenantService.GetListAsync();
} }
/// <summary>
/// 租户树
/// </summary>
/// <returns></returns>
[HttpGet("tree")]
public async Task<List<TenantTreeResponse>> GetTreeAsync()
{
return await _tenantService.GetTreeAsync();
}
/// <summary> /// <summary>
/// 获取租户 /// 获取租户
/// </summary> /// </summary>

View File

@@ -6,6 +6,11 @@ public class TenantSaveRequest
{ {
public int? Id { get; set; } public int? Id { get; set; }
/// <summary>
/// 上级
/// </summary>
public string PCode { get; set; }
/// <summary> /// <summary>
/// 代码 /// 代码
/// </summary> /// </summary>

View File

@@ -0,0 +1,12 @@
using Newtonsoft.Json;
namespace Dlhis.Sys.Identity.Response;
public class TenantTreeResponse : TenantResponse
{
/// <summary>
/// 下级
/// </summary>
[JsonProperty(Order = 99)]
public List<TenantTreeResponse> Next { get; set; }
}

View File

@@ -10,6 +10,8 @@ public interface ITenantService : IScopeDependency
Task<List<TenantResponse>> GetListAsync(); Task<List<TenantResponse>> GetListAsync();
Task<List<TenantTreeResponse>> GetTreeAsync();
Task<TenantResponse> GetTenantByCode(string code); Task<TenantResponse> GetTenantByCode(string code);
Task<List<TenantResponse>> GetMyTenantsAsync(); Task<List<TenantResponse>> GetMyTenantsAsync();

View File

@@ -1,4 +1,5 @@
using Dlhis.Entity.Sys.Entities; using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Sys.Entities;
using Dlhis.Sys.Identity.Request; using Dlhis.Sys.Identity.Request;
using Dlhis.Sys.Identity.Response; using Dlhis.Sys.Identity.Response;
using Kurisu.AspNetCore.Utils.Extensions; using Kurisu.AspNetCore.Utils.Extensions;
@@ -14,11 +15,9 @@ public class TenantService : ITenantService
private readonly IDbContext _dbContext; private readonly IDbContext _dbContext;
private readonly ICurrentUser _currentUser; private readonly ICurrentUser _currentUser;
public TenantService(IDbContext dbContext, IConfiguration configuration, ICurrentUser currentUser) public TenantService(IDbContext dbContext, ICurrentUser currentUser)
{ {
var identityConnection = configuration.GetSection("SqlSugarOptions:IdentityConnectionString").Value; _dbContext = dbContext.ChangeDb("identity");
dbContext.Client.CurrentConnectionConfig.ConnectionString = identityConnection;
_dbContext = dbContext;
_currentUser = currentUser; _currentUser = currentUser;
} }
@@ -52,6 +51,7 @@ public class TenantService : ITenantService
{ {
await _dbContext.InsertAsync(new TenantEntity await _dbContext.InsertAsync(new TenantEntity
{ {
PCode = request.PCode,
Code = request.Code, Code = request.Code,
Name = request.Name, Name = request.Name,
}); });
@@ -89,4 +89,29 @@ public class TenantService : ITenantService
return myTenants; return myTenants;
} }
public async Task<List<TenantTreeResponse>> GetTreeAsync()
{
var tenants = await _dbContext.Queryable<TenantEntity>()
.Select<TenantTreeResponse>()
.ToListAsync();
var tops = tenants.Where(x => x.PCode.IsEmpty()).ToList();
foreach (var item in tops)
{
RecursionGetNext(item, tenants);
}
return tops;
}
private void RecursionGetNext(TenantTreeResponse current, List<TenantTreeResponse> data)
{
var next = data.Where(x => x.PCode == current.Code).ToList();
current.Next = next;
foreach (var item in next)
{
RecursionGetNext(item, data);
}
}
} }

View File

@@ -5,6 +5,7 @@ using Dlhis.Sys.Menu.Services;
using Dlhis.Sys.Menu.Response; using Dlhis.Sys.Menu.Response;
using Dlhis.Sys.Menu.Request; using Dlhis.Sys.Menu.Request;
using Dlhis.Entity.Sys.Enums; using Dlhis.Entity.Sys.Enums;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Aop;
namespace Dlhis.Sys.Menu.Controllers; namespace Dlhis.Sys.Menu.Controllers;

View File

@@ -1,8 +1,5 @@
using Kurisu.AspNetCore.Startup; using Kurisu.AspNetCore.Startup;
using Kurisu.Core.DataAccess.Entity;
using Kurisu.SqlSugar.Extensions; using Kurisu.SqlSugar.Extensions;
using Kurisu.SqlSugar.Services;
using Microsoft.AspNetCore.Mvc.Filters;
using SqlSugar; using SqlSugar;
namespace Dlhis.Sys; namespace Dlhis.Sys;
@@ -37,102 +34,3 @@ public class SqlSugarPack : BaseAppPack
}); });
} }
} }
/// <summary>
/// 日志
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class LogAttribute : Attribute, IAsyncActionFilter
{
public LogAttribute(string title)
{
Title = title;
}
/// <summary>
///
/// </summary>
public string Title { get; set; }
/// <summary>
/// 是否处理差异
/// </summary>
public bool Diff { get; set; }
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
PreSetting(context.HttpContext);
await next();
}
private void PreSetting(HttpContext httpContext)
{
var options = httpContext.RequestServices.GetService<ISqlSugarOptionsService>();
options.Title = Title;
options.Diff = Diff;
options.BatchNo = Guid.NewGuid();
options.RoutePath = httpContext.Request.Path.Value;
options.RaiseTime = DateTime.Now;
}
}
/// <summary>
/// 事务
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class TransactionalAttribute : Attribute, IAsyncActionFilter
{
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
var dbContext = context.HttpContext.RequestServices.GetRequiredService<ISqlSugarClient>();
await dbContext.Ado.BeginTranAsync();
try
{
var result = await next();
if (result.Exception != null)
{
throw result.Exception;
}
else
{
await dbContext.Ado.CommitTranAsync();
}
}
catch (Exception)
{
await dbContext.Ado.RollbackTranAsync();
throw;
}
}
}
/// <summary>
/// 忽略租户
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class IgnoreTenantAttribute : Attribute, IAsyncActionFilter
{
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
var dbContext = context.HttpContext.RequestServices.GetRequiredService<ISqlSugarClient>();
var options = context.HttpContext!.RequestServices.GetService<ISqlSugarOptionsService>();
try
{
dbContext.QueryFilter.ClearAndBackup<ITenantId>();
options.IgnoreTenant = true;
await next();
}
finally
{
options.IgnoreTenant = false;
dbContext.QueryFilter.Restore();
}
}
}