xufenglei
2020-03-23 eee9aaa6095c507e5130db240d1ba4cb8273d299
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package com.moral.task;
 
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.moral.service.DeviceService;
import com.moral.service.HistoryDailyService;
import com.moral.service.HistoryHourlyService;
import com.moral.service.HistoryMinutelyService;
import com.moral.service.SensorService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
 
//@Component
public class HistoryTableInsertTask {
    private static transient Logger logger = LoggerFactory.getLogger(HistoryTableInsertTask.class);
 
    @Resource
    private SensorService sensorService;
 
    @Resource
    private DeviceService deviceService;
 
    @Resource
    private HistoryMinutelyService historyMinutelyService;
 
    @Resource
    private HistoryHourlyService historyHourlyService;
 
    @Resource
    private HistoryDailyService historyDailyService;
 
    //@XxlJob("historyMinutely")
    public ReturnT insertHistoryMinutelyTable(String params) {
        LocalDateTime time = LocalDateTime.now();
        int year = time.getYear();
        int month = time.getMonthValue();
        String monthStr = month < 10 ? ("0" + month) : month + "";
        String yearAndMonth = year + monthStr;
        LocalDateTime endTime = time.truncatedTo(ChronoUnit.MINUTES);
        LocalDateTime startTime = endTime.minusMinutes(1);
        List<String> sensorKeys = sensorService.getSensorKeys();
        Map<String, Object> devices = new HashMap<>();
        devices.put("sensorKeys", sensorKeys);
        devices.put("start", startTime);
        devices.put("end", endTime);
        try {
            List<Map<String, Object>> minutelyData = deviceService.getSensorData(devices);
            XxlJobLogger.log("historyMinutelyData:" + minutelyData.size());
            List<Map<String, Object>> minutelyDataList = new ArrayList<>();
            for (Map<String, Object> deviceData : minutelyData) {
                if (!ObjectUtils.isEmpty(deviceData)) {
                    Map<String, Object> minutelyDataMap = new LinkedHashMap<>();
                    JSONObject jo = new JSONObject(true);
                    minutelyDataMap.put("mac", deviceData.get("mac"));
                    minutelyDataMap.put("time", startTime);
                    JSONArray jsonArray = new JSONArray();
                    for (String key : deviceData.keySet()) {
                        if (!key.equals("mac") && !key.startsWith("M")) {
                            List<Object> date = new ArrayList<>();
                            date.add(deviceData.get(key));
                            date.add(deviceData.get("MIN" + key));
                            date.add(deviceData.get("MAX" + key));
                            jo.put(key, date);
                        }
                    }
                    jsonArray.add(jo);
                    minutelyDataMap.put("json", jsonArray.get(0).toString());
                    minutelyDataList.add(minutelyDataMap);
                }
            }
            if (!CollectionUtils.isEmpty(minutelyDataList)) {
                historyMinutelyService.insertHistoryMinutely(minutelyDataList, yearAndMonth);
                ReturnT returnT = new ReturnT(200, "插入分钟表成功");
                return returnT;
            }
        } catch (Exception e) {
            XxlJobLogger.log("historyMinutelyException:" + e.getMessage());
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        ReturnT returnT = new ReturnT(500, "插入分钟表失败");
        return returnT;
    }
 
    //@XxlJob("historyHourly")
    public ReturnT insertHistoryHourlyTable(String params) {
        LocalDateTime time = LocalDateTime.now();
        int year = time.getYear();
        int month = time.getMonthValue();
        String monthStr = month < 10 ? ("0" + month) : month + "";
        String yearAndMonth = year + monthStr;
        LocalDateTime endTime = time.truncatedTo(ChronoUnit.HOURS);
        LocalDateTime startTime = endTime.minusHours(1);
        List<String> sensorKeys = sensorService.getSensorKeys();
        List<String> macs = deviceService.getMacs();
        Map<String, Object> devices = new HashMap<>();
        devices.put("sensorKeys", sensorKeys);
        devices.put("start", startTime);
        devices.put("end", endTime);
        devices.put("macs", macs);
        devices.put("yearAndMonth", yearAndMonth);
        try {
            List<Map<String, Object>> hourlyData = historyMinutelyService.getMinutelySensorData(devices);
            XxlJobLogger.log("historyHourlyData:" + hourlyData.size());
            List<Map<String, Object>> hourlyDataList = new ArrayList<>();
            for (Map<String, Object> deviceData : hourlyData) {
                if (!ObjectUtils.isEmpty(deviceData)) {
                    Map<String, Object> hourlyDataMap = new LinkedHashMap<>();
                    JSONObject jo = new JSONObject(true);
                    hourlyDataMap.put("mac", deviceData.get("mac"));
                    hourlyDataMap.put("time", startTime);
                    JSONArray jsonArray = new JSONArray();
                    for (String key : deviceData.keySet()) {
                        if (!key.equals("mac") && !key.startsWith("M")) {
                            List<Object> date = new ArrayList<>();
                            date.add(deviceData.get(key));
                            date.add(new BigDecimal(deviceData.get("MIN" + key).toString()));
                            date.add(new BigDecimal(deviceData.get("MAX" + key).toString()));
                            jo.put(key, date);
                        }
                    }
                    jsonArray.add(jo);
                    hourlyDataMap.put("json", jsonArray.get(0).toString());
                    hourlyDataList.add(hourlyDataMap);
                }
            }
            if (!CollectionUtils.isEmpty(hourlyDataList)) {
                historyHourlyService.insertHistoryHourly(hourlyDataList);
                ReturnT returnT = new ReturnT(200, "插入小时表成功");
                return returnT;
            }
        } catch (Exception e) {
            XxlJobLogger.log("historyHourlyException:" + e.getMessage());
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        ReturnT returnT = new ReturnT(500, "插入小时表失败");
        return returnT;
    }
 
    //@XxlJob("historyDaily")
    public ReturnT insertHistoryDailyTable(String params) {
        LocalDateTime time = LocalDateTime.now();
        LocalDateTime endTime = time.truncatedTo(ChronoUnit.DAYS);
        LocalDateTime startTime = endTime.minusDays(1);
        List<String> sensorKeys = sensorService.getSensorKeys();
        List<String> macs = deviceService.getMacs();
        Map<String, Object> devices = new HashMap<>();
        devices.put("sensorKeys", sensorKeys);
        devices.put("start", startTime);
        devices.put("end", endTime);
        devices.put("macs", macs);
        try {
            List<Map<String, Object>> dailyData = historyHourlyService.getHourlySensorData(devices);
            XxlJobLogger.log("historyDailyData:" + dailyData.size());
            List<Map<String, Object>> dailyDataList = new ArrayList<>();
            for (Map<String, Object> deviceData : dailyData) {
                if (!ObjectUtils.isEmpty(deviceData)) {
                    Map<String, Object> dailyDataMap = new LinkedHashMap<>();
                    JSONObject jo = new JSONObject(true);
                    dailyDataMap.put("mac", deviceData.get("mac"));
                    dailyDataMap.put("time", startTime);
                    JSONArray jsonArray = new JSONArray();
                    for (String key : deviceData.keySet()) {
                        if (!key.equals("mac") && !key.startsWith("M")) {
                            List<Object> date = new ArrayList<>();
                            date.add(deviceData.get(key));
                            if (deviceData.get("MIN" + key) instanceof String) {
                                date.add(new BigDecimal(deviceData.get("MIN" + key).toString()));
                                date.add(new BigDecimal(deviceData.get("MAX" + key).toString()));
                            } else if (deviceData.get("MIN" + key) instanceof byte[]) {
                                date.add(new BigDecimal(new String((byte[]) (deviceData.get("MIN" + key)))));
                                date.add(new BigDecimal(new String((byte[]) (deviceData.get("MAX" + key)))));
                            }
                            jo.put(key, date);
                        }
                    }
                    jsonArray.add(jo);
                    dailyDataMap.put("json", jsonArray.get(0).toString());
                    dailyDataList.add(dailyDataMap);
                }
            }
            if (!CollectionUtils.isEmpty(dailyDataList)) {
                historyDailyService.insertHistoryDaily(dailyDataList);
                ReturnT returnT = new ReturnT(200, "插入天表成功");
                return returnT;
            }
        } catch (Exception e) {
            XxlJobLogger.log("historyDailyException:" + e.getMessage());
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        ReturnT returnT = new ReturnT(500, "插入天表失败");
        return returnT;
    }
}