fix 参数控制非余额制消费

This commit is contained in:
2024-02-23 21:06:47 +08:00
parent e259c3ab92
commit 412e5589e6
3 changed files with 40 additions and 14 deletions

View File

@@ -336,7 +336,20 @@ public class OrderService : IOrderService
{
if (settings.TryGetValue(item, out var v))
{
return v.Adapt<T>();
try
{
if (int.TryParse(v, out var n))
{
return n.Adapt<T>();
}
return v.Adapt<T>();
}
catch (Exception)
{
return defaultValue;
}
}
return defaultValue;
@@ -467,24 +480,29 @@ public class OrderService : IOrderService
//校验
await Verify(settings, shifts, costDateTime, order);
var allowNoBalance = GetSetting<bool>(settings, CanteenSettingItemEnumType.AllowNoBalance);
await ContinueUpdateAsync(async () =>
{
var canteenUser = await _dbContext.Queryable<CanteenUserEntity>().Where(x => x.UserNo == order.UserNo).FirstAsync();
//金额+-
canteenUser.Balance += order.Amount;
if (canteenUser.Balance < 0)
if (!allowNoBalance)
{
throw new UserFriendlyException("余额不足,无法消费");
var canteenUser = await _dbContext.Queryable<CanteenUserEntity>().Where(x => x.UserNo == order.UserNo).FirstAsync();
//金额+-
canteenUser.Balance += order.Amount;
if (canteenUser.Balance < 0)
{
throw new UserFriendlyException("余额不足,无法消费");
}
var effect = await _dbContext.Updateable<CanteenUserEntity>()
.SetColumns(x => x.Balance == canteenUser.Balance)
.SetColumns(x => x.Version == x.Version + 1)
.Where(x => x.UserNo == order.UserNo && x.Version == canteenUser.Version)//必须与查询时的version一致
.ExecuteCommandAsync();
if (effect <= 0) throw new VersionExceptions("更新版本冲突");
}
var effect = await _dbContext.Updateable<CanteenUserEntity>()
.SetColumns(x => x.Balance == canteenUser.Balance)
.SetColumns(x => x.Version == x.Version + 1)
.Where(x => x.UserNo == order.UserNo && x.Version == canteenUser.Version)//必须与查询时的version一致
.ExecuteCommandAsync();
if (effect <= 0) throw new VersionExceptions("更新版本冲突");
//写入记录
var balanceRecord = order.Adapt<BalanceRecordEntity>();
await _dbContext.InsertAsync(balanceRecord);