133 lines
4.2 KiB
C#
133 lines
4.2 KiB
C#
using Dlhis.Basic.BusinessPlace.Services;
|
|
using Dlhis.Basic.Dishes.Request;
|
|
using Dlhis.Basic.Dishes.Response;
|
|
using Dlhis.Entity.Basic.Entity;
|
|
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;
|
|
private readonly IBusinessPlacePermissionsService _businessPlacePermissionsService;
|
|
|
|
public DishesCategoryService(IDbContext dbContext, IBusinessPlacePermissionsService businessPlacePermissionsService)
|
|
{
|
|
_dbContext = dbContext;
|
|
}
|
|
|
|
public async Task<List<DishesCategoryTreeResponse>> TreeAsync(Guid businessPlaceCode)
|
|
{
|
|
var mybCodes = await GetMyBusinessPlaceCodesAsync();
|
|
mybCodes = mybCodes.Where(x => x == businessPlaceCode).ToList();
|
|
|
|
var categories = await _dbContext.Queryable<DishesCategoryEntity>()
|
|
.LeftJoin<BusinessPlaceEntity>((x, bplace) => x.BusinessPlaceCode == bplace.Code)
|
|
.Where(x => mybCodes.Contains(x.BusinessPlaceCode))
|
|
.Select((x, bplace) => new DishesCategoryTreeResponse
|
|
{
|
|
BusinessPlaceCode = bplace.Code,
|
|
Code = x.Code,
|
|
Name = x.Name,
|
|
PCode = x.PCode,
|
|
})
|
|
.ToListAsync();
|
|
|
|
var tops = categories.Where(x => !x.PCode.HasValue).ToList();
|
|
|
|
tops.ForEach(x =>
|
|
{
|
|
RecursionGetNext(x, categories);
|
|
});
|
|
|
|
return tops;
|
|
}
|
|
|
|
public async Task<List<DishesCategoryTreeResponse>> TreeAllAsync(Guid businessPlaceCode)
|
|
{
|
|
var categories = await _dbContext.Queryable<DishesCategoryEntity>()
|
|
.LeftJoin<BusinessPlaceEntity>((x, bplace) => x.BusinessPlaceCode == bplace.Code)
|
|
.Where(x => x.BusinessPlaceCode == businessPlaceCode)
|
|
.Select((x, bplace) => new DishesCategoryTreeResponse
|
|
{
|
|
BusinessPlaceCode = bplace.Code,
|
|
Code = x.Code,
|
|
Name = x.Name,
|
|
PCode = x.PCode,
|
|
})
|
|
.ToListAsync();
|
|
|
|
var tops = categories.Where(x => !x.PCode.HasValue).ToList();
|
|
|
|
tops.ForEach(x =>
|
|
{
|
|
RecursionGetNext(x, categories);
|
|
});
|
|
|
|
return tops;
|
|
}
|
|
|
|
|
|
|
|
public async Task SaveAsync(DishesCategorySaveRequest request)
|
|
{
|
|
await CheckBusinessPlacePermissionAsync(request.BusinessPlaceCode);
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检查营业点权限
|
|
/// </summary>
|
|
/// <param name="businessPlaceCode"></param>
|
|
/// <returns></returns>
|
|
private async Task CheckBusinessPlacePermissionAsync(Guid businessPlaceCode)
|
|
{
|
|
var codes = await GetMyBusinessPlaceCodesAsync();
|
|
codes.Contains(businessPlaceCode).ThrowIfFalse("无权操作当前营业点");
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 获取我管理的营业点
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private async Task<List<Guid>> GetMyBusinessPlaceCodesAsync()
|
|
{
|
|
var mybCodes = (await _businessPlacePermissionsService.GetMyAsync()).Select(x => x.Code).ToList();
|
|
return mybCodes;
|
|
}
|
|
}
|