From 110de1d2a7aa1bd36b91c8d8b22032c1f432e499 Mon Sep 17 00:00:00 2001 From: JunFeng Wu Date: Wed, 26 Apr 2023 15:07:23 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=8B=E5=8D=95?= =?UTF-8?q?=E6=9B=B4=E6=94=B9=E5=BA=93=E5=AD=98=E5=B9=B6=E5=8F=91=E8=B6=85?= =?UTF-8?q?=E4=B9=B0=E9=97=AE=E9=A2=98=20=E4=BF=AE=E5=A4=8D=E5=A4=9A?= =?UTF-8?q?=E5=95=86=E5=93=81=E5=90=88=E5=B9=B6=E4=B8=8B=E5=8D=95=EF=BC=8C?= =?UTF-8?q?=E5=85=B6=E4=B8=AD=E4=B8=80=E5=95=86=E5=93=81=E5=BA=93=E5=AD=98?= =?UTF-8?q?=E4=B8=8D=E8=B6=B3=EF=BC=8C=E5=AF=BC=E8=87=B4=E6=95=B4=E4=B8=AA?= =?UTF-8?q?=E4=B8=8B=E5=8D=95=E5=A4=B1=E8=B4=A5=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E4=B8=8B=E5=8D=95=E6=9B=B4=E6=94=B9=E5=BA=93=E5=AD=98=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=EF=BC=8C=E8=AE=A2=E5=8D=95=E7=8A=B6=E6=80=81=E4=BE=9D?= =?UTF-8?q?=E7=84=B6=E5=BE=85=E6=94=AF=E4=BB=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Good/CoreCmsGoodsRepository.cs | 2 +- .../Order/CoreCmsOrderServices.cs | 61 ++++++++++++++++--- 2 files changed, 52 insertions(+), 11 deletions(-) diff --git a/CoreCms.Net.Repository/Good/CoreCmsGoodsRepository.cs b/CoreCms.Net.Repository/Good/CoreCmsGoodsRepository.cs index a69c241..d59ef1f 100644 --- a/CoreCms.Net.Repository/Good/CoreCmsGoodsRepository.cs +++ b/CoreCms.Net.Repository/Good/CoreCmsGoodsRepository.cs @@ -803,7 +803,7 @@ namespace CoreCms.Net.Repository //更新记录。 bl = DbClient.Updateable() .SetColumns(it => it.freezeStock == it.freezeStock + num) - .Where(p => p.id == productModel.id && insertNum >= num).ExecuteCommandHasChange(); + .Where(p => p.id == productModel.id && insertNum >= num && p.freezeStock < p.stock).ExecuteCommandHasChange(); break; case "send": //发货 bl = DbClient.Updateable() diff --git a/CoreCms.Net.Services/Order/CoreCmsOrderServices.cs b/CoreCms.Net.Services/Order/CoreCmsOrderServices.cs index 1a960ac..4584211 100644 --- a/CoreCms.Net.Services/Order/CoreCmsOrderServices.cs +++ b/CoreCms.Net.Services/Order/CoreCmsOrderServices.cs @@ -249,24 +249,58 @@ namespace CoreCms.Net.Services //开始事务处理 await _dal.InsertAsync(order); - + //上面保存好订单表,下面保存订单的其他信息 if (orderItems != null) { jm.msg = "更改库存"; + // foreach (var item in orderItems) + // { + // var res = _goodsServices.ChangeStock(item.productId, GlobalEnumVars.OrderChangeStockType.order.ToString(), item.nums); + // if (res.status == false) + // { + // jm.msg = "更新库存数据失败"; + // return jm; + // } + // } + //更改库存 - foreach (var item in orderItems) + var avaliableOrderItems = orderItems.Where(item => { var res = _goodsServices.ChangeStock(item.productId, GlobalEnumVars.OrderChangeStockType.order.ToString(), item.nums); + if (res.status == false) { - - jm.msg = "更新库存数据失败"; - return jm; + jm.msg += $"{item.name}库存不足"; } + + return res.status; + + + }).ToList(); + + + if (avaliableOrderItems.Count == 0) + { + await _orderItemServices.InsertCommandAsync(orderItems); + + await _dal.UpdateAsync(n => new CoreCmsOrder() + { + status = (int)GlobalEnumVars.OrderStatus.Cancel, + updateTime = DateTime.Now + }, + m => m.orderId == order.orderId); + + //清除购物车信息 + await _cartServices.DeleteAsync(p => ids.Contains(p.id) && p.userId == userId && p.type == orderType); + + + jm.msg = "下单失败,库存不足"; + return jm; } - jm.msg = "订单明细更新" + orderItems.Count; - var outItems = await _orderItemServices.InsertCommandAsync(orderItems); + + jm.msg = "订单明细更新" + avaliableOrderItems.Count; + var outItems = await _orderItemServices.InsertCommandAsync(avaliableOrderItems); var outItemsBool = outItems > 0; if (outItemsBool == false) { @@ -275,6 +309,13 @@ namespace CoreCms.Net.Services jm.data = outItems; return jm; } + + + + + + + //优惠券核销 if (!string.IsNullOrEmpty(couponCode)) { @@ -304,21 +345,21 @@ namespace CoreCms.Net.Services break; case (int)GlobalEnumVars.OrderType.PinTuan: //拼团模式去校验拼团是否存在,并添加拼团记录 - var pinTuanRes = await _pinTuanRecordServices.OrderAdd(order, orderItems, teamId); + var pinTuanRes = await _pinTuanRecordServices.OrderAdd(order, avaliableOrderItems, teamId); if (pinTuanRes.status == false) { return pinTuanRes; } break; case (int)GlobalEnumVars.OrderType.Group: - var groupRes = await _promotionRecordServices.OrderAdd(order, orderItems, objectId, orderType); + var groupRes = await _promotionRecordServices.OrderAdd(order, avaliableOrderItems, objectId, orderType); if (groupRes.status == false) { return groupRes; } break; case (int)GlobalEnumVars.OrderType.Skill: - var rskillRes = await _promotionRecordServices.OrderAdd(order, orderItems, objectId, orderType); + var rskillRes = await _promotionRecordServices.OrderAdd(order, avaliableOrderItems, objectId, orderType); if (rskillRes.status == false) { return rskillRes; -- Gitee From 7d7e408ddb739321ace06aa3a3faae00d57814ac Mon Sep 17 00:00:00 2001 From: JunFeng Wu Date: Fri, 28 Apr 2023 15:22:38 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E9=A2=86=E5=8F=96=E4=BC=98=E6=83=A0?= =?UTF-8?q?=E5=88=B8=E5=BC=80=E5=90=AF=E4=BA=8B=E5=8A=A1=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E5=9C=A8=E6=9F=A5=E8=AF=A2=E4=BC=98=E6=83=A0=E5=88=B8=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E6=97=B6=E5=8A=A0update=E9=94=81=E8=A7=A3=E5=86=B3?= =?UTF-8?q?=E5=B9=B6=E5=8F=91=E9=A2=86=E4=BC=98=E6=83=A0=E5=88=B8=E4=BA=A7?= =?UTF-8?q?=E7=94=9F=E8=B6=85=E4=B9=B0=E9=97=AE=E9=A2=98=E3=80=82=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BC=98=E6=83=A0=E5=88=B8=E9=A2=86=E5=8F=96?= =?UTF-8?q?=E6=95=B0=E9=87=8F=E8=AE=BE=E7=BD=AE=E4=B8=BA0=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=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 -- Gitee