From 67fc39c356d8b480b7005cb2fa4769a0a6c6cfb1 Mon Sep 17 00:00:00 2001 From: sunpengfei <i@angelzzz.com> Date: 星期五, 13 六月 2025 14:19:56 +0800 Subject: [PATCH] pref:优化事务 --- LifePayment/LifePayment.HttpApi/LifePay/WxPayNotifyController.cs | 6 + LifePayment/LifePayment.HttpApi/LifePay/ACOOLYNotifyController.cs | 1 LifePayment/LifePayment.Application/LifePay/LifePayService.cs | 149 +++++++++++++++++++----------------- LifePayment/LifePayment.HttpApi/LifePay/AliPayNotifyController.cs | 59 ++++++++------ 4 files changed, 120 insertions(+), 95 deletions(-) diff --git a/LifePayment/LifePayment.Application/LifePay/LifePayService.cs b/LifePayment/LifePayment.Application/LifePay/LifePayService.cs index 258cc91..ddfd419 100644 --- a/LifePayment/LifePayment.Application/LifePay/LifePayService.cs +++ b/LifePayment/LifePayment.Application/LifePay/LifePayService.cs @@ -1533,82 +1533,91 @@ { await using var orderLock = await distributedLock.TryAcquireAsync($"LockKey:UpdateOrder:{orderNo}", TimeSpan.FromSeconds(60)); _logger.LogInformation($"閿侊細LockKey:UpdateOrder:{orderNo} - {orderLock != null}"); - - var order = await _lifePayOrderRepository.Where(x => x.OrderNo == orderNo).FirstOrDefaultAsync(); - - CheckExtensions.IfTrueThrowUserFriendlyException(order == null, "璁㈠崟涓嶅瓨鍦�"); - - if (order.ACOOLYStatus.HasValue && (int)order.ACOOLYStatus > (int)acoolyStatus) + try { - _logger.LogInformation($"璁㈠崟锛坽orderNo}锛夌敱{order.ACOOLYStatus}鑷硔acoolyStatus}澶辫触锛屼笉鍙洖婊氱姸鎬�"); - return; - } + var order = await _lifePayOrderRepository.Where(x => x.OrderNo == orderNo).FirstOrDefaultAsync(); - if (order.LifePayOrderStatus == status && order.ACOOLYStatus == acoolyStatus) - { - _logger.LogInformation($"璁㈠崟锛坽orderNo}锛夊凡澶勭悊璇ョ姸鎬�"); - return; - } + CheckExtensions.IfTrueThrowUserFriendlyException(order == null, "璁㈠崟涓嶅瓨鍦�"); - order.ActualReceivedAmount = actualParValue; - order.PlatformDeductionAmount = Math.Round((order.ActualReceivedAmount ?? 0) * (order.PlatformRate ?? 0) / 100, 2); - - if (acoolyOrderNo.IsNotNullOrEmpty()) - { - order.ACOOLYOrderNo = acoolyOrderNo; - } - - if (status == LifePayOrderStatusEnum.寰呴��娆�) - { - order.LifePayRefundStatus = LifePayRefundStatusEnum.寰呴��娆�; - } - - if (refundApplyRemark.IsNotNullOrEmpty()) - { - order.RefundApplyRemark = refundApplyRemark; - } - - order.LifePayOrderStatus = status; - order.ACOOLYStatus = acoolyStatus; - - if (order.LifePayOrderStatus == LifePayOrderStatusEnum.宸插畬鎴� - || order.LifePayOrderStatus == LifePayOrderStatusEnum.宸查��娆�) - { - order.FinishTime = DateTime.Now; - } - - if (order.ACOOLYStatus == ACOOLYStatusEnum.宸查��娆� || order.ACOOLYStatus == ACOOLYStatusEnum.閮ㄥ垎鍏呭�兼垚鍔�) - { - order.RefundPrice = Math.Round((1 - ((order.ActualReceivedAmount ?? 0) / (order.RechargeAmount ?? 0))) * (order.PayAmount ?? 0), 2); - } - - if (order.LifePayOrderStatus == LifePayOrderStatusEnum.宸插畬鎴�) - { - ///缁撶畻娓犻亾浣i噾 - /// 姣涘埄 - var grossProfit = order.RechargeAmount * (order.ChannleRate - order.PlatformRate) / 100; - /// 娓犻亾浣i噾 锛�(鍏呭�奸潰棰� * 娓犻亾鎶樻墸姣斾緥)-(鍏呭�奸潰棰� * 骞冲彴鎶樻墸姣斾緥)锛�* 浣i噾姣斾緥 - var channlesRakePrice = grossProfit * (order.ChannlesRakeRate) / 100; - if (channlesRakePrice.HasValue) + if (order.ACOOLYStatus.HasValue && (int)order.ACOOLYStatus > (int)acoolyStatus) { - LifePayChannlesRake lifePayChannlesRake = new LifePayChannlesRake() - { - OrderNo = order.OrderNo, - PayAmount = order.RechargeAmount ?? 0, - ChannlesRakeRate = order.ChannlesRakeRate ?? 0, - ChannlesRakePrice = channlesRakePrice ?? 0, - FinishTime = order.FinishTime.Value, - ChannelId = order.ChannelId, - }; - await _lifePayChannlesRakeRepository.InsertAsync(lifePayChannlesRake); + _logger.LogInformation($"璁㈠崟锛坽orderNo}锛夌敱{order.ACOOLYStatus}鑷硔acoolyStatus}澶辫触锛屼笉鍙洖婊氱姸鎬�"); + return; } + + if (order.LifePayOrderStatus == status && order.ACOOLYStatus == acoolyStatus) + { + _logger.LogInformation($"璁㈠崟锛坽orderNo}锛夊凡澶勭悊璇ョ姸鎬�"); + return; + } + + order.ActualReceivedAmount = actualParValue; + order.PlatformDeductionAmount = Math.Round((order.ActualReceivedAmount ?? 0) * (order.PlatformRate ?? 0) / 100, 2); + + if (acoolyOrderNo.IsNotNullOrEmpty()) + { + order.ACOOLYOrderNo = acoolyOrderNo; + } + + if (status == LifePayOrderStatusEnum.寰呴��娆�) + { + order.LifePayRefundStatus = LifePayRefundStatusEnum.寰呴��娆�; + } + + if (refundApplyRemark.IsNotNullOrEmpty()) + { + order.RefundApplyRemark = refundApplyRemark; + } + + order.LifePayOrderStatus = status; + order.ACOOLYStatus = acoolyStatus; + + if (order.LifePayOrderStatus == LifePayOrderStatusEnum.宸插畬鎴� + || order.LifePayOrderStatus == LifePayOrderStatusEnum.宸查��娆�) + { + order.FinishTime = DateTime.Now; + } + + if (order.ACOOLYStatus == ACOOLYStatusEnum.宸查��娆� || order.ACOOLYStatus == ACOOLYStatusEnum.閮ㄥ垎鍏呭�兼垚鍔�) + { + order.RefundPrice = Math.Round((1 - ((order.ActualReceivedAmount ?? 0) / (order.RechargeAmount ?? 0))) * (order.PayAmount ?? 0), 2); + } + + if (order.LifePayOrderStatus == LifePayOrderStatusEnum.宸插畬鎴�) + { + ///缁撶畻娓犻亾浣i噾 + /// 姣涘埄 + var grossProfit = order.RechargeAmount * (order.ChannleRate - order.PlatformRate) / 100; + /// 娓犻亾浣i噾 锛�(鍏呭�奸潰棰� * 娓犻亾鎶樻墸姣斾緥)-(鍏呭�奸潰棰� * 骞冲彴鎶樻墸姣斾緥)锛�* 浣i噾姣斾緥 + var channlesRakePrice = grossProfit * (order.ChannlesRakeRate) / 100; + if (channlesRakePrice.HasValue) + { + LifePayChannlesRake lifePayChannlesRake = new LifePayChannlesRake() + { + OrderNo = order.OrderNo, + PayAmount = order.RechargeAmount ?? 0, + ChannlesRakeRate = order.ChannlesRakeRate ?? 0, + ChannlesRakePrice = channlesRakePrice ?? 0, + FinishTime = order.FinishTime.Value, + ChannelId = order.ChannelId, + }; + await _lifePayChannlesRakeRepository.InsertAsync(lifePayChannlesRake); + } + } + + /// 鍒涘缓鐢熸椿缂磋垂娑堣垂璁板綍 + await _lifePayOrderService.CreatLifePayConsumption(acoolyStatus, order.OrderNo, order.ACOOLYOrderNo, + order.PlatformDeductionAmount ?? 0, order.ChannelId, order.CreationTime, order.FinishTime, order.ChannleRate, parValue, actualParValue); + + await CurrentUnitOfWork.CompleteAsync(); + + _logger.LogInformation("浜嬪姟瀹屾垚"); } - - /// 鍒涘缓鐢熸椿缂磋垂娑堣垂璁板綍 - await _lifePayOrderService.CreatLifePayConsumption(acoolyStatus, order.OrderNo, order.ACOOLYOrderNo, - order.PlatformDeductionAmount ?? 0, order.ChannelId, order.CreationTime, order.FinishTime, order.ChannleRate, parValue, actualParValue); - - await CurrentUnitOfWork.SaveChangesAsync(); + catch (Exception ex) + { + await CurrentUnitOfWork.RollbackAsync(); + _logger.LogError($"渚涘簲鍟嗗洖璋冨紓甯革細{ex.Message}", ex); + } } /// <summary> diff --git a/LifePayment/LifePayment.HttpApi/LifePay/ACOOLYNotifyController.cs b/LifePayment/LifePayment.HttpApi/LifePay/ACOOLYNotifyController.cs index d44e90d..c6304e0 100644 --- a/LifePayment/LifePayment.HttpApi/LifePay/ACOOLYNotifyController.cs +++ b/LifePayment/LifePayment.HttpApi/LifePay/ACOOLYNotifyController.cs @@ -41,6 +41,7 @@ /// <returns></returns> [HttpPost] [AllowAnonymous] + [UnitOfWork(false)] public async Task ACOOLYNotify() { _logger.LogError("ACOOLY鍥炶皟閫氱煡寮�濮嬭繘鍏�"); diff --git a/LifePayment/LifePayment.HttpApi/LifePay/AliPayNotifyController.cs b/LifePayment/LifePayment.HttpApi/LifePay/AliPayNotifyController.cs index 9450126..7a44105 100644 --- a/LifePayment/LifePayment.HttpApi/LifePay/AliPayNotifyController.cs +++ b/LifePayment/LifePayment.HttpApi/LifePay/AliPayNotifyController.cs @@ -49,6 +49,7 @@ /// </summary> /// <returns></returns> [HttpPost] + [UnitOfWork(false)] public async Task<ContentResult> AliRechargeNotify() { _logger.LogError($"鐢熸椿绠″鏀粯瀹濆厖鍊煎洖璋冮�氱煡锛氳繘鍏ユ敮浠樺疂鍥炶皟"); @@ -65,34 +66,44 @@ if (input.OutTradeNo.Contains("JF")) { - if (input.TradeStatus == LifePaymentConstant.AliPayStatus.鏀粯鎴愬姛 && input.OutBizNo.IsNullOrEmpty()) + try { - await _lifePayService.LifePaySuccessHandler(input.OutTradeNo, input.TradeNo); - // 鎻掑叆鏀舵敮娴佹按 - await _lifePayOrderService.AddLifePayExpensesReceipts(new AddLifePayExpensesReceiptsInput() + if (input.TradeStatus == LifePaymentConstant.AliPayStatus.鏀粯鎴愬姛 && input.OutBizNo.IsNullOrEmpty()) { - OrderNo = input.OutTradeNo, - OutOrderNo = input.TradeNo, - LifePayType = LifePayTypeEnum.AliPay, - ExpensesReceiptsType = ExpensesReceiptsTypeEnum.Expenses - }); - await CurrentUnitOfWork.SaveChangesAsync(); + await _lifePayService.LifePaySuccessHandler(input.OutTradeNo, input.TradeNo); + // 鎻掑叆鏀舵敮娴佹按 + await _lifePayOrderService.AddLifePayExpensesReceipts(new AddLifePayExpensesReceiptsInput() + { + OrderNo = input.OutTradeNo, + OutOrderNo = input.TradeNo, + LifePayType = LifePayTypeEnum.AliPay, + ExpensesReceiptsType = ExpensesReceiptsTypeEnum.Expenses + }); + await CurrentUnitOfWork.SaveChangesAsync(); + _logger.LogInformation("浜嬪姟瀹屾垚"); + } + else if (((input.TradeStatus == LifePaymentConstant.AliPayStatus.鏀粯鎴愬姛 || input.TradeStatus == LifePaymentConstant.AliPayStatus.瓒呮椂鍏抽棴) + && input.OutBizNo.IsNotNullOrEmpty() + && (input.RefundFee.HasValue && input.RefundFee > 0))) + { + await _lifePayService.LifePayRefundsHandler(input.OutTradeNo, LifePayRefundStatusEnum.宸查��娆�); + // 鎻掑叆鏀舵敮娴佹按 + await _lifePayOrderService.AddLifePayExpensesReceipts(new AddLifePayExpensesReceiptsInput() + { + OrderNo = input.OutTradeNo, + OutRefundNo = input.OutBizNo, + OutOrderNo = input.TradeNo, + LifePayType = LifePayTypeEnum.AliPay, + ExpensesReceiptsType = ExpensesReceiptsTypeEnum.Receipts + }); + await CurrentUnitOfWork.CompleteAsync(); + _logger.LogInformation("浜嬪姟瀹屾垚"); + } } - else if (((input.TradeStatus == LifePaymentConstant.AliPayStatus.鏀粯鎴愬姛 || input.TradeStatus == LifePaymentConstant.AliPayStatus.瓒呮椂鍏抽棴) - && input.OutBizNo.IsNotNullOrEmpty() - && (input.RefundFee.HasValue && input.RefundFee > 0))) + catch (Exception ex) { - await _lifePayService.LifePayRefundsHandler(input.OutTradeNo, LifePayRefundStatusEnum.宸查��娆�); - // 鎻掑叆鏀舵敮娴佹按 - await _lifePayOrderService.AddLifePayExpensesReceipts(new AddLifePayExpensesReceiptsInput() - { - OrderNo = input.OutTradeNo, - OutRefundNo = input.OutBizNo, - OutOrderNo = input.TradeNo, - LifePayType = LifePayTypeEnum.AliPay, - ExpensesReceiptsType = ExpensesReceiptsTypeEnum.Receipts - }); - await CurrentUnitOfWork.SaveChangesAsync(); + await CurrentUnitOfWork.RollbackAsync(); + _logger.LogError($"鏀粯瀹濇敮浠樺洖璋冨紓甯革細{ex.Message}", ex); } } else diff --git a/LifePayment/LifePayment.HttpApi/LifePay/WxPayNotifyController.cs b/LifePayment/LifePayment.HttpApi/LifePay/WxPayNotifyController.cs index 9e30708..e958bac 100644 --- a/LifePayment/LifePayment.HttpApi/LifePay/WxPayNotifyController.cs +++ b/LifePayment/LifePayment.HttpApi/LifePay/WxPayNotifyController.cs @@ -53,6 +53,7 @@ /// <param name="input"></param> /// <returns></returns> [HttpPost] + [UnitOfWork(false)] public async Task<WxRechargeNotifyResult> WxRechargeNotify(WxRechargeNotifyInput input) { try @@ -94,7 +95,8 @@ AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(10) }); - await CurrentUnitOfWork.SaveChangesAsync(); + await CurrentUnitOfWork.CompleteAsync(); + _logger.LogInformation("浜嬪姟瀹屾垚"); } else { @@ -113,6 +115,8 @@ } catch (Exception ex) { + await CurrentUnitOfWork.RollbackAsync(); + _logger.LogError($"寰俊鏀粯鍥炶皟寮傚父锛歿ex.Message}", ex); return new WxRechargeNotifyResult { Code = "FAIL", -- Gitblit v1.9.1