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.Application/LifePay/LifePayService.cs |  106 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 102 insertions(+), 4 deletions(-)

diff --git a/LifePayment/LifePayment.Application/LifePay/LifePayService.cs b/LifePayment/LifePayment.Application/LifePay/LifePayService.cs
index 3179294..5581eb3 100644
--- a/LifePayment/LifePayment.Application/LifePay/LifePayService.cs
+++ b/LifePayment/LifePayment.Application/LifePay/LifePayService.cs
@@ -18,6 +18,7 @@
 using Volo.Abp;
 using Volo.Abp.Application.Services;
 using Volo.Abp.Data;
+using Volo.Abp.DistributedLocking;
 using Volo.Abp.Domain.Repositories;
 using Volo.Abp.EventBus.Distributed;
 using Z.EntityFramework.Plus;
@@ -44,6 +45,7 @@
     private readonly ILifePayOrderService _lifePayOrderService;
     private readonly IDataFilter dataFilter;
     private readonly IChannelFilter _channelFilter;
+    private readonly IAbpDistributedLock distributedLock;
     private readonly IAliPayApi _aliPayApi;
     private readonly IAlipayInterfaceManager _alipayInterfaceManager;
     private readonly IWxPayApi _wxPayApi;
@@ -73,7 +75,8 @@
                           IRepository<LifePayChannles, Guid> lifePayChannlesRep,
                           IRepository<LifePayAccount, Guid> lifePayAccount,
                           IDataFilter dataFilter,
-                          IChannelFilter channelFilter)
+                          IChannelFilter channelFilter,
+                          IAbpDistributedLock distributedLock)
     {
         _logger = logger;
         _aCOOLYManager = aCOOLYManager;
@@ -95,6 +98,7 @@
         _lifePayAccount = lifePayAccount;
         this.dataFilter = dataFilter;
         _channelFilter = channelFilter;
+        this.distributedLock = distributedLock;
         _operateHistory = operateHistory;
     }
 
@@ -308,7 +312,41 @@
                                 PlatformDeductionAmount = a.PlatformDeductionAmount,
                                 ACOOLYStatus = a.ACOOLYStatus,
                                 LifePayRefundStatus = a.LifePayRefundStatus,
+                                OrderParamDetailJsonStr = a.OrderParamDetailJsonStr,
                             }).GetPageResult(input.PageModel);
+        if (result != null && result.Data != null && result.Data.Count > 0)
+        {
+            foreach (var s in result.Data)
+            {
+                if (!string.IsNullOrWhiteSpace(s.OrderParamDetailJsonStr))
+                {
+                    if (s.LifePayOrderType == LifePayOrderTypeEnum.PhoneOrder)
+                    {
+                        var name = s.OrderParamDetailJsonStr.JsonToObject<LifePhoneData>()?.IspCode;
+                        if (Enum.TryParse<IspCodeEnum>(name, true, out var @enum))
+                        {
+                            s.Operator = @enum.GetDescription();
+                        }
+                    }
+                    else if (s.LifePayOrderType == LifePayOrderTypeEnum.ElectricOrder)
+                    {
+                        var name = s.OrderParamDetailJsonStr.JsonToObject<LifeElectricData>()?.ElectricType;
+                        if (Enum.TryParse<ElectricTypeEnum>(name, true, out var @enum))
+                        {
+                            s.Operator = @enum.GetDescription();
+                        }
+                    }
+                    else if (s.LifePayOrderType == LifePayOrderTypeEnum.GasOrder)
+                    {
+                        var name = s.OrderParamDetailJsonStr.JsonToObject<LifeGasData>()?.GasOrgType;
+                        if (Enum.TryParse<GasOrgCodeEnum>(name, true, out var @enum))
+                        {
+                            s.Operator = @enum.GetDescription();
+                        }
+                    }
+                }
+            }
+        }
         return result;
     }
 
@@ -768,7 +806,8 @@
             ACOOLYStatus = x.ACOOLYStatus,
             //RefundApplyRemark = x.RefundApplyRemark,
             ChannelName = x.ChannelName,
-            PlatformPrice = x.PlatformDeductionAmount.HasValue ? x.PlatformDeductionAmount.Value : 0.00m
+            PlatformPrice = x.PlatformDeductionAmount.HasValue ? x.PlatformDeductionAmount.Value : 0.00m,
+            OrderParamDetailJsonStr = x.OrderParamDetailJsonStr,
         }).OrderByDescending(r => r.CreationTime).ToListAsync();
         var i = 0;
         result.ForEach(s =>
@@ -786,6 +825,33 @@
             s.ACOOLYStatusStr = s.ACOOLYStatus.GetDescription();
             s.LifePayOrderStatusStr = s.LifePayOrderStatus.GetDescription();
             s.FinishTimeStr = !s.FinishTime.HasValue ? string.Empty : s.FinishTime.Value.ToString(LifePaymentConstant.DateTimeFormatStr.yyyyMMddHHmmss);
+            if (!string.IsNullOrWhiteSpace(s.OrderParamDetailJsonStr))
+            {
+                if (s.LifePayOrderType == LifePayOrderTypeEnum.PhoneOrder)
+                {
+                    var name = s.OrderParamDetailJsonStr.JsonToObject<LifePhoneData>()?.IspCode;
+                    if (Enum.TryParse<IspCodeEnum>(name, true, out var @enum))
+                    {
+                        s.Operator = @enum.GetDescription();
+                    }
+                }
+                else if (s.LifePayOrderType == LifePayOrderTypeEnum.ElectricOrder)
+                {
+                    var name = s.OrderParamDetailJsonStr.JsonToObject<LifeElectricData>()?.ElectricType;
+                    if (Enum.TryParse<ElectricTypeEnum>(name, true, out var @enum))
+                    {
+                        s.Operator = @enum.GetDescription();
+                    }
+                }
+                else if (s.LifePayOrderType == LifePayOrderTypeEnum.GasOrder)
+                {
+                    var name = s.OrderParamDetailJsonStr.JsonToObject<LifeGasData>()?.GasOrgType;
+                    if (Enum.TryParse<GasOrgCodeEnum>(name, true, out var @enum))
+                    {
+                        s.Operator = @enum.GetDescription();
+                    }
+                }
+            }
         });
         CheckExtensions.IfTrueThrowUserFriendlyException(result.IsNullOrEmpty(), "鏆傛棤鏁版嵁瀵煎嚭");
         return result;
