61 lines
1.7 KiB
C#
61 lines
1.7 KiB
C#
using Dlhis.Basic.Organization.Response;
|
|
using Dlhis.Entity.Basic.Entity;
|
|
using Dlhis.Sys.Api;
|
|
using Kurisu.SqlSugar.Services;
|
|
using Mapster;
|
|
|
|
namespace Dlhis.Basic.Organization.Services.Implements;
|
|
|
|
public class DepartmentService : IDepartmentService
|
|
{
|
|
private readonly IDbContext _dbContext;
|
|
private readonly ITenantApi _tenantApi;
|
|
|
|
public DepartmentService(IDbContext dbContext, ITenantApi tenantApi)
|
|
{
|
|
_dbContext = dbContext;
|
|
_tenantApi = tenantApi;
|
|
}
|
|
|
|
public async Task<List<DepartmentTreeResponse>> GetTreeAsync()
|
|
{
|
|
var departments = await _dbContext.Queryable<DepartmentEntity>().Select<DepartmentTreeResponse>().ToListAsync();
|
|
//00001000 一定是这个开头的
|
|
var tops = departments.Where(x => x.PNo == "00001000").ToList();
|
|
|
|
tops.ForEach(x =>
|
|
{
|
|
RecursionGetNext(x, departments);
|
|
});
|
|
|
|
var myTenantResult = await _tenantApi.GetMyTenantsAsync();
|
|
var tenants = myTenantResult.Data;
|
|
|
|
return tenants.Select(x => new DepartmentTreeResponse
|
|
{
|
|
Name = x.Name,
|
|
No = x.Code,
|
|
PNo = null,
|
|
Tenant = x.Code,
|
|
_Next = tops.Select(item =>
|
|
{
|
|
var i = item.Adapt<DepartmentTreeResponse>();
|
|
i.PNo = x.Code;
|
|
return i;
|
|
|
|
}).ToList()
|
|
})
|
|
.ToList();
|
|
}
|
|
|
|
private void RecursionGetNext(DepartmentTreeResponse current, List<DepartmentTreeResponse> data)
|
|
{
|
|
var next = data.Where(x => x.PNo == current.No).ToList();
|
|
current._Next = next;
|
|
foreach (var item in next)
|
|
{
|
|
RecursionGetNext(item, data);
|
|
}
|
|
}
|
|
}
|