新版仿ok交易所-后端
1
zj
2026-05-21 98a5e458479e7f0034fda19e12cf7c04675f88c8
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package com.yami.trading.admin.facade;
 
import cn.hutool.core.collection.CollectionUtil;
import com.google.common.collect.Lists;
import com.yami.trading.bean.data.domain.Realtime;
import com.yami.trading.bean.item.domain.Item;
import com.yami.trading.bean.item.dto.MarketQuotations;
import com.yami.trading.bean.item.dto.MarketQuotationsAdjust;
import com.yami.trading.bean.model.Log;
import com.yami.trading.common.constants.Constants;
import com.yami.trading.common.lang.LangUtils;
import com.yami.trading.common.util.Arith;
import com.yami.trading.common.util.IPHelper;
import com.yami.trading.common.util.ThreadUtils;
import com.yami.trading.huobi.data.AdjustmentValueCache;
import com.yami.trading.huobi.data.DataCache;
import com.yami.trading.huobi.data.internal.AdjustmentValueService;
import com.yami.trading.huobi.data.model.AdjustmentValue;
import com.yami.trading.security.common.model.YamiSysUser;
import com.yami.trading.security.common.util.SecurityUtils;
import com.yami.trading.service.data.DataService;
import com.yami.trading.service.item.ItemService;
import com.yami.trading.service.system.LogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.MessageFormat;
import java.util.*;
 
@Component
@Slf4j
public class MarketQuotationsFacade {
    @Qualifier("dataService")
    @Autowired
    private DataService dataService;
 
    @Autowired
    private AdjustmentValueService adjustmentValueService;
 
    @Autowired
    private ItemService itemService;
 
    @Autowired
    private LogService logService;
 
    /**
     * 行情列表
     *
     * @return
     */
    public List<MarketQuotations> marketQuotationsList(List<String> symbols) {
        if (CollectionUtils.isEmpty(symbols)) {
            return null;
        }
        List<MarketQuotations> resultList = Lists.newLinkedList();
        List<Realtime> realtimes = this.dataService.realtime(StringUtils.join(symbols, ","));
        Set<String> symbolKey = new HashSet<String>();
        for (Realtime realtime : realtimes) {
            Integer decimal = itemService.getDecimal(realtime.getSymbol());
            MarketQuotations marketQuotations = new MarketQuotations();
            if (symbolKey.contains(realtime.getSymbol())) continue;
            String name = itemService.findBySymbol(realtime.getSymbol()).getName();
            marketQuotations.setName(name);
 
            marketQuotations.setSymbol(realtime.getSymbol());
            BigDecimal currentValue = this.adjustmentValueService.getCurrentValue(realtime.getSymbol());
            if (currentValue == null) {
                marketQuotations.setAdjustValue("0");
                marketQuotations.setNewPrice(Arith.str(realtime.getClose(), decimal));
            } else {
                marketQuotations.setAdjustValue(Arith.str(currentValue, decimal));
                // 原来价格是调整之后减去调整值
                marketQuotations.setNewPrice(realtime.getClose().subtract(currentValue).setScale(decimal, BigDecimal.ROUND_HALF_UP).toPlainString());
            }
            marketQuotations.setAfterValue(Arith.str(realtime.getClose(), decimal));
            resultList.add(marketQuotations);
            symbolKey.add(realtime.getSymbol());
        }
        return resultList;
    }
 
