110 lines
4.2 KiB
C#
110 lines
4.2 KiB
C#
using Dlhis.Canteen.Order.Repository;
|
|
using Dlhis.Canteen.Order.Request;
|
|
using Dlhis.Canteen.Order.Response;
|
|
using Dlhis.Entity.Basic.Entity;
|
|
using Dlhis.Entity.Canteen.Entities;
|
|
using Dlhis.Entity.Canteen.Enums;
|
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions;
|
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
|
using Kurisu.AspNetCore.UnifyResultAndValidation;
|
|
using Kurisu.AspNetCore.Utils;
|
|
using Kurisu.AspNetCore.Utils.Extensions;
|
|
using Mapster;
|
|
|
|
namespace Dlhis.Canteen.Order.Services.ConsumeHandlers;
|
|
|
|
/// <summary>
|
|
/// 生日券消费处理
|
|
/// </summary>
|
|
[Service(nameof(BalanceRecordSourceEnumType.Birthday))]
|
|
public class BirthdayConsumeHandler : IConsumeHandler
|
|
{
|
|
private readonly IDbContext _dbContext;
|
|
private readonly IOrderCommonHandler _orderCommonHandler;
|
|
private readonly IOrderRepository _orderRepository;
|
|
|
|
public BirthdayConsumeHandler(IDbContext dbContext,
|
|
IOrderCommonHandler orderCommonHandler,
|
|
IOrderRepository orderRepository)
|
|
{
|
|
_dbContext = dbContext;
|
|
_orderCommonHandler = orderCommonHandler;
|
|
_orderRepository = orderRepository;
|
|
}
|
|
|
|
public async Task<DefaultApiResult<OrderResultResponse>> ConsumeAsync(OrderAddRequest request, GainfoEntity gainfo)
|
|
{
|
|
var msg = string.Empty;
|
|
var ticketNo = request.UserUniqueNo;
|
|
|
|
var exists = await _dbContext.Queryable<TicketEntity>()
|
|
.Where(x => x.Type == TicketEnumType.Birthday)
|
|
.Where(x => x.QrCode == ticketNo)
|
|
.AnyAsync();
|
|
|
|
exists.ThrowIfFalse("生日券无效");
|
|
|
|
try
|
|
{
|
|
var checkData = await _orderCommonHandler.PreCheckAsync(request.DeviceUniqueNo, request.CostDateTime!.Value);
|
|
var isSupermarket = await _dbContext.Queryable<BusinessPlaceEntity>()
|
|
.Where(x => x.Code == checkData.BusinessPlaceCode)
|
|
.Select(x => x.Catalog == CanteenBusinessPlaceCatalogEnumType.Supermarket)
|
|
.SingleAsync();
|
|
|
|
isSupermarket.ThrowIfFalse("请在超市消费");
|
|
|
|
var order = await _orderRepository.CreateDeviceConsumeOrderAsync(checkData, request.Adapt<ConsumeRequest>(), gainfo);
|
|
|
|
await _orderCommonHandler.TryConsumeAsync(order.Id, async () =>
|
|
{
|
|
await CommonHelper.RetryAsync(async () =>
|
|
{
|
|
var ticket = await _dbContext.Queryable<TicketEntity>()
|
|
.Where(x => x.Type == TicketEnumType.Birthday)
|
|
.Where(x => x.QrCode == ticketNo)
|
|
.SingleAsync();
|
|
|
|
//金额+-
|
|
ticket.Balance += order.Amount;
|
|
(ticket.Balance < 0).ThrowIfTrue("余额不足,无法消费");
|
|
|
|
await _dbContext.Updateable<TicketEntity>()
|
|
.SetColumns(x => x.Balance == ticket.Balance)
|
|
.SetColumns(x => x.Version == x.Version + 1)
|
|
.SetColumns(x => x.ModifiedTime == DateTime.Now)
|
|
.Where(x => x.QrCode == ticketNo && x.Version == ticket.Version) //必须与查询时的version一致
|
|
.ExecuteCommandThrowVersionAsync();
|
|
|
|
//写入记录
|
|
var balanceRecord = order.Adapt<BalanceRecordEntity>();
|
|
balanceRecord.Remark = $"{ticket.QrCode}(生日券消费)";
|
|
await _dbContext.InsertAsync(balanceRecord);
|
|
}, 5);
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
msg = ex.Message;
|
|
}
|
|
|
|
//获取餐券余额
|
|
var data = await _dbContext.Queryable<TicketEntity>()
|
|
.LeftJoin<GainfoEntity>((x, ga) => x.UserCode == ga.Code)
|
|
.Where(x => x.Type == TicketEnumType.Birthday && x.QrCode == ticketNo)
|
|
.Select((x, ga) => new OrderResultResponse
|
|
{
|
|
UserCode = x.UserCode,
|
|
UserName = ga.Name,
|
|
CurrentBalance = x.Balance
|
|
})
|
|
.SingleAsync();
|
|
|
|
return new DefaultApiResult<OrderResultResponse>
|
|
{
|
|
Code = msg.IsPresent() ? ApiStateCode.Error : ApiStateCode.Success,
|
|
Data = data,
|
|
Msg = msg
|
|
};
|
|
}
|
|
} |