using Aop.Api.Domain; using Furion; using Furion.DatabaseAccessor; using Furion.FriendlyException; using Furion.Schedule; using Microsoft.EntityFrameworkCore; using Org.BouncyCastle.Ocsp; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace FlexJobApi.Core { public class RefreshEnterpriseWalletStatusJob( IRepository rep, AlipayUtils alipayUtils ) : IJob { private readonly IRepository rep = rep; private readonly AlipayUtils alipayUtils = alipayUtils; [UnitOfWork(false)] public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken) { var env = App.GetConfig("Environment"); if (env != "Local") { var entities = await rep.AsQueryable() .Include(it => it.Enterprise) .Where(it => it.Access == EnumEnterpriseWalletAccess.Alipay && (it.SignStatus == EnumEnterpriseWalletSignStatus.Apply || it.AccountBookStatus == null)) .ToListAsync(); foreach (var entity in entities) { 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 = entity.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.UpdateAsync(entity); } } } } } }