sunpengfei
6 天以前 8ffda541b48cbf619f8493196da4fb44d4f3ddc5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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<EnterpriseWallet> rep,
            AlipayUtils alipayUtils
        ) : IJob
    {
        private readonly IRepository<EnterpriseWallet> rep = rep;
        private readonly AlipayUtils alipayUtils = alipayUtils;
 
        [UnitOfWork(false)]
        public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
        {
            var env = App.GetConfig<string>("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);
                    }
                }
            }
        }
    }
}