From 412e5589e6711f6ffcf6c82562288cb917d7ab18 Mon Sep 17 00:00:00 2001 From: liguoyang Date: Fri, 23 Feb 2024 21:06:47 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E5=8F=82=E6=95=B0=E6=8E=A7=E5=88=B6?= =?UTF-8?q?=E9=9D=9E=E4=BD=99=E9=A2=9D=E5=88=B6=E6=B6=88=E8=B4=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Order/Services/Implements/OrderService.cs | 46 +++++++++++++------ .../Implements/CanteenSettingService.cs | 1 + .../Enums/CanteenSettingItemEnumType.cs | 7 +++ 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/Src/Dlhis.Canteen/Order/Services/Implements/OrderService.cs b/Src/Dlhis.Canteen/Order/Services/Implements/OrderService.cs index 6796f695..c2cabe68 100644 --- a/Src/Dlhis.Canteen/Order/Services/Implements/OrderService.cs +++ b/Src/Dlhis.Canteen/Order/Services/Implements/OrderService.cs @@ -336,7 +336,20 @@ public class OrderService : IOrderService { if (settings.TryGetValue(item, out var v)) { - return v.Adapt(); + try + { + + if (int.TryParse(v, out var n)) + { + return n.Adapt(); + } + + return v.Adapt(); + } + catch (Exception) + { + return defaultValue; + } } return defaultValue; @@ -467,24 +480,29 @@ public class OrderService : IOrderService //校验 await Verify(settings, shifts, costDateTime, order); + var allowNoBalance = GetSetting(settings, CanteenSettingItemEnumType.AllowNoBalance); + await ContinueUpdateAsync(async () => { - var canteenUser = await _dbContext.Queryable().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().Where(x => x.UserNo == order.UserNo).FirstAsync(); + //金额+- + canteenUser.Balance += order.Amount; + if (canteenUser.Balance < 0) + { + throw new UserFriendlyException("余额不足,无法消费"); + } + + var effect = await _dbContext.Updateable() + .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() - .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(); await _dbContext.InsertAsync(balanceRecord); diff --git a/Src/Dlhis.Canteen/Setting/Services/Implements/CanteenSettingService.cs b/Src/Dlhis.Canteen/Setting/Services/Implements/CanteenSettingService.cs index 0d24c61e..cb707ca8 100644 --- a/Src/Dlhis.Canteen/Setting/Services/Implements/CanteenSettingService.cs +++ b/Src/Dlhis.Canteen/Setting/Services/Implements/CanteenSettingService.cs @@ -90,6 +90,7 @@ public class CanteenSettingService : ICanteenSettingService CanteenSettingItemEnumType.InitBalance => IsValidFormat(value), //bool + CanteenSettingItemEnumType.AllowNoBalance => IsValidFormat(value, request.Item), CanteenSettingItemEnumType.AllowSendAtMeal => IsValidFormat(value, request.Item), CanteenSettingItemEnumType.AllowRepeatInOneShift => IsValidFormat(value, request.Item), CanteenSettingItemEnumType.AllowCrossTenant => IsValidFormat(value, request.Item), diff --git a/Src/Dlhis.Entity/Canteen/Enums/CanteenSettingItemEnumType.cs b/Src/Dlhis.Entity/Canteen/Enums/CanteenSettingItemEnumType.cs index 1b01b10f..a5ac6a98 100644 --- a/Src/Dlhis.Entity/Canteen/Enums/CanteenSettingItemEnumType.cs +++ b/Src/Dlhis.Entity/Canteen/Enums/CanteenSettingItemEnumType.cs @@ -71,6 +71,13 @@ public enum CanteenSettingItemEnumType AllowCrossTenant, + /// + /// 允许不使用余额消费 + /// + [Description("允许不使用余额消费(0/1)")] + AllowNoBalance, + + //--------------------------------------------------------------------[400,500)---------------------------------------------------------------------------------- ///