@@ -814,7 +880,8 @@
             ACOOLYStatus = x.ACOOLYStatus,
             //RefundApplyRemark = x.RefundApplyRemark,
             ChannelName = x.ChannelName,
-            PlatformPrice = x.PlatformDeductionAmount.HasValue ? x.PlatformDeductionAmount.Value : 0.00m
+            PlatformPrice = x.PlatformDeductionAmount.HasValue ? x.PlatformDeductionAmount.Value : 0.00m,
+            OrderParamDetailJsonStr = x.OrderParamDetailJsonStr,
         }).OrderByDescending(r => r.CreationTime).ToListAsync();
         var i = 0;
         result.ForEach(s =>
@@ -832,6 +899,33 @@
             s.ACOOLYStatusStr = s.ACOOLYStatus.GetDescription();
             s.LifePayOrderStatusStr = s.LifePayOrderStatus.GetDescription();
             s.FinishTimeStr = !s.FinishTime.HasValue ? string.Empty : s.FinishTime.Value.ToString(LifePaymentConstant.DateTimeFormatStr.yyyyMMddHHmmss);
+            if (!string.IsNullOrWhiteSpace(s.OrderParamDetailJsonStr))
+            {
+                if (s.LifePayOrderType == LifePayOrderTypeEnum.PhoneOrder)
+                {
+                    var name = s.OrderParamDetailJsonStr.JsonToObject<LifePhoneData>()?.IspCode;
+                    if (Enum.TryParse<IspCodeEnum>(name, true, out var @enum))
+                    {
+                        s.Operator = @enum.GetDescription();
+                    }
+                }
+                else if (s.LifePayOrderType == LifePayOrderTypeEnum.ElectricOrder)
+                {
+                    var name = s.OrderParamDetailJsonStr.JsonToObject<LifeElectricData>()?.ElectricType;
+                    if (Enum.TryParse<ElectricTypeEnum>(name, true, out var @enum))
+                    {
+                        s.Operator = @enum.GetDescription();
+                    }
+                }
+                else if (s.LifePayOrderType == LifePayOrderTypeEnum.GasOrder)
+                {
+                    var name = s.OrderParamDetailJsonStr.JsonToObject<LifeGasData>()?.GasOrgType;
+                    if (Enum.TryParse<GasOrgCodeEnum>(name, true, out var @enum))
+                    {
+                        s.Operator = @enum.GetDescription();
+                    }
+                }
+            }
         });
         CheckExtensions.IfTrueThrowUserFriendlyException(result.IsNullOrEmpty(), "鏆傛棤鏁版嵁瀵煎嚭");
         return result;
@@ -1390,7 +1484,7 @@
             order.ACOOLYOrderNo = result.ACOOLYOrderNo;
             order.ACOOLYStatus = ACOOLYStatusEnum.鍏呭�间腑;
             await _lifePayOrderRepository.UpdateAsync(order);
-            _logger.LogInformation("鐢熸椿缂磋垂璁㈠崟鐘舵�侊細" + order.LifePayOrderStatus.ToString());
+            _logger.LogInformation("鐢熸椿缂磋垂璁㈠崟锛�" + order.ToJson());
 
             /// 鍒涘缓鐢熸椿缂磋垂娑堣垂璁板綍
             await _lifePayOrderService.CreatLifePayConsumption(ACOOLYStatusEnum.鍏呭�间腑, order.OrderNo, order.ACOOLYOrderNo,
@@ -1438,7 +1532,10 @@
     public async Task ACOOLYOrderNotifyHandler(string orderNo, string acoolyOrderNo, LifePayOrderStatusEnum status, ACOOLYStatusEnum acoolyStatus, decimal payAmount, string refundApplyRemark, decimal? parValue = 0, decimal? actualParValue = 0)
     {
         var order = await _lifePayOrderRepository.Where(x => x.OrderNo == orderNo).FirstOrDefaultAsync();
+
         CheckExtensions.IfTrueThrowUserFriendlyException(order == null, "璁㈠崟涓嶅瓨鍦�");
+
+        await using var orderLock = await distributedLock.TryAcquireAsync($"LockKey:UpdateOrder:{orderNo}", TimeSpan.FromSeconds(60));
 
         if (order.ACOOLYStatus.HasValue && (int)order.ACOOLYStatus > (int)acoolyStatus)
         {
@@ -2224,6 +2321,7 @@
                           PlatformDeductionAmount = a.PlatformDeductionAmount,
                           ACOOLYStatus = a.ACOOLYStatus,
                           LifePayRefundStatus = a.LifePayRefundStatus,
+                          OrderParamDetailJsonStr = a.OrderParamDetailJsonStr
                       });
 
 

--
Gitblit v1.9.1