fix 订单有效

This commit is contained in:
cheche
2024-01-10 17:13:24 +08:00
parent 15dfaf48dd
commit e7e7e78fd5

View File

@@ -148,6 +148,55 @@ public class OrderService : IOrderService
DateTime costDateTime) DateTime costDateTime)
{ {
var order = await _dbContext.Queryable<CanteenOrderEntity>().Where(x => x.Id == orderId).FirstAsync(); var order = await _dbContext.Queryable<CanteenOrderEntity>().Where(x => x.Id == orderId).FirstAsync();
await Verify(settings, shifts, costDateTime, order);//校验
for (int i = 0; i < 5; i++)
{
try
{
var canteenUser = await _dbContext.Queryable<CanteenUserEntity>().Where(x => x.UserNo == order.UserId).FirstAsync();
//金额+-
canteenUser.Balance += order.Amount;
if (order.Type == BalanceRecordTypeEnumType.Out)
{
if (canteenUser.Balance < 0)
{
throw new UserFriendlyException("余额不足,无法消费");
}
}
var effect = await _dbContext.Updateable<CanteenUserEntity>()
.SetColumns(x => x.Balance == canteenUser.Balance + order.Amount)
.SetColumns(x => x.Version == x.Version + 1)
.Where(x => x.UserNo == order.UserId && x.Version == canteenUser.Version)//必须与查询时的version一致
.ExecuteCommandAsync();
if (effect <= 0)
{
throw new VersionExceptions("更新版本冲突");
}
var balanceRecord = order.Adapt<BalanceRecordEntity>();
await _dbContext.InsertAsync(balanceRecord);
order.Status = BalanceRecordStatusEnumType.Valid; //订单改为有效
break;
}
catch (Exception ex) when (ex is VersionExceptions)
{
_logger.LogError(ex, ex.Message);
}
catch (Exception)
{
throw;
}
}
}
private async Task Verify(Dictionary<CanteenSettingItemEnumType, string> settings, List<CanteenBusinessShiftEntity> shifts, DateTime costDateTime, CanteenOrderEntity order)
{
//在线 //在线
if (!order.IsOffline) if (!order.IsOffline)
{ {
@@ -207,48 +256,5 @@ public class OrderService : IOrderService
throw new UserFriendlyException($"超出当天消费限制({dayCostMax}),无法消费"); throw new UserFriendlyException($"超出当天消费限制({dayCostMax}),无法消费");
} }
} }
for (int i = 0; i < 5; i++)
{
try
{
var canteenUser = await _dbContext.Queryable<CanteenUserEntity>().Where(x => x.UserNo == order.UserId).FirstAsync();
//金额+-
canteenUser.Balance += order.Amount;
if (order.Type == BalanceRecordTypeEnumType.Out)
{
if (canteenUser.Balance < 0)
{
throw new UserFriendlyException("余额不足,无法消费");
}
}
var effect = await _dbContext.Updateable<CanteenUserEntity>()
.SetColumns(x => x.Balance == canteenUser.Balance + order.Amount)
.SetColumns(x => x.Version == x.Version + 1)
.Where(x => x.UserNo == order.UserId && x.Version == canteenUser.Version)//必须与查询时的version一致
.ExecuteCommandAsync();
if (effect <= 0)
{
throw new VersionExceptions("更新版本冲突");
}
var balanceRecord = order.Adapt<BalanceRecordEntity>();
await _dbContext.InsertAsync(balanceRecord);
break;
}
catch (Exception ex) when (ex is VersionExceptions)
{
_logger.LogError(ex, ex.Message);
}
catch (Exception)
{
throw;
}
}
} }
} }