From aa86cb57b09c5300db3c33d75d91d8b51a4b636f Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期五, 16 六月 2023 09:27:45 +0800
Subject: [PATCH] 1

---
 src/main/java/com/yssh/scheduled/ReadCsvTask.java |  122 ++++++++++++++++++++++++++++------------
 1 files changed, 86 insertions(+), 36 deletions(-)

diff --git a/src/main/java/com/yssh/scheduled/ReadCsvTask.java b/src/main/java/com/yssh/scheduled/ReadCsvTask.java
index 89732a4..cd321c6 100644
--- a/src/main/java/com/yssh/scheduled/ReadCsvTask.java
+++ b/src/main/java/com/yssh/scheduled/ReadCsvTask.java
@@ -1,14 +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 org.springframework.beans.factory.annotation.Autowired;
+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.Value;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 
@@ -19,63 +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;
 
-	private final SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHH");
+	@Value("${csv.voc_max}")
+	private int vocMax = 120;
 
-	@Scheduled(cron = "${csv.cron}")
+	@Value("${csv.cron_max}")
+	private int cronMax = 48;
+
+	private final static List<String> md5List = new ArrayList<>();
+
+	private final static SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHH");
+
 	public void corpReserveDataSync() {
-		loadVoc();
-		loadCsv();
+		loadVocSync();
+		loadCsvSync();
 	}
 
-	private void loadVoc() {
+	@Scheduled(cron = "${csv.voc}")
+	private void loadVocSync() {
+		vocValsService.deleteLastYear();
 		Calendar calendar = getCalendar(96);
-		for (int i = 0; i < 192; i++) {
+		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.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 loadCsv() {
+	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 < 25; i++) {
+		for (int i = 0; i < cronMax; i++) {
 			calendar.add(Calendar.HOUR, -1);
 			String time = format.format(calendar.getTime());
-			String filePath = csvFilePathConfig.getFilePath() + "\\\\" + time + ".csv";
+			String filePath = csvFilePathConfig.getFilePath() + 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;
 			}
 
@@ -87,24 +127,34 @@
 			}
 
 			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());
 		}
 	}
 
-	private Calendar getCalendar(int start) {
+	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);
@@ -120,7 +170,7 @@
 		String filePath = csvFilePathConfig.getFilePath() + "\\\\" + time + ".csv";
 		File file = new File(filePath);
 		if (!file.exists()) {
-			logger.error(file.getAbsolutePath() + " is not exist 锛�");
+			logger.info(file.getAbsolutePath() + " is not exist 锛�");
 			return;
 		}
 		//鍒涘缓琛�
@@ -130,7 +180,7 @@
 		EasyCsv.read(filePath, SuYuan.class, new CsvParser(suYuanService, time)).doRead();
 		//璁$畻棰勮/鎶ヨ,骞朵笖杩涜鍏ュ簱鎿嶄綔
 		try {
-			Thread.sleep(1000 * 60 * 1);
+			Thread.sleep(60 * 1000);
 		} catch (InterruptedException e) {
 			logger.error("鐫$湢涓�鍒嗛挓鍚庢墽琛岄璀�/鎶ヨ璁$畻,骞朵笖杩涜鍏ュ簱鎿嶄綔鍑虹幇寮傚父锛屽紓甯稿師鍥犳槸锛�", e);
 			e.printStackTrace();

--
Gitblit v1.9.3