| | |
| | | using FlexJobApi.Core; |
| | | using Aop.Api.Domain; |
| | | using Azure; |
| | | using FlexJobApi.Core; |
| | | using Furion.DatabaseAccessor; |
| | | using Furion.FriendlyException; |
| | | using Mapster; |
| | | using MediatR; |
| | | using Microsoft.EntityFrameworkCore; |
| | | using StackExchange.Redis; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Linq; |
| | |
| | | /// </summary> |
| | | public class EnterpriseWalletQueryHandler( |
| | | IRepository<EnterpriseWallet> rep, |
| | | IRepository<EnterpriseWalletTransaction> repEnterpriseWalletTransaction, |
| | | IRepository<EnterpriseWalletExpandindirectOrder> repEnterpriseWalletExpandindirectOrder, |
| | | IRepository<Enterprise> repEnterprise, |
| | | AlipayUtils alipayUtils |
| | | ) : |
| | | IRequestHandler<GetEnterpriseWalletQuery, GetEnterpriseWalletQueryResult> |
| | | IRequestHandler<GetEnterpriseWalletQuery, GetEnterpriseWalletQueryResult>, |
| | | IRequestHandler<GetEnterpriseWalletTransactionQuery, GetEnterpriseWalletTransactionQueryResult> |
| | | { |
| | | private readonly IRepository<EnterpriseWallet> rep = rep; |
| | | private readonly IRepository<EnterpriseWalletTransaction> repEnterpriseWalletTransaction = repEnterpriseWalletTransaction; |
| | | private readonly IRepository<EnterpriseWalletExpandindirectOrder> repEnterpriseWalletExpandindirectOrder = repEnterpriseWalletExpandindirectOrder; |
| | | private readonly IRepository<Enterprise> repEnterprise = repEnterprise; |
| | | private readonly AlipayUtils alipayUtils = alipayUtils; |
| | | |
| | |
| | | var entity = await rep.AsQueryable() |
| | | .Where(it => it.EnterpriseId == request.EnterpriseId && it.Access == request.Access) |
| | | .FirstOrDefaultAsync(); |
| | | if (entity == null) throw Oops.Oh(EnumErrorCodeType.s404, "企业钱包"); |
| | | if (entity == null) |
| | | { |
| | | return new GetEnterpriseWalletQueryResult |
| | | { |
| | | Access = request.Access, |
| | | SignStatus = EnumEnterpriseWalletSignStatus.Wait, |
| | | ExpandindirectOrderStatus = EnumEnterpriseWalletExpandindirectOrderStatus.Wait |
| | | }; |
| | | } |
| | | var update = false; |
| | | if (entity.Access == EnumEnterpriseWalletAccess.Alipay) |
| | | { |
| | | if (entity.SignStatus == EnumEnterpriseWalletSignStatus.Apply) |
| | |
| | | entity.PrincipalOpenId = response.PrincipalOpenId; |
| | | entity.ZmOpenId = response.ZmOpenId; |
| | | entity.CreditAuthMode = response.CreditAuthMode; |
| | | await rep.UpdateNowAsync(entity); |
| | | update = true; |
| | | } |
| | | |
| | | if (entity.SignStatus == EnumEnterpriseWalletSignStatus.Normal && entity.AccountBookStatus != EnumEnterpriseWalletAccountBookStatus.Normal) |
| | | { |
| | | var accountBookResponse = alipayUtils.FundAccountbookCreate(new Aop.Api.Domain.AlipayFundAccountbookCreateModel |
| | | var response = alipayUtils.FundAccountbookCreate(new Aop.Api.Domain.AlipayFundAccountbookCreateModel |
| | | { |
| | | MerchantUserId = entity.Code, |
| | | MerchantUserType = "BUSINESS_ORGANIZATION", |
| | |
| | | cert_no = enterprise.SocietyCreditCode |
| | | }.ToJson() |
| | | }); |
| | | if (accountBookResponse.IsError) throw Oops.Oh(EnumErrorCodeType.s510, accountBookResponse.SubMsg ?? accountBookResponse.Msg); |
| | | entity.AccountBookId = accountBookResponse.AccountBookId; |
| | | entity.BankAccName = accountBookResponse.ExtCardInfo.BankAccName; |
| | | entity.CardBank = accountBookResponse.ExtCardInfo.CardBank; |
| | | entity.CardBranch = accountBookResponse.ExtCardInfo.CardBranch; |
| | | entity.CardDeposit = accountBookResponse.ExtCardInfo.CardDeposit; |
| | | entity.CardLocation = accountBookResponse.ExtCardInfo.CardLocation; |
| | | entity.CardNo = accountBookResponse.ExtCardInfo.CardNo; |
| | | entity.AccountBookStatus = accountBookResponse.ExtCardInfo.Status == "A" |
| | | if (response.IsError) throw Oops.Oh(EnumErrorCodeType.s510, response.SubMsg ?? response.Msg); |
| | | entity.AccountBookId = response.AccountBookId; |
| | | entity.BankAccName = response.ExtCardInfo.BankAccName; |
| | | entity.CardBank = response.ExtCardInfo.CardBank; |
| | | entity.CardBranch = response.ExtCardInfo.CardBranch; |
| | | entity.CardDeposit = response.ExtCardInfo.CardDeposit; |
| | | entity.CardLocation = response.ExtCardInfo.CardLocation; |
| | | entity.CardNo = response.ExtCardInfo.CardNo; |
| | | entity.AccountBookStatus = response.ExtCardInfo.Status == "A" |
| | | ? EnumEnterpriseWalletAccountBookStatus.Normal |
| | | : EnumEnterpriseWalletAccountBookStatus.Exception; |
| | | await rep.UpdateNowAsync(entity); |
| | | update = true; |
| | | } |
| | | |
| | | if (entity.AccountBookStatus == EnumEnterpriseWalletAccountBookStatus.Normal) |
| | | { |
| | | var response = alipayUtils.FundAccountbookQuery(new AlipayFundAccountbookQueryModel |
| | | { |
| | | AccountBookId = entity.AccountBookId, |
| | | SceneCode = "SATF_FUND_BOOK", |
| | | MerchantUserId = entity.Code, |
| | | }); |
| | | if (response.IsError) throw Oops.Oh(EnumErrorCodeType.s510, response.SubMsg ?? response.Msg); |
| | | entity.Balance = response.AvailableAmount.ToDecimal() ?? 0; |
| | | update = true; |
| | | } |
| | | } |
| | | |
| | | if (update) |
| | | { |
| | | await rep.UpdateNowAsync(entity); |
| | | } |
| | | |
| | | var model = entity.Adapt<GetEnterpriseWalletQueryResult>(); |
| | | |
| | | var expandindirectOrder = await repEnterpriseWalletExpandindirectOrder.AsQueryable().AsNoTracking() |
| | | .OrderByDescending(it => it.CreatedTime) |
| | | .Where(it => it.WalletId == model.Id) |
| | | .Select(it => new |
| | | { |
| | | it.OrderStatus |
| | | }) |
| | | .FirstOrDefaultAsync(); |
| | | model.ExpandindirectOrderStatus = expandindirectOrder == null |
| | | ? EnumEnterpriseWalletExpandindirectOrderStatus.Wait |
| | | : expandindirectOrder.OrderStatus; |
| | | |
| | | return model; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 查询企业钱包交易详情 |
| | | /// </summary> |
| | | /// <param name="request"></param> |
| | | /// <param name="cancellationToken"></param> |
| | | /// <returns></returns> |
| | | public async Task<GetEnterpriseWalletTransactionQueryResult> Handle(GetEnterpriseWalletTransactionQuery request, CancellationToken cancellationToken) |
| | | { |
| | | var entity = await repEnterpriseWalletTransaction.AsQueryable() |
| | | .Where(it => it.Id == request.Id) |
| | | .FirstOrDefaultAsync(); |
| | | if (entity == null) throw Oops.Oh(EnumErrorCodeType.s404, "交易"); |
| | | 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 repEnterpriseWalletTransaction.UpdateAsync(entity); |
| | | if (entity.TransactionStatus == EnumEnterpriseWalletTransactionStatus.Success) |
| | | { |
| | | await GetBalance(entity.WalletId); |
| | | } |
| | | return entity.Adapt<GetEnterpriseWalletTransactionQueryResult>(); |
| | | } |
| | | |
| | | private async Task GetBalance(Guid id) |
| | | { |
| | | var entity = await rep.AsQueryable() |
| | | .Where(it => it.Id == id) |
| | | .FirstOrDefaultAsync(); |
| | | if (entity == null) throw Oops.Oh(EnumErrorCodeType.s404, "企业钱包"); |
| | | var response = alipayUtils.FundAccountbookQuery(new AlipayFundAccountbookQueryModel |
| | | { |
| | | AccountBookId = entity.AccountBookId, |
| | | SceneCode = "SATF_FUND_BOOK", |
| | | MerchantUserId = entity.Code, |
| | | }); |
| | | if (response.IsError) throw Oops.Oh(EnumErrorCodeType.s510, response.SubMsg ?? response.Msg); |
| | | entity.Balance = response.AvailableAmount.ToDecimal() ?? 0; |
| | | await rep.UpdateNowAsync(entity); |
| | | } |
| | | |
| | | } |
| | | } |