|
@@ -1,12 +1,9 @@
|
|
|
package com.liangjiang.price_collection.service.impl;
|
|
package com.liangjiang.price_collection.service.impl;
|
|
|
|
|
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
-import com.liangjiang.price_collection.dto.BackupsInfo;
|
|
|
|
|
import com.liangjiang.price_collection.dto.ExchangeCoinDto;
|
|
import com.liangjiang.price_collection.dto.ExchangeCoinDto;
|
|
|
import com.liangjiang.price_collection.dto.PriceDto;
|
|
import com.liangjiang.price_collection.dto.PriceDto;
|
|
|
import com.liangjiang.price_collection.dto.PriceInfoDto;
|
|
import com.liangjiang.price_collection.dto.PriceInfoDto;
|
|
|
-import com.liangjiang.price_collection.mapper.TableMapper;
|
|
|
|
|
-import com.liangjiang.price_collection.service.IBackupsInfoService;
|
|
|
|
|
import com.liangjiang.price_collection.service.ITableService;
|
|
import com.liangjiang.price_collection.service.ITableService;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
@@ -20,131 +17,92 @@ import java.util.*;
|
|
|
@Service
|
|
@Service
|
|
|
@RequiredArgsConstructor
|
|
@RequiredArgsConstructor
|
|
|
public class TableServiceImpl implements ITableService {
|
|
public class TableServiceImpl implements ITableService {
|
|
|
- private final TableMapper tableMapper;
|
|
|
|
|
- private final IBackupsInfoService backupsInfoService;
|
|
|
|
|
|
|
+ private final RedisServiceImpl redisService;
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
- public Set<String> getExchangeSelect (){
|
|
|
|
|
- List<String> tableNames = this.getTableNames();
|
|
|
|
|
- if (CollectionUtil.isEmpty(tableNames)){
|
|
|
|
|
- return new HashSet<>();
|
|
|
|
|
- }
|
|
|
|
|
- Set<String> exchanges = new HashSet<>();
|
|
|
|
|
-
|
|
|
|
|
- for (String tableName : tableNames){
|
|
|
|
|
- String[] infos = tableName.split("_");
|
|
|
|
|
- if(infos.length != 3){
|
|
|
|
|
-// System.out.println("exchange = " + tableName);
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
- // 交易所和类型
|
|
|
|
|
- exchanges.add(String.format("%s_%s", infos[1], infos[2]));
|
|
|
|
|
- }
|
|
|
|
|
- return exchanges;
|
|
|
|
|
|
|
+ public Set<String> getExchangeSelect() {
|
|
|
|
|
+ // 使用Redis服务获取所有交易所
|
|
|
|
|
+ return redisService.getAllExchanges();
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
- public Set<String> getCoinSelect(String[] exchanges){
|
|
|
|
|
- if(exchanges == null || exchanges.length == 0){
|
|
|
|
|
- return new HashSet<>();
|
|
|
|
|
- }
|
|
|
|
|
- List<String> tableNames = this.getTableNames();
|
|
|
|
|
- if (CollectionUtil.isEmpty(tableNames)){
|
|
|
|
|
|
|
+ public Set<String> getCoinSelect(String[] exchanges) {
|
|
|
|
|
+ if (exchanges == null || exchanges.length < 2) {
|
|
|
return new HashSet<>();
|
|
return new HashSet<>();
|
|
|
}
|
|
}
|
|
|
- Map<String, Set<String>> exchange_coin = new HashMap<>();
|
|
|
|
|
- for (String exchange : exchanges){
|
|
|
|
|
- exchange_coin.put(exchange, new HashSet<>());
|
|
|
|
|
- }
|
|
|
|
|
- for (String tableName : tableNames){
|
|
|
|
|
- String[] infos = tableName.split("_");
|
|
|
|
|
- if(infos.length != 3){
|
|
|
|
|
-// System.out.println("exchange = " + tableName);
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
- // 交易所和类型
|
|
|
|
|
- String key = String.format("%s_%s", infos[1], infos[2]);
|
|
|
|
|
- if (exchange_coin.containsKey(key)){
|
|
|
|
|
- // 存在加入币对
|
|
|
|
|
- exchange_coin.get(key).add(infos[0]);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- Set<Set<String>> values = new HashSet<>(exchange_coin.values());
|
|
|
|
|
- // 返回交集
|
|
|
|
|
- return getUnion(values);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // 使用Redis服务获取共有币对
|
|
|
|
|
+ return redisService.getCommonCoins(exchanges);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public List<String> getTableNames(){
|
|
public List<String> getTableNames(){
|
|
|
- return tableMapper.getTableName();
|
|
|
|
|
|
|
+ // 在Redis模式下不需要表名
|
|
|
|
|
+ return new ArrayList<>();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public boolean initExchange(ExchangeCoinDto dto){
|
|
public boolean initExchange(ExchangeCoinDto dto){
|
|
|
- if(dto ==null || CollectionUtil.isEmpty(dto.getCoin())){
|
|
|
|
|
- return false;
|
|
|
|
|
- }
|
|
|
|
|
- try {
|
|
|
|
|
- Set<String> tableNames = new HashSet<>();
|
|
|
|
|
- dto.getCoin().forEach(coin -> {
|
|
|
|
|
- String tableName = String.format("%s_%s_%s", coin.replaceAll("_", ""), dto.getSource(), dto.getB_type()).toUpperCase();
|
|
|
|
|
- tableNames.add(tableName);
|
|
|
|
|
- });
|
|
|
|
|
- // 建表
|
|
|
|
|
- this.tableMapper.createTables(tableNames);
|
|
|
|
|
- return true;
|
|
|
|
|
- }catch (Exception ex){
|
|
|
|
|
- log.error("建表初始化失败!", ex);
|
|
|
|
|
- return false;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ // Redis模式下不需要初始化表
|
|
|
|
|
+ return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public void deleteData(List<String> tableNames, Integer time){
|
|
public void deleteData(List<String> tableNames, Integer time){
|
|
|
- this.tableMapper.deleteData(tableNames, time);
|
|
|
|
|
|
|
+ // 清理过期数据由Redis的过期机制和定时任务处理
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public void savePriceBatch(List<PriceInfoDto> dtos){
|
|
public void savePriceBatch(List<PriceInfoDto> dtos){
|
|
|
- if(CollectionUtil.isEmpty(dtos)){
|
|
|
|
|
|
|
+ if (CollectionUtil.isEmpty(dtos)) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
- List<BackupsInfo> addDtos = new LinkedList<>();
|
|
|
|
|
- dtos.forEach(dto -> {
|
|
|
|
|
- addDtos.add(dto.convertAdd());
|
|
|
|
|
- });
|
|
|
|
|
|
|
+
|
|
|
try {
|
|
try {
|
|
|
- // 写入数据
|
|
|
|
|
- this.tableMapper.savePrices(addDtos);
|
|
|
|
|
- } catch (Exception ex){
|
|
|
|
|
- log.error("插入报错", ex);
|
|
|
|
|
- backupsInfoService.saveBatch(addDtos);
|
|
|
|
|
|
|
+ // 使用Redis服务保存数据
|
|
|
|
|
+ redisService.savePriceData(dtos);
|
|
|
|
|
+ } catch (Exception ex) {
|
|
|
|
|
+ log.error("保存价格数据失败", ex);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public List<PriceDto> getPriceInfo(String tableName, Integer startTime, Integer endTime){
|
|
public List<PriceDto> getPriceInfo(String tableName, Integer startTime, Integer endTime){
|
|
|
- return this.tableMapper.getPriceInfo(tableName, startTime, endTime);
|
|
|
|
|
|
|
+ // 从表名解析币对和交易所
|
|
|
|
|
+ String[] parts = tableName.split("_");
|
|
|
|
|
+ if (parts.length < 2) {
|
|
|
|
|
+ return new ArrayList<>();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ String coin = parts[0];
|
|
|
|
|
+ String exchange = parts[1];
|
|
|
|
|
+
|
|
|
|
|
+ return redisService.getPriceData(coin, exchange, startTime, endTime);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public PriceDto getLastByTime(String tableName, Integer startTime){
|
|
public PriceDto getLastByTime(String tableName, Integer startTime){
|
|
|
- return this.tableMapper.getLastByTime(tableName, startTime);
|
|
|
|
|
|
|
+ // 从表名解析币对和交易所
|
|
|
|
|
+ String[] parts = tableName.split("_");
|
|
|
|
|
+ if (parts.length < 2) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ String coin = parts[0];
|
|
|
|
|
+ String exchange = parts[1];
|
|
|
|
|
+
|
|
|
|
|
+ return redisService.getLastPriceData(coin, exchange, startTime);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 3-10: 最少有3个并发消费者,最多可以扩展到10个并发消费者。消息处理的顺序性无法保证(此处业务不涉及强顺序)
|
|
// 3-10: 最少有3个并发消费者,最多可以扩展到10个并发消费者。消息处理的顺序性无法保证(此处业务不涉及强顺序)
|
|
|
@JmsListener(destination = "priceQueue", concurrency = "5-10")
|
|
@JmsListener(destination = "priceQueue", concurrency = "5-10")
|
|
|
public void receivePriceInfo(ObjectMessage objectMessage){
|
|
public void receivePriceInfo(ObjectMessage objectMessage){
|
|
|
-// long start = System.currentTimeMillis();
|
|
|
|
|
try {
|
|
try {
|
|
|
List<PriceInfoDto> param = (List<PriceInfoDto>) objectMessage.getObject();
|
|
List<PriceInfoDto> param = (List<PriceInfoDto>) objectMessage.getObject();
|
|
|
this.savePriceBatch(param);
|
|
this.savePriceBatch(param);
|
|
|
} catch (Exception ex){
|
|
} catch (Exception ex){
|
|
|
log.error("接收消息错误", ex);
|
|
log.error("接收消息错误", ex);
|
|
|
}
|
|
}
|
|
|
-// long end = System.currentTimeMillis();
|
|
|
|
|
-// System.out.println("耗时 = " + (start - end));
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public static Set<String> getUnion(Set<Set<String>> sets) {
|
|
public static Set<String> getUnion(Set<Set<String>> sets) {
|