using Dlhis.Canteen.Order.Response;
using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Canteen.Entities;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
namespace Dlhis.Canteen.Order.Services.Implements;
public class MiddleService : IMiddleService
{
private readonly IDbContext _dbContext;
private readonly IBalanceService _balanceService;
public MiddleService(IDbContext dbContext, IBalanceService balanceService)
{
_dbContext = dbContext;
_balanceService = balanceService;
}
///
public async Task MiddleUploadAsync(BalanceRecordEntity entity)
{
entity.Id = 0;
entity.Amount = -Math.Abs(entity.Amount);
entity.Remark = "离线消费提交";
if (!await _dbContext.Queryable().AnyAsync(x => x.No == entity.No))
{
await _dbContext.InsertAsync(entity);
await _balanceService.ReCalcAsync(entity.UserCode);
}
//获取人员余额
return await _dbContext.Queryable()
.LeftJoin((x, ga) => x.UserNo == ga.No)
.LeftJoin((x, ga, department) => ga.DepartmentNo == department.No)
.Where(x => x.UserNo == entity.UserNo)
.Select((x, ga, department) => new OrderResultResponse
{
UserCode = ga.Code,
UserName = ga.Name,
DepartmentNo = ga.DepartmentNo,
DepartmentName = department.Name,
CurrentBalance = x.Balance
})
.SingleAsync();
}
}