125 lines
4.7 KiB
C#
125 lines
4.7 KiB
C#
using Dlhis.Basic.Dishes.Request;
|
|
using Dlhis.Basic.Dishes.Response;
|
|
using Dlhis.Basic.Places.Services;
|
|
using Dlhis.Constants;
|
|
using Dlhis.Entity.Basic.Entity;
|
|
using Dlhis.Entity.Canteen.Entities;
|
|
using Dlhis.Entity.Canteen.Enums;
|
|
using Kurisu.AspNetCore.Authentication.Abstractions;
|
|
using Kurisu.AspNetCore.DataAccess.Dto;
|
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions;
|
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
|
using Kurisu.AspNetCore.Utils.Extensions;
|
|
using Mapster;
|
|
|
|
namespace Dlhis.Basic.Dishes.Services.Implements;
|
|
|
|
public class DishesService : IDishesService
|
|
{
|
|
private readonly IDbContext _dbContext;
|
|
private readonly ICurrentUser _currentUser;
|
|
private readonly IBusinessPlacePermissionsService _businessPlacePermissionsService;
|
|
|
|
public DishesService(IDbContext dbContext, ICurrentUser currentUser, IBusinessPlacePermissionsService businessPlacePermissionsService)
|
|
{
|
|
_dbContext = dbContext;
|
|
_currentUser = currentUser;
|
|
_businessPlacePermissionsService = businessPlacePermissionsService;
|
|
}
|
|
|
|
public async Task<Pagination<DishesResponse>> GetDishesPageAsync(DishesPageRequest request)
|
|
{
|
|
var myCodes = await GetMyBusinessPlaceCodesAsync();
|
|
|
|
return await _dbContext.Queryable<DishesEntity>()
|
|
.LeftJoin<DishesCategoryEntity>((x, category) => x.CategoryCode == category.Code)
|
|
.WhereIF(request.Search.IsPresent(), x => x.CustomCode == request.Search || x.Name.Contains(request.Search))
|
|
.WhereIF(request.BusinessPlaceCodes.IsPresent(), x => request.BusinessPlaceCodes.Contains(x.BusinessPlaceCode))
|
|
.Where(x => myCodes.Contains(x.BusinessPlaceCode))
|
|
.OrderBy(x => x.CreateTime, SqlSugar.OrderByType.Desc)
|
|
.Select((x, category) => new DishesResponse
|
|
{
|
|
Id = x.Id,
|
|
CurrentUnits = x.Units.ToString(),
|
|
CategoryName = category.Name,
|
|
}, true)
|
|
.ToPageListAsync(request);
|
|
}
|
|
|
|
|
|
public async Task SaveDishesAsync(DishesSaveRequest request)
|
|
{
|
|
if (!request.Id.HasValue)
|
|
{
|
|
var dishers = request.Adapt<DishesEntity>();
|
|
dishers.CustomCode = request.CustomCode;
|
|
await _dbContext.InsertAsync(dishers);
|
|
}
|
|
else
|
|
{
|
|
var entity = await _dbContext.Queryable<DishesEntity>().Where(x => x.Id == request.Id).FirstAsync();
|
|
entity.ThrowIfNull("菜品不存在,无法更新");
|
|
|
|
request.Adapt(entity);
|
|
await _dbContext.UpdateAsync(entity);
|
|
}
|
|
}
|
|
|
|
public async Task<DishesMenusBookingSettingResponse> GetDishesMenusBookingSetting(GetDishesMenusBookingRequest request)
|
|
{
|
|
var subjectId = _currentUser.GetIntSubjectId();
|
|
var gainfo = await _dbContext.Queryable<GainfoEntity>().Where(x => x.IdentityUserId == subjectId).FirstAsync();
|
|
var jinkoCode = _currentUser.GetUserClaim(UserClaimConstants.JinkoCode) ?? string.Empty;
|
|
if (jinkoCode.IsPresent())
|
|
{
|
|
gainfo = await _dbContext.Queryable<GainfoEntity>().Where(x => x.Code == jinkoCode).FirstAsync();
|
|
}
|
|
|
|
// 读取最晚订餐的时间
|
|
var value = await _dbContext.Queryable<CanteenSettingEntity>()
|
|
.Where(x => x.UniqueType == CanteenSettingUnqiueEnumType.Tenant && x.Item == CanteenSettingItemEnumType.BookingDeadline && x.Enable)
|
|
.Select(x => x.Value)
|
|
.FirstAsync();
|
|
|
|
if (!TimeOnly.TryParse(value, out var deadline))
|
|
deadline = new TimeOnly(8, 30);
|
|
|
|
|
|
var payTypes = new List<BalanceRecordSourceEnumDto>() { new() { Type = BalanceRecordSourceEnumType.Cash } };
|
|
|
|
var selectTenantId = await _dbContext.Queryable<BusinessPlaceEntity>()
|
|
.Where(x => x.Code == request.BusinessPlaceCode)
|
|
.Select(x => x.TenantId)
|
|
.FirstAsync();
|
|
|
|
if (gainfo.Factory == selectTenantId)
|
|
{
|
|
payTypes.Add(new BalanceRecordSourceEnumDto { Type = BalanceRecordSourceEnumType.LocalTenantSubsidies });
|
|
}
|
|
|
|
return new DishesMenusBookingSettingResponse
|
|
{
|
|
ServerTime = DateTime.Now,
|
|
Deadline = DateTime.Today.Add(deadline.ToTimeSpan()),
|
|
PayTypes = payTypes
|
|
};
|
|
}
|
|
|
|
public async Task<DishesDetailResponse> GetDishesDetailByIdAsync(long id)
|
|
{
|
|
return await _dbContext.Queryable<DishesEntity>()
|
|
.Where(x => x.Id == id)
|
|
.Select<DishesDetailResponse>()
|
|
.SingleAsync();
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 获取我的营业点
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private async Task<List<Guid>> GetMyBusinessPlaceCodesAsync()
|
|
{
|
|
return (await _businessPlacePermissionsService.GetMyAsync()).Select(x => x.Code).ToList();
|
|
}
|
|
} |