118 lines
3.4 KiB
C#
118 lines
3.4 KiB
C#
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;
|
|
using Kurisu.Core.DataAccess.Dto;
|
|
using Kurisu.Core.User.Abstractions;
|
|
using Kurisu.SqlSugar.Extensions;
|
|
using Kurisu.SqlSugar.Services;
|
|
|
|
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 userId = _currentUser.GetIntSubjectId();
|
|
var myTenants = await _dbContext.Queryable<UserTenantEntity>()
|
|
.LeftJoin<TenantEntity>((ut, t) => ut.TenantCode == t.Code)
|
|
.Where(ut => ut.UserId == userId)
|
|
.Select((ut, t) => new TenantResponse
|
|
{
|
|
Id = t.Id,
|
|
Code = t.Code,
|
|
Name = t.Name,
|
|
})
|
|
.ToListAsync();
|
|
|
|
if (myTenants.IsEmpty())
|
|
{
|
|
myTenants = await GetListAsync();
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|