1
zj
2024-08-14 5578112574d523d639765dc491ea6e0c6dc51ded
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;
@@ -40,6 +41,7 @@
     * 同步mexc交易所交易对
     */
    @Scheduled(cron = "0 0/30 * * * ?")
//    @Scheduled(cron = "0/10 * * * * ?")
    public void syncCurrency() {
        //  使用Lock来确保同步
        syncCurrencyLock.lock();
@@ -73,15 +75,8 @@
            List<CurrencyMexcBo> getList = gson.fromJson(symbolsJson, new TypeToken<List<CurrencyMexcBo>>() {
            }.getType());
            //  获取数据库中已有的symbol列表
            List<Currency> dbList = currencyService.list();
            Set<String> symbolSet = dbList.stream().map(Currency::getSymbol).collect(Collectors.toSet());
            //  比对接口返回的数据和数据库中已有的数据,找出新增的数据
            List<CurrencyMexcBo> saveList = getList.stream()
                    .filter(CurrencyMexcBo -> !symbolSet.contains(CurrencyMexcBo.getSymbol()))
                    .collect(Collectors.toList());
            List<Currency> currencies = ConverterUtil.convertToList(saveList, Currency.class);
            currencyService.remove(new LambdaQueryWrapper<Currency>().eq(Currency::getSource,"mexc"));
            List<Currency> currencies = ConverterUtil.convertToList(getList, Currency.class);
            //  批量保存新增数据到数据库
            if(CollectionUtils.isNotEmpty(currencies)){
                currencyService.saveBatch(currencies);