diff --git a/Src/Dlhis.Sys.Api/ITenantApi.cs b/Src/Dlhis.Sys.Api/ITenantApi.cs index efa3634f..72feba3b 100644 --- a/Src/Dlhis.Sys.Api/ITenantApi.cs +++ b/Src/Dlhis.Sys.Api/ITenantApi.cs @@ -1,4 +1,4 @@ -using Dlhis.User.Api.Response; +using Dlhis.Sys.Api.Response; using Kurisu.Core.Result; using Kurisu.RemoteCall.Attributes; @@ -18,6 +18,11 @@ public interface ITenantApi [Get("/api/tenants/list")] Task>> GetTenantsAsync(); + + [Auth] + [Get("/api/tenants/tree")] + Task>> GetTenantTreeAsync(); + /// /// 获取所有租户(内部使用) /// diff --git a/Src/Dlhis.Sys.Api/Response/TenantResponse.cs b/Src/Dlhis.Sys.Api/Response/TenantResponse.cs index e20f2110..329c0b98 100644 --- a/Src/Dlhis.Sys.Api/Response/TenantResponse.cs +++ b/Src/Dlhis.Sys.Api/Response/TenantResponse.cs @@ -1,4 +1,4 @@ -namespace Dlhis.User.Api.Response; +namespace Dlhis.Sys.Api.Response; public class TenantResponse { diff --git a/Src/Dlhis.Sys.Api/Response/TenantTreeResponse.cs b/Src/Dlhis.Sys.Api/Response/TenantTreeResponse.cs new file mode 100644 index 00000000..a81ba190 --- /dev/null +++ b/Src/Dlhis.Sys.Api/Response/TenantTreeResponse.cs @@ -0,0 +1,12 @@ +using Newtonsoft.Json; + +namespace Dlhis.Sys.Api.Response; + +public class TenantTreeResponse : TenantResponse +{ + /// + /// 下级 + /// + [JsonProperty(Order = 99)] + public List Next { get; set; } +} diff --git a/Src/Dlhis.Sys/Identity/Controllers/TenantsController.cs b/Src/Dlhis.Sys/Identity/Controllers/TenantsController.cs index f34fb7ab..4018d12d 100644 --- a/Src/Dlhis.Sys/Identity/Controllers/TenantsController.cs +++ b/Src/Dlhis.Sys/Identity/Controllers/TenantsController.cs @@ -51,6 +51,16 @@ public class TenantsController : ControllerBase return await _tenantService.GetListAsync(); } + /// + /// 租户树 + /// + /// + [HttpGet("tree")] + public async Task> GetTreeAsync() + { + return await _tenantService.GetTreeAsync(); + } + /// /// 获取租户 /// diff --git a/Src/Dlhis.Sys/Identity/Request/TenantSaveRequest.cs b/Src/Dlhis.Sys/Identity/Request/TenantSaveRequest.cs index a065d331..700c3ba0 100644 --- a/Src/Dlhis.Sys/Identity/Request/TenantSaveRequest.cs +++ b/Src/Dlhis.Sys/Identity/Request/TenantSaveRequest.cs @@ -6,6 +6,11 @@ public class TenantSaveRequest { public int? Id { get; set; } + /// + /// 上级 + /// + public string PCode { get; set; } + /// /// 代码 /// diff --git a/Src/Dlhis.Sys/Identity/Response/TenantTreeResponse.cs b/Src/Dlhis.Sys/Identity/Response/TenantTreeResponse.cs new file mode 100644 index 00000000..dc940ba0 --- /dev/null +++ b/Src/Dlhis.Sys/Identity/Response/TenantTreeResponse.cs @@ -0,0 +1,12 @@ +using Newtonsoft.Json; + +namespace Dlhis.Sys.Identity.Response; + +public class TenantTreeResponse : TenantResponse +{ + /// + /// 下级 + /// + [JsonProperty(Order = 99)] + public List Next { get; set; } +} diff --git a/Src/Dlhis.Sys/Identity/Services/ITenantService.cs b/Src/Dlhis.Sys/Identity/Services/ITenantService.cs index 4c58ad63..c7fb53d8 100644 --- a/Src/Dlhis.Sys/Identity/Services/ITenantService.cs +++ b/Src/Dlhis.Sys/Identity/Services/ITenantService.cs @@ -10,6 +10,8 @@ public interface ITenantService : IScopeDependency Task> GetListAsync(); + Task> GetTreeAsync(); + Task GetTenantByCode(string code); Task> GetMyTenantsAsync(); diff --git a/Src/Dlhis.Sys/Identity/Services/Implements/TenantService.cs b/Src/Dlhis.Sys/Identity/Services/Implements/TenantService.cs index 7dfbbff8..9302cc6e 100644 --- a/Src/Dlhis.Sys/Identity/Services/Implements/TenantService.cs +++ b/Src/Dlhis.Sys/Identity/Services/Implements/TenantService.cs @@ -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.Response; using Kurisu.AspNetCore.Utils.Extensions; @@ -14,11 +15,9 @@ public class TenantService : ITenantService private readonly IDbContext _dbContext; 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.Client.CurrentConnectionConfig.ConnectionString = identityConnection; - _dbContext = dbContext; + _dbContext = dbContext.ChangeDb("identity"); _currentUser = currentUser; } @@ -52,6 +51,7 @@ public class TenantService : ITenantService { await _dbContext.InsertAsync(new TenantEntity { + PCode = request.PCode, Code = request.Code, Name = request.Name, }); @@ -89,4 +89,29 @@ public class TenantService : ITenantService return myTenants; } + + public async Task> GetTreeAsync() + { + var tenants = await _dbContext.Queryable() + .Select() + .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 data) + { + var next = data.Where(x => x.PCode == current.Code).ToList(); + current.Next = next; + foreach (var item in next) + { + RecursionGetNext(item, data); + } + } } diff --git a/Src/Dlhis.Sys/Menu/Controllers/MenusController.cs b/Src/Dlhis.Sys/Menu/Controllers/MenusController.cs index 97808ff2..cf0247f4 100644 --- a/Src/Dlhis.Sys/Menu/Controllers/MenusController.cs +++ b/Src/Dlhis.Sys/Menu/Controllers/MenusController.cs @@ -5,6 +5,7 @@ using Dlhis.Sys.Menu.Services; using Dlhis.Sys.Menu.Response; using Dlhis.Sys.Menu.Request; using Dlhis.Entity.Sys.Enums; +using Kurisu.AspNetCore.DataAccess.SqlSugar.Aop; namespace Dlhis.Sys.Menu.Controllers; diff --git a/Src/Dlhis.Sys/SqlSugarPack.cs b/Src/Dlhis.Sys/SqlSugarPack.cs index 3c58cc41..9458f466 100644 --- a/Src/Dlhis.Sys/SqlSugarPack.cs +++ b/Src/Dlhis.Sys/SqlSugarPack.cs @@ -1,8 +1,5 @@ using Kurisu.AspNetCore.Startup; -using Kurisu.Core.DataAccess.Entity; using Kurisu.SqlSugar.Extensions; -using Kurisu.SqlSugar.Services; -using Microsoft.AspNetCore.Mvc.Filters; using SqlSugar; namespace Dlhis.Sys; @@ -36,103 +33,4 @@ public class SqlSugarPack : BaseAppPack }; }); } -} - - - - -/// -/// 日志 -/// -[AttributeUsage(AttributeTargets.Method)] -public class LogAttribute : Attribute, IAsyncActionFilter -{ - public LogAttribute(string title) - { - Title = title; - } - - /// - /// - /// - public string Title { get; set; } - - /// - /// 是否处理差异 - /// - 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(); - - options.Title = Title; - options.Diff = Diff; - options.BatchNo = Guid.NewGuid(); - options.RoutePath = httpContext.Request.Path.Value; - options.RaiseTime = DateTime.Now; - } -} - -/// -/// 事务 -/// -[AttributeUsage(AttributeTargets.Method)] -public class TransactionalAttribute : Attribute, IAsyncActionFilter -{ - public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) - { - var dbContext = context.HttpContext.RequestServices.GetRequiredService(); - 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; - } - } -} - -/// -/// 忽略租户 -/// -[AttributeUsage(AttributeTargets.Method)] -public class IgnoreTenantAttribute : Attribute, IAsyncActionFilter -{ - public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) - { - var dbContext = context.HttpContext.RequestServices.GetRequiredService(); - var options = context.HttpContext!.RequestServices.GetService(); - try - { - dbContext.QueryFilter.ClearAndBackup(); - options.IgnoreTenant = true; - await next(); - } - finally - { - options.IgnoreTenant = false; - dbContext.QueryFilter.Restore(); - } - } -} - - +} \ No newline at end of file