| | |
| | | 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; |
| | |
| | | * 同步mexc交易所交易对 |
| | | */ |
| | | @Scheduled(cron = "0 0/30 * * * ?") |
| | | // @Scheduled(cron = "0/10 * * * * ?") |
| | | public void syncCurrency() { |
| | | // 使用Lock来确保同步 |
| | | syncCurrencyLock.lock(); |
| | |
| | | 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); |