燕山石化溯源三维电子沙盘-【后端】-服务
13693261870
2023-07-14 db44f336e46825afc855466512065cc08e5790bd
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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.DictRecordService;
import com.yssh.service.SuYuanService;
import com.yssh.service.VocValsService;
import com.yssh.service.WarningAnalyseService;
import com.yssh.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import com.github.biyanwen.EasyCsv;
 
import javax.annotation.Resource;
 
@Component
@EnableScheduling
public class ScheduleConfig {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
 
    @Resource
    private VocValsService vocValsService;
 
    @Resource
    private SuYuanService suYuanService;
 
    @Resource
    private CsvFilePathConfig csvFilePathConfig;
 
    @Resource
    private DictRecordService dictRecordService;
 
    @Resource
    private WarningAnalyseService warningAnalyseService;
 
    @Value("${csv.voc_max}")
    private int vocMax = 96;
 
    @Value("${csv.cron_max}")
    private int cronMax = 48;
 
    private static boolean isBusy = false;
 
    private final static List<String> md5List = new ArrayList<>();
 
    private final static SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHH");
 
    public void corpReserveDataSync() {
        loadVocSync();
        loadCsvSync();
    }
 
    @Scheduled(cron = "${csv.cron}")
    private void loadCsvSync() {
        if (isBusy) return;
 
        isBusy = true;
        logger.info("***************** 开始执行CSV入库 *****************" + "\n");
        Calendar calendar = getCalendar(1);
        for (int i = 0; i < cronMax; i++) {
            try {
                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;
                }
                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");
        isBusy = 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(96);
        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); // 删除已入库
            }
 
            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);
    }
}