lizijie
2022-09-09 33d898d8146223184fcaab82a9e16649434a1ab6
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
package com.moral.strategy;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.moral.service.DeviceService;
import com.moral.service.HistoryHourlyService;
import com.moral.service.HistoryMinutelyService;
import com.moral.service.SensorService;
import com.moral.util.DateUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.log.XxlJobLogger;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ObjectUtils;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
 
@Service
public class RepairHourlyData implements RepairDataStrategy {
    @Resource
    SensorService sensorService;
    @Resource
    DeviceService deviceService;
    @Resource
    HistoryMinutelyService historyMinutelyService;
    @Resource
    RedisTemplate redisTemplate;
    @Resource
    HistoryHourlyService historyHourlyService;
 
 
    @Override
    public void repairData(String time,MultiValueMap<String,String> result) {
 
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date endTimeDate = new Date(Long.parseLong(time) * 1000);
        Date startTimeDate = DateUtil.rollHour(endTimeDate, -1);
        StringBuilder endTime = new StringBuilder(sdf.format(endTimeDate));
        StringBuilder startTime = new StringBuilder(sdf.format(startTimeDate));
        endTime.replace(14, 19, "00:00");
        startTime.replace(14, 19, "00:00");
        String yearAndMonth = DateUtil.getYear(startTimeDate) + DateUtil.getMonth(startTimeDate);
 
        List<String> sensorKeys = sensorService.getSensorKeys();
        List<String> macs = deviceService.getMacs();
        Map<String, Object> devices = new HashMap<>();
        devices.put("sensorKeys", sensorKeys);
        devices.put("start", startTime.toString());
        devices.put("end", endTime.toString());
        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", endTime.toString());
                    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);
                    hourlyDataMap.put("json", jsonArray.get(0).toString());
                    hourlyDataList.add(hourlyDataMap);
                }
            }
            if (!CollectionUtils.isEmpty(hourlyDataList)) {
                historyHourlyService.insertHistoryHourly(hourlyDataList);
                result.add("200", "修补小时数据成功-"+endTime);
                return;
            }
        } catch (Exception e) {
            XxlJobLogger.log("historyHourlyException:" + e.getMessage());
            result.add("500", "修补小时数据失败-"+endTime);
            redisTemplate.opsForList().leftPush("unSuccessRepair_data","hourly_"+endTime);
            return;
        }
        result.add("500", "修补小时数据失败-"+endTime);
        redisTemplate.opsForList().leftPush("unSuccessRepair_data","hourly_"+endTime);
    }
}