66 lines
1.9 KiB
C#
66 lines
1.9 KiB
C#
using Dlhis.Basic.Dishes.Request;
|
|
using Dlhis.Basic.Dishes.Response;
|
|
using Dlhis.Entity.Canteen.Entities;
|
|
using Kurisu.AspNetCore.Utils.Extensions;
|
|
using Kurisu.SqlSugar.Services;
|
|
using Mapster;
|
|
|
|
namespace Dlhis.Basic.Dishes.Services.Implements;
|
|
|
|
public class DishesCategoryService : IDishesCategoryService
|
|
{
|
|
private readonly IDbContext _dbContext;
|
|
|
|
public DishesCategoryService(IDbContext dbContext)
|
|
{
|
|
_dbContext = dbContext;
|
|
}
|
|
|
|
public async Task<List<DishesCategoryTreeResponse>> TreeAsync()
|
|
{
|
|
var categories = await _dbContext.Queryable<DishesCategoryEntity>().Select<DishesCategoryTreeResponse>().ToListAsync();
|
|
var tops = categories.Where(x => !x.PCode.HasValue).ToList();
|
|
|
|
tops.ForEach(x =>
|
|
{
|
|
RecursionGetNext(x, categories);
|
|
});
|
|
|
|
return tops;
|
|
}
|
|
|
|
public async Task SaveAsync(DishesCategorySaveRequest request)
|
|
{
|
|
if (!request.Id.HasValue)
|
|
{
|
|
var entity = request.Adapt<DishesCategoryEntity>();
|
|
entity.Code = Guid.NewGuid();
|
|
await _dbContext.InsertAsync(entity);
|
|
}
|
|
else
|
|
{
|
|
var entity = await _dbContext.Queryable<DishesCategoryEntity>().Where(x => x.Id == request.Id).FirstAsync();
|
|
entity.ThrowIfNull("分类不存在,无法更新");
|
|
request.Adapt(entity);
|
|
|
|
await _dbContext.UpdateAsync(entity);
|
|
}
|
|
}
|
|
|
|
|
|
//************************************************************************************************************************
|
|
|
|
private static void RecursionGetNext(DishesCategoryTreeResponse current, List<DishesCategoryTreeResponse> data)
|
|
{
|
|
var next = data.Where(x => x.PCode == current.Code).ToList();
|
|
if (next.Any())
|
|
{
|
|
current.Next = next;
|
|
foreach (var item in next)
|
|
{
|
|
RecursionGetNext(item, data);
|
|
}
|
|
}
|
|
}
|
|
}
|