lingling
2025-03-19 74f5570dceeb1a5a88c0a685705a9e4cc88881ce
LifePayment/LifePayment.Application/LifePay/LifePayService.cs
@@ -1,33 +1,25 @@
using DynamicQuery.Net.Dto.Input;
using LifePayment.Application.Contracts;
using LifePayment.Application.Contracts;
using LifePayment.Domain;
using LifePayment.Domain.LifePay;
using LifePayment.Domain.Models;
using LifePayment.Domain.Shared;
using MailKit.Search;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Nest;
using Newtonsoft.Json;
using NPOI.OpenXmlFormats.Dml.Chart;
using NPOI.SS.Formula.Functions;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Data;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.EventBus.Distributed;
using ZeroD.Util;
using ZeroD.Util.Fadd;
using static LifePayment.Domain.Shared.LifePaymentConstant;
using static IdentityServer4.Models.IdentityResources;
using Volo.Abp.Data;
using Z.EntityFramework.Plus;
using ZeroD.Util;
using static LifePayment.Domain.Shared.LifePaymentConstant;
namespace LifePayment.Application;
@@ -37,12 +29,14 @@
    private readonly ILogger<LifePayService> _logger;
    private readonly IRepository<LifePayRate, Guid> _lifePayRateRepository;
    private readonly IRepository<LifePayPremium, Guid> _lifePayPremiumRepository;
    private readonly IRepository<LifePayOrder, Guid> _lifePayOrderRepository;
    private readonly IRepository<LifePayUser, Guid> _lifePayUserRepository;
    private readonly IRepository<LifePayIntroInfo, Guid> _lifePayIntroInfoRepository;
    private readonly IRepository<LifePayChannles, Guid> _lifePayChannlesRep;
    private readonly IRepository<LifePayAccount, Guid> _lifePayAccount;
    private readonly IDataFilter dataFilter;
    private readonly IChannelFilter _channelFilter;
    private readonly IAliPayApi _aliPayApi;
    private readonly IWxPayApi _wxPayApi;
    private readonly WxPayOption _wxPayOptions;
@@ -55,18 +49,23 @@
                          IRepository<LifePayRate, Guid> lifePayRateRepository,
                          IRepository<LifePayOrder, Guid> lifePayOrderRepository,
                          IRepository<LifePayUser, Guid> lifePayUserRepository,
                          IRepository<LifePayPremium, Guid> lifePayPremiumRepository,
                          IRepository<LifePayIntroInfo, Guid> lifePayIntroInfoRepository,
                          IAliPayApi aliPayApi,
                          IWxPayApi wxPayApi,
                          IOptions<WxPayOption> wxPayOptions,
                          IRepository<LifePayChannles, Guid> lifePayChannlesRep,
                          IRepository<LifePayAccount, Guid> lifePayAccount,
                          IDataFilter dataFilter)
                          IDataFilter dataFilter,
                          IChannelFilter channelFilter)
    {
        _logger = logger;
        _aCOOLYManager = aCOOLYManager;
        _lifePayRateRepository = lifePayRateRepository;
        _lifePayOrderRepository = lifePayOrderRepository;
        _lifePayUserRepository = lifePayUserRepository;
        _lifePayPremiumRepository = lifePayPremiumRepository;
        _lifePayIntroInfoRepository = lifePayIntroInfoRepository;
        _aliPayApi = aliPayApi;
        _wxPayApi = wxPayApi;
        _wxPayOptions = wxPayOptions.Value;
@@ -74,6 +73,7 @@
        _lifePayChannlesRep = lifePayChannlesRep;
        _lifePayAccount = lifePayAccount;
        this.dataFilter = dataFilter;
        _channelFilter = channelFilter;
    }
    #region 查询
