1
dd
2025-12-26 73e608df889ebeca1c87cb8d77676923b8872a83
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package com.nq.service.impl;
 
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.nq.dao.StockDzMapper;
import com.nq.dao.StockMapper;
import com.nq.dao.StockSettingMapper;
import com.nq.enums.EConfigKey;
import com.nq.enums.EStockType;
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;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
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 com.fasterxml.jackson.databind.ObjectMapper;
 
@Service
public class PriceServicesImpl  implements IPriceServices {
 
 
 
    @Resource
    StockSettingMapper stockSettingMapper;
 
 
    @Resource
    StockMapper stockMapper;
 
    @Autowired
    IStockConfigServices iStockConfigServices;
 
    @Resource
    StockDzMapper stockDZMapper;
 
    @Override
    public BigDecimal getNowPrice(String stockCode) {
        Stock stock = stockMapper.selectOne(new QueryWrapper<Stock>().eq("stock_code",stockCode));
        StockSetting stockSetting = stockSettingMapper.selectOne(new QueryWrapper<StockSetting>().eq("stock_code",stockCode));
        if(stockSetting != null){
            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());
                    if(null != s){
                        Map<String, Object> stringObjectMap = jsonToMap(s);
                        return   new BigDecimal(stringObjectMap.get("Last").toString()).multiply(new BigDecimal(stockSetting.getPrice()));
                    }
                }
            }
        }
 
        String s = doGet(stock.getStockCode());
        if(null != s) {
            Map<String, Object> stringObjectMap = jsonToMap(s);
            return  new BigDecimal(stringObjectMap.get("Last").toString());
        }
        return BigDecimal.ZERO;
    }
 
    @Override
    public Map<String, Object> getNewStock(String stockCode) {
        Stock stock = stockMapper.selectOne(new QueryWrapper<Stock>().eq("stock_code",stockCode));
        String s = doGet(stock.getStockCode());
        if(null != s){
            Map<String, Object> stringObjectMap = jsonToMap(s);
            return   stringObjectMap;
        }
        return null;
    }
 
 
 
    public static Map<String, Object> jsonToMap(String json) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            Object[] array = objectMapper.readValue(json, Object[].class);
            Gson gson = new Gson();
            String s = gson.toJson(array[0]);
            Map<String, Object> map = objectMapper.readValue(s, Map.class);
            return map;
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
 
    private static String lastResult = null;
    private static long lastExecuteTime = 0;
    private static final long MIN_INTERVAL_MS = 3000;
    private static final Object cacheLock = new Object();
 
    public static String doGet(String pid) {
        long currentTime = System.currentTimeMillis();
 
        // 第一次快速检查(不加锁)
        if (lastResult != null && (currentTime - lastExecuteTime) < MIN_INTERVAL_MS) {
            return lastResult;
        }
 
        // 同步块内再次检查并执行
        synchronized (cacheLock) {
            currentTime = System.currentTimeMillis();
            if (lastResult != null && (currentTime - lastExecuteTime) < MIN_INTERVAL_MS) {
                return lastResult;
            }
 
            // 执行POST请求
            String newResult = doActualPost(pid);
 
            // 更新缓存
            lastResult = newResult;
            lastExecuteTime = System.currentTimeMillis();
 
            return newResult;
        }
    }
 
    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);
            }
 
            // 读取响应
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            StringBuilder response = new StringBuilder();
 
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
 
            return response.toString();
 
        } catch (Exception e) {
            e.printStackTrace();
            // 返回缓存或错误信息
            if (lastResult != null) {
                return lastResult;
            }
            return "{\"error\":\"请求失败:" + e.getMessage() + "\"}";
        }
    }
 
    @Override
    public boolean isLimitUpBuy(String stockCode) {
        Stock stock = stockMapper.selectOne(new QueryWrapper<Stock>().eq("stock_code",stockCode));
        StockRealTimeBean stockRealTimeBean =  RedisKeyUtil.getCacheRealTimeStock(stock);
        BigDecimal pcp = new BigDecimal(stockRealTimeBean.getPcp());
        StockConfig stockConfig = iStockConfigServices.queryByKey(EConfigKey.LIMIT_UP_POINT.getCode());
        if(stockConfig == null){
            return true;
        }
 
 
        if(pcp.compareTo(new BigDecimal(0))<0){
            return  true;
        }
 
        if(new BigDecimal(stockConfig.getCValue()).compareTo(pcp)<=0){
            StockConfig limitConfig = iStockConfigServices.queryByKey(EConfigKey.LIMIT_UP_IS_BUY.getCode());
            if(limitConfig.getCValue().equals("1")){
                return  true;
            }
            return false;
        }
        return true;
    }
 
    @Override
    public boolean isLimitDownSell(String stockCode) {
        Stock stock = stockMapper.selectOne(new QueryWrapper<Stock>().eq("stock_code",stockCode));
        StockRealTimeBean stockRealTimeBean =  RedisKeyUtil.getCacheRealTimeStock(stock);
        BigDecimal pcp = new BigDecimal(stockRealTimeBean.getPcp());
        StockConfig stockConfig = iStockConfigServices.queryByKey(EConfigKey.LIMIT_DOWN_POINT.getCode());
        if(stockConfig == null){
            return true;
        }
        if(pcp.compareTo(new BigDecimal(stockConfig.getCValue()))<=0){
            StockConfig limitConfig = iStockConfigServices.queryByKey(EConfigKey.LIMIT_DOWN_IS_SELL.getCode());
            if(!limitConfig.getCValue().equals("1")){
                return false;
            }
        }
        return true;
    }
}