Files
jinko.yikatong/Src/Dlhis.Basic/Organization/Services/Implements/DepartmentService.cs
2024-02-20 11:15:23 +08:00

73 lines
2.3 KiB
C#

using Dlhis.Basic.Organization.Request;
using Dlhis.Basic.Organization.Response;
using Dlhis.Entity.Basic.Entity;
using Kurisu.AspNetCore.Utils.Extensions;
using Kurisu.Core.DataAccess.Dto;
using Kurisu.SqlSugar.Services;
using Mapster;
using System.Collections.Generic;
namespace Dlhis.Basic.Organization.Services.Implements;
public class DepartmentService : IDepartmentService
{
private readonly IDbContext _dbContext;
public DepartmentService(IDbContext dbContext)
{
_dbContext = dbContext;
}
public async Task<List<DepartmentTreeResponse>> GetTreeAsync()
{
var departments = await _dbContext.Queryable<DepartmentEntity>().ToListAsync();
var tops = departments.Where(x => x.PNo.IsEmpty()).Adapt<List<DepartmentTreeResponse>>();
tops.ForEach(x =>
{
RecursionGetNext(x, departments);
});
return tops;
}
public async Task<Pagination<DepartmentTreeResponse>> GetTreePageAsync(DepartmentPageRequest request)
{
var departments = await _dbContext
.Queryable<DepartmentEntity>()
.WhereIF(!request.Search.IsEmpty(),x => x.Name.Contains(request.Search) || x.No.Contains(request.Search)).ToListAsync();
var tops = departments.Where(x => x.PNo.IsEmpty()).Adapt<List<DepartmentTreeResponse>>();
tops.ForEach(x =>
{
RecursionGetNext(x, departments);
});
// 分页操作
var startIndex = (request.PageIndex - 1) * request.PageSize;
var pagedData = tops.Skip(startIndex).Take(request.PageSize).ToList();
var pagination = new Pagination<DepartmentTreeResponse>
{
PageIndex = request.PageIndex,
PageSize = request.PageSize,
Total = tops.Count, // 使用原始列表的总数进行分页
Data = pagedData
};
return pagination;
}
private void RecursionGetNext(DepartmentTreeResponse current, List<DepartmentEntity> departments)
{
var next = departments.Where(x => x.PNo == current.No).ToList();
current.Next = next.Adapt<List<DepartmentTreeResponse>>();
foreach (var item in next)
{
RecursionGetNext(item.Adapt<DepartmentTreeResponse>(), departments);
}
}
}