sunpengfei
2025-06-09 7a89586d7754d5ff1c38cbfdbda6c357507f5620
LifePayment/LifePayment.Application/LifePay/LifePayService.cs
@@ -1,4 +1,5 @@
using LifePayment.Application.Contracts;
using Alipay.EasySDK.Payment.Common.Models;
using LifePayment.Application.Contracts;
using LifePayment.Domain;
using LifePayment.Domain.LifePay;
using LifePayment.Domain.Models;
@@ -9,6 +10,7 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
@@ -19,18 +21,7 @@
using Volo.Abp.EventBus.Distributed;
using Z.EntityFramework.Plus;
using ZeroD.Util;
using Alipay.EasySDK.Payment.Common.Models;
using static LifePayment.Domain.Shared.LifePaymentConstant;
using ZeroD.Util.Fadd;
using Nest;
using Volo.Abp.Domain.Entities;
using Volo.Abp.ObjectMapping;
using NPOI.SS.Formula.Functions;
using StackExchange.Redis;
using static Volo.Abp.Identity.Settings.IdentitySettingNames;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
using LifePayment.Application.LifePay;
using Alipay.AopSdk.Core.Domain;
namespace LifePayment.Application;
@@ -59,10 +50,12 @@
    private readonly InitSetting _initSettingOptions;
    private readonly IACOOLYManager _aCOOLYManager;
    private readonly IRepository<LifePayPromoter, Guid> lifePayPromoterRepository;
    public LifePayService(IDistributedEventBus distributedEventBus,
                          ILogger<LifePayService> logger,
                          IACOOLYManager aCOOLYManager,
                          IRepository<LifePayPromoter, Guid> lifePayPromoterRepository,
                          IRepository<LifePayRate, Guid> lifePayRateRepository,
                          IRepository<LifePayOrder, Guid> lifePayOrderRepository,
                          IRepository<LifePayUser, Guid> lifePayUserRepository,
@@ -83,6 +76,7 @@
    {
        _logger = logger;
        _aCOOLYManager = aCOOLYManager;
        this.lifePayPromoterRepository = lifePayPromoterRepository;
        _lifePayRateRepository = lifePayRateRepository;
        _lifePayOrderRepository = lifePayOrderRepository;
        _lifePayUserRepository = lifePayUserRepository;
@@ -280,16 +274,16 @@
                                DiscountAmount = a.DiscountAmount ?? 0,
                                FinishTime = a.FinishTime,
                                Id = a.Id,
                                LifePayOrderStatus = a.LifePayOrderStatus?? 0,
                                LifePayOrderStatus = a.LifePayOrderStatus ?? 0,
                                LifePayOrderType = a.LifePayOrderType,
                                LifePayType = a.LifePayType,
                                OrderNo = a.OrderNo,
                                PayAmount = a.PayAmount ?? 0,
                                PhoneNumber = a.PhoneNumber,
                                RechargeAmount = a.RechargeAmount??0,
                                RechargeAmount = a.RechargeAmount ?? 0,
                                UserId = a.UserId,
                                OutOrderNo = a.OutOrderNo,
                                PayStatus = a.PayStatus?? 0,
                                PayStatus = a.PayStatus ?? 0,
                                PayTime = a.PayTime,
                                ACOOLYOrderNo = a.ACOOLYOrderNo,
                                RefundCredentialsImgUrl = a.RefundCredentialsImgUrl.GetOssPath(),
@@ -300,6 +294,7 @@
                                RefundTime = a.RefundTime,
                                ChannelName = b.ChannlesName,
                                ActualRechargeAmount = a.RechargeAmount,
                                ActualReceivedAmount = a.ActualReceivedAmount,
                                PlatformDeductionAmount = a.PlatformDeductionAmount,
                                ACOOLYStatus = a.ACOOLYStatus,
                                LifePayRefundStatus = a.LifePayRefundStatus,
@@ -358,6 +353,7 @@
                                RefundPrice = a.RefundPrice,
                                ChannelName = b.ChannlesName,
                                ActualRechargeAmount = a.RechargeAmount,
                                ActualReceivedAmount = a.ActualReceivedAmount,
                                PlatformDeductionAmount = a.PlatformDeductionAmount,
                                ACOOLYStatus = a.ACOOLYStatus,
                                LifePayRefundStatus = a.LifePayRefundStatus,
@@ -388,8 +384,9 @@
            ACOOLYStatus = x.ACOOLYStatus,
            RefundApplyRemark = x.RefundApplyRemark,
            RechargeAmount = x.RechargeAmount,
            ActualReceivedAmount = x.ActualReceivedAmount.ToString("F2"),
            ActualReceivedAmount = (x.ActualReceivedAmount ?? 0).ToString("F2"),
            LifePayRefundStatus = x.LifePayRefundStatus,
            OrderNo = x.OrderNo,
        }).OrderByDescending(r => r.CreationTime).ToListAsync();
        var i = 0;
