lizijie
2022-09-06 1b440360d4e36a6844c4e557b01da29e5d151ff2
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
package com.moral.task;
 
import com.alibaba.fastjson.JSON;
import com.moral.service.HistoryFiveMinutelyService;
import com.moral.service.SensorService;
import com.moral.util.DateUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
 
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
 
@Component
public class HistoryFiveMinutelyTask {
    @Resource
    HistoryFiveMinutelyService historyFiveMinutelyService;
 
    @Resource
    SensorService sensorService;
 
    @Resource
    RedisTemplate redisTemplate;
 
    @XxlJob("createHistoryFiveMinutelyTb")
    public ReturnT createHistoryMinutelyTb(String param) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        date = DateUtil.rollMon(date, 1);
        String year = DateUtil.getYear(date);
        String month = DateUtil.getMonth(date).length() == 1 ? "0".concat(DateUtil.getMonth(date)) : DateUtil.getMonth(date);
        try {
            historyFiveMinutelyService.createHistoryFiveMinutelyTable(year + month);
        } catch (Exception e) {
            return new ReturnT(500, "建立五分钟表失败");
        }
        return new ReturnT(200, "建立五分钟表成功");
    }
 
 
    /*分钟表插入时间为每分钟的第二十秒
     * 分钟数据读取时间,每五分钟的第三十秒开始读取
     * EG: 10:15:30  插入10:10--10:15的数据
     *     10:20:30  插入10:15--10:20的数据*/
    @XxlJob("insertHistoryFiveMinutelyTb")
    public ReturnT insertHistoryFiveMinutely(String param) {
        //获取当前时间
        Date errorDate = new Date();
        try {
            /*根据时间从分钟表数据钟查询五分钟数据的平均值*/
            Map<String, Object> params = getStartAndEndTime();
            List<String> sensorKeys = sensorService.getSensorKeys();
            params.put("sensorKeys", sensorKeys);
            List<Map<String, Object>> fiveMinutesSensorDatas;
            fiveMinutesSensorDatas = historyFiveMinutelyService.getFiveMinutesSensorData(params);
 
            /*将得到的数据进行转换*/
            List<Map<String, Object>> insertDatas;
            insertDatas = new ArrayList<>();
            for (Map<String, Object> data : fiveMinutesSensorDatas) {
                String mac = (String) data.get("mac");
                data.remove("mac");
                Map<String, Object> keyAndValueMap = new LinkedHashMap<>();
                Map<String, Object> insertDataMap = new LinkedHashMap<>();
                data.forEach((key, value) -> {
                    key = key.substring(3);
                    List<Object> list = null;
                    if (ObjectUtils.isEmpty(keyAndValueMap.get(key))) {
                        list = new ArrayList<>();
                    } else {
                        list = (List<Object>) keyAndValueMap.get(key);
                    }
                    if (value instanceof Double) {
                        value = String.valueOf(value);
                        value = value.equals("0.0") ? 0 : Double.valueOf((String) value);
                    }
                    list.add(value);
                    keyAndValueMap.put(key, list);
                });
                String keyAndValueJson = JSON.toJSONString(keyAndValueMap);
                insertDataMap.put("mac", mac);
                insertDataMap.put("time", params.get("end"));
                insertDataMap.put("json", keyAndValueJson);
                insertDatas.add(insertDataMap);
            }
            /*将数据插入数据库*/
            if (!ObjectUtils.isEmpty(insertDatas)) {
                historyFiveMinutelyService.insertHistoryFiveMinutely(insertDatas, (String) params.get("yearAndMonth"));
                return new ReturnT(200, "插入五分钟数据成功");
            }
        } catch (Exception e) {
            XxlJobLogger.log("historyFiveMinutelyException:" + e.getMessage());
            e.printStackTrace();
        }
 
        List record = new ArrayList();
        record.add("repairFiveMinutelyData_" + errorDate.getTime() / 1000);
        redisTemplate.opsForList().leftPushAll("unrepair_data", record);
        return new ReturnT(500, "插入五分钟数据失败");
    }
 
    /**
     * @Description: 根据当前时间获取到查询的开始时间和结束时间以及年月字符串
     * @Param:
     * @return:
     * @Author: 下雨听风
     * @Date: 2020/10/15
     */
    private Map<String, Object> getStartAndEndTime() {
        Map<String, Object> map = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String minute = DateUtil.getMinute(date);
        String year = "";
        String month = "";
        String yearAndMonth = "";
        String startTime = "";
        String endTime = "";
        Integer endMinute = Integer.parseInt(String.valueOf(minute.charAt(minute.length() - 1)));
        if (endMinute >= 5) {
            StringBuilder time = new StringBuilder(sdf.format(date));
            startTime = time.replace(15, 19, "0:00").toString();
            endTime = time.replace(15, 19, "5:00").toString();
        } else {
            StringBuilder endTimesb = new StringBuilder(sdf.format(date));
            endTime = endTimesb.replace(15, 19, "0:00").toString();
            date = DateUtil.rollMinute(date, -5);
            StringBuilder startTimesb = new StringBuilder(sdf.format(date));
            startTime = startTimesb.replace(15, 19, "5:00").toString();
 
        }
 
        year = DateUtil.getYear(date);
        month = DateUtil.getMonth(date);
        yearAndMonth = year + month;
        map.put("start", startTime);
        map.put("end", endTime);
        map.put("yearAndMonth", yearAndMonth);
        return map;
    }
 
}