@@ -137,17 +137,44 @@
    }
    /// <summary>
    /// 获取手续费费率
    /// </summary>
    /// <returns></returns>
    public async Task<List<LifePayPremiumListOutput>> GetPremium()
    {
        return await _lifePayPremiumRepository.Where(x => x.IsDeleted == false).Select(x => new LifePayPremiumListOutput() { Id = x.Id, Rate = x.Rate, PremiumType = x.PremiumType })
                                           .ToListAsync();
    }
    /// <summary>
    /// 须知配置
    /// </summary>
    /// <returns></returns>
    public async Task<List<LifePayIntroInfoOutput>> GetIntroInfo(LifePayOrderTypeEnum lifePayType)
    {
        return await _lifePayIntroInfoRepository.Where(x => x.IsDeleted == false && x.LifePayType == lifePayType).OrderBy(x => x.Sequence)
            .Select(x => new LifePayIntroInfoOutput() { Type = x.Type, ContentSummary = x.ContentSummary, Content = x.Content, LifePayType = x.LifePayType, Path = x.Path, Sequence = x.Sequence })
                                           .ToListAsync();
    }
    /// <summary>
    /// 获取用户分页数据
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<PageOutput<UserListOutput>> GetUserPage(PageInput input)
    public async Task<PageOutput<UserListOutput>> GetUserPage(QueryUserPageInput input)
    {
        return await _lifePayUserRepository.Select(x =>
        return await _channelFilter.GetChannelLifePayUserFilter(_lifePayUserRepository).Where(x => x.IsDeleted == false)
            .WhereIf(!string.IsNullOrEmpty(input.QueryCondition), x => x.PhoneNumber.Contains(input.QueryCondition) || x.Name.Contains(input.QueryCondition))
            .WhereIf(input.CreationTime.HasValue, x => x.CreationTime.ToString("yyyy-MM-dd") == input.CreationTime.Value.ToString("yyyy-MM-dd"))
                                            .Select(x =>
                                                   new UserListOutput()
                                                   {
                                                       Id = x.Id,
                                                       CreationChannle = string.IsNullOrEmpty(x.CreationChannleNum) ? "" : _lifePayChannlesRep.Where(c => c.ChannlesNum == x.CreationChannleNum).FirstOrDefault().ChannlesName,
                                                       LastLoginChannle = string.IsNullOrEmpty(x.LastLoginChannleNum) ? "" : _lifePayChannlesRep.Where(c => c.ChannlesNum == x.LastLoginChannleNum).FirstOrDefault().ChannlesName,
                                                       PhoneNumber = x.PhoneNumber,
                                                       Name = string.IsNullOrEmpty(x.Name) ? "" : x.Name,
                                                       CreationTime = x.CreationTime,
                                                       LastLoginTime = x.LastLoginTime
                                                   })
@@ -182,7 +209,6 @@
                                    })
                                    .GetPageResult(input.PageModel);
        }
    }
    /// <summary>
@@ -192,7 +218,8 @@
    /// <returns></returns>
    public async Task<PageOutput<LifePayOrderListOutput>> GetLifePayOrderPage(QueryLifePayOrderListInput input)
    {
        var result = await (from a in _lifePayOrderRepository.Where(x => x.PayStatus != LifePayStatusEnum.未支付)
        var result = await (from a in _channelFilter.GetChannelLifePayOrderFilter(_lifePayOrderRepository)
                      .WhereIf(input.BeginFinishTime.HasValue, x => x.FinishTime >= input.BeginFinishTime)
                                            .WhereIf(input.EndFinishTime.HasValue, x => x.FinishTime <= input.EndFinishTime)
                                            .WhereIf(input.BeginPayTime.HasValue, x => x.PayTime >= input.BeginPayTime)
@@ -203,7 +230,7 @@
                                            .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))
                            join b in _lifePayChannlesRep on a.ChannelId equals b.Id into temp
                            join b in _lifePayChannlesRep on a.ChannelId equals b.ChannlesNum into temp
                            from b in temp.DefaultIfEmpty()
                            select new LifePayOrderListOutput
                            {
@@ -245,7 +272,7 @@
            return new PageOutput<UserLifePayOrderOutput>();
        }
        return await _lifePayOrderRepository.Where(x => x.PayStatus != LifePayStatusEnum.未支付)
        return await _channelFilter.GetChannelLifePayOrderFilter(_lifePayOrderRepository).Where(x => x.PayStatus != LifePayStatusEnum.未支付)
                                            .WhereIf(input.BeginFinishTime.HasValue, x => x.FinishTime >= input.BeginFinishTime)
                                            .WhereIf(input.EndFinishTime.HasValue, x => x.FinishTime <= input.EndFinishTime)
                                            .WhereIf(input.BeginPayTime.HasValue, x => x.PayTime >= input.BeginPayTime)
@@ -324,19 +351,12 @@
    public async Task<PageOutput<CreateEditPayChannelsInput>> GetLifePayChannlesPage(PageInput input)
    {
        return await GetLifePayChannlesListFilter().GetPageResult(input.PageModel);
    }
        return await _lifePayChannlesRep.Select(x =>
                                                new CreateEditPayChannelsInput
                                                {
                                                    Id = x.Id,
                                                    ChannlesRate = x.ChannlesRate,
                                                    ChannlesName = x.ChannlesName,
                                                    ChannlesNum = x.ChannlesNum,
                                                    Status = x.Status,
                                                    SwitchType = x.SwitchType,
                                                    ChannlesType = x.ChannlesType,
                                                })
                                            .GetPageResult(input.PageModel);
    public async Task<List<CreateEditPayChannelsInput>> GetLifePayChannlesAllList()
    {
        return await GetLifePayChannlesListFilter().Where(x => x.Status == LifePayChannelsStatsEnum.启用).ToListAsync();
    }
    public async Task<CreateEditPayChannelsInput> GetLifePayChannlesDto(Guid id)
@@ -346,6 +366,7 @@
                                                    {
                                                        Id = x.Id,
                                                        ChannlesRate = x.ChannlesRate,
                                                        ChannlesRakeRate = x.ChannlesRakeRate,
                                                        ChannlesName = x.ChannlesName,
                                                        ChannlesNum = x.ChannlesNum,
                                                        Status = x.Status,
@@ -361,6 +382,7 @@
                                                    {
                                                        Id = x.Id,
                                                        ChannlesRate = x.ChannlesRate,
                                                        ChannlesRakeRate = x.ChannlesRakeRate,
                                                        ChannlesName = x.ChannlesName,
                                                        ChannlesNum = x.ChannlesNum,
                                                        Status = x.Status,
@@ -372,7 +394,7 @@
    public async Task<UserLifePayOrderRefundOutput> GetUserLifePayOrderRefund(Guid id)
    {
        var order = await _lifePayOrderRepository.Where(x => x.Id == id).Select(x =>
        var order = await _channelFilter.GetChannelLifePayOrderFilter(_lifePayOrderRepository).Where(x => x.Id == id).Select(x =>
                                                    new UserLifePayOrderRefundOutput
                                                    {
                                                        Id = x.Id,
@@ -538,7 +560,7 @@
            PayAmount = amount.PayAmont,
            DiscountAmount = amount.DiscountAmount,
            RechargeAmount = amount.RechargeAmount,
            ChannelId = channle.Id
            ChannelId = channle.ChannlesNum
        };
        await CreateLifePayOrder(orderInput);
@@ -582,7 +604,7 @@
            PayAmount = amount.PayAmont,
            DiscountAmount = amount.DiscountAmount,
            RechargeAmount = amount.RechargeAmount,
            ChannelId = channle.Id
            ChannelId = channle.ChannlesNum
        };
        await CreateLifePayOrder(orderInput);
@@ -626,7 +648,7 @@
            PayAmount = amount.PayAmont,
            DiscountAmount = amount.DiscountAmount,
            RechargeAmount = amount.RechargeAmount,
            ChannelId = channle.Id
            ChannelId = channle.ChannlesNum
        };
        await CreateLifePayOrder(orderInput);
@@ -1046,6 +1068,15 @@
        await _lifePayOrderRepository.UpdateAsync(order);
    }
    /// <summary>
    /// 微信退款
    /// </summary>
    /// <param name="outTradeNo"></param>
    /// <param name="reason"></param>
    /// <param name="refund"></param>
    /// <param name="total"></param>
    /// <param name="currency"></param>
    /// <returns></returns>
    public async Task<WxPayDomesticRefundsReponse> WxPayDomesticRefunds(string outTradeNo, string reason, int refund, int total, string currency = "CNY")
    {
        WxPayDomesticRefundsRequest req = new WxPayDomesticRefundsRequest
@@ -1066,8 +1097,13 @@
        return res;
    }
    public async Task<int> AddUpdateUserAccount(AddUpdateUserAccountInput input)
    {
        var user = await _lifePayUserRepository.Where(r => r.Id == input.UserId && !r.IsDeleted).FirstOrDefaultAsync();
        CheckExtensions.IfTrueThrowUserFriendlyException(user == null, "用户不存在");
        if (input.Id.HasValue)
        {
            var userAccount = await _lifePayAccount.Where(x => x.UserId == input.UserId && x.Id == input.Id)
@@ -1104,6 +1140,16 @@
            };
            await _lifePayAccount.InsertAsync(userAccount);
        }
        if (input.LifePayType == LifePayOrderTypeEnum.话费订单)
        {
            var extraProperties = JsonConvert.DeserializeObject<Model_UserAccountExtraProperties>(input.ExtraProperties);
            if (!string.IsNullOrEmpty(extraProperties.Name))
            {
                user.Name = extraProperties.Name;
            }
        }
        return Constant.SUCCESS;
    }
@@ -1172,6 +1218,7 @@
            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;
@@ -1219,6 +1266,7 @@
        CheckExtensions.IfTrueThrowUserFriendlyException(dto.Status == LifePayChannelsStatsEnum.禁用, "当前渠道商已禁用");
        return true;
    }
    public async Task CreateEditLifePayRate(List<LifePayRateInput> input)
    {
        CheckExtensions.IfTrueThrowUserFriendlyException(input.Where(r => r.Rate <= 0).Any(), "折扣不得小于等于0");
@@ -1248,11 +1296,62 @@
        {
            await _lifePayRateRepository.InsertManyAsync(listData);
        }
    }
    public async Task CreateEditLifePayPremium(List<LifePayPremiumInput> input)
    {
        CheckExtensions.IfTrueThrowUserFriendlyException(input.Where(r => r.Rate < 0).Any(), "折扣不得小于0");
        var listData = new List<LifePayPremium>();
        foreach (var item in input)
        {
            var tmpDto = await _lifePayPremiumRepository.FirstOrDefaultAsync(r => r.PremiumType == item.PremiumType);
            if (tmpDto != null)
            {
                tmpDto.Rate = item.Rate;
            }
            else
            {
                listData.Add(new LifePayPremium
                {
                    CreationTime = DateTime.Now,
                    Rate = item.Rate,
                    PremiumType = item.PremiumType,
                    Id = Guid.NewGuid(),
                });
            }
        }
        if (listData.Any())
        {
            await _lifePayPremiumRepository.InsertManyAsync(listData);
        }
    }
    public async Task EditIntroInfo(LifePayIntroInfoInput input)
    {
        var listData = new List<LifePayIntroInfo>();
        foreach (var item in input.Data)
        {
            var tmpDto = await _lifePayIntroInfoRepository.Where(r => r.LifePayType == input.LifePayType).DeleteAsync();
            listData.Add(new LifePayIntroInfo
            {
                LifePayType = input.LifePayType,
                CreationTime = DateTime.Now,
                Type = item.Type,
                ContentSummary = item.ContentSummary,
                Content = item.Content,
                Path = item.Path,
                Sequence = item.Sequence,
                Id = Guid.NewGuid(),
            });
        }
        if (listData.Any())
        {
            await _lifePayIntroInfoRepository.InsertManyAsync(listData);
        }
    }
    #endregion
    #region 私有
@@ -1474,7 +1573,7 @@
                                            .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))
                      join b in _lifePayChannlesRep on a.ChannelId equals b.Id into temp
                      join b in _lifePayChannlesRep on a.ChannelId equals b.ChannlesNum into temp
                      from b in temp.DefaultIfEmpty()
                      select new LifePayOrderListOutput
                      {
@@ -1503,6 +1602,21 @@
        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,
                    });
    }
    #endregion
}