【优化】优化下单创建订单事务内业务逻辑,优先对秒杀,团购,拼团,库存等进行处理,然后订单业务实现,其他情况下直接事务回滚。

This commit is contained in:
jianweie
2023-05-18 15:23:59 +08:00
parent ce8ffb7f87
commit dd05c3b90c

View File

@@ -85,6 +85,7 @@ namespace CoreCms.Net.Services
private readonly IWeChatTransactionComponentOrderServices _chatTransactionComponentOrderServices; private readonly IWeChatTransactionComponentOrderServices _chatTransactionComponentOrderServices;
private readonly WeChat.Service.HttpClients.IWeChatApiHttpClientFactory _weChatApiHttpClientFactory; private readonly WeChat.Service.HttpClients.IWeChatApiHttpClientFactory _weChatApiHttpClientFactory;
private IUnitOfWork _unitOfWork;
public CoreCmsOrderServices(ICoreCmsOrderRepository dal public CoreCmsOrderServices(ICoreCmsOrderRepository dal
, IHttpContextAccessor httpContextAccessor , IHttpContextAccessor httpContextAccessor
@@ -110,7 +111,7 @@ namespace CoreCms.Net.Services
, ICoreCmsPaymentsServices paymentsServices , ICoreCmsPaymentsServices paymentsServices
, ICoreCmsBillRefundServices billRefundServices , ICoreCmsBillRefundServices billRefundServices
, ICoreCmsBillLadingServices billLadingServices , ICoreCmsBillLadingServices billLadingServices
, ICoreCmsBillReshipServices billReshipServices, ICoreCmsMessageCenterServices messageCenterServices, ICoreCmsGoodsCommentServices goodsCommentServices, ISysTaskLogServices taskLogServices, ICoreCmsPromotionRecordServices promotionRecordServices, IRedisOperationRepository redisOperationRepository, ICoreCmsUserWeChatInfoServices userWeChatInfoServices, IWeChatTransactionComponentOrderServices tcOrderServices, ICoreCmsCheckBeforeAddOrderServices checkBeforeAddOrderServices, IWeChatApiHttpClientFactory weChatApiHttpClientFactory, IWeChatTransactionComponentOrderServices chatTransactionComponentOrderServices) , ICoreCmsBillReshipServices billReshipServices, ICoreCmsMessageCenterServices messageCenterServices, ICoreCmsGoodsCommentServices goodsCommentServices, ISysTaskLogServices taskLogServices, ICoreCmsPromotionRecordServices promotionRecordServices, IRedisOperationRepository redisOperationRepository, ICoreCmsUserWeChatInfoServices userWeChatInfoServices, IWeChatTransactionComponentOrderServices tcOrderServices, ICoreCmsCheckBeforeAddOrderServices checkBeforeAddOrderServices, IWeChatApiHttpClientFactory weChatApiHttpClientFactory, IWeChatTransactionComponentOrderServices chatTransactionComponentOrderServices, IUnitOfWork unitOfWork)
{ {
this._dal = dal; this._dal = dal;
base.BaseDal = dal; base.BaseDal = dal;
@@ -149,6 +150,7 @@ namespace CoreCms.Net.Services
_checkBeforeAddOrderServices = checkBeforeAddOrderServices; _checkBeforeAddOrderServices = checkBeforeAddOrderServices;
_weChatApiHttpClientFactory = weChatApiHttpClientFactory; _weChatApiHttpClientFactory = weChatApiHttpClientFactory;
_chatTransactionComponentOrderServices = chatTransactionComponentOrderServices; _chatTransactionComponentOrderServices = chatTransactionComponentOrderServices;
_unitOfWork = unitOfWork;
} }
#region #region
@@ -234,7 +236,10 @@ namespace CoreCms.Net.Services
public async Task<WebApiCallBack> ToAdd(int userId, int orderType, string cartIds, int receiptType, int ushipId, int storeId, string ladingName, string ladingMobile, string memo, int point, string couponCode, int source, int scene, int taxType, string taxName, string taxCode, int objectId, int teamId, int requireOrder, int requiredFundType, string traceId) public async Task<WebApiCallBack> ToAdd(int userId, int orderType, string cartIds, int receiptType, int ushipId, int storeId, string ladingName, string ladingMobile, string memo, int point, string couponCode, int source, int scene, int taxType, string taxName, string taxCode, int objectId, int teamId, int requireOrder, int requiredFundType, string traceId)
{ {
var jm = new WebApiCallBack() { methodDescription = "创建订单" }; var jm = new WebApiCallBack() { methodDescription = "创建订单" };
try
{
//开始事务处理
_unitOfWork.BeginTran();
var order = new CoreCmsOrder var order = new CoreCmsOrder
{ {
orderId = CommonHelper.GetSerialNumberType((int)GlobalEnumVars.SerialNumberType.), orderId = CommonHelper.GetSerialNumberType((int)GlobalEnumVars.SerialNumberType.),
@@ -251,6 +256,7 @@ namespace CoreCms.Net.Services
var deliveryRes = await FormatOrderDelivery(order, receiptType, ushipId, storeId, ladingName, ladingMobile); var deliveryRes = await FormatOrderDelivery(order, receiptType, ushipId, storeId, ladingName, ladingMobile);
if (!deliveryRes.status) if (!deliveryRes.status)
{ {
_unitOfWork.RollbackTran();
return deliveryRes; return deliveryRes;
} }
else else
@@ -264,6 +270,7 @@ namespace CoreCms.Net.Services
var orderRes = await FormatOrder(order, userId, ids, areaId, point, couponCode, ushipId, receiptType, objectId); var orderRes = await FormatOrder(order, userId, ids, areaId, point, couponCode, ushipId, receiptType, objectId);
if (!orderRes.status) if (!orderRes.status)
{ {
_unitOfWork.RollbackTran();
return orderRes; return orderRes;
} }
else else
@@ -283,8 +290,6 @@ namespace CoreCms.Net.Services
order.createTime = DateTime.Now; order.createTime = DateTime.Now;
order.scene = scene; order.scene = scene;
//开始事务处理
await _dal.InsertAsync(order);
//插入获取的下单前的微信视频号直接数据 //插入获取的下单前的微信视频号直接数据
var check = new CoreCmsCheckBeforeAddOrder(); var check = new CoreCmsCheckBeforeAddOrder();
@@ -297,17 +302,23 @@ namespace CoreCms.Net.Services
await _checkBeforeAddOrderServices.InsertAsync(check); await _checkBeforeAddOrderServices.InsertAsync(check);
//上面保存好订单表,下面保存订单的其他信息 //上面保存好订单表,下面保存订单的其他信息
if (orderItems != null) if (orderItems == null)
{ {
jm.msg = "订单明细获取失败";
return jm;
}
jm.msg = "更改库存"; jm.msg = "更改库存";
//更改库存 //更改库存
var avaliableOrderItems = orderItems.Where(item => var avaliableOrderItems = orderItems.Where(item =>
{ {
var res = _goodsServices.ChangeStock(item.productId, GlobalEnumVars.OrderChangeStockType.order.ToString(), item.nums); var res = _goodsServices.ChangeStock(item.productId,
GlobalEnumVars.OrderChangeStockType.order.ToString(), item.nums);
if (res.status == false) if (res.status == false)
{ {
jm.msg += $"{item.name}库存不足"; jm.msg += $"{item.name}库存不足";
} }
return res.status; return res.status;
}).ToList(); }).ToList();
@@ -323,7 +334,9 @@ namespace CoreCms.Net.Services
m => m.orderId == order.orderId); m => m.orderId == order.orderId);
//清除购物车信息 //清除购物车信息
await _cartServices.DeleteAsync(p => ids.Contains(p.id) && p.userId == userId && p.type == orderType); _unitOfWork.RollbackTran();
await _cartServices.DeleteAsync(p =>
ids.Contains(p.id) && p.userId == userId && p.type == orderType);
jm.msg = "下单失败,库存不足"; jm.msg = "下单失败,库存不足";
return jm; return jm;
} }
@@ -333,10 +346,12 @@ namespace CoreCms.Net.Services
var outItemsBool = outItems > 0; var outItemsBool = outItems > 0;
if (outItemsBool == false) if (outItemsBool == false)
{ {
_unitOfWork.RollbackTran();
jm.msg = "订单明细更新失败"; jm.msg = "订单明细更新失败";
jm.data = outItems; jm.data = outItems;
return jm; return jm;
} }
//优惠券核销 //优惠券核销
if (!string.IsNullOrEmpty(couponCode)) if (!string.IsNullOrEmpty(couponCode))
{ {
@@ -344,9 +359,11 @@ namespace CoreCms.Net.Services
var couponRes = await _couponServices.UsedMultipleCoupon(arr, order.orderId); var couponRes = await _couponServices.UsedMultipleCoupon(arr, order.orderId);
if (couponRes.status == false) if (couponRes.status == false)
{ {
_unitOfWork.RollbackTran();
return couponRes; return couponRes;
} }
} }
//积分核销 //积分核销
if (order.point > 0) if (order.point > 0)
{ {
@@ -355,6 +372,7 @@ namespace CoreCms.Net.Services
(int)GlobalEnumVars.UserPointSourceTypes.PointTypeDiscount, "订单" + order.orderId + "使用"); (int)GlobalEnumVars.UserPointSourceTypes.PointTypeDiscount, "订单" + order.orderId + "使用");
if (pointLogRes.status == false) if (pointLogRes.status == false)
{ {
_unitOfWork.RollbackTran();
return pointLogRes; return pointLogRes;
} }
} }
@@ -370,22 +388,32 @@ namespace CoreCms.Net.Services
var pinTuanRes = await _pinTuanRecordServices.OrderAdd(order, avaliableOrderItems, teamId); var pinTuanRes = await _pinTuanRecordServices.OrderAdd(order, avaliableOrderItems, teamId);
if (pinTuanRes.status == false) if (pinTuanRes.status == false)
{ {
_unitOfWork.RollbackTran();
return pinTuanRes; return pinTuanRes;
} }
break; break;
case (int)GlobalEnumVars.OrderType.Group: case (int)GlobalEnumVars.OrderType.Group:
var groupRes = await _promotionRecordServices.OrderAdd(order, avaliableOrderItems, objectId, orderType); var groupRes =
await _promotionRecordServices.OrderAdd(order, avaliableOrderItems, objectId,
orderType);
if (groupRes.status == false) if (groupRes.status == false)
{ {
_unitOfWork.RollbackTran();
return groupRes; return groupRes;
} }
break; break;
case (int)GlobalEnumVars.OrderType.Seckill: case (int)GlobalEnumVars.OrderType.Seckill:
var seckillRes = await _promotionRecordServices.OrderAdd(order, avaliableOrderItems, objectId, orderType); var seckillRes =
await _promotionRecordServices.OrderAdd(order, avaliableOrderItems, objectId,
orderType);
if (seckillRes.status == false) if (seckillRes.status == false)
{ {
_unitOfWork.RollbackTran();
return seckillRes; return seckillRes;
} }
break; break;
case (int)GlobalEnumVars.OrderType.Bargain: case (int)GlobalEnumVars.OrderType.Bargain:
//砍价模式 //砍价模式
@@ -393,7 +421,9 @@ namespace CoreCms.Net.Services
break; break;
} }
}
//校验后再创建订单
await _dal.InsertAsync(order);
//清除购物车信息 //清除购物车信息
await _cartServices.DeleteAsync(p => ids.Contains(p.id) && p.userId == userId && p.type == orderType); await _cartServices.DeleteAsync(p => ids.Contains(p.id) && p.userId == userId && p.type == orderType);
@@ -556,9 +586,18 @@ namespace CoreCms.Net.Services
await _messageCenterServices.SendMessage(order.userId, GlobalEnumVars.PlatformMessageTypes.CreateOrder.ToString(), JObject.FromObject(order)); await _messageCenterServices.SendMessage(order.userId, GlobalEnumVars.PlatformMessageTypes.CreateOrder.ToString(), JObject.FromObject(order));
} }
_unitOfWork.CommitTran();
jm.status = true; jm.status = true;
jm.data = order; jm.data = order;
}
catch (Exception e)
{
_unitOfWork.RollbackTran();
jm.status = false;
jm.otherData = e.ToString();
}
return jm; return jm;
} }