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;
using System.Text;
using System.Threading.Tasks;
namespace FlexJobApi.UserServer.Application
{
///
/// 企业钱包查询处理器
///
public class EnterpriseWalletQueryHandler(
IRepository rep,
IRepository repEnterpriseWalletTransaction,
IRepository repEnterprise,
AlipayUtils alipayUtils
) :
IRequestHandler,
IRequestHandler
{
private readonly IRepository rep = rep;
private readonly IRepository repEnterpriseWalletTransaction = repEnterpriseWalletTransaction;
private readonly IRepository repEnterprise = repEnterprise;
private readonly AlipayUtils alipayUtils = alipayUtils;
///
/// 查询企业钱包详情
///
///
///
///
[UnitOfWork(false)]
public async Task Handle(GetEnterpriseWalletQuery request, CancellationToken cancellationToken)
{
var logier = JwtUtils.GetCurrentLogier();
if (request.EnterpriseId == null || logier.Type == EnumUserType.Enterprise)
{
request.EnterpriseId = logier.EnterpriseId;
}
var enterprise = repEnterprise.AsQueryable().AsNoTracking()
.Where(it => it.Id == request.EnterpriseId)
.FirstOrDefault();
if (enterprise == null) throw Oops.Oh(EnumErrorCodeType.s404, "企业");
var entity = await rep.AsQueryable()
.Where(it => it.EnterpriseId == request.EnterpriseId && it.Access == request.Access)
.FirstOrDefaultAsync();
if (entity == null) throw Oops.Oh(EnumErrorCodeType.s404, "企业钱包");
var update = false;
if (entity.Access == EnumEnterpriseWalletAccess.Alipay)
{
if (entity.SignStatus == EnumEnterpriseWalletSignStatus.Apply)
{
var response = alipayUtils.UserAgreementQuery(new Aop.Api.Domain.AlipayUserAgreementQueryModel
{
PersonalProductCode = entity.PersonalProductCode,
SignScene = entity.SignScene,
ExternalAgreementNo = entity.Code,
ThirdPartyType = entity.ThirdPartyType,
});
if (response.IsError) throw Oops.Oh(EnumErrorCodeType.s510, response.SubMsg ?? response.Msg);
entity.AgreementNo = response.AgreementNo;
entity.SignTime = response.SignTime.ToDateTime();
entity.ValidTime = response.ValidTime.ToDateTime();
entity.InvalidTime = response.InvalidTime.ToDateTime();
entity.SignStatus = response.Status == "TEMP"
? EnumEnterpriseWalletSignStatus.Apply
: response.Status == "NORMAL"
? EnumEnterpriseWalletSignStatus.Normal
: response.Status == "STOP"
? EnumEnterpriseWalletSignStatus.Stop
: throw Oops.Oh(EnumErrorCodeType.s510, "状态异常");
entity.PricipalType = response.PricipalType;
entity.AlipayLogonId = response.AlipayLogonId;
entity.PrincipalId = response.PrincipalId;
entity.PrincipalOpenId = response.PrincipalOpenId;
entity.ZmOpenId = response.ZmOpenId;
entity.CreditAuthMode = response.CreditAuthMode;
update = true;
}
if (entity.SignStatus == EnumEnterpriseWalletSignStatus.Normal && entity.AccountBookStatus != EnumEnterpriseWalletAccountBookStatus.Normal)
{
var response = alipayUtils.FundAccountbookCreate(new Aop.Api.Domain.AlipayFundAccountbookCreateModel
{
MerchantUserId = entity.Code,
MerchantUserType = "BUSINESS_ORGANIZATION",
SceneCode = "SATF_FUND_BOOK",
ExtInfo = new
{
agreement_no = entity.AgreementNo,
cert_no = enterprise.SocietyCreditCode
}.ToJson()
});
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;
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();
return model;
}
///
/// 查询企业钱包交易详情
///
///
///
///
public async Task 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();
}
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);
}
}
}