1
zj
2024-08-14 5578112574d523d639765dc491ea6e0c6dc51ded
websocketSerivce/src/main/java/org/example/task/BitgetStock.java
@@ -1,6 +1,8 @@
package org.example.task;
import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson.JSON;
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;
@@ -8,6 +10,7 @@
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
@@ -19,6 +22,7 @@
import org.example.pojo.Currency;
import org.example.pojo.bo.CurrencyBitgetBo;
import org.example.server.impl.CurrencySerivceImpl;
import org.example.util.ConverterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@@ -28,6 +32,7 @@
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.SQLOutput;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -54,6 +59,7 @@
     * 同步bitget交易所交易对
     */
    @Scheduled(cron = "0 0/45 * * * ?")
//    @Scheduled(cron = "0/10 * * * * ?")
    public void syncCurrency() {
        //  使用Lock来确保同步
        syncCurrencyLock.lock();
@@ -83,14 +89,10 @@
            }.getType());
            getList.parallelStream().forEach(person -> person.setSymbol(StringUtils.remove(person.getSymbol(), "-")));
            //  获取数据库中已有的symbol列表
            List<Currency> dbList = currencyService.list();
            Set<String> symbolSet = dbList.stream().map(Currency::getSymbol).collect(Collectors.toSet());
            currencyService.remove(new LambdaQueryWrapper<Currency>().eq(Currency::getSource,"bitget"));
            //  比对接口返回的数据和数据库中已有的数据,找出新增的数据
            List<Currency> saveList = getList.stream()
                    .filter(currency -> !symbolSet.contains(currency.getSymbol()))
                    .map(currency -> {
                        Currency newCurrency = new Currency();
                        newCurrency.setSymbol(currency.getSymbol());
@@ -100,7 +102,6 @@
                        return newCurrency;
                    })
                    .collect(Collectors.toList());
            //  批量保存新增数据到数据库
            if (CollectionUtils.isNotEmpty(saveList)) {
                currencyService.saveBatch(saveList);