@@ -457,6 +454,7 @@
            PremiumPrice = orderpirce.PremiumPrice,
            Profit = orderpirce.Profit,
            RefundOrderNo = order.RefundOrderNo,
            ActualReceivedAmount = order.ActualReceivedAmount
        };
        return result;
@@ -566,7 +564,7 @@
                                                        DiscountAmount = x.DiscountAmount ?? 0,
                                                        FinishTime = x.FinishTime,
                                                        Id = x.Id,
                                                        LifePayOrderStatus = x.LifePayOrderStatus?? 0 ,
                                                        LifePayOrderStatus = x.LifePayOrderStatus ?? 0,
                                                        LifePayOrderType = x.LifePayOrderType,
                                                        LifePayType = x.LifePayType,
                                                        OrderNo = x.OrderNo,
@@ -609,7 +607,7 @@
    public async Task<List<CreateEditPayChannelsInput>> GetLifePayChannlesAllList(QueryLifePayChannlesInput input)
    {
        return await GetLifePayChannlesListFilter().WhereIf(input.Status.HasValue,x => x.Status == input.Status).ToListAsync();
        return await GetLifePayChannlesListFilter().WhereIf(input.Status.HasValue, x => x.Status == input.Status).ToListAsync();
    }
    public async Task<CreateEditPayChannelsInput> GetLifePayChannlesDto(Guid id)
@@ -907,7 +905,7 @@
        return await _wxPayApi.WxPayTradeQuery(outTradeNo);
    }
    #endregion
