From f7bb0825bc06b8cea32caa44d2326dde51990e77 Mon Sep 17 00:00:00 2001
From: zhengyiming <540361168@qq.com>
Date: 星期四, 11 九月 2025 11:10:55 +0800
Subject: [PATCH] feat: 公告

---
 LifePayment/LifePayment.Application/LifePay/LifePayService.cs |  217 ++++++++++++++++++++++++++++++-----------------------
 1 files changed, 123 insertions(+), 94 deletions(-)

diff --git a/LifePayment/LifePayment.Application/LifePay/LifePayService.cs b/LifePayment/LifePayment.Application/LifePay/LifePayService.cs
index 2162785..dcbc06f 100644
--- a/LifePayment/LifePayment.Application/LifePay/LifePayService.cs
+++ b/LifePayment/LifePayment.Application/LifePay/LifePayService.cs
@@ -1,5 +1,6 @@
 锘縰sing Alipay.EasySDK.Payment.Common.Models;
 using LifePayment.Application.Contracts;
+using LifePayment.Application.LifePay;
 using LifePayment.Domain;
 using LifePayment.Domain.LifePay;
 using LifePayment.Domain.Models;
@@ -23,6 +24,7 @@
 using Volo.Abp.EventBus.Distributed;
 using Z.EntityFramework.Plus;
 using ZeroD.Util;
+using ZeroD.Util.Fadd;
 using static LifePayment.Domain.Shared.LifePaymentConstant;
 
 namespace LifePayment.Application;
@@ -49,6 +51,7 @@
     private readonly IAliPayApi _aliPayApi;
     private readonly IAlipayInterfaceManager _alipayInterfaceManager;
     private readonly IWxPayApi _wxPayApi;
+    private readonly ILifePayRateService _lifePayRateService;
     private readonly WxPayOption _wxPayOptions;
     private readonly InitSetting _initSettingOptions;
 
@@ -76,6 +79,7 @@
                           IRepository<LifePayAccount, Guid> lifePayAccount,
                           IDataFilter dataFilter,
                           IChannelFilter channelFilter,
+                          ILifePayRateService lifePayRateService,
                           IAbpDistributedLock distributedLock)
     {
         _logger = logger;
@@ -100,6 +104,7 @@
         _channelFilter = channelFilter;
         this.distributedLock = distributedLock;
         _operateHistory = operateHistory;
+        _lifePayRateService = lifePayRateService;
     }
 
     #region 鏌ヨ
@@ -511,7 +516,7 @@
     public async Task<LifePayRefundOrderOutput> GetLifePayRefundOrderDetail(string orderNo)
     {
         var order = await _lifePayOrderRepository.Where(x => x.OrderNo == orderNo).FirstOrDefaultAsync();
-        var platformRate = await _lifePayRateRepository.FirstOrDefaultAsync(r => r.RateType == LifePayRateTypeEnum.渚涘簲鍟嗘姌鎵d环);
+        //var platformRate = await _lifePayRateRepository.FirstOrDefaultAsync(r => r.RateType == LifePayRateTypeEnum.渚涘簲鍟嗘姌鎵d环);
         var channle = await _lifePayChannlesRep.FirstOrDefaultAsync(r => r.ChannlesNum == order.ChannelId);
         var premium = await _lifePayPremiumRepository.Where(x => x.PremiumType == order.LifePayType).FirstOrDefaultAsync();
         CheckExtensions.IfTrueThrowUserFriendlyException(order == null, "璁㈠崟涓嶅瓨鍦�");
@@ -1085,11 +1090,14 @@
         && x.OrderParamDetailJsonStr.Contains(input.ProductData.Phone)).AnyAsync();
         CheckExtensions.IfTrueThrowUserFriendlyException(repeatOrder, "鎮ㄦ湁鍚屾埛鍙疯鍗曟鍦ㄥ厖鍊间腑锛岃鍕块噸澶嶅厖鍊�");
 
-        var rate = await GetLifePayRate(channle, LifePayRateTypeEnum.榛樿璇濊垂鎶樻墸);
+        var rateChannel = await GetLifePayRate(channle,input.ProductData.IspCode);
+        //骞冲彴鎶樻墸
+        var rate = rateChannel.Rate;
 
         var amount = CalculateAmount(input.ProductData.ParValue, rate);
 
-        var platformRate = await _lifePayRateRepository.FirstOrDefaultAsync(r => r.RateType == LifePayRateTypeEnum.渚涘簲鍟嗘姌鎵d环);
+        //渚涘簲鍟嗘姌鎵d环
+        var platformRate = rateChannel.SupplierRate;
 
         var orderInput = new CreateLifePayOrderInput
         {
@@ -1105,8 +1113,8 @@
             DiscountAmount = amount.DiscountAmount,
             RechargeAmount = amount.RechargeAmount,
             ChannelId = channle.ChannlesNum,
-            PlatformRate = platformRate.Rate,
-            PlatformDeductionAmount = amount.RechargeAmount * platformRate.Rate / 100,
+            PlatformRate = platformRate,
+            PlatformDeductionAmount = amount.RechargeAmount * platformRate / 100,
             ChannleRate = rate,
             ChannlesRakeRate = channle.ChannlesRakeRate,
         };
@@ -1139,10 +1147,13 @@
          && x.OrderParamDetailJsonStr.Contains(input.ProductData.ElectricAccount)).AnyAsync();
         CheckExtensions.IfTrueThrowUserFriendlyException(repeatOrder, "鎮ㄦ湁鍚屾埛鍙疯鍗曟鍦ㄥ厖鍊间腑锛岃鍕块噸澶嶅厖鍊�");
 
+        var rateChannel = await GetLifePayRate(channle, input.ProductData.ElectricType);
+        //骞冲彴鎶樻墸
+        var rate = rateChannel.Rate;
 
-        var rate = await GetLifePayRate(channle, LifePayRateTypeEnum.榛樿鐢佃垂鎶樻墸);
         var amount = CalculateAmount(input.ProductData.ParValue, rate);
-        var platformRate = await _lifePayRateRepository.FirstOrDefaultAsync(r => r.RateType == LifePayRateTypeEnum.渚涘簲鍟嗘姌鎵d环);
+        //渚涘簲鍟嗘姌鎵d环
+        var platformRate = rateChannel.SupplierRate;
 
         var orderInput = new CreateLifePayOrderInput
         {
@@ -1157,8 +1168,8 @@
             DiscountAmount = amount.DiscountAmount,
             RechargeAmount = amount.RechargeAmount,
             ChannelId = channle.ChannlesNum,
-            PlatformRate = platformRate.Rate,
-            PlatformDeductionAmount = amount.RechargeAmount * platformRate.Rate / 100,
+            PlatformRate = platformRate,
+            PlatformDeductionAmount = amount.RechargeAmount * platformRate / 100,
             ChannleRate = rate,
             ChannlesRakeRate = channle.ChannlesRakeRate,
         };
@@ -1191,12 +1202,14 @@
         && x.OrderParamDetailJsonStr.Contains(input.ProductData.GasAccount)).AnyAsync();
         CheckExtensions.IfTrueThrowUserFriendlyException(repeatOrder, "鎮ㄦ湁鍚屾埛鍙疯鍗曟鍦ㄥ厖鍊间腑锛岃鍕块噸澶嶅厖鍊�");
 
-
-        var rate = await GetLifePayRate(channle, LifePayRateTypeEnum.榛樿鐕冩皵鎶樻墸);
+        var rateChannel = await GetLifePayRate(channle, input.ProductData.GasOrgType);
+        //骞冲彴鎶樻墸
+        var rate = rateChannel.Rate;
 
         var amount = CalculateAmount(input.ProductData.ParValue, rate);
 
-        var platformRate = await _lifePayRateRepository.FirstOrDefaultAsync(r => r.RateType == LifePayRateTypeEnum.渚涘簲鍟嗘姌鎵d环);
+        //渚涘簲鍟嗘姌鎵d环
+        var platformRate = rateChannel.SupplierRate;
 
         var orderInput = new CreateLifePayOrderInput
         {
@@ -1212,8 +1225,8 @@
             DiscountAmount = amount.DiscountAmount,
             RechargeAmount = amount.RechargeAmount,
             ChannelId = channle.ChannlesNum,
-            PlatformRate = platformRate.Rate,
-            PlatformDeductionAmount = amount.RechargeAmount * platformRate.Rate / 100,
+            PlatformRate = platformRate,
+            PlatformDeductionAmount = amount.RechargeAmount * platformRate / 100,
             ChannleRate = rate,
             ChannlesRakeRate = channle.ChannlesRakeRate,
         };
@@ -1483,7 +1496,6 @@
             order.OutRequestNo = result.RequestNo.IsNullOrEmpty() ? null : result.RequestNo;
             order.ACOOLYOrderNo = result.ACOOLYOrderNo;
             order.ACOOLYStatus = ACOOLYStatusEnum.鍏呭�间腑;
-            await _lifePayOrderRepository.UpdateAsync(order);
             _logger.LogInformation("鐢熸椿缂磋垂璁㈠崟锛�" + order.ToJson());
 
             /// 鍒涘缓鐢熸椿缂磋垂娑堣垂璁板綍
@@ -1498,9 +1510,9 @@
             order.LifePayRefundStatus = LifePayRefundStatusEnum.寰呴��娆�;
             order.ACOOLYStatus = ACOOLYStatusEnum.鍏呭�煎け璐�;
             order.RefundApplyRemark = ex.Message;
-            await _lifePayOrderRepository.UpdateAsync(order);
-            _logger.LogError("鐢熸椿缂磋垂璁㈠崟鐘舵�侊細" + order.LifePayOrderStatus.ToString());
         }
+        _logger.LogError("鐢熸椿缂磋垂璁㈠崟鐘舵�侊細" + order.LifePayOrderStatus.ToString());
+        await _lifePayOrderRepository.UpdateAsync(order);
     }
 
     public async Task LifePayRefundsHandler(string orderNo, LifePayRefundStatusEnum refundStatus)
@@ -1531,83 +1543,93 @@
     /// <returns></returns>
     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)
+        _logger.LogInformation($"閿侊細LockKey:UpdateOrder:{orderNo} - {orderLock != null}");
+        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);
-        }
-
-        await _lifePayOrderRepository.UpdateAsync(order);
-
-        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;
             }
-        }
 
-        /// 鍒涘缓鐢熸椿缂磋垂娑堣垂璁板綍
-        await _lifePayOrderService.CreatLifePayConsumption(acoolyStatus, order.OrderNo, order.ACOOLYOrderNo,
-                    order.PlatformDeductionAmount ?? 0, order.ChannelId, order.CreationTime, order.FinishTime, order.ChannleRate, parValue, actualParValue);
+            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.SaveChangesAsync();
+
+            _logger.LogInformation("浜嬪姟瀹屾垚");
+        }
+        catch (Exception ex)
+        {
+            await CurrentUnitOfWork.RollbackAsync();
+            _logger.LogError($"渚涘簲鍟嗗洖璋冨紓甯革細{ex.Message}", ex);
+        }
     }
 
     /// <summary>
@@ -1907,6 +1929,7 @@
                 dto.PromoterId = promoter.Id;
             }
 
+            //TODO 鍚庣画闇�瑕佷慨鏀� 鐜板湪鐨勪緵搴斿晢鎶樻墸浠锋牴鎹繍钀ュ晢涓嶅悓 閰嶇疆涓嶅悓 涓嶆槸缁熶竴閰嶇疆浜�
             var rate = await _lifePayRateRepository.FirstOrDefaultAsync(it => it.RateType == LifePayRateTypeEnum.渚涘簲鍟嗘姌鎵d环);
             CheckExtensions.IfTrueThrowUserFriendlyException(rate != null && input.ChannlesRate < rate.Rate, "娓犻亾鎶樻墸鏃犳硶浣庝簬渚涘簲鍟嗘姌鎵�");
 
@@ -2351,7 +2374,7 @@
                                          .WhereIf(input.ACOOLYStatus.HasValue, x => x.ACOOLYStatus == input.ACOOLYStatus.Value)
                                          .WhereIf(input.LifePayOrderType.HasValue, x => x.LifePayOrderType == input.LifePayOrderType.Value)
                                          .WhereIf(input.UserId.HasValue, x => x.UserId == input.UserId.Value)
-                                         .WhereIf(input.KeyWords.IsNotNullOrEmpty(), x => x.PhoneNumber.Contains(input.KeyWords) || x.OrderNo.Contains(input.KeyWords) || x.OutOrderNo.Contains(input.KeyWords) || x.ACOOLYOrderNo.Contains(input.KeyWords))
+                                         .WhereIf(input.KeyWords.IsNotNullOrEmpty(), x => x.PhoneNumber.Contains(input.KeyWords) || x.RefundOrderNo.Contains(input.KeyWords) || x.OrderNo.Contains(input.KeyWords) || x.OutOrderNo.Contains(input.KeyWords) || x.ACOOLYOrderNo.Contains(input.KeyWords))
                       join b in _lifePayChannlesRep on a.ChannelId equals b.ChannlesNum into temp
                       from b in temp.DefaultIfEmpty()
                       select new LifePayOrderListOutput
@@ -2439,18 +2462,24 @@
         };
     }
 
-    public async Task<decimal> GetLifePayRate(CreateEditPayChannelsInput channel, LifePayRateTypeEnum lifePayRateType)
+    public async Task<GetLifePayRateOutput> GetLifePayRate(CreateEditPayChannelsInput channel, string code)
     {
+        var rateChannel = await _lifePayRateService.GetRateChannelByCode(code);
+            CheckExtensions.IfTrueThrowUserFriendlyException(rateChannel.Id.IsEmpty(), "鏈厤缃姌鎵�");
         if (channel == null)
         {
-            var rate = await GetRate();
-            CheckExtensions.IfTrueThrowUserFriendlyException(rate.IsNullOrEmpty(), "鏈厤缃姌鎵�");
-            var result = rate.FirstOrDefault(x => x.RateType == lifePayRateType).Rate;
-            return result;
+            return new GetLifePayRateOutput
+            {
+                Rate = rateChannel.PlatformRate,
+                SupplierRate = rateChannel.SupplierRate
+            };
         }
         else
         {
-            return channel.ChannlesRate;
+            return new GetLifePayRateOutput {
+                Rate= channel.ChannlesRate,
+                SupplierRate = rateChannel.SupplierRate
+            };
         }
     }
 

--
Gitblit v1.9.1