1
zj
2024-08-14 dedaaf738a1d483b4594d46622eaa9791f7e5bcd
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
package org.example.task;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.example.pojo.Currency;
import org.example.pojo.bo.CurrencyBitgetBo;
import org.example.pojo.bo.CurrencyGateBo;
import org.example.pojo.bo.CurrencyMexcBo;
import org.example.server.impl.CurrencySerivceImpl;
import org.example.common.MarketDataClient;
import org.example.util.ConverterUtil;
import org.example.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
 
/**
 * @program: demo
 * @description: gate交易所
 * @create: 2024-07-15 17:23
 **/
@Component
@Slf4j
public class GateStock {
 
    @Autowired
    private CurrencySerivceImpl currencyService;
 
    private final Lock syncCurrencyLock = new ReentrantLock();
 
    /**
     * 同步gate交易所交易对
     */
    @Scheduled(cron = "0 0/40 * * * ?")
//    @Scheduled(cron = "0/10 * * * * ?")
    public void syncCurrency() {
        //  使用Lock来确保同步
        syncCurrencyLock.lock();
        log.info("【同步gate交易所交易对】---->开始");
        try {
            sync();
        } catch (Exception e) {
            log.error("【同步gate交易所交易对】出现异常:    " + e.getMessage());
            e.printStackTrace();
        } finally {
            syncCurrencyLock.unlock();
            log.info("【同步gate交易所交易对】---->结束");
        }
    }
 
    public void sync() throws JsonProcessingException {
        //  调用外部接口获取数据
        String json = MarketDataClient.doGet("https://api.gateio.ws/api/v4/spot/currency_pairs");
 
        //  对返回数据格式进行校验
        if (json != null && !json.isEmpty()) {
            Gson gson = new Gson();
            List<CurrencyGateBo> getList = gson.fromJson(json, new TypeToken<List<CurrencyGateBo>>() {
            }.getType());
 
            getList.parallelStream().forEach(person -> person.setId(StringUtils.remove(person.getId(), "_")));
 
            List<Currency> dbList = currencyService.list(new LambdaQueryWrapper<Currency>().eq(Currency::getSource, "gate"));
            Set<String> symbolSet = dbList.stream().map(Currency::getSymbol).collect(Collectors.toSet());
            List<Currency> removeList = dbList.stream()
                    .filter(currency -> !symbolSet.contains(currency.getSymbol()))
                    .collect(Collectors.toList());
 
            if(CollectionUtils.isNotEmpty(removeList)){
                removeList.forEach(f->{
                    RedisUtil.delete("gate"+f.getSymbol());
                });
            }
 
 
            currencyService.remove(new LambdaQueryWrapper<Currency>().eq(Currency::getSource,"gate"));
 
            //  比对接口返回的数据和数据库中已有的数据,找出新增的数据
            List<Currency> saveList = getList.stream()
                    .map(currency -> {
                        Currency newCurrency = new Currency();
                        newCurrency.setSymbol(currency.getId());
                        newCurrency.setBaseAsset(currency.getBase());
                        newCurrency.setQuoteAsset(currency.getQuote());
                        newCurrency.setSource(currency.getSource());
                        return newCurrency;
                    })
                    .collect(Collectors.toList());
            //  批量保存新增数据到数据库
            if(CollectionUtils.isNotEmpty(saveList)){
                currencyService.saveBatch(saveList);
            }
        } else {
            log.info("外部接口返回数据为空");
        }
    }
 
}