package com.yami.trading.admin.task;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.yami.trading.bean.etf.domain.EtfMinuteKLine;
|
import com.yami.trading.bean.item.domain.Item;
|
import com.yami.trading.bean.model.Wallet;
|
import com.yami.trading.service.WalletService;
|
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.CompletableFuture;
|
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;
|
|
/**
|
* 同步系统所需要的股票
|
*/
|
@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));
|
log.info("用户id{}已补完待补", wallet.getUserId());
|
} 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 {
|
}
|
}
|