peter
2026-01-15 0ababc615d5b381f856ca84fb8dc715cdb2c4b5b
src/main/java/com/nq/service/impl/PriceServicesImpl.java
@@ -1,5 +1,7 @@
package com.nq.service.impl;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.gson.Gson;
@@ -11,6 +13,7 @@
import com.nq.pojo.*;
import com.nq.service.IPriceServices;
import com.nq.service.IStockConfigServices;
import com.nq.utils.PropertiesUtil;
import com.nq.utils.http.HttpClientRequest;
import com.nq.utils.redis.RedisKeyUtil;
import com.nq.utils.timeutil.TimeUtil;
@@ -18,16 +21,18 @@
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.*;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import  java.io.BufferedReader;
import  java.io.InputStreamReader;
import  java.net.HttpURLConnection;
import  java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import com.fasterxml.jackson.databind.ObjectMapper;
@Service
@@ -51,25 +56,32 @@
    @Override
    public BigDecimal getNowPrice(String stockCode) {
        Stock stock = stockMapper.selectOne(new QueryWrapper<Stock>().eq("stock_code",stockCode));
        if(null == stock){
            return BigDecimal.ZERO;
        }
        StockSetting stockSetting = stockSettingMapper.selectOne(new QueryWrapper<StockSetting>().eq("stock_code",stockCode));
        if(stockSetting != null){
            if(TimeUtil.isTradingHour(stockSetting.getStartTime(),stockSetting.getEndTime())){
            Date newDate = new Date();
            DateTime startTime = DateUtil.parseDateTime(stockSetting.getStartTime());
            DateTime endTime = DateUtil.parseDateTime(stockSetting.getEndTime());
            if(newDate.after(startTime) && newDate.before(endTime)){
//            if(TimeUtil.isTradingHour(stockSetting.getStartTime(),stockSetting.getEndTime())){
                if(stockSetting.getType().equals("0")){
                    return  new BigDecimal(stockSetting.getPrice());
                }else{
                    String s = doGet(stock.getStockCode());
                    String s = doPost(stock.getStockCode());
                    if(null != s){
                        Map<String, Object> stringObjectMap = jsonToMap(s);
                        return   new BigDecimal(stringObjectMap.get("last").toString()).multiply(new BigDecimal(stockSetting.getPrice()));
                        return   new BigDecimal(stringObjectMap.get("Last").toString()).multiply(new BigDecimal(stockSetting.getPrice()));
                    }
                }
            }
        }
        String s = doGet(stock.getStockCode());
        String s = doPost(stock.getStockCode());
        if(null != s) {
            Map<String, Object> stringObjectMap = jsonToMap(s);
            return  new BigDecimal(stringObjectMap.get("last").toString());
            return  new BigDecimal(stringObjectMap.get("Last").toString());
        }
        return BigDecimal.ZERO;
    }
@@ -77,7 +89,7 @@
    @Override
    public Map<String, Object> getNewStock(String stockCode) {
        Stock stock = stockMapper.selectOne(new QueryWrapper<Stock>().eq("stock_code",stockCode));
        String s = doGet(stock.getStockCode());
        String s = doPost(stock.getStockCode());
        if(null != s){
            Map<String, Object> stringObjectMap = jsonToMap(s);
            return   stringObjectMap;
@@ -100,26 +112,101 @@
        }
    }
    public String doGet(String pid){
        String  apiUrl  =  "http://api-in-2.js-stock.top/stock?pid="+pid+"&key=eVKtHt7aG4m6ozwWL9qG";
        try  {
            URL  url  =  new  URL(apiUrl);
            HttpURLConnection  connection  =  (HttpURLConnection)  url.openConnection();
            connection.setRequestMethod("GET");
    // 缓存条目类
    private static class CacheEntry {
        String result;
        long lastExecuteTime;
            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();
        CacheEntry(String result, long lastExecuteTime) {
            this.result = result;
            this.lastExecuteTime = lastExecuteTime;
        }
        return null;
    }
    // 按pid存储缓存条目
    private static final Map<String, CacheEntry> cacheMap = new ConcurrentHashMap<>();
    // 按pid存储锁对象,实现细粒度锁
    private static final Map<String, Object> lockMap = new ConcurrentHashMap<>();
    private static final long MIN_INTERVAL_MS = 3000;
    public static String doPost(String pid) {
        long currentTime = System.currentTimeMillis();
        // 1. 快速检查缓存(无锁)
        CacheEntry cached = cacheMap.get(pid);
        if (cached != null && (currentTime - cached.lastExecuteTime) < MIN_INTERVAL_MS) {
            return cached.result;
        }
        // 2. 获取该pid对应的锁对象
        Object pidLock = lockMap.computeIfAbsent(pid, k -> new Object());
        // 3. 同步块内再次检查
        synchronized (pidLock) {
            currentTime = System.currentTimeMillis();
            cached = cacheMap.get(pid);
            if (cached != null && (currentTime - cached.lastExecuteTime) < MIN_INTERVAL_MS) {
                return cached.result;
            }
            // 4. 执行POST请求
            String newResult = doActualPost(pid);
            // 5. 更新该pid的缓存
            if (newResult != null) {
                cacheMap.put(pid, new CacheEntry(newResult, System.currentTimeMillis()));
                return newResult;
            } else if (cached != null) {
                // 请求失败,返回旧缓存
                return cached.result;
            }
            return "{\"error\":\"请求失败且无缓存数据\"}";
        }
    }
    private static String doActualPost(String pid) {
        String apiUrl = PropertiesUtil.getProperty("JS_IN_HTTP_URL") +
                "stock?key=" + PropertiesUtil.getProperty("JS_IN_KEY");
        try {
            URL url = new URL(apiUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setDoOutput(true);
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(10000);
            String postData = "pid=" + pid;
            try (OutputStream os = connection.getOutputStream()) {
                byte[] input = postData.getBytes("utf-8");
                os.write(input, 0, input.length);
            }
            // 读取响应
            try (BufferedReader in = new BufferedReader(
                    new InputStreamReader(connection.getInputStream()))) {
                StringBuilder response = new StringBuilder();
                String inputLine;
                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                return response.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
            // 不再直接返回旧缓存,由上层处理
            return null;
        }
    }
    @Override