@@ -929,21 +927,17 @@
        var channle = await GetLifePayChannlesDtoByNum(input.ChannelId);
        CheckExtensions.IfTrueThrowUserFriendlyException(channle == null, "渠道不存在");
        var repeatOrder = await _lifePayOrderRepository.Where(x => x.LifePayOrderType == LifePayOrderTypeEnum.PhoneOrder
        var repeatOrder = await _lifePayOrderRepository.Where(x => x.LifePayOrderType == LifePayOrderTypeEnum.PhoneOrder
        && x.PayStatus == LifePayStatusEnum.已支付
        && x.LifePayOrderStatus == LifePayOrderStatusEnum.充值中
        && x.OrderParamDetailJsonStr.Contains(input.ProductData.Phone)).AnyAsync();
         CheckExtensions.IfTrueThrowUserFriendlyException(repeatOrder, "您有同户号订单正在充值中,请勿重复充值");
        //var rate = await GetRate();
        //CheckExtensions.IfTrueThrowUserFriendlyException(rate.IsNullOrEmpty(), "未配置折扣");
        CheckExtensions.IfTrueThrowUserFriendlyException(repeatOrder, "您有同户号订单正在充值中,请勿重复充值");
        var rate = await GetLifePayRate(channle, LifePayRateTypeEnum.默认话费折扣);
        var amount = CalculateAmount(input.ProductData.ParValue, rate);
        var platformRate = await _lifePayRateRepository.FirstOrDefaultAsync(r => r.RateType == LifePayRateTypeEnum.供应商折扣价);
        var orderInput = new CreateLifePayOrderInput
        {
@@ -963,7 +957,6 @@
            PlatformDeductionAmount = amount.RechargeAmount * platformRate.Rate / 100,
            ChannleRate = rate,
            ChannlesRakeRate = channle.ChannlesRakeRate,
            //ChannlesRakePrice = amount.RechargeAmount * (channle.ChannlesRate - platformRate.Rate) / 100 * channle.ChannlesRakeRate / 100
        };
        await CreateLifePayOrder(orderInput);
@@ -989,8 +982,6 @@
        CheckExtensions.IfTrueThrowUserFriendlyException(channle == null, "渠道不存在");
        CheckExtensions.IfTrueThrowUserFriendlyException(channle.Status == LifePayChannelsStatsEnum.禁用, "渠道已被禁用");
        //var rate = await GetRate();
        //CheckExtensions.IfTrueThrowUserFriendlyException(rate.IsNullOrEmpty(), "未配置折扣");
        var repeatOrder = await _lifePayOrderRepository.Where(x => x.LifePayOrderType == LifePayOrderTypeEnum.ElectricOrder && x.PayStatus == LifePayStatusEnum.已支付
         && x.LifePayOrderStatus == LifePayOrderStatusEnum.充值中 && x.OrderParamDetailJsonStr.Contains(input.ProductData.ElectricType)
         && x.OrderParamDetailJsonStr.Contains(input.ProductData.ElectricAccount)).AnyAsync();
@@ -1006,7 +997,6 @@
            OrderNo = channle.ChannlesNum + CreateOrderNo(),
            LifePayOrderStatus = LifePayOrderStatusEnum.充值中,
            LifePayOrderType = LifePayOrderTypeEnum.ElectricOrder,
            // LifePayType = input.LifePayType,
            OrderParamDetailJsonStr = JsonConvert.SerializeObject(input.ProductData),
            UserId = user.Id,
            PayStatus = LifePayStatusEnum.未支付,
@@ -1016,6 +1006,7 @@
            RechargeAmount = amount.RechargeAmount,
            ChannelId = channle.ChannlesNum,
            PlatformRate = platformRate.Rate,
            PlatformDeductionAmount = amount.RechargeAmount * platformRate.Rate / 100,
            ChannleRate = rate,
            ChannlesRakeRate = channle.ChannlesRakeRate,
        };
@@ -1043,8 +1034,6 @@
        CheckExtensions.IfTrueThrowUserFriendlyException(channle == null, "渠道不存在");
        CheckExtensions.IfTrueThrowUserFriendlyException(channle.Status == LifePayChannelsStatsEnum.禁用, "渠道已被禁用");
        //var rate = await GetRate();
        //CheckExtensions.IfTrueThrowUserFriendlyException(rate.IsNullOrEmpty(), "未配置折扣");
        var repeatOrder = await _lifePayOrderRepository.Where(x => x.LifePayOrderType == LifePayOrderTypeEnum.GasOrder && x.PayStatus == LifePayStatusEnum.已支付
        && x.LifePayOrderStatus == LifePayOrderStatusEnum.充值中 && x.OrderParamDetailJsonStr.Contains(input.ProductData.GasOrgType)
        && x.OrderParamDetailJsonStr.Contains(input.ProductData.GasAccount)).AnyAsync();
@@ -1072,6 +1061,7 @@
            RechargeAmount = amount.RechargeAmount,
            ChannelId = channle.ChannlesNum,
            PlatformRate = platformRate.Rate,
            PlatformDeductionAmount = amount.RechargeAmount * platformRate.Rate / 100,
            ChannleRate = rate,
            ChannlesRakeRate = channle.ChannlesRakeRate,
        };
@@ -1383,17 +1373,18 @@
    /// <param name="orderNo"></param>
    /// <param name="outOrderNo"></param>
    /// <returns></returns>
    public async Task ACOOLYOrderNotifyHandler(string orderNo, string acoolyOrderNo, LifePayOrderStatusEnum status, ACOOLYStatusEnum acoolyStatus, decimal payAmount, string refundApplyRemark)
    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, "订单不存在");
        if (order.LifePayOrderStatus >= LifePayOrderStatusEnum.已完成)
        {
            return;
        }
        //if (order.LifePayOrderStatus == LifePayOrderStatusEnum.已完成)
        //{
        //    return;
        //}
        order.PlatformDeductionAmount = payAmount;
        order.ActualReceivedAmount = actualParValue;
        if (acoolyOrderNo.IsNotNullOrEmpty())
        {
            order.ACOOLYOrderNo = acoolyOrderNo;
@@ -1411,7 +1402,14 @@
        order.LifePayOrderStatus = status;
        order.ACOOLYStatus = acoolyStatus;
        order.FinishTime = DateTime.Now;
        if (order.LifePayOrderStatus == LifePayOrderStatusEnum.已完成
            || order.LifePayOrderStatus == LifePayOrderStatusEnum.已退款)
        {
            order.FinishTime = DateTime.Now;
            order.PlatformDeductionAmount = Math.Round((order.ActualReceivedAmount ?? 0) * (order.PlatformRate ?? 0) / 100, 2);
            order.RefundPrice = Math.Round((1 - ((order.ActualReceivedAmount ?? 0) / (order.RechargeAmount ?? 0))) * (order.PayAmount ?? 0), 2);
        }
        await _lifePayOrderRepository.UpdateAsync(order);
        if (order.LifePayOrderStatus == LifePayOrderStatusEnum.已完成)
@@ -1438,7 +1436,7 @@
        /// 创建生活缴费消费记录
        await _lifePayOrderService.CreatLifePayConsumption(acoolyStatus, order.OrderNo, order.ACOOLYOrderNo,
                    order.PlatformDeductionAmount ?? 0, order.ChannelId, order.CreationTime, order.FinishTime);
                    order.PlatformDeductionAmount ?? 0, order.ChannelId, order.CreationTime, order.FinishTime, order.ChannleRate, parValue, actualParValue);
    }
    /// <summary>
@@ -1519,6 +1517,8 @@
        order.RefundTime = DateTime.Now;
        order.RefundCheckUserId = CurrentUser.Id;
        order.RefundPrice = input.RefundPrice;
        order.ActualReceivedAmount = Math.Round((1 - ((order.RefundPrice ?? 0) / (order.PayAmount ?? 0))) * (order.RechargeAmount ?? 0), 2);
        order.PlatformDeductionAmount = Math.Round((order.ActualReceivedAmount ?? 0) * (order.PlatformRate ?? 0) / 100, 2);
        //await _lifePayOrderRepository.UpdateAsync(order);
@@ -1699,6 +1699,10 @@
            {
                order.LifePayOrderStatus = LifePayOrderStatusEnum.已完成;
            }
            else
            {
                order.LifePayOrderStatus = LifePayOrderStatusEnum.退款失败;
            }
            order.RefundCheckRemark = input.RefundCheckRemark;
            order.RefundCheckUserId = CurrentUser.Id;
@@ -1724,13 +1728,22 @@
        {
            var dto = await _lifePayChannlesRep.FirstOrDefaultAsync(r => r.Id == input.Id.Value);
            CheckExtensions.IfTrueThrowUserFriendlyException(dto == null, "未获取到渠道折扣数据");
            if (!string.IsNullOrWhiteSpace(input.PromoterIdNumber) || !string.IsNullOrWhiteSpace(input.PromoterName))
            {
                var promoter = await lifePayPromoterRepository.FirstOrDefaultAsync(it => it.IdNumber == input.PromoterIdNumber && it.Name == input.PromoterName);
                CheckExtensions.IfTrueThrowUserFriendlyException(promoter == null, "该推广人不存在");
                dto.PromoterId = promoter.Id;
            }
            dto.ChannlesName = input.ChannlesName;
            dto.ChannlesNum = input.ChannlesNum;
            dto.ChannlesRate = input.ChannlesRate;
            dto.ChannlesRakeRate = input.ChannlesRakeRate;
            dto.SwitchType = input.SwitchType;
            dto.ChannlesType = input.ChannlesType;
            dto.Status = input.Status;
            dto.AgentType = input.AgentType;
            dto.AreaProvinceId = input.AreaProvinceId;
            dto.AreaCityId = input.AreaCityId;
            #region 记录日志
            await LifePayOrderHistory("渠道管理", "编辑", input.Id.Value, TableType.LifePayChannles);
@@ -1742,6 +1755,13 @@
            CheckExtensions.IfTrueThrowUserFriendlyException(input.ChannlesRate < 0.01m, "渠道折扣设置错误");
            input.Id = Guid.NewGuid();
            var entity = ObjectMapper.Map<CreateEditPayChannelsInput, LifePayChannles>(input);
            if (!string.IsNullOrWhiteSpace(input.PromoterIdNumber) || !string.IsNullOrWhiteSpace(input.PromoterName))
            {
                var promoter = await lifePayPromoterRepository.FirstOrDefaultAsync(it => it.IdNumber == input.PromoterIdNumber && it.Name == input.PromoterName);
                CheckExtensions.IfTrueThrowUserFriendlyException(promoter == null, "该推广人不存在");
                entity.PromoterId = promoter.Id;
            }
            await _lifePayChannlesRep.InsertAsync(entity);
            #region 记录日志
@@ -2191,20 +2211,28 @@
                      });
        return result;
    }
    private IQueryable<CreateEditPayChannelsInput> GetLifePayChannlesListFilter()
    {
        return _lifePayChannlesRep.Select(x =>
                    new CreateEditPayChannelsInput
                    {
                        Id = x.Id,
                        ChannlesRate = x.ChannlesRate,
                        ChannlesRakeRate = x.ChannlesRakeRate,
                        ChannlesName = x.ChannlesName,
                        ChannlesNum = x.ChannlesNum,
                        Status = x.Status,
                        SwitchType = x.SwitchType,
                        ChannlesType = x.ChannlesType,
                    });
        return from x in _lifePayChannlesRep
               join p in lifePayPromoterRepository on x.PromoterId equals p.Id into pg
               from pgd in pg.DefaultIfEmpty()
               select new CreateEditPayChannelsInput
               {
                   Id = x.Id,
                   ChannlesRate = x.ChannlesRate,
                   ChannlesRakeRate = x.ChannlesRakeRate,
                   ChannlesName = x.ChannlesName,
                   ChannlesNum = x.ChannlesNum,
                   Status = x.Status,
                   SwitchType = x.SwitchType,
                   ChannlesType = x.ChannlesType,
                   AgentType = x.AgentType,
                   AreaProvinceId = x.AreaProvinceId,
                   AreaCityId = x.AreaCityId,
                   PromoterIdNumber = pgd.IdNumber,
                   PromoterName = pgd.Name,
               };
    }
    public async Task<OrderPriceReturn> GetOrderPrice(decimal price, decimal priceAmount, decimal platformRate, decimal channleRate,
@@ -2276,12 +2304,12 @@
            default:
                break;
        }
        return new SetPayTypeReturn()
        {
            Desc = desc,
            OrderNo = order.OrderNo,
            PayAmount = order.PayAmount.HasValue? order.PayAmount.Value : 0,
            PayAmount = order.PayAmount.HasValue ? order.PayAmount.Value : 0,
            LifePayType = lifePayType,
        };
    }