燕山石化溯源三维电子沙盘-【后端】-服务
1
13693261870
2023-05-15 74610a5c137f8ed3a8ed023a0517360cda273760
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.yssh.scheduled;
 
 
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import com.github.biyanwen.EasyCsv;
import com.yssh.config.CsvFilePathConfig;
import com.yssh.entity.DictRecord;
import com.yssh.entity.SuYuan;
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;
 
 
@Component
public class ReadCsvTask {
 
    protected final Log logger = LogFactory.getLog(this.getClass());
 
    @Autowired
    private CsvFilePathConfig csvFilePathConfig;
 
    @Autowired
    private SuYuanServiceImpl suYuanService;
 
    @Autowired
    private IDictRecordService dictRecordService;
 
    @Autowired
    private IWarningAnalyseService warningAnalyseService;
 
    @Scheduled(cron = "${csv.cron}")
    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);*/
 
        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++) {
            calendar.add(Calendar.HOUR, -1);
            String time = dateFormat.format(calendar.getTime());
            String filePath = csvFilePathConfig.getFilePath() + "\\\\" + time + ".csv";
 
            File file = new File(filePath);
            if (!file.exists()) {
                logger.error(file.getAbsolutePath() + " is not exist !");
                continue;
            }
 
            // 创建表
            String newTableName = TableStrategy.getTableStrategy(time);
            Integer rows = suYuanService.isTableExists(newTableName);
            if (rows > 0) {
                continue;
            }
 
            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(60000);
            } catch (InterruptedException e) {
                logger.error("睡眠一分钟后执行预警/报警计算,并且进行入库操作出现异常,异常原因是:", e);
                e.printStackTrace();
            }
            warningAnalyseService.warningOperationStorage(calendar.getTime());
        }
    }
}