新版仿ok交易所-后端
zyy
2025-10-28 e84b3c45f7ecef39926b582b10d03b30ff77ee95
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
package com.yami.trading.admin.task;
 
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.yami.trading.api.websocket.WebSocketServer;
import com.yami.trading.api.websocket.WebSocketSession;
import com.yami.trading.bean.data.domain.Realtime;
import com.yami.trading.bean.data.domain.StockMarket;
import com.yami.trading.bean.item.domain.Item;
import com.yami.trading.common.util.MarketOpenChecker;
import com.yami.trading.common.util.ThreadUtils;
import com.yami.trading.common.web.ResultObject;
import com.yami.trading.huobi.data.AdjustmentValueCache;
import com.yami.trading.huobi.data.DataCache;
import com.yami.trading.huobi.data.internal.DataDBService;
import com.yami.trading.service.item.ItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
 
/**
 * 行情数据推送Job
 */
@Component
@Slf4j
public class RealtimePushJob implements Runnable {
    @Autowired
    private ItemService itemService;
    @Autowired
    DataDBService dataDBService;
 
    public void start() {
        new Thread(this, "realtimePushJob").start();
        log.info("启动realtimePushJob!");
    }
 
    public void run() {
 
        while (true) {
            try {
                this.realtimeHandle();
            } catch (Exception e) {
                log.error("run fail", e);
            } finally {
                ThreadUtils.sleep(1000);
            }
        }
    }
 
    private void realtimeHandle() {
        try {
            Map<String, String> realtimeResultMap = new HashMap<>();
 
            // 行情实时价格
            if (!WebSocketServer.realtimeMap.isEmpty()) {
 
                // 客户端请求的所有币种,去重集合
                Set<String> symbolSet = new HashSet<String>();
                for (String socketKey : WebSocketServer.realtimeMap.keySet()) {
                    WebSocketSession webSocketSession = WebSocketServer.realtimeMap.get(socketKey);
                    String symbolKey = webSocketSession.getParam();
                    symbolSet.add(symbolKey);
                }
 
                for (String symbol : symbolSet) {
                    Realtime realtimeData = DataCache.getRealtime(symbol);
                    if (realtimeData == null) {
                        log.error("realtimeHandle 获取{} 数据为空", symbol);
                    }
                    this.realtimeRevise(realtimeResultMap, realtimeData, symbol);
                }
 
                if (realtimeResultMap.isEmpty()) {
                    return;
                }
 
                for (String socketKey : WebSocketServer.realtimeMap.keySet()) {
//                    long timeMillins = System.currentTimeMillis();
                    //WebSocketServer server = WebSocketServer.realtimeMap.get(socketKey);
//                    if (server.getTimeStr() != 0 && timeMillins > server.getTimeStr()) {
//                        server.onClose();
//                        return;
//                    }
                    WebSocketSession webSocketSession = WebSocketServer.realtimeMap.get(socketKey);
 
                    String type = webSocketSession.getType();
                    String symbolKey = webSocketSession.getParam();
                    WebSocketServer.sendToMessageById(socketKey, realtimeResultMap.get(symbolKey), type);
                }
            }
 
        } catch (Throwable e) {
            e.printStackTrace();
        }
 
    }
 
    /**
     * 行情实时价格解析
     */
    private void realtimeRevise(Map<String, String> realtimeResultMap, Realtime realtime, String symbol) {
 
        ResultObject realtimeResult = new ResultObject();
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        if (realtime == null) {
            return;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        Integer decimal = itemService.getDecimal(symbol);
 
        map.put("symbol", symbol);
        map.put("timestamp", realtime.getTs());
        map.put("current_time", realtime.getCurrentTime());
        map.put("name", realtime.getName());
        map.put("change_ratio", dataDBService.getChangeRatio(realtime, symbol));
        map.put("netChange", realtime.getNetChange());
        map.put("open", realtime.getOpen());
        map.put("close", realtime.getClose());
        map.put("high", realtime.getHigh());
        map.put("low", realtime.getLow());
 
        BigDecimal currentValue = AdjustmentValueCache.getCurrentValue().get(symbol);
        /*if (currentValue != null) {
            map.put("low", realtime.getLow().add(currentValue));
        }
        if (currentValue != null) {
            BigDecimal newLow = realtime.getLow().add(currentValue);
            if (newLow.compareTo(realtime.getHigh()) > 0) {
                map.put("high", realtime.getHigh().add(currentValue));
            }
            map.put("low", newLow);
        }*/
 
        if( realtime.getVolume() != null){
            map.put("volume", realtime.getVolume().setScale(2, RoundingMode.HALF_UP));
        }else{
            map.put("volume", realtime.getVolume());
        }
        if( realtime.getAmount() != null){
            map.put("amount", realtime.getAmount().setScale(2, RoundingMode.HALF_UP));
        }else{
            map.put("amount", realtime.getAmount());
        }
        map.put("ask", realtime.getAsk());
        map.put("bid", realtime.getBid());
        StockMarket market = DataCache.getMarket(symbol);
        Item bySymbol = itemService.findBySymbol(symbol);
        if("1".equals(bySymbol.getFake())){
            // 假ETF默认取美股
            market = DataCache.getMarket("AAPL");
        }
        if(Item.forex.equalsIgnoreCase(bySymbol.getType())){
             market = new StockMarket();
             market.setTime_zone( "Asia/Shanghai");
             if(MarketOpenChecker.isMarketOpenByItemCloseType(bySymbol.getOpenCloseType())){
                 market.setStatus("交易中");
             }else{
                 market.setStatus("未开盘");
             }
             market.calculate();
             map.put("market", market);
        }
        if (market != null) {
            market.calculate();
            map.put("market", market);
        }
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(realtime);
        for (String key : stringObjectMap.keySet()) {
            if (!map.containsKey(key)) {
                map.put(key, stringObjectMap.get(key));
                BigDecimal open = realtime.getClose();
                map.put("open", open != null ? open.setScale(decimal, RoundingMode.HALF_UP) : null);
                BigDecimal close = realtime.getClose();
                map.put("close", close != null ? close.setScale(decimal, RoundingMode.HALF_UP) : null);
                BigDecimal high = realtime.getHigh();
                map.put("high", high != null ? high.setScale(decimal, RoundingMode.HALF_UP) : null);
                BigDecimal low = realtime.getLow();
                map.put("low", low != null ? low.setScale(decimal, RoundingMode.HALF_UP) : null);
                /*if (currentValue != null) {
                    BigDecimal newLow = low != null ? low.add(currentValue).setScale(decimal, RoundingMode.HALF_UP) : BigDecimal.ZERO;
                    if (newLow.compareTo(high) > 0) {
                        map.put("high", high.add(currentValue).setScale(decimal, RoundingMode.HALF_UP));
                    }
                    map.put("low", newLow);
                }*/
 
            }
        }
 
        list.add(map);
        realtimeResult.setData(list);
        realtimeResultMap.put(realtime.getSymbol(), JSONObject.toJSONString(realtimeResult));
    }
 
}