1
zj
2024-08-22 9f3859466f402f3e4145dbdd405a8da2da14015e
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
package com.nq.utils.task.digiccy;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
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 com.nq.dao.CurrencyBeanMapper;
import com.nq.enums.EStockType;
import com.nq.pojo.CurrencyBean;
import com.nq.service.CurrencyBeanService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.io.IOException;
import java.util.Currency;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
 
 
/**
 * @program: dabaogp
 * @description: 获取所有交易对     暂时不放开,采用手动添加
 * @create: 2024-08-21 11:33
 **/
@Slf4j
@Component
public class GetSymbolsTask {
 
    @Autowired
    private CurrencyBeanService currencyBeanService;
 
    private final AtomicBoolean symbolsData = new AtomicBoolean(false);
 
    private final Lock symbolsLock = new ReentrantLock();
    
    /**
     * 同步系统所需要的股票
     */
//    @Scheduled(cron = "0/10 * * * * ?")
    public void symbolsTask() {
        if (symbolsData.get()) { // 判断任务是否在处理中
            return;
        }
        if (symbolsLock.tryLock()) {
            try {
                symbolsData.set(true); // 设置处理中标识为true
                getSymbols();
            } catch (IOException e) {
                log.error("火币获取交易对报错:"+e.getMessage());
            } finally {
                symbolsLock.unlock();
                symbolsData.set(false); // 设置处理中标识为false
            }
        }
    }
 
    private void getSymbols() throws IOException {
        String json = doGet();
        if (json != null && !json.isEmpty()) {
            ObjectMapper objectMapper = new ObjectMapper();
            Map<String, Object> map = objectMapper.readValue(json, new TypeReference<Map<String, Object>>() {
            });
            String symbolsJson = objectMapper.writeValueAsString(map.get("data"));
            Gson gson = new Gson();
            List<CurrencyBean> currencyBeans= gson.fromJson(symbolsJson, new TypeToken<List<CurrencyBean>>() {}.getType());
 
            List<CurrencyBean> dbList = currencyBeanService.list(new LambdaQueryWrapper<CurrencyBean>());
 
            //  删除已经下架的币种
//            Set<String> symbolSet = currencyBeans.stream().map(CurrencyBean::getSc).collect(Collectors.toSet());
//            List<CurrencyBean> removeList = dbList.stream()
//                    .filter(currency -> !symbolSet.contains(currency.getSc()))
//                    .collect(Collectors.toList());
//
//            if(CollectionUtils.isNotEmpty(removeList)){
//                removeList.forEach(f->{
//                    currencyBeanService.remove(new LambdaQueryWrapper<CurrencyBean>().eq(CurrencyBean::getSc,f.getSc()));
//                });
//            }
 
            //  比对接口返回的数据和数据库中已有的数据,找出新增的数据
            Set<String> loclSymbolSet = dbList.stream().map(CurrencyBean::getSc).collect(Collectors.toSet());
            List<CurrencyBean> saveList = currencyBeans.stream()
                    .filter(currency -> !loclSymbolSet.contains(currency.getSc()))
                    .map(currency -> {
                        CurrencyBean newCurrency = new CurrencyBean();
                        newCurrency.setSc(currency.getSc());
                        newCurrency.setDn(currency.getDn());
                        newCurrency.setBcdn(currency.getBcdn());
                        newCurrency.setQcdn(currency.getQcdn());
                        newCurrency.setTe(currency.getTe());
                        return newCurrency;
                    })
                    .collect(Collectors.toList());
 
            //  批量保存新增数据到数据库
            if (CollectionUtils.isNotEmpty(saveList)) {
                currencyBeanService.saveBatch(saveList);
            }
        } else {
            log.info("同步bitget交易所交易对,外部接口返回数据为空");
        }
    }
 
    private String doGet() throws IOException {
        HttpClient httpClient = HttpClients.createDefault();
        HttpGet request = new HttpGet("https://api-aws.huobi.pro/v2/settings/common/symbols");
        HttpResponse response = httpClient.execute(request);
        try {
            // 处理响应内容
            HttpEntity entity = response.getEntity();
            String responseBody = EntityUtils.toString(entity);
            return responseBody;
        } finally {
            // 确保释放资源
            EntityUtils.consume(response.getEntity());
        }
    }
}