package com.yssh.config; 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 com.yssh.entity.DictRecord; import com.yssh.entity.SuYuan; import com.yssh.entity.VocVals; import com.yssh.service.*; import com.yssh.utils.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import com.github.biyanwen.EasyCsv; import javax.annotation.Resource; @Configuration @EnableScheduling public class ScheduleConfig { protected final Logger logger = LoggerFactory.getLogger(this.getClass()); @Resource VocValsService vocValsService; @Resource SuYuanService suYuanService; @Resource CsvFilePathConfig csvFilePathConfig; @Resource DictRecordService dictRecordService; @Resource WarningAnalyseService warningAnalyseService; @Resource XlsReportService xlsReportService; @Resource EmailService emailService; @Value("${csv.voc_max}") private int vocMax = 96; @Value("${csv.cron_max}") private int cronMax = 48; @Value("${email.enable}") private Boolean enableEmail; private static boolean flag = false; private static final Object OBJ = new Object(); private final static List md5List = new ArrayList<>(); private final static SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHH"); public void corpReserveDataSync() { //loadVocSync(); loadCsvSync(); } @Scheduled(cron = "${csv.cron}") private void loadCsvSync() { synchronized (OBJ) { if (flag) return; flag = true; } logger.info("***************** 开始执行CSV入库 *****************" + "\n"); Calendar calendar = getCalendar(1); calendar.add(Calendar.HOUR, -cronMax); for (int i = 0; i < cronMax; i++) { try { calendar.add(Calendar.HOUR, 1); String time = format.format(calendar.getTime()); System.out.println(time); String filePath = csvFilePathConfig.getFilePath() + File.separator + time + ".csv"; File file = new File(filePath); if (!file.exists()) { //logger.info(file.getAbsolutePath() + " is not exist !"); continue; } String newTableName = TableStrategy.getTableStrategy(time); // 创建表 Integer rows = suYuanService.isTableExists(newTableName); if (rows > 0) { continue; } suYuanService.createNewTable(newTableName); DictRecord dr = dictRecordService.selectByCreateTime(Long.parseLong(time)); 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(); warningAnalyseService.warningOperationStorage(calendar.getTime()); } catch (Exception e) { logger.error(e.getMessage(), e); } } logger.info("***************** CSV入库执行完成 *****************" + "\n"); flag = false; } 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; } @Scheduled(cron = "${csv.voc}") private void loadVocSync() { logger.info("***************** 开始全域CSV入库 *****************" + "\n"); vocValsService.deleteLastYear(); Calendar calendar = getCalendar(vocMax); for (int i = 0; i < vocMax; i++) { calendar.add(Calendar.HOUR, -1); String time = format.format(calendar.getTime()); String filePath = csvFilePathConfig.getVocPath() + File.separator + time + ".csv"; File file = new File(filePath); if (!file.exists()) { //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); // 删除已入库 continue; } try { //addFileMd5(md5); logger.info("loadVocSync: " + filePath); Date date = (Date) calendar.getTime().clone(); EasyCsv.read(filePath, VocVals.class, new VocParser(vocValsService, date)).doRead(); } catch (Exception e) { logger.error(e.getMessage(), e); } } logger.info("***************** 全域CSV执行完成 *****************" + "\n"); } private void addFileMd5(String md5) { md5List.add(md5); if (md5List.size() > 512) { md5List.remove(0); } } 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); } /** * 创建日报:每日0时1分 */ @Scheduled(cron = "0 1 0 * * ?") public void createDayReport() { Calendar calendar = getCalendar(1); for (int i = 0; i < 31; i++) { try { calendar.add(Calendar.DATE, -1); xlsReportService.createDayReport(calendar.getTime()); // System.out.println(DateUtils.getYyyyMmDd(calendar.getTime())); } catch (Exception ex) { logger.error(ex.getMessage(), ex); } } } /** * 创建周报:每周一0时5分 */ @Scheduled(cron = "0 5 0 ? * MON") public void createWeekReport() { Calendar calendar = getCalendar(7 * 24); for (int i = 1; i < 8; i++) { try { calendar.add(Calendar.DATE, -7); xlsReportService.createWeekReport(calendar.getTime()); // System.out.println(DateUtils.getYyyyMmDd(calendar.getTime())); } catch (Exception ex) { logger.error(ex.getMessage(), ex); } } } /** * 创建月报:每月1日0时9分 */ @Scheduled(cron = "0 9 0 1 * ?") public void createMonthReport() { Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.add(Calendar.MONTH, 1); for (int i = 0; i < 6; i++) { try { calendar.add(Calendar.MONTH, -1); xlsReportService.createMonthReport(calendar.getTime()); // System.out.println(DateUtils.getYyyyMmDd(calendar.getTime())); } catch (Exception ex) { logger.error(ex.getMessage(), ex); } } } @Scheduled(cron = "${email.times}") public void autoCalcData() { if (enableEmail) { //System.out.println("${email.times}"); logger.info("***************** 开始数据预警计算 *****************" + "\n"); emailService.calcData(); logger.info("***************** 数据预警计算完成 *****************" + "\n"); } } }