From 7d7e408ddb739321ace06aa3a3faae00d57814ac Mon Sep 17 00:00:00 2001 From: JunFeng Wu Date: Fri, 28 Apr 2023 15:22:38 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A2=86=E5=8F=96=E4=BC=98=E6=83=A0=E5=88=B8?= =?UTF-8?q?=E5=BC=80=E5=90=AF=E4=BA=8B=E5=8A=A1=EF=BC=8C=E5=B9=B6=E5=9C=A8?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E4=BC=98=E6=83=A0=E5=88=B8=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E6=97=B6=E5=8A=A0update=E9=94=81=E8=A7=A3=E5=86=B3=E5=B9=B6?= =?UTF-8?q?=E5=8F=91=E9=A2=86=E4=BC=98=E6=83=A0=E5=88=B8=E4=BA=A7=E7=94=9F?= =?UTF-8?q?=E8=B6=85=E4=B9=B0=E9=97=AE=E9=A2=98=E3=80=82=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E4=BC=98=E6=83=A0=E5=88=B8=E9=A2=86=E5=8F=96=E6=95=B0?= =?UTF-8?q?=E9=87=8F=E8=AE=BE=E7=BD=AE=E4=B8=BA0=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E6=B2=A1=E6=9C=89=E8=BF=94=E5=9B=9E=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CoreCms.Net.IRepository/IBaseRepository.cs | 5 +- CoreCms.Net.Repository/BaseRepository.cs | 8 ++- .../Promotion/CoreCmsPromotionServices.cs | 5 +- .../Controllers/CouponController.cs | 67 +++++++++++++------ 4 files changed, 60 insertions(+), 25 deletions(-) diff --git a/CoreCms.Net.IRepository/IBaseRepository.cs b/CoreCms.Net.IRepository/IBaseRepository.cs index 53eccce..9b902ec 100644 --- a/CoreCms.Net.IRepository/IBaseRepository.cs +++ b/CoreCms.Net.IRepository/IBaseRepository.cs @@ -203,8 +203,11 @@ namespace CoreCms.Net.IRepository /// /// 条件表达式树 /// 是否使用WITH(NOLOCK) + /// 是否使用事务锁 + /// 事务锁类型 /// - Task QueryByClauseAsync(Expression> predicate, bool blUseNoLock = false); + Task QueryByClauseAsync(Expression> predicate, bool blUseNoLock = false,bool blUseTranLock = false, + DbLockType dbLockType = DbLockType.Wait); /// /// 根据条件查询数据 diff --git a/CoreCms.Net.Repository/BaseRepository.cs b/CoreCms.Net.Repository/BaseRepository.cs index 6d877ab..e4dd0fd 100644 --- a/CoreCms.Net.Repository/BaseRepository.cs +++ b/CoreCms.Net.Repository/BaseRepository.cs @@ -331,12 +331,16 @@ namespace CoreCms.Net.Repository /// /// 条件表达式树 /// 是否使用WITH(NOLOCK) + /// 是否使用事务锁 + /// 事务锁类型 /// - public async Task QueryByClauseAsync(Expression> predicate, bool blUseNoLock = false) + public async Task QueryByClauseAsync(Expression> predicate, bool blUseNoLock = false,bool blUseTranLock = false, + DbLockType dbLockType = DbLockType.Wait) { return blUseNoLock ? await DbBaseClient.Queryable().With(SqlWith.NoLock).FirstAsync(predicate) - : await DbBaseClient.Queryable().FirstAsync(predicate); + : (blUseTranLock? await DbBaseClient.Queryable().TranLock(dbLockType).FirstAsync(predicate) + : await DbBaseClient.Queryable().FirstAsync(predicate)); } /// diff --git a/CoreCms.Net.Services/Promotion/CoreCmsPromotionServices.cs b/CoreCms.Net.Services/Promotion/CoreCmsPromotionServices.cs index 3a462c3..61b6501 100644 --- a/CoreCms.Net.Services/Promotion/CoreCmsPromotionServices.cs +++ b/CoreCms.Net.Services/Promotion/CoreCmsPromotionServices.cs @@ -465,16 +465,17 @@ namespace CoreCms.Net.Services where = where.And(p => p.isDel == false); //是否被删除 - var info = await _dal.QueryByClauseAsync(where); + var info = await _dal.QueryByClauseAsync(where,false,true); if (info != null) { jm.data = info; //判断最大领取数量 if (info.maxRecevieNums == 0) { - jm.status = true; + jm.status = false; return jm; } + var receiveCount = await _couponServices.GetCountAsync(p => p.promotionId == promotionId); if (receiveCount >= info.maxRecevieNums) { diff --git a/CoreCms.Net.Web.WebApi/Controllers/CouponController.cs b/CoreCms.Net.Web.WebApi/Controllers/CouponController.cs index b3dfa72..11f3dd5 100644 --- a/CoreCms.Net.Web.WebApi/Controllers/CouponController.cs +++ b/CoreCms.Net.Web.WebApi/Controllers/CouponController.cs @@ -13,6 +13,7 @@ using System.Linq; using System.Threading.Tasks; using CoreCms.Net.Auth.HttpContextUser; using CoreCms.Net.Configuration; +using CoreCms.Net.IRepository.UnitOfWork; using CoreCms.Net.IServices; using CoreCms.Net.Model.Entities; using CoreCms.Net.Model.FromBody; @@ -33,18 +34,21 @@ namespace CoreCms.Net.Web.WebApi.Controllers private readonly IHttpContextUser _user; private readonly ICoreCmsCouponServices _couponServices; private readonly ICoreCmsPromotionServices _promotionServices; + private readonly IUnitOfWork _unionOfWork; /// /// 构造函数 /// /// /// /// + /// public CouponController(IHttpContextUser user - , ICoreCmsCouponServices couponServices, ICoreCmsPromotionServices promotionServices) + , ICoreCmsCouponServices couponServices, ICoreCmsPromotionServices promotionServices, IUnitOfWork unionOfWork) { _user = user; _couponServices = couponServices; _promotionServices = promotionServices; + _unionOfWork = unionOfWork; } //公共接口==================================================================================================== @@ -144,32 +148,55 @@ namespace CoreCms.Net.Web.WebApi.Controllers jm.msg = GlobalErrorCodeVars.Code15006; return jm; } - //判断优惠券是否可以领取? - var promotionModel = await _promotionServices.ReceiveCoupon(entity.id); - if (promotionModel.status == false) + + try { - return promotionModel; - } + _unionOfWork.BeginTran(); - var promotion = (CoreCmsPromotion)promotionModel.data; - if (promotion == null) - { - jm.msg = GlobalErrorCodeVars.Code15019; - return jm; - } - if (promotion.maxNums > 0) - { - //判断用户是否已领取?领取次数 - var couponResult = await _couponServices.GetMyCoupon(_user.ID, entity.id, "all", 1, 9999); - if (couponResult.status && couponResult.code >= promotion.maxNums) + //判断优惠券是否可以领取? + var promotionModel = await _promotionServices.ReceiveCoupon(entity.id); + if (promotionModel.status == false) { - jm.msg = GlobalErrorCodeVars.Code15018; + _unionOfWork.RollbackTran(); + return promotionModel; + } + + + + var promotion = (CoreCmsPromotion)promotionModel.data; + if (promotion == null) + { + _unionOfWork.RollbackTran(); + jm.msg = GlobalErrorCodeVars.Code15019; return jm; } + + if (promotion.maxNums > 0) + { + //判断用户是否已领取?领取次数 + var couponResult = await _couponServices.GetMyCoupon(_user.ID, entity.id, "all", 1, 9999); + if (couponResult.status && couponResult.code >= promotion.maxNums) + { + _unionOfWork.RollbackTran(); + jm.msg = GlobalErrorCodeVars.Code15018; + return jm; + } + } + + jm = await _couponServices.AddData(_user.ID, entity.id, promotion); + + _unionOfWork.CommitTran(); + + jm.otherData = promotionModel; + + } + catch (Exception e) + { + _unionOfWork.RollbackTran(); + jm.msg = GlobalErrorCodeVars.Code10000; } - jm = await _couponServices.AddData(_user.ID, entity.id, promotion); - jm.otherData = promotionModel; + return jm; } #endregion