ipo
zyy
2025-12-31 f7d714ecc4210b96de9e78eacf6fad394533a082
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
package com.yami.trading.admin.task;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yami.trading.bean.ipo.UserPromiseRecord;
import com.yami.trading.bean.model.Wallet;
import com.yami.trading.service.WalletService;
import com.yami.trading.service.ipo.UserPromiseRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
 
 
@Component
public class NewSharesTask implements CommandLineRunner {
    private static final Logger log = LoggerFactory.getLogger(NewSharesTask.class);
 
    private final AtomicBoolean syncINStockData = new AtomicBoolean(false);
 
    private final Lock syncINStockDataLock = new ReentrantLock();
 
    @Autowired
    WalletService walletService;
 
    @Autowired
    UserPromiseRecordService userPromiseRecordService;
 
    /**
     * 待补自动扣费
     */
    @Scheduled(cron = "0 0/5 * * * ?")
    public void syncINStockData() {
 
        if (syncINStockData.get()) { // 判断任务是否在处理中
            return;
        }
        if (syncINStockDataLock.tryLock()) {
            try {
                syncINStockData.set(true);
                LambdaQueryWrapper<Wallet> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.gt(Wallet::getMoney, BigDecimal.ZERO);
                queryWrapper.gt(Wallet::getAmountToBeCovered, BigDecimal.ZERO);
                List<Wallet> walletList = walletService.list(queryWrapper);
                if (!walletList.isEmpty()) {
                    log.info("==================进入待补自动扣费================");
                    walletList.forEach((wallet) -> {
                        BigDecimal money = wallet.getMoney();
                        BigDecimal amountToBeCovered = wallet.getAmountToBeCovered();
                        if (money.compareTo(amountToBeCovered) >= 0) {
                            wallet.setMoney(money.subtract(amountToBeCovered));
                            wallet.setAmountToBeCovered(BigDecimal.ZERO);
                            log.info("用户id{}已补完待补", wallet.getUserId());
 
                            LambdaQueryWrapper<UserPromiseRecord> queryWrapper2 = new LambdaQueryWrapper<>();
                            queryWrapper2.eq(UserPromiseRecord::getUserId, wallet.getUserId());
                            queryWrapper2.eq(UserPromiseRecord::getStatus, 1);
                            List<UserPromiseRecord> userPromiseRecords = userPromiseRecordService.list(queryWrapper2);
                            if (!userPromiseRecords.isEmpty()) {
                                userPromiseRecords.forEach((userPromiseRecord) -> {
                                    userPromiseRecord.setStatus(2);
                                    log.info("订单{}已确认认缴", userPromiseRecord.getOrderNo());
                                });
                                userPromiseRecordService.updateBatchById(userPromiseRecords);
                            }
                        } else {
                            //资金不足
                            BigDecimal newAmountToBeCovered = amountToBeCovered.subtract(money);
                            wallet.setMoney(BigDecimal.ZERO);
                            wallet.setAmountToBeCovered(newAmountToBeCovered);
                            log.info("用户id{}补待补{}", wallet.getUserId(), money.toString());
                        }
                        walletService.updateById(wallet);
                    });
                }
 
 
 
            } catch (Exception e) {
                log.error("待补自动扣费出错", e);
            } finally {
                syncINStockDataLock.unlock();
                syncINStockData.set(false);
            }
        }
    }
 
 
 
 
    @Override
    public void run(String... args) throws Exception {
    }
}