    /**
     * 获取详情
     *
     * @param symbol
     * @return
     */
    public MarketQuotationsAdjust getDetails(String symbol) {
        MarketQuotationsAdjust marketQuotationsAdjust = new MarketQuotationsAdjust();
        Integer decimal = itemService.getDecimal(symbol);
        marketQuotationsAdjust.setSymbol(symbol);
        Realtime realtime = DataCache.getRealtime(symbol);
        if (realtime == null) {
            List<Realtime> realtimeList = this.dataService.realtime(symbol);
            if (CollectionUtil.isEmpty(realtimeList)) {
                log.error("{} 当前没有实时价格", symbol);
                return marketQuotationsAdjust;
            }
            realtime = realtimeList.get(0);
        }
        Item item = this.itemService.findBySymbol(symbol);
        BigDecimal currentValue = this.adjustmentValueService.getCurrentValue(symbol);
        if (currentValue == null) {
            marketQuotationsAdjust.setAdjustValue(Arith.str(0, decimal));
        } else {
            marketQuotationsAdjust.setAdjustValue(Arith.str(currentValue, decimal));
        }
        if (currentValue == null) {
            marketQuotationsAdjust.setNewPrice(Arith.str(realtime.getClose(), decimal));
        } else {
            marketQuotationsAdjust.setNewPrice(realtime.getClose().subtract(currentValue).setScale(decimal, RoundingMode.HALF_UP).toPlainString());
        }
        marketQuotationsAdjust.setPips(Arith.str(item.getPips(), decimal));
        marketQuotationsAdjust.setAfterValue(Arith.str(realtime.getClose(), decimal));
 
        AdjustmentValue delayValue = this.adjustmentValueService.getDelayValue(symbol);
 
        if (delayValue != null) {
            marketQuotationsAdjust.setDelayValue(Arith.str(delayValue.getValue(), decimal));
            marketQuotationsAdjust.setDelaySecond("" + delayValue.getSecond());
        }
        return marketQuotationsAdjust;
    }
 
    /**
     * 行情调整预计算值
     *
     * @param symbol
     * @param type
     * @param value
     * @return
     */
    public Map<String, String> calculateValue(String symbol, String type, double value) {
        Map<String, String> resultMap = new HashMap<String, String>();
        Integer decimal = itemService.getDecimal(symbol);
 
        Realtime realtime = null;
 
        realtime = this.dataService.realtime(symbol).get(0);
        Item item = this.itemService.findBySymbol(symbol);
        BigDecimal currentValue = this.adjustmentValueService.getCurrentValue(symbol);
        if (currentValue == null) {
            resultMap.put("newPrice", Arith.str(realtime.getClose(), decimal));
        } else {
            resultMap.put("newPrice", realtime.getClose().subtract(currentValue).toPlainString());
        }
 
        double temp;
        if (type.equalsIgnoreCase("0")) {
            temp = Arith.add(value, item.getPips().doubleValue());
            // 调整量
            resultMap.put("adjustCurrentValue", Arith.str(temp, decimal));
            // 调整后的值
            resultMap.put("adjustValueAfter", Arith.add(realtime.getClose().doubleValue(), temp, decimal));
        } else if (type.equalsIgnoreCase("1")) {
            temp = Arith.sub(value, item.getPips().doubleValue());
 
            resultMap.put("adjustCurrentValue", temp + "");
            resultMap.put("adjustValueAfter", Arith.add(realtime.getClose().doubleValue(), temp, decimal));
        } else {
            temp = value;
            resultMap.put("adjustCurrentValue", temp + "");
            resultMap.put("adjustValueAfter", Arith.add(realtime.getClose().doubleValue(), temp, decimal));
        }
 
        if (currentValue == null) {
            resultMap.put("adjustValue", Arith.str(item.getPips(), decimal));
        } else {
            resultMap.put("adjustValue", Arith.add(temp, currentValue.doubleValue(), decimal));
        }
        AdjustmentValue delayValue = this.adjustmentValueService.getDelayValue(symbol);
 
        if (delayValue != null) {
            resultMap.put("delayValue", Arith.str(delayValue.getValue(), decimal));
            resultMap.put("delaySecond", delayValue.getSecond() + "");
        }
        return resultMap;
 
    }
 
    /**
     * 提交调整(与 getValue.action 预计算使用同一套 type 规则)。
     */
    public void adjust(String symbol, Double second, BigDecimal value, String type) {
        AdjustmentValueCache.getDelayValue().remove(symbol);
        AdjustmentValueCache.getPreAllocatedAdjustments().remove(symbol);
        AdjustmentValueCache.getCurrentAdjustmentIndex().remove(symbol);
        AdjustmentValueCache.getFrequency().remove(symbol);
 
        String adjustType = StringUtils.isBlank(type) ? "2" : type;
        BigDecimal effectiveDelta = resolveEffectiveAdjustDelta(symbol, adjustType, value);
        if (effectiveDelta == null || effectiveDelta.compareTo(BigDecimal.ZERO) == 0) {
            log.warn("行情调整无效 symbol={} value={} type={}", symbol, value, adjustType);
            return;
        }
 
        BigDecimal beforeAdjust = adjustmentValueService.getCurrentValue(symbol);
        double secondVal = second == null ? 0D : second;
 
        String logContent = MessageFormat.format(
                "ip:" + IPHelper.getIpAddr() + ",管理员调整行情,币种:{0},调整前累计:{1},本次增量:{2},type:{3},延迟秒:{4}",
                symbol,
                beforeAdjust == null ? "0" : beforeAdjust.toPlainString(),
                effectiveDelta.toPlainString(),
                adjustType,
                secondVal);
 
        adjustmentValueService.adjust(symbol, effectiveDelta, secondVal);
 
        // 立即生效时同步内存行情,避免只改缓存累计值但页面仍显示旧价
        if (secondVal <= 0) {
            syncRealtimeCacheAfterAdjust(symbol, effectiveDelta);
        } else {
            log.info("延迟调整已提交 symbol={} 目标增量={} 时长约{}秒,价格将分步变化(非瞬间到位)",
                    symbol, effectiveDelta.toPlainString(), secondVal);
        }
 
        saveLog(logContent);
        ThreadUtils.sleep(500);
    }
 
    /**
     * 与 calculateValue 一致:type 0/1 在输入值基础上加减 pips,type 2 直接使用输入值作为本次增量。
     */
    private BigDecimal resolveEffectiveAdjustDelta(String symbol, String type, BigDecimal inputValue) {
        if (inputValue == null) {
            return null;
        }
        Item item = itemService.findBySymbol(symbol);
        if (item == null) {
            return inputValue;
        }
        double pips = item.getPips() != null ? item.getPips().doubleValue() : 0D;
        double v = inputValue.doubleValue();
        if ("0".equalsIgnoreCase(type)) {
            return BigDecimal.valueOf(Arith.add(v, pips));
        }
        if ("1".equalsIgnoreCase(type)) {
            return BigDecimal.valueOf(Arith.sub(v, pips));
        }
        return inputValue;
    }
 
    /** 将本次增量反映到 DataCache,与 WebSocket/采集任务展示逻辑一致 */
    private void syncRealtimeCacheAfterAdjust(String symbol, BigDecimal effectiveDelta) {
        Realtime realtime = DataCache.getRealtime(symbol);
        if (realtime == null) {
            List<Realtime> list = dataService.realtime(symbol);
            if (CollectionUtil.isEmpty(list)) {
                return;
            }
            realtime = list.get(0);
        }
        Integer decimal = itemService.getDecimal(symbol);
        realtime.setClose(realtime.getClose().add(effectiveDelta).setScale(decimal, RoundingMode.HALF_UP));
        if (realtime.getAsk() != null) {
            realtime.setAsk(realtime.getAsk().add(effectiveDelta).setScale(decimal, RoundingMode.HALF_UP));
        }
        if (realtime.getBid() != null) {
            realtime.setBid(realtime.getBid().add(effectiveDelta).setScale(decimal, RoundingMode.HALF_UP));
        }
        DataCache.putRealtime(symbol, realtime);
    }
 
    public void saveLog(String content) {
        YamiSysUser sysUser = SecurityUtils.getSysUser();
        Log log = new Log();
        log.setCategory(Constants.LOG_CATEGORY_OPERATION);
        log.setOperator(sysUser.getUsername());
        log.setUsername(sysUser.getUsername());
        log.setUserId(sysUser.getUserId().toString());
        log.setLog(content);
        log.setCreateTime(new Date());
        logService.save(log);
    }
}