新版仿ok交易所-后端
1
zyy
2025-09-19 0ec6e1c49ae75b852ff224dd09033d8020621bd8
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
package com.yami.trading.service.item;
 
import cn.hutool.core.collection.CollectionUtil;
import com.alicp.jetcache.Cache;
import com.alicp.jetcache.CacheManager;
import com.alicp.jetcache.anno.CacheInvalidate;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import com.alicp.jetcache.template.QuickConfig;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.yami.trading.bean.item.domain.Item;
import com.yami.trading.bean.item.dto.ItemDTO;
import com.yami.trading.bean.item.dto.ItemLeverageDTO;
import com.yami.trading.bean.robot.domain.Robot;
import com.yami.trading.common.util.ApplicationContextUtils;
import com.yami.trading.common.util.MarketOpenChecker;
import com.yami.trading.common.util.StringUtils;
import com.yami.trading.dao.item.ItemMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.PostConstruct;
import java.io.Serializable;
import java.time.Duration;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 产品Service
 *
 * @author lucas
 * @version 2023-03-10
 */
@Service
@Transactional
@Slf4j
public class ItemService extends ServiceImpl<ItemMapper, Item> {
    public static final String ITEM_CACHE = "mdItemCache";
    public static final String ALL = "mdAll";
 
    @Autowired
    private ItemLeverageService itemLeverageService;
 
    private Map<String, Integer> symbolDecimal = Maps.newHashMap();
 
    @Autowired
    private CacheManager cacheManager;
    private Cache<String, List<Item>> itemCache;
 
    @PostConstruct
    public void init() {
        QuickConfig qc = QuickConfig.newBuilder(ITEM_CACHE)
                .expire(Duration.ofSeconds(3600))
                .cacheType(CacheType.REMOTE) // two level cache
                .build();
        itemCache = cacheManager.getOrCreateCache(qc);
        itemCache.put(ALL, list());
 
    }
 
    public List<Item> findByType(String type) {
        List<Item> items =  ApplicationContextUtils.getApplicationContext().getBean(ItemService.class).list();
        if(items == null){
            LambdaQueryWrapper<Item> queryWrapper = new LambdaQueryWrapper<Item>()
                    .eq(Item::getType, type);
            return super.baseMapper.selectList(queryWrapper);
 
        }
        return items.stream().filter(i -> i.getType().equalsIgnoreCase(type)).collect(Collectors.toList());
 
    }
 
 
    /**
     * 获取多个币对,每个类型的数量
     * @param symbols
     * @return
     */
    public Map<String, Integer> typeCountGroupByType(Collection<String> symbols) {
        // 避免为空时候报错
        symbols.add("-1");
        Map<String, Integer> typeCount = new HashMap<>();
        for(String type : Item.types){
            typeCount.put(type, 0);
        }
        if(CollectionUtil.isEmpty(symbols)){
            return typeCount;
        }
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("SYMBOL", symbols);
        queryWrapper.select("SYMBOL", "TYPE", "count(*) as count")
                .groupBy("TYPE");
        List<Map<String, Object>> maps = baseMapper.selectMaps(queryWrapper);
        int sum = 0;
        for(Map<String, Object> data : maps){
            typeCount.put(data.get("TYPE").toString(), Integer.parseInt(data.get("count").toString()));
            sum += Integer.parseInt(data.get("count").toString());
        }
        typeCount.put("all", sum);
        return typeCount;
 
 
 
    }
 
    /**
     * 通过code 找对象,
     *
     * @param symbol
     * @return
     */
    public Item findBySymbol(String symbol) {
        List<Item> items = ApplicationContextUtils.getApplicationContext().getBean(ItemService.class).list();
        if (CollectionUtil.isNotEmpty(items)) {
            Optional<Item> first = items.stream().filter(i -> symbol.equalsIgnoreCase(i.getSymbol())).findFirst();
            return first.orElse(null);
        }
        LambdaQueryWrapper<Item> queryWrapper = new LambdaQueryWrapper<Item>()
                .eq(Item::getSymbol, symbol)
                .last("LIMIT 1");
        return super.baseMapper.selectOne(queryWrapper);
    }
 
 
    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public ItemDTO findById(String id) {
 
 
        ItemDTO item = baseMapper.findById(id);
        if (item != null) {
            QueryWrapper wrapper = new QueryWrapper();
            List<ItemLeverageDTO> levels = itemLeverageService.findByItemId(id);
            item.setLevels(levels);
        }
        return item;
    }
 
    /**
     * 自定义分页检索
     *
     * @param page
     * @param queryWrapper
     * @return
     */
    public IPage<ItemDTO> findPage(Page<ItemDTO> page, QueryWrapper queryWrapper) {
        queryWrapper.eq("a.del_flag", 0); // 排除已经删除
        return baseMapper.findList(page, queryWrapper);
    }
 
    @Cached(name = ITEM_CACHE, key = "'itemAll'", expire = 3600)
    @Override
    public List<Item> list() {
        List<Item> list = super.list(new LambdaQueryWrapper<>(Item.class).eq(Item::getType,Item.cryptos));
        symbolDecimal = list.stream()
                .collect(Collectors.toMap(Item::getSymbol, Item::getDecimals, (s1, s2) -> s2));
        return list;
    }
 
    @Override
    @CacheInvalidate(name = ITEM_CACHE)
    public boolean updateById(Item item) {
        return super.updateById(item);
    }
 
    @Override
    @CacheInvalidate(name = ITEM_CACHE)
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }
 
    public void reloadListAndCache() {
        init();
    }
 
    /**
     * 获取品种精度
     *
     * @param symbol
     * @return
     */
    public Integer getDecimal(String symbol) {
        return symbolDecimal.getOrDefault(symbol, 0);
    }
 
    public List<String> getAllSymbol() {
 
        List<Item> list = list();
        List<String> result = new ArrayList<>();
        for (Item item : list) {
            result.add(item.getSymbol());
        }
 
        return result;
    }
 
    public List<Item> cacheGetAll() {
        return ApplicationContextUtils.getApplicationContext().getBean(ItemService.class).list(new LambdaQueryWrapper<>(Item.class).eq(Item::getType,Item.cryptos));
    }
 
 
    public List<Item> cacheGetByMarket(String symbol) {
        List<Item> cacheGetAll = cacheGetAll();
        if (StringUtils.isNullOrEmpty(symbol)) {
            return cacheGetAll;
        }
        List<Item> result = new ArrayList<Item>();
        for (Item item : cacheGetAll) {
            if (symbol.equals(item.getSymbol()))
                result.add(item);
        }
        return result;
    }
 
    /**
     * 当前是否开盘
     * @param symbol
     * @return
     */
    public boolean isOpen(String symbol){
        Item bySymbol = findBySymbol(symbol);
        return MarketOpenChecker.isMarketOpenByItemCloseType(bySymbol.getOpenCloseType());
    }
 
    /**
     * 是否开放合约
     * @param item
     * @return
     */
    public boolean isContractTrading(Item item) {
        item = findBySymbol(item.getSymbol());
        //虚拟币新币才判断
        if (item.getType().equals(Item.cryptos) && (item.getCurrencyType() != null && item.getCurrencyType() == 1)) {
            if (item.getTradeType() != null && item.getTradeType().equals("0")) {
                return false;
            }
        }
        return true;
    }
 
    /**
     * 是否停牌状态
     * @return
     */
    public boolean isSuspended(String symbol) {
        //Item item = findBySymbol(symbol);
        Item item = getOne(new LambdaQueryWrapper<Item>().eq(Item::getSymbol, symbol));
        if (item != null) {
            //虚拟币新币才判断
            if (item.getType().equals(Item.cryptos) /*&& (item.getCurrencyType() != null && item.getCurrencyType() == 1)*/) {
                //item = getById(item.getUuid());
                if (item.getStatus() != null && item.getStatus() == 0) {
                    return true;
                }
            }
        }
        return false;
    }
 
}