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
using Aop.Api.Domain;
using Furion;
using Furion.DatabaseAccessor;
using Furion.FriendlyException;
using Furion.Schedule;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
 
namespace FlexJobApi.Core.Jobs
{
    public class RefreshEnterpriseWalletTransactionStatusJob(
            IRepository<EnterpriseWalletTransaction> rep,
            IRepository<EnterpriseWallet> repEnterpriseWallet,
            AlipayUtils alipayUtils
        ) : IJob
    {
        private readonly IRepository<EnterpriseWalletTransaction> rep = rep;
        private readonly IRepository<EnterpriseWallet> repEnterpriseWallet = repEnterpriseWallet;
        private readonly AlipayUtils alipayUtils = alipayUtils;
 
        public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
        {
            var env = App.GetConfig<string>("Environment");
            if (env != "Local")
            {
                var entities = await rep.AsQueryable()
                .Where(it =>
                    it.TransactionStatus == EnumEnterpriseWalletTransactionStatus.WaitPay
                    || it.TransactionStatus == EnumEnterpriseWalletTransactionStatus.Dealing)
                .ToListAsync();
                if (entities.IsNotNull())
                {
                    foreach (var entity in entities)
                    {
                        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))
                            .ToListAsync();
                        foreach (var wallet in wallets)
                        {
                            var response = alipayUtils.FundAccountbookQuery(new AlipayFundAccountbookQueryModel
                            {
                                AccountBookId = wallet.AccountBookId,
                                SceneCode = "SATF_FUND_BOOK",
                                MerchantUserId = wallet.Code,
                            });
                            if (response.IsError) throw Oops.Oh(EnumErrorCodeType.s510, response.SubMsg ?? response.Msg);
                            wallet.Balance = response.AvailableAmount.ToDecimal() ?? 0;
                            await repEnterpriseWallet.UpdateAsync(wallet);
                        }
                    }
                }
            }
        }
    }
}