|
@@ -0,0 +1,80 @@
|
|
|
|
|
+package com.liangjiang.price_collection.service.impl;
|
|
|
|
|
+
|
|
|
|
|
+import com.google.common.collect.Interner;
|
|
|
|
|
+import com.google.common.collect.Interners;
|
|
|
|
|
+import com.liangjiang.price_collection.dto.BackupsInfo;
|
|
|
|
|
+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 lombok.RequiredArgsConstructor;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.jdbc.BadSqlGrammarException;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+@Service
|
|
|
|
|
+@RequiredArgsConstructor
|
|
|
|
|
+public class TableServiceImpl implements ITableService {
|
|
|
|
|
+ private final TableMapper tableMapper;
|
|
|
|
|
+ private final IBackupsInfoService backupsInfoService;
|
|
|
|
|
+ private final Interner<Object> interner = Interners.newWeakInterner();
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public boolean createTable(String source, String coin, String type){
|
|
|
|
|
+ String tableName = String.format("%s_%s_%s", source, coin, type).toUpperCase();
|
|
|
|
|
+ synchronized (interner.intern(tableName)){
|
|
|
|
|
+ try {
|
|
|
|
|
+ tableMapper.createTable(tableName);
|
|
|
|
|
+ return true;
|
|
|
|
|
+ } catch (Exception ex){
|
|
|
|
|
+ log.error(tableName + "创建表失败", ex);
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<String> getTableNames(){
|
|
|
|
|
+ return tableMapper.getTableName();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void savePriceBatch(List<PriceInfoDto> dtos){
|
|
|
|
|
+ dtos.forEach(this::savePrice);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void savePrice(PriceInfoDto dto){
|
|
|
|
|
+ String tableName = String.format("%s_%s_%s", dto.getSource(), dto.getCoin(), dto.getType()).toUpperCase();
|
|
|
|
|
+ try {
|
|
|
|
|
+ // 尝试先写入
|
|
|
|
|
+ this.tableMapper.savePrice(tableName, dto.getTime(), dto.getAsk(), dto.getBid());
|
|
|
|
|
+ } catch (BadSqlGrammarException ex){// 捕获表不存在的异常
|
|
|
|
|
+ // 建表
|
|
|
|
|
+ this.createTable(dto.getSource(), dto.getCoin(), dto.getType());
|
|
|
|
|
+ try {
|
|
|
|
|
+ // 写入
|
|
|
|
|
+ this.tableMapper.savePrice(tableName, dto.getTime(), dto.getAsk(), dto.getBid());
|
|
|
|
|
+ } catch (Exception er){
|
|
|
|
|
+ // 写入存档表
|
|
|
|
|
+ BackupsInfo backupsInfo = new BackupsInfo();
|
|
|
|
|
+ backupsInfo.setTableName(tableName);
|
|
|
|
|
+ backupsInfo.setAsk(dto.getAsk());
|
|
|
|
|
+ backupsInfo.setBid(dto.getBid());
|
|
|
|
|
+ backupsInfo.setTime(dto.getTime());
|
|
|
|
|
+ backupsInfoService.save(backupsInfo);
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e){ // 其他异常
|
|
|
|
|
+ // 写入存档表
|
|
|
|
|
+ BackupsInfo backupsInfo = new BackupsInfo();
|
|
|
|
|
+ backupsInfo.setTableName(tableName);
|
|
|
|
|
+ backupsInfo.setAsk(dto.getAsk());
|
|
|
|
|
+ backupsInfo.setBid(dto.getBid());
|
|
|
|
|
+ backupsInfo.setTime(dto.getTime());
|
|
|
|
|
+ backupsInfoService.save(backupsInfo);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|