yuzixiang
2020-05-21 f846329e976139e423f3015b1daba698825e4f93
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.task;
 
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.HashMap;
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 com.alibaba.fastjson.JSONObject;
import com.moral.service.AlarmService;
import com.moral.service.DeviceService;
import com.moral.service.SensorService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
 
@Component
public class AlarmTableInsertTask {
    private static transient Logger logger = LoggerFactory.getLogger(AlarmTableInsertTask.class);
    @Resource
    private AlarmService alarmService;
 
    @Resource
    private SensorService sensorService;
 
    @Resource
    private DeviceService deviceService;
 
    @XxlJob("alarmDaily")
    public ReturnT insertAlarmDailyTable(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();
 
        String year = startTime.getYear() + "";
        String month = null;
        int monthValue = startTime.getMonthValue();
        if (monthValue < 10) {
            month = "0" + monthValue;
        } else {
            month = monthValue + "";
        }
        String yearAndMonth = year + month;
 
        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);
 
        List<Map<String, Object>> dailyData = alarmService.getAvgAlarmData(devices);
        List<Map<String, Object>> dailyDataList = new ArrayList<>();
        for (Map<String, Object> deviceData : dailyData) {
            Map<String, Object> map = new HashMap<>();
            for (String sensorKey : sensorKeys) {
                if (deviceData.get(sensorKey) != null) {
                    map.put(sensorKey, deviceData.get(sensorKey).toString());
                }
            }
            JSONObject json = new JSONObject(map);
            deviceData.put("json", json);
        }
 
        for (Map<String, Object> dailyMap : dailyData) {
            Map<String, Object> map = new HashMap<>();
            String mac = dailyMap.get("mac").toString();
            String json = dailyMap.get("json").toString();
            String state = dailyMap.get("state").toString();
            map.put("mac", mac);
            map.put("json", json);
            map.put("state", state);
            map.put("time", startTime);
            dailyDataList.add(map);
        }
        if (!CollectionUtils.isEmpty(dailyDataList)) {
            alarmService.insertAlarmDaily(dailyDataList);
            return new ReturnT(200, "插入天表成功");
        }
        return new ReturnT(500, "插入天表失败");
    }
 
 
    @XxlJob("createAlarmSubTable")
    public void createAlarmSubTable(String params) {
        LocalDateTime time = LocalDateTime.now().plusMonths(1);;
        String year = time.getYear() + "";
        String month = time.getMonthValue() + "";
        if (time.getMonthValue() < 10) {
            month = "0" + time.getMonthValue();
        }
        alarmService.createTable(year + month);
    }
}