1
zj
2024-08-15 8d53c609a6589158d68b5945cdb3061460a43d47
websocketSerivce/src/main/java/org/example/task/MexcStock.java
@@ -1,5 +1,6 @@
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;
@@ -10,9 +11,11 @@
import org.example.common.MexcMarketDataClient;
import org.example.pojo.Currency;
import org.example.pojo.ExchangeInfo;
import org.example.pojo.bo.CurrencyKucoin;
import org.example.pojo.bo.CurrencyMexcBo;
import org.example.server.impl.CurrencySerivceImpl;
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;
@@ -39,7 +42,8 @@
    /**
     * 同步mexc交易所交易对
     */
    @Scheduled(cron = "0 0/30 * * * ?")
    @Scheduled(cron = "0 0/45 * * * ?")
//    @Scheduled(cron = "0/10 * * * * ?")
    public void syncCurrency() {
        //  使用Lock来确保同步
        syncCurrencyLock.lock();
@@ -74,12 +78,28 @@
            }.getType());
            //  获取数据库中已有的symbol列表
            List<Currency> dbList = currencyService.list();
            Set<String> symbolSet = dbList.stream().map(Currency::getSymbol).collect(Collectors.toSet());
            List<Currency> dbList = currencyService.list(new LambdaQueryWrapper<Currency>().eq(Currency::getSource, "mexc"));
            //  删除已经下架的币种
            Set<String> symbolSet = getList.stream().map(CurrencyMexcBo::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("mexc"+f.getSymbol());
                    currencyService.remove(new LambdaQueryWrapper<Currency>().eq(Currency::getSymbol,f.getSymbol()));
                });
            }
            //  比对接口返回的数据和数据库中已有的数据,找出新增的数据
            Set<String> loclSymbolSet = dbList.stream().map(Currency::getSymbol).collect(Collectors.toSet());
            List<CurrencyMexcBo> saveList = getList.stream()
                    .filter(CurrencyMexcBo -> !symbolSet.contains(CurrencyMexcBo.getSymbol()))
                    .filter(CurrencyMexcBo -> !loclSymbolSet.contains(CurrencyMexcBo.getSymbol()))
                    .collect(Collectors.toList());
            List<Currency> currencies = ConverterUtil.convertToList(saveList, Currency.class);
            //  批量保存新增数据到数据库