新版仿ok交易所-后端
1
zj
2026-03-11 75c2cb06b27b8812427f16c8657dc2eb9b4d09e5
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
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;
 
    }
 
    public void adjust(String symbol, Double second, BigDecimal value) {
        AdjustmentValueCache.getDelayValue().remove(symbol);
        AdjustmentValueCache.getPreAllocatedAdjustments().remove(symbol);
        AdjustmentValueCache.getCurrentAdjustmentIndex().remove(symbol);
        AdjustmentValueCache.getFrequency().remove(symbol);
 
        BigDecimal currentValue = this.adjustmentValueService.getCurrentValue(symbol);
        if (currentValue == null) {
            Realtime realtime = this.dataService.realtime(symbol).get(0);
            currentValue = realtime.getClose();
        }
        String log = MessageFormat.format("ip:" + IPHelper.getIpAddr() + ",管理员调整行情,币种:{0},原值:{1},调整值:{2},调整时间:{3}",
                symbol, currentValue.toPlainString(), value.toPlainString(), second);
 
        this.adjustmentValueService.adjust(symbol, value, second);
        saveLog(log);
        ThreadUtils.sleep(1000);
 
    }
 
    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);
    }
}