燕山石化溯源三维电子沙盘-【后端】-服务
1
13693261870
2023-06-16 5b17ccbbe08971f861ff097c62241d2b79e4b5f6
src/main/java/com/yssh/scheduled/ReadCsvTask.java
@@ -1,16 +1,18 @@
package com.yssh.scheduled;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.yssh.entity.VocVals;
import com.yssh.service.VocValsService;
import com.yssh.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@@ -21,70 +23,99 @@
import com.yssh.service.IDictRecordService;
import com.yssh.service.IWarningAnalyseService;
import com.yssh.service.impl.SuYuanServiceImpl;
import com.yssh.utils.CsvParser;
import com.yssh.utils.DateUtils;
import com.yssh.utils.TableStrategy;
import javax.annotation.Resource;
@Component
public class ReadCsvTask {
   protected final Logger logger = LoggerFactory.getLogger(this.getClass());
   protected final Log logger = LogFactory.getLog(this.getClass());
   @Resource
   private VocValsService vocValsService;
   @Autowired
   private CsvFilePathConfig csvFilePathConfig;
   @Autowired
   @Resource
   private SuYuanServiceImpl suYuanService;
   @Autowired
   @Resource
   private CsvFilePathConfig csvFilePathConfig;
   @Resource
   private IDictRecordService dictRecordService;
   @Autowired
   @Resource
   private IWarningAnalyseService warningAnalyseService;
   @Scheduled(cron = "${csv.cron}")
   @Value("${csv.cron_max}")
   private int cronMax = 48;
   @Value("${csv.voc_max}")
   private int vocMax = 120;
   private final static List<String> md5List = new ArrayList<>();
   private final static SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHH");
   public void corpReserveDataSync() {
      /*Date date = DateUtils.getNowDate();
      String time = DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, date);
        String filePath = csvFilePathConfig.getFilePath() + "\\\\" + time + ".csv";
        File file = new File(filePath);
        if (!file.exists()) {
            logger.error(file.getAbsolutePath() + " is not exist !");
            return;
        }
        //创建表
        String newTableName = TableStrategy.getTableStrategy(time);
        suYuanService.createNewTable(newTableName);
         dictRecordService.insertDictRecord(new DictRecord(1L, newTableName, Long.parseLong(time), ""));
      EasyCsv.read(filePath, SuYuan.class, new CsvParser(suYuanService, time)).doRead();
      //计算预警/报警,并且进行入库操作
      try {
         Thread.sleep(1000 * 60 * 1);
      } catch (InterruptedException e) {
         logger.error("睡眠一分钟后执行预警/报警计算,并且进行入库操作出现异常,异常原因是:", e);
         e.printStackTrace();
      }
      warningAnalyseService.warningOperationStorage(date);*/
      loadVocSync();
      loadCsvSync();
   }
      SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHH");
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(new Date());
      calendar.add(Calendar.HOUR, 1);
      // 测试 -> 2023-04-23 19:00
      //calendar.set(Calendar.MONTH, 3);
      //calendar.set(Calendar.DAY_OF_MONTH, 23);
      //calendar.set(Calendar.HOUR_OF_DAY, 20);
      for (int i = 0; i < 25; i++) {
   @Scheduled(cron = "${csv.voc}")
   private void loadVocSync() {
      vocValsService.deleteLastYear();
      Calendar calendar = getCalendar(96);
      for (int i = 0; i < cronMax; i++) {
         calendar.add(Calendar.HOUR, -1);
         String time = dateFormat.format(calendar.getTime());
         String filePath = csvFilePathConfig.getFilePath() + "\\\\" + time + ".csv";
         String time = format.format(calendar.getTime());
         String filePath = csvFilePathConfig.getVocPath() + File.separator + time + ".csv";
         File file = new File(filePath);
         if (!file.exists()) {
            logger.error(file.getAbsolutePath() + " is not exist !");
            logger.info(file.getAbsolutePath() + " is not exist !");
            continue;
         }
         String md5 = FileUtils.getFileMd5(filePath);
         if (null != md5 && md5List.contains(md5)) {
            continue; // csv已入库
         }
         int count = vocValsService.countByTime(time);
         if (count > 0) {
            vocValsService.deleteByTime(time); // 删除已入库
         }
         addFileMd5(md5);
         try {
            logger.info("loadVocSync: " + filePath);
            Date date = (Date) calendar.getTime().clone();
            EasyCsv.read(filePath, VocVals.class, new VocParser(vocValsService, date)).doRead();
            // Thread.sleep(3 * 1000);
         } catch (Exception e) {
            logger.error(e.getMessage(), e);
         }
      }
   }
   private void addFileMd5(String md5) {
      md5List.add(md5);
      if (md5List.size() > 512) {
         md5List.remove(0);
      }
   }
   @Scheduled(cron = "${csv.cron}")
   private void loadCsvSync() {
      Calendar calendar = getCalendar(1);
      for (int i = 0; i < vocMax; i++) {
         calendar.add(Calendar.HOUR, -1);
         String time = format.format(calendar.getTime());
         String filePath = csvFilePathConfig.getFilePath() + File.separator + time + ".csv";
         File file = new File(filePath);
         if (!file.exists()) {
            logger.info(file.getAbsolutePath() + " is not exist !");
            continue;
         }
@@ -96,17 +127,64 @@
         }
         suYuanService.createNewTable(newTableName);
         dictRecordService.insertDictRecord(new DictRecord(1L, newTableName, Long.parseLong(time), ""));
         Long createTime = Long.parseLong(time);
         DictRecord dr = dictRecordService.selectByCreateTime(createTime);
         if (null == dr) {
            dictRecordService.insertDictRecord(new DictRecord(1L, newTableName, Long.parseLong(time), ""));
         }
         logger.info("loadCsvSync: " + filePath);
         EasyCsv.read(filePath, SuYuan.class, new CsvParser(suYuanService, time)).doRead();
         try {
            // 计算预警/报警,并且进行入库操作
            Thread.sleep(60000);
         } catch (InterruptedException e) {
            logger.error("睡眠一分钟后执行预警/报警计算,并且进行入库操作出现异常,异常原因是:", e);
            e.printStackTrace();
         }
//         try {
//            // 计算预警/报警,并且进行入库操作
//            Thread.sleep(2 * 60 * 1000);
//         } catch (InterruptedException e) {
//            logger.error("睡眠一分钟后执行预警/报警计算,并且进行入库操作出现异常,异常原因是:", e);
//            e.printStackTrace();
//         }
         warningAnalyseService.warningOperationStorage(calendar.getTime());
      }
   }
   public Calendar getCalendar(int start) {
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(new Date());
      calendar.add(Calendar.HOUR, start);
      calendar.set(Calendar.MINUTE, 0);
      calendar.set(Calendar.SECOND, 0);
      calendar.set(Calendar.MILLISECOND, 0);
      // 测试 -> 2023-04-23 19:00
      //calendar.set(Calendar.MONTH, 3);
      //calendar.set(Calendar.DAY_OF_MONTH, 23);
      //calendar.set(Calendar.HOUR_OF_DAY, 20);
      return calendar;
   }
   private void loadCsv_old() {
      Date date = DateUtils.getNowDate();
      String time = DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, date);
      String filePath = csvFilePathConfig.getFilePath() + "\\\\" + time + ".csv";
      File file = new File(filePath);
      if (!file.exists()) {
         logger.info(file.getAbsolutePath() + " is not exist !");
         return;
      }
      //创建表
      String newTableName = TableStrategy.getTableStrategy(time);
      suYuanService.createNewTable(newTableName);
      dictRecordService.insertDictRecord(new DictRecord(1L, newTableName, Long.parseLong(time), ""));
      EasyCsv.read(filePath, SuYuan.class, new CsvParser(suYuanService, time)).doRead();
      //计算预警/报警,并且进行入库操作
      try {
         Thread.sleep(60 * 1000);
      } catch (InterruptedException e) {
         logger.error("睡眠一分钟后执行预警/报警计算,并且进行入库操作出现异常,异常原因是:", e);
         e.printStackTrace();
      }
      warningAnalyseService.warningOperationStorage(date);
   }
}