122 lines
3.8 KiB
C#
122 lines
3.8 KiB
C#
using Dlhis.Entity.Sys.Entities;
|
|
using Dlhis.Sys.Identity.Request;
|
|
using Dlhis.Sys.Identity.Response;
|
|
using Kurisu.AspNetCore.Authentication.Abstractions;
|
|
using Kurisu.AspNetCore.DataAccess.Dto;
|
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions;
|
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
|
using Kurisu.AspNetCore.Utils.Extensions;
|
|
|
|
namespace Dlhis.Sys.Identity.Services.Implements;
|
|
|
|
public class TenantService : ITenantService
|
|
{
|
|
private readonly IDbContext _dbContext;
|
|
private readonly ICurrentUser _currentUser;
|
|
|
|
public TenantService(IDbContext dbContext, ICurrentUser currentUser)
|
|
{
|
|
_dbContext = dbContext.ChangeDb("identity");
|
|
_currentUser = currentUser;
|
|
}
|
|
|
|
public async Task<Pagination<TenantResponse>> GetPageAsync(TenantPageRequest request)
|
|
{
|
|
return await _dbContext.Queryable<TenantEntity>()
|
|
.WhereIF(request.Search.IsPresent(), (tenant) => tenant.Name.Contains(request.Search))
|
|
.Select<TenantResponse>()
|
|
.ToPageListAsync(request);
|
|
}
|
|
|
|
public async Task<List<TenantResponse>> GetListAsync()
|
|
{
|
|
return await _dbContext.Queryable<TenantEntity>()
|
|
.Select<TenantResponse>()
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<TenantResponse> GetTenantByCode(string code)
|
|
{
|
|
return await _dbContext.Queryable<TenantEntity>()
|
|
.Where(x => x.Code == code)
|
|
.Select<TenantResponse>()
|
|
.FirstAsync();
|
|
}
|
|
|
|
|
|
public async Task SaveTenantAsync(TenantSaveRequest request)
|
|
{
|
|
if (!request.Id.HasValue)
|
|
{
|
|
await _dbContext.InsertAsync(new TenantEntity
|
|
{
|
|
PCode = request.PCode,
|
|
Code = request.Code,
|
|
Name = request.Name,
|
|
});
|
|
}
|
|
else
|
|
{
|
|
var entity = await _dbContext.Queryable<TenantEntity>().FirstAsync(x => x.Id == request.Id);
|
|
entity.ThrowIfNull("基地/租户信息不存在");
|
|
|
|
entity.Code = request.Code;
|
|
entity.Name = request.Name;
|
|
|
|
await _dbContext.UpdateAsync(entity);
|
|
}
|
|
}
|
|
|
|
public async Task<List<TenantResponse>> GetMyTenantsAsync()
|
|
{
|
|
var tenantId = _currentUser.GetStringTenantId();
|
|
var tenantIds = _currentUser.GetUserClaim("tenants").Split(",").ToList();
|
|
|
|
var myTenants = await _dbContext.Queryable<TenantEntity>()
|
|
.Where(x => tenantIds.Contains(x.Code))
|
|
.Select(x => new TenantResponse
|
|
{
|
|
Id = x.Id,
|
|
PCode = x.PCode,
|
|
Code = x.Code,
|
|
Name = x.Name,
|
|
})
|
|
.ToListAsync();
|
|
|
|
var currentTenant = myTenants.FirstOrDefault(x => x.Code == tenantId);
|
|
var otherTenants = myTenants.Where(x => x.Code != tenantId);
|
|
|
|
var result = new List<TenantResponse>();
|
|
if (currentTenant != null)
|
|
{
|
|
result.Add(currentTenant);
|
|
}
|
|
|
|
result.AddRange(otherTenants);
|
|
return result;
|
|
}
|
|
|
|
public async Task<List<TenantTreeResponse>> GetTreeAsync()
|
|
{
|
|
var tenants = await _dbContext.Queryable<TenantEntity>()
|
|
.Select<TenantTreeResponse>()
|
|
.ToListAsync();
|
|
|
|
var tops = tenants.Where(x => x.PCode.IsEmpty()).ToList();
|
|
tops.ForEach(item => RecursionGetNext(item, tenants));
|
|
|
|
return tops;
|
|
}
|
|
|
|
//****************************************************************** help methods *****************************************************************************
|
|
|
|
private static 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);
|
|
}
|
|
}
|
|
} |