zhengyiming
2025-06-09 9a7e2f2a8c0017f9445e9281bb5f574596dbe61c
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
using LifePayment.Application;
using LifePayment.Application.Contracts;
using LifePayment.Application.LifePay;
using LifePayment.Domain.Models;
using LifePayment.Domain.Shared;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Nest;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.BackgroundWorkers;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Threading;
using Volo.Abp.Uow;
using ZeroD.Util;
 
namespace LifePayment.Worker.Worker
{
    public class CheckUnPayOrderWork : AsyncPeriodicBackgroundWorkerBase
    {
        private readonly int _doWorkHour = 0;
        private readonly int _doWorkMinute = 1;
        private readonly ILifePayOrderService lifePayOrderService;
        private readonly ILifePayService lifePayService;
        private readonly IRepository<LifePayOrder, Guid> lifePayOrderRepository;
 
        public CheckUnPayOrderWork(
            AbpAsyncTimer timer,
            IServiceScopeFactory serviceScopeFactory,
            ILifePayOrderService lifePayOrderService,
            ILifePayService lifePayService,
            IRepository<LifePayOrder, Guid> lifePayOrderRepository) : base(timer, serviceScopeFactory)
        {
            timer.Period = (int)TimeSpan.FromMinutes(10).TotalMilliseconds;
            this.lifePayOrderService = lifePayOrderService;
            this.lifePayService = lifePayService;
            this.lifePayOrderRepository = lifePayOrderRepository;
        }
 
        [UnitOfWork]
        protected override async Task DoWorkAsync(PeriodicBackgroundWorkerContext workerContext)
        {
            try
            {
                Logger.LogInformation($"检查未支付订单状态开始: {DateTime.Now}");
                var end = DateTime.Now;
                var start = end.AddMinutes(-15);
                var orderNos = lifePayOrderRepository.Where(it => it.PayStatus == LifePayStatusEnum.未支付 && it.CreationTime >= start && it.CreationTime <= end).Select(it => it.OrderNo).ToList();
                foreach (var orderNo in orderNos)
                {
                    var wxPayNotice = await lifePayService.WxPayTradeQuery(orderNo);
                    var json = wxPayNotice.ToJson();
                    Logger.LogInformation($"订单({wxPayNotice.OutTradeNo})信息: {json}");
                    if (wxPayNotice.OutTradeNo.Contains("JF") && wxPayNotice.TradeState == LifePaymentConstant.WxPayStatus.支付成功)
                    {
                        await lifePayService.LifePaySuccessHandler(wxPayNotice.OutTradeNo, wxPayNotice.TransactionId);
 
                        // 插入收支流水
                        await lifePayOrderService.AddLifePayExpensesReceipts(new AddLifePayExpensesReceiptsInput()
                        {
                            OrderNo = wxPayNotice.OutTradeNo,
                            OutOrderNo = wxPayNotice.TransactionId,
                            LifePayType = LifePayTypeEnum.WxPay,
                            ExpensesReceiptsType = ExpensesReceiptsTypeEnum.Expenses,
                            Amount = wxPayNotice.Amount.Total
                        });
 
                        Logger.LogInformation("已改为支付成功");
                    }
                    else
                    {
                        Logger.LogInformation("未更新");
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogError($"检查未支付订单状态发生异常: {DateTime.Now}" + ex.Message);
            }
 
 
        }
    }
}