From 5c45bc53e2c18e45130c21048df4af4896455c6d Mon Sep 17 00:00:00 2001 From: sunpengfei <i@angelzzz.com> Date: 星期五, 12 九月 2025 15:58:48 +0800 Subject: [PATCH] feat:开发 --- FlexJobApi.Core/Jobs/RefreshEnterpriseWalletTransactionStatusJob.cs | 201 +++++++++++++++++++++++++++++++++++-------------- 1 files changed, 143 insertions(+), 58 deletions(-) diff --git a/FlexJobApi.Core/Jobs/RefreshEnterpriseWalletTransactionStatusJob.cs b/FlexJobApi.Core/Jobs/RefreshEnterpriseWalletTransactionStatusJob.cs index bfa894c..8e80471 100644 --- a/FlexJobApi.Core/Jobs/RefreshEnterpriseWalletTransactionStatusJob.cs +++ b/FlexJobApi.Core/Jobs/RefreshEnterpriseWalletTransactionStatusJob.cs @@ -1,11 +1,14 @@ 锘縰sing Aop.Api.Domain; +using Furion; using Furion.DatabaseAccessor; +using Furion.DistributedIDGenerator; using Furion.FriendlyException; using Furion.Schedule; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; +using System.Security.Cryptography.Xml; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -15,83 +18,165 @@ { public class RefreshEnterpriseWalletTransactionStatusJob( IRepository<EnterpriseWalletTransaction> rep, + IRepository<UserWalletTransaction> repUserWalletTransaction, IRepository<EnterpriseWallet> repEnterpriseWallet, + IRepository<UserWallet> repUserWallet, AlipayUtils alipayUtils ) : IJob { private readonly IRepository<EnterpriseWalletTransaction> rep = rep; + private readonly IRepository<UserWalletTransaction> repUserWalletTransaction = repUserWalletTransaction; private readonly IRepository<EnterpriseWallet> repEnterpriseWallet = repEnterpriseWallet; + private readonly IRepository<UserWallet> repUserWallet = repUserWallet; private readonly AlipayUtils alipayUtils = alipayUtils; + [UnitOfWork(false)] public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken) { - var entities = await rep.AsQueryable() - .Where(it => - it.TransactionStatus == EnumEnterpriseWalletTransactionStatus.WaitPay - || it.TransactionStatus == EnumEnterpriseWalletTransactionStatus.Dealing) - .ToListAsync(); - if (entities.IsNotNull()) + var env = App.GetConfig<string>("Environment"); + if (env != "Local") { - foreach (var entity in entities) + var entities = await rep.AsQueryable() + .Where(it => + it.TransactionStatus == EnumWalletTransactionStatus.WaitPay + || it.TransactionStatus == EnumWalletTransactionStatus.Dealing) + .ToListAsync(); + if (entities.IsNotNull()) { - var response = alipayUtils.FundTransCommonQuery(new Aop.Api.Domain.AlipayFundTransCommonQueryModel - { - ProductCode = entity.ProductCode, - BizScene = entity.BizScene, - OutBizNo = entity.Code - }); - if (response.IsError) throw Oops.Oh(EnumErrorCodeType.s510, response.SubMsg ?? response.Msg); - entity.OrderId = response.OrderId; - entity.InflowSettleSerialNo = response.InflowSettleSerialNo; - entity.SettleSerialNo = response.SettleSerialNo; - entity.ReceiverOpenId = response.ReceiverOpenId; - entity.ReceiverUserId = response.ReceiverUserId; - entity.PayFundOrderId = response.PayFundOrderId; - entity.ArrivalTimeEnd = response.ArrivalTimeEnd.ToDateTime(); - entity.OrderFee = response.OrderFee.ToDecimal(); - entity.ErrorCode = response.ErrorCode; - entity.FailReason = response.FailReason; - entity.FailInstReason = response.FailInstReason; - entity.FailInstName = response.FailInstName; - entity.FailInstErrorCode = response.FailInstErrorCode; - entity.SubStatus = response.SubStatus; - entity.TransDate = response.PayDate.ToDateTime(); - entity.Status = response.Status; - entity.TransactionStatus = response.Status == "SUCCESS" - ? EnumEnterpriseWalletTransactionStatus.Success - : response.Status == "DEALING" - ? EnumEnterpriseWalletTransactionStatus.Dealing - : response.Status == "REFUND" - ? EnumEnterpriseWalletTransactionStatus.Refund - : response.Status == "FAIL" - ? EnumEnterpriseWalletTransactionStatus.Fail - : throw Oops.Oh(EnumErrorCodeType.s510, $"鏈瘑鍒殑鐘舵�侊細{response.Status}"); - await rep.UpdateAsync(entity); - } - var walletIds = entities - .Where(it => it.TransactionStatus == EnumEnterpriseWalletTransactionStatus.Success) - .Select(it => it.WalletId) - .Distinct() - .ToList(); - if (walletIds.IsNotNull()) - { - var wallets = await repEnterpriseWallet.AsQueryable() - .Where(it => walletIds.Contains(it.Id)) + var ids = entities.Select(it => it.Id).ToList(); + var withdraws = await repUserWalletTransaction.AsQueryable() + .Where(it => + it.EnterpriseWalletTransactionId.HasValue + && ids.Contains(it.EnterpriseWalletTransactionId.Value)) .ToListAsync(); - foreach (var wallet in wallets) + var userWalletIds = withdraws.DistinctSelect(it => it.WalletId).ToList(); + var userWallets = await repUserWallet.AsQueryable() + .Where(it => userWalletIds.Contains(it.Id)) + .ToListAsync(); + foreach (var entity in entities) { - var response = alipayUtils.FundAccountbookQuery(new AlipayFundAccountbookQueryModel + var response = alipayUtils.FundTransCommonQuery(new Aop.Api.Domain.AlipayFundTransCommonQueryModel { - AccountBookId = wallet.AccountBookId, - SceneCode = "SATF_FUND_BOOK", - MerchantUserId = wallet.Code, + ProductCode = entity.ProductCode, + BizScene = entity.BizScene, + OutBizNo = entity.Code }); - if (response.IsError) throw Oops.Oh(EnumErrorCodeType.s510, response.SubMsg ?? response.Msg); - wallet.Balance = response.AvailableAmount.ToDecimal() ?? 0; - await repEnterpriseWallet.UpdateAsync(wallet); + if (response.IsError) + { + entity.ErrorCode = response.ErrorCode; + entity.FailReason = response.FailReason; + } + else + { + entity.OrderId = response.OrderId; + entity.InflowSettleSerialNo = response.InflowSettleSerialNo; + entity.SettleSerialNo = response.SettleSerialNo; + entity.ReceiverOpenId = response.ReceiverOpenId; + entity.ReceiverUserId = response.ReceiverUserId; + entity.PayFundOrderId = response.PayFundOrderId; + entity.ArrivalTimeEnd = response.ArrivalTimeEnd.ToDateTime(); + entity.OrderFee = response.OrderFee.ToDecimal(); + entity.ErrorCode = response.ErrorCode; + entity.FailReason = response.FailReason; + entity.FailInstReason = response.FailInstReason; + entity.FailInstName = response.FailInstName; + entity.FailInstErrorCode = response.FailInstErrorCode; + entity.SubStatus = response.SubStatus; + entity.TransDate = response.PayDate.ToDateTime(); + entity.Status = response.Status; + entity.TransactionStatus = response.Status == "SUCCESS" + ? EnumWalletTransactionStatus.Success + : response.Status == "DEALING" + ? EnumWalletTransactionStatus.Dealing + : response.Status == "REFUND" + ? EnumWalletTransactionStatus.Refund + : EnumWalletTransactionStatus.Fail; + } + await rep.UpdateNowAsync(entity); + + var withdraw = withdraws.FirstOrDefault(it => it.EnterpriseWalletTransactionId == entity.Id); + if (withdraw != null) + { + var userWallet = userWallets.FirstOrDefault(it => it.Id == withdraw.WalletId); + if (userWallet != null) + { + withdraw.ErrorCode = entity.ErrorCode; + withdraw.FailReason = entity.FailReason; + withdraw.EnterpriseWalletTransactionId = entity.Id; + withdraw.TransactionStatus = entity.TransactionStatus; + withdraw.TransDate = entity.TransDate; + withdraw.ArrivalTimeEnd = entity.ArrivalTimeEnd; + withdraw.ServiceFee = entity.OrderFee ?? 0; + if (withdraw.TransactionStatus == EnumWalletTransactionStatus.Success) + { + withdraw.ActualAmount = withdraw.Amount; + } + else + { + withdraw.ActualAmount = 0; + + var order = new UserWalletTransaction(); + order.WalletId = withdraw.WalletId; + order.Type = EnumUserWalletTransactionType.Income; + order.OperatorUserId = withdraw.OperatorUserId; + order.OperatorTime = withdraw.OperatorTime; + order.Title = $"鏀跺叆-鎻愮幇澶辫触閫�娆�"; + order.Amount = withdraw.Amount; + order.ActualAmount = order.Amount; + order.Balance = userWallet.Balance; + order.AfterBalance = userWallet.Balance + order.Amount; + order.TransDate = DateTime.Now; + order.TransactionStatus = EnumWalletTransactionStatus.Success; + await SetCode(order); + await repUserWalletTransaction.InsertNowAsync(order); + + userWallet.Balance = order.AfterBalance; + await repUserWallet.UpdateNowAsync(userWallet); + } + } + } + } + var walletIds = entities + .Where(it => it.TransactionStatus == EnumWalletTransactionStatus.Success) + .Select(it => it.WalletId) + .Distinct() + .ToList(); + if (walletIds.IsNotNull()) + { + var wallets = await repEnterpriseWallet.AsQueryable() + .Where(it => walletIds.Contains(it.Id)) + .ToListAsync(); + foreach (var wallet in wallets) + { + var response = alipayUtils.FundAccountbookQuery(new AlipayFundAccountbookQueryModel + { + AccountBookId = wallet.AccountBookId, + SceneCode = "SATF_FUND_BOOK", + MerchantUserId = wallet.Code, + ExtInfo = new + { + agreement_no = wallet.AgreementNo + }.ToJson() + }); + if (response.IsError) throw Oops.Oh(EnumErrorCodeType.s510, response.SubMsg ?? response.Msg); + wallet.Balance = response.AvailableAmount.ToDecimal() ?? 0; + await repEnterpriseWallet.UpdateAsync(wallet); + } } } } } + + private async Task SetCode(UserWalletTransaction entity) + { + entity.Code = $"{DateTime.Now:yyyyMMddHHmmss}{new Random(IDGen.NextID().GetHashCode()).Next(1000, 9999)}"; + var exist = await repUserWalletTransaction.AsQueryable().AsNoTracking() + .AnyAsync(it => it.Code == entity.Code); + if (exist) + { + await SetCode(entity); + } + } + } } -- Gitblit v1.9.1