From e0f2b29f3349d43e7f4222e079c54ba9cc4b6ee1 Mon Sep 17 00:00:00 2001
From: sunpengfei <i@angelzzz.com>
Date: 星期一, 16 六月 2025 11:09:23 +0800
Subject: [PATCH] fix:支付回调及供应商回调并发问题修订

---
 LifePayment/LifePayment.HttpApi/LifePay/WxPayNotifyController.cs |   17 +++++++++++++++--
 1 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/LifePayment/LifePayment.HttpApi/LifePay/WxPayNotifyController.cs b/LifePayment/LifePayment.HttpApi/LifePay/WxPayNotifyController.cs
index de9e04b..8d1ae98 100644
--- a/LifePayment/LifePayment.HttpApi/LifePay/WxPayNotifyController.cs
+++ b/LifePayment/LifePayment.HttpApi/LifePay/WxPayNotifyController.cs
@@ -1,6 +1,7 @@
 锘縰sing LifePayment.Application.Contracts;
 using LifePayment.Domain;
 using LifePayment.Domain.Shared;
+using Medallion.Threading;
 using Microsoft.AspNetCore.Mvc;
 using Microsoft.Extensions.Caching.Distributed;
 using Microsoft.Extensions.Logging;
@@ -10,6 +11,7 @@
 using System.Threading.Tasks;
 using Volo.Abp.AspNetCore.Mvc;
 using Volo.Abp.Caching;
+using Volo.Abp.DistributedLocking;
 using Volo.Abp.Uow;
 using ZeroD.Util;
 
@@ -26,6 +28,7 @@
         private readonly ILifePayService _lifePayService;
         private readonly ILifePayOrderService _lifePayOrderService;
         private readonly IDistributedCache<string> distributedCache;
+        private readonly IAbpDistributedLock distributedLock;
         private readonly ILogger<WxPayNotifyController> _logger;
 
         public WxPayNotifyController(
@@ -33,13 +36,15 @@
                ILogger<WxPayNotifyController> logger,
                ILifePayService lifePayService,
                ILifePayOrderService lifePayOrderService,
-               IDistributedCache<string> distributedCache)
+               IDistributedCache<string> distributedCache,
+               IAbpDistributedLock distributedLock)
         {
             _wxPayApi = wxPayApi;
             _logger = logger;
             _lifePayService = lifePayService;
             _lifePayOrderService = lifePayOrderService;
             this.distributedCache = distributedCache;
+            this.distributedLock = distributedLock;
         }
 
         /// <summary>
@@ -48,7 +53,7 @@
         /// <param name="input"></param>
         /// <returns></returns>
         [HttpPost]
-        [UnitOfWork]
+        [UnitOfWork(false)]
         public async Task<WxRechargeNotifyResult> WxRechargeNotify(WxRechargeNotifyInput input)
         {
             try
@@ -59,10 +64,14 @@
                 _logger.LogInformation($"鐢熸椿绠″寰俊鍏呭�煎洖璋冮�氱煡data锛�" + data);
                 var wxPayNotice = JsonConvert.DeserializeObject<WxPayNotice>(data);
 
+                await using var orderLock = await distributedLock.TryAcquireAsync($"LockKey:UpdateOrder:{wxPayNotice.OutTradeNo}", TimeSpan.FromSeconds(60));
+                _logger.LogInformation($"閿侊細LockKey:UpdateOrder:{wxPayNotice.OutTradeNo} - {orderLock != null}");
+
                 if (wxPayNotice.OutTradeNo.Contains("JF"))
                 {
                     if (wxPayNotice.TradeState == LifePaymentConstant.WxPayStatus.鏀粯鎴愬姛)
                     {
+
                         var key = $"WxRechargeNotify_{wxPayNotice.OutTradeNo}";
                         if (string.IsNullOrWhiteSpace(distributedCache.Get(key)))
                         {
@@ -85,6 +94,9 @@
                             {
                                 AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(10)
                             });
+
+                            await CurrentUnitOfWork.SaveChangesAsync();
+                            _logger.LogInformation("浜嬪姟瀹屾垚");
                         }
                         else
                         {
@@ -104,6 +116,7 @@
             catch (Exception ex)
             {
                 await CurrentUnitOfWork.RollbackAsync();
+                _logger.LogError($"寰俊鏀粯鍥炶皟寮傚父锛歿ex.Message}", ex);
                 return new WxRechargeNotifyResult
                 {
                     Code = "FAIL",

--
Gitblit v1.9.1