1
zj
2024-10-11 5f97f550b3fb60ac2142d7ca3c78d6bd04f18b80
src/main/java/com/nq/service/impl/StockServiceImpl.java
@@ -19,6 +19,7 @@
import com.nq.pojo.*;
import com.nq.pojo.reponse.RPageInfo;
import com.nq.service.*;
import com.nq.utils.ConverterUtil;
import com.nq.utils.http.HttpClientRequest;
import com.nq.utils.PropertiesUtil;
import com.nq.utils.redis.RedisKeyUtil;
@@ -134,7 +135,7 @@
        List<Stock> stockList = new ArrayList<>();
        if (stockType.equals("99")) {
            PageHelper.startPage(pageNum, pageSize);
            stockList.addAll(stockMapper.findZtStockListByKeyWords(keyWords, stockPlate, "IN", Integer.valueOf(0)));
            stockList.addAll(stockMapper.findZtStockListByKeyWords(keyWords, stockPlate, "JP", Integer.valueOf(0)));
        } else if (stockType.equals("100")) {
            User user = iUserService.getCurrentRefreshUser(request);
            if (user == null) {
@@ -184,21 +185,14 @@
            stockListVOS.addAll(Objects.requireNonNull(StockApi.getStockReailTimes(stockList)));
        }
        // Create a list to hold Futures
        List<CompletableFuture<Void>> futures = new ArrayList<>();
        // Use CompletableFuture to perform asynchronous processing for each stockListVO
        for (StockListVO stockListVO : stockListVOS) {
            CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
                stockListVO.setNowPrice(iPriceServices.getNowPrice(stockListVO.getCode(), stockType).toString());
                Map<String, Object> newStock = iPriceServices.getNewStock(stockListVO.getCode());
                stockListVO.setHcrateP(newStock.get("pcp").toString());
            });
            futures.add(future);
            Stock stock = new Stock();
            stock.setStockType(stockListVO.getStock_type());
            stock.setStockCode(stockListVO.getCode());
            StockRealTimeBean cacheRealTimeStock = RedisKeyUtil.getCacheRealTimeStock(stock);
            stockListVO.setNowPrice(cacheRealTimeStock.getLast());
            stockListVO.setHcrateP(cacheRealTimeStock.getPcp());
        }
        // Wait for all futures to complete
        CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
        RPageInfo pageInfo = new RPageInfo();
        pageInfo.setList(stockListVOS);
@@ -240,6 +234,28 @@
        map.put("introduction", introduction);
        map.put("stock", stockVO);
        return ServerResponse.createBySuccess(map);
    }
    public String doGet(String pid, EStockType eStockType) {
        String apiUrl = eStockType.getStockUrl() + "/indices?country_id="+eStockType.getContryId()+"&key=" + eStockType.getStockKey();
        try {
            URL url = new URL(apiUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            return response.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public Map getSingleStock(String code) {
@@ -460,11 +476,11 @@
    public Object getKData(String pid, String interval, String stockType) {
        EStockType eStockType = EStockType.getEStockTypeByCode(stockType);
        if(eStockType == EStockType.JP){
            Object object = HttpUtil.get(eStockType.stockUrl + "api/all/getKData.do?pid=" + pid + "&interval=" + interval + "&stockType=in");
            Object object = HttpUtil.get(eStockType.stockUrl + "kline?pid=" + pid + "&interval=" + interval + "&key=" + eStockType.stockKey);
            Gson gson = new Gson();
            List<kData> dataList = gson.fromJson(object.toString(), new TypeToken<List<kData>>(){}.getType());
            Stock stock = stockMapper.selectOne(new LambdaQueryWrapper<Stock>().eq(Stock::getStockCode, pid).eq(Stock::getStockType, "IN"));
            Stock stock = stockMapper.selectOne(new LambdaQueryWrapper<Stock>().eq(Stock::getStockCode, pid).eq(Stock::getStockType, "JP"));
            BigDecimal nowPrice = iPriceServices.getNowPrice(stock.getStockCode());
            Map singleStock = getSingleStock(stock.getStockCode());
            StockVO stockVO = (StockVO)singleStock.get("stock");