diff --git a/Src/Dlhis.Canteen/Order/Services/Implements/OrderService.cs b/Src/Dlhis.Canteen/Order/Services/Implements/OrderService.cs index 6679a3d2..5f723f87 100644 --- a/Src/Dlhis.Canteen/Order/Services/Implements/OrderService.cs +++ b/Src/Dlhis.Canteen/Order/Services/Implements/OrderService.cs @@ -148,6 +148,55 @@ public class OrderService : IOrderService DateTime costDateTime) { var order = await _dbContext.Queryable().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().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() + .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(); + 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 settings, List shifts, DateTime costDateTime, CanteenOrderEntity order) + { //在线 if (!order.IsOffline) { @@ -207,48 +256,5 @@ public class OrderService : IOrderService throw new UserFriendlyException($"超出当天消费限制({dayCostMax}),无法消费"); } } - - - for (int i = 0; i < 5; i++) - { - try - { - var canteenUser = await _dbContext.Queryable().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() - .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(); - await _dbContext.InsertAsync(balanceRecord); - - break; - } - catch (Exception ex) when (ex is VersionExceptions) - { - _logger.LogError(ex, ex.Message); - } - catch (Exception) - { - throw; - } - } } }