From 1605af1ced748313e99f38e5eb6888768fbc7d54 Mon Sep 17 00:00:00 2001
From: sunpengfei <i@angelzzz.com>
Date: 星期三, 11 六月 2025 14:46:43 +0800
Subject: [PATCH] fix:支付回调和供应商回调的并发问题

---
 LifePayment/LifePayment.Worker/Worker/CheckUnPayOrderWork.cs |   52 ++++++++++++++++++++++++++++++----------------------
 1 files changed, 30 insertions(+), 22 deletions(-)

diff --git a/LifePayment/LifePayment.Worker/Worker/CheckUnPayOrderWork.cs b/LifePayment/LifePayment.Worker/Worker/CheckUnPayOrderWork.cs
index b16edd2..5bd2dec 100644
--- a/LifePayment/LifePayment.Worker/Worker/CheckUnPayOrderWork.cs
+++ b/LifePayment/LifePayment.Worker/Worker/CheckUnPayOrderWork.cs
@@ -6,16 +6,19 @@
 using Microsoft.Extensions.DependencyInjection;
 using Microsoft.Extensions.Logging;
 using Nest;
+using StackExchange.Redis;
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using Volo.Abp.BackgroundWorkers;
+using Volo.Abp.DistributedLocking;
 using Volo.Abp.Domain.Repositories;
 using Volo.Abp.Threading;
 using Volo.Abp.Uow;
 using ZeroD.Util;
+using ZeroD.Util.Fadd;
 
 namespace LifePayment.Worker.Worker
 {
@@ -25,6 +28,7 @@
         private readonly int _doWorkMinute = 1;
         private readonly ILifePayOrderService lifePayOrderService;
         private readonly ILifePayService lifePayService;
+        private readonly IAbpDistributedLock distributedLock;
         private readonly IRepository<LifePayOrder, Guid> lifePayOrderRepository;
 
         public CheckUnPayOrderWork(
@@ -32,11 +36,13 @@
             IServiceScopeFactory serviceScopeFactory,
             ILifePayOrderService lifePayOrderService,
             ILifePayService lifePayService,
+            IAbpDistributedLock distributedLock,
             IRepository<LifePayOrder, Guid> lifePayOrderRepository) : base(timer, serviceScopeFactory)
         {
-            timer.Period = (int)TimeSpan.FromMinutes(10).TotalMilliseconds;
+            timer.Period = (int)TimeSpan.FromSeconds(5).TotalMilliseconds;
             this.lifePayOrderService = lifePayOrderService;
             this.lifePayService = lifePayService;
+            this.distributedLock = distributedLock;
             this.lifePayOrderRepository = lifePayOrderRepository;
         }
 
@@ -51,30 +57,32 @@
                 var orders = lifePayOrderRepository.Where(it => it.PayStatus == LifePayStatusEnum.鏈敮浠� && it.CreationTime >= start && it.CreationTime <= end).ToList();
                 foreach (var order in orders)
                 {
+                    await using var orderLock = await distributedLock.TryAcquireAsync($"LockKey:UpdateOrder:{order.OrderNo}", TimeSpan.FromSeconds(60));
+
                     Logger.LogInformation($"璁㈠崟锛歿order.OrderNo}-{order.ToJson()}");
-                    //var wxPayNotice = await lifePayService.WxPayTradeQuery(order.OrderNo);
-                    //var json = wxPayNotice.ToJson();
-                    //Logger.LogInformation($"璁㈠崟锛坽wxPayNotice.OutTradeNo}锛変俊鎭�: {json}");
-                    //if (wxPayNotice.OutTradeNo.Contains("JF") && wxPayNotice.TradeState == LifePaymentConstant.WxPayStatus.鏀粯鎴愬姛)
-                    //{
-                    //    await lifePayService.LifePaySuccessHandler(wxPayNotice.OutTradeNo, wxPayNotice.TransactionId);
+                    var wxPayNotice = await lifePayService.WxPayTradeQuery(order.OrderNo);
+                    var json = wxPayNotice.ToJson();
+                    Logger.LogInformation($"璁㈠崟锛坽wxPayNotice.OutTradeNo}锛変俊鎭�: {json}");
+                    if (wxPayNotice.OutTradeNo.Contains("JF") && wxPayNotice.TradeState == LifePaymentConstant.WxPayStatus.鏀粯鎴愬姛)
+                    {
+                        await lifePayService.LifePaySuccessHandler(wxPayNotice.OutTradeNo, wxPayNotice.TransactionId);
 
-                    //    // 鎻掑叆鏀舵敮娴佹按
-                    //    await lifePayOrderService.AddLifePayExpensesReceipts(new AddLifePayExpensesReceiptsInput()
-                    //    {
-                    //        OrderNo = wxPayNotice.OutTradeNo,
-                    //        OutOrderNo = wxPayNotice.TransactionId,
-                    //        LifePayType = LifePayTypeEnum.WxPay,
-                    //        ExpensesReceiptsType = ExpensesReceiptsTypeEnum.Expenses,
-                    //        Amount = wxPayNotice.Amount.Total
-                    //    });
+                        // 鎻掑叆鏀舵敮娴佹按
+                        await lifePayOrderService.AddLifePayExpensesReceipts(new AddLifePayExpensesReceiptsInput()
+                        {
+                            OrderNo = wxPayNotice.OutTradeNo,
+                            OutOrderNo = wxPayNotice.TransactionId,
+                            LifePayType = LifePayTypeEnum.WxPay,
+                            ExpensesReceiptsType = ExpensesReceiptsTypeEnum.Expenses,
+                            Amount = wxPayNotice.Amount.Total
+                        });
 
-                    //    Logger.LogInformation("宸叉敼涓烘敮浠樻垚鍔�");
-                    //}
-                    //else
-                    //{
-                    //    Logger.LogInformation("鏈洿鏂�");
-                    //}
+                        Logger.LogInformation("宸叉敼涓烘敮浠樻垚鍔�");
+                    }
+                    else
+                    {
+                        Logger.LogInformation("鏈洿鏂�");
+                    }
                 }
             }
             catch (Exception ex)

--
Gitblit v1.9.1