zyy
2025-07-08 53a149d9193c1df46bbfd0e5a344bce26576f7e3
src/main/java/com/nq/utils/task/stock/StockTask.java
@@ -2,26 +2,33 @@
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.nq.Repository.StockRepository;
import com.nq.dao.StockMapper;
import com.nq.dao.UserPositionMapper;
import com.nq.enums.EStockType;
import com.nq.pojo.DataStockBean;
import com.nq.pojo.ReponseBase;
import com.nq.pojo.Stock;
import com.nq.pojo.UserPosition;
import com.nq.pojo.*;
import com.nq.service.IMandatoryLiquidationService;
import com.nq.service.IStockService;
import com.nq.service.IUserPositionService;
import com.nq.utils.PropertiesUtil;
import com.nq.utils.http.HttpClientRequest;
import com.nq.utils.http.HttpRequest;
import com.nq.utils.redis.RedisKeyUtil;
import net.sf.json.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@@ -31,6 +38,7 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
@Component
@@ -45,6 +53,9 @@
    @Autowired
    UserPositionMapper userPositionMapper;
    @Autowired
    StockRepository stockRepository;
    private final Lock stockConstraintLock = new ReentrantLock();
@@ -70,19 +81,26 @@
            return;
        }
        if (syncINStockDataLock.tryLock()) {
            ExecutorService executor = Executors.newFixedThreadPool(2);
            Future<?> usFuture = null;
            Future<?> mxFuture = null;
            ExecutorService executor = Executors.newFixedThreadPool(4);
            Future<?> future1 = null;
            Future<?> future2 = null;
            Future<?> future3 = null;
            Future<?> future4 = null;
            try {
                syncINStockData.set(true); // 设置处理中标识为true
                // 并行执行US和MX的股票数据加载
                usFuture = executor.submit(() -> loadAllStock(EStockType.US));
                mxFuture = executor.submit(() -> loadAllStock(EStockType.MX));
                // 同步股票数据
                future1 = executor.submit(() -> loadAllStock(EStockType.US));
                future2 = executor.submit(() -> loadAllStock(EStockType.MX));
                // 同步指数数据
                future3 = executor.submit(() -> syncIndices(EStockType.US));
                future4 = executor.submit(() -> syncIndices(EStockType.MX));
                // 等待两个任务都完成
                usFuture.get();
                mxFuture.get();
                // 等待任务都完成
                future1.get();
                future2.get();
                future3.get();
                future4.get();
            } catch (Exception e) {
                Thread.currentThread().interrupt();
                log.error("同步股票数据出错", e);
@@ -97,6 +115,67 @@
        }
    }
    private void syncIndices(EStockType eStockType) {
        List<DataStockBean> list = new ArrayList<>();
        int totleStock = 1;
        try {
            while (totleStock > list.size()) {
                try {
                    //String result = HttpClientRequest.doGet(eStockType.stockUrl + "indices?key=" + eStockType.getStockKey() + "&country_id=" + eStockType.getContryId());
                    //ReponseBase reponseBase = new Gson().fromJson(result, ReponseBase.class);
                    String result = HttpRequest.doGrabGet(eStockType.stockUrl + "indices?key=" + eStockType.getStockKey() + "&country_id=" + eStockType.getContryId());
                    // 把JSON数据解析为List<DataStockBean>
                    Type listType = new TypeToken<List<DataStockBean>>(){}.getType();
                    list = new Gson().fromJson(result, listType);
                    totleStock = list.size();
                } catch (Exception e) {
                    e.printStackTrace();
                    break;
                }
            }
            if (list.isEmpty()) {
                return;
            }
            List<String> stockCodeList = list.stream().map(DataStockBean::getId).collect(Collectors.toList());
            List<Stock> stockList = stockMapper.selectList(new QueryWrapper<Stock>().in("stock_code", stockCodeList));
            List<Stock> updateStockList = new ArrayList<>();
            for (DataStockBean o : list) {
                Stock stock = stockList.stream()
                        .filter(x -> x.getStockCode().equals(o.getId()))
                        .findFirst()
                        .orElse(null);
                if (stock == null) {
                    stock = new Stock();
                }
                stock.setStockCode(o.getId());
                stock.setStockName(o.getName());
                stock.setStockType(eStockType.getCode());
                if (o.getType() == null) {
                    stock.setStockGid(eStockType.getCode());
                } else {
                    stock.setStockGid(o.getType());
                }
                stock.setStockSpell(o.getSymbol());
                stock.setIsLock(0);
                stock.setIsShow(0);
                stock.setDataBase(0);
                stock.setAddTime(new Date());
                updateStockList.add(stock);
                /*if (stock.getId() == null) {
                    stockMapper.insert1(stock);
                } else {
                    stockMapper.updateById(stock);
                }*/
                RedisKeyUtil.setCaCheKeyBaseStock(eStockType, o);
            }
            stockRepository.saveAll(updateStockList);
            log.info("同步指数 数据 成功 {}  总共同步数据 {}", eStockType.getCode(), list.size());
        } catch (Exception e) {
            log.error("同步指数列表出现异常: {}", e.getMessage());
        }
    }
    /**
     * 同步美国股票
@@ -149,8 +228,20 @@
                    break;
                }
            }
            if (list.isEmpty()) {
                return;
            }
            List<String> stockCodeList = list.stream().map(DataStockBean::getId).collect(Collectors.toList());
            List<Stock> stockList = stockMapper.selectList(new QueryWrapper<Stock>().in("stock_code", stockCodeList));
            int i = 1;
            List<Stock> updateStockList = new ArrayList<>();
            System.out.println(new Date());
            for (DataStockBean o : list) {
                Stock stock = stockMapper.findStockByCode(o.getId());
                //Stock stock = stockMapper.findStockByCode(o.getId());
                Stock stock = stockList.stream()
                        .filter(x -> x.getStockCode().equals(o.getId()))
                        .findFirst()
                        .orElse(null);
                if (stock == null) {
                    stock = new Stock();
                    stock.setStockCode(o.getId());
@@ -166,7 +257,7 @@
                    stock.setIsShow(0);
                    stock.setDataBase(0);
                    stock.setAddTime(new Date());
                    stockMapper.insert1(stock);
                    //stockMapper.insert1(stock);
                } else {
                    stock.setStockCode(o.getId());
                    stock.setStockName(o.getName());
@@ -181,10 +272,15 @@
                    stock.setIsShow(0);
                    stock.setDataBase(0);
                    stock.setAddTime(new Date());
                    stockMapper.updateById(stock);
                    //stockMapper.updateById(stock);
                }
                updateStockList.add(stock);
                RedisKeyUtil.setCaCheKeyBaseStock(eStockType, o);
                System.out.println(i);
                i++;
            }
            stockRepository.saveAll(updateStockList);
            System.out.println(new Date());
            log.info("同步股票 数据 成功 {}  总共同步数据 {}", eStockType.getCode(), list.size());
        } catch (
                Exception e) {
@@ -198,7 +294,7 @@
     * 强制平仓
     */
//    @Scheduled(cron = "0/1 * * * * ?")
    public void stockConstraint() {
    /*public void stockConstraint() {
        if (stockConstraint.get()) { // 判断任务是否在处理中
            return;
        }
@@ -219,5 +315,5 @@
        } else {
            log.info("强制平仓任务--------->上次任务还未执行完成,本次任务忽略");
        }
    }
    }*/
}