1
zj
2024-09-24 93feeedde5a4be55aaacfdd2307f44f8ceab5cc2
websocketSerivce/src/main/java/org/example/task/GateStock.java
@@ -10,10 +10,13 @@
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;
@@ -44,7 +47,7 @@
    /**
     * 同步gate交易所交易对
     */
    @Scheduled(cron = "0 0/40 * * * ?")
    @Scheduled(cron = "0 0/35 * * * ?")
//    @Scheduled(cron = "0/10 * * * * ?")
    public void syncCurrency() {
        //  使用Lock来确保同步
@@ -73,13 +76,27 @@
            getList.parallelStream().forEach(person -> person.setId(StringUtils.remove(person.getId(), "_")));
            //  获取数据库中已有的symbol列表
            List<Currency> dbList = currencyService.list(new LambdaQueryWrapper<Currency>().eq(Currency::getSource, "gate"));
            Set<String> symbolSet = dbList.stream().map(Currency::getSymbol).collect(Collectors.toSet());
            //  删除已经下架的币种
            Set<String> symbolSet = getList.stream().map(CurrencyGateBo::getId).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::getSymbol,f.getSymbol()));
                });
            }
            //  比对接口返回的数据和数据库中已有的数据,找出新增的数据
            Set<String> loclSymbolSet = dbList.stream().map(Currency::getSymbol).collect(Collectors.toSet());
            List<Currency> saveList = getList.stream()
                    .filter(currency -> !symbolSet.contains(currency.getId()))
                    .filter(currency -> !loclSymbolSet.contains(currency.getId()))
                    .map(currency -> {
                        Currency newCurrency = new Currency();
                        newCurrency.setSymbol(currency.getId());