jinpengyong
2020-06-03 8143ce1b902f99d8333475aa06e3c08e5e4164cf
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
package com.moral.service.impl;
 
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
 
import com.alibaba.druid.support.json.JSONUtils;
import com.moral.mapper.AlarmMapper;
import com.moral.service.AlarmService;
import com.moral.service.SensorService;
 
 
@Service
public class AlarmServiceImpl implements AlarmService {
 
    @Resource
    private AlarmMapper alarmMapper;
 
    @Resource
    private SensorService sensorService;
 
    @Override
    public void insertAlarmDaily() {
        LocalDateTime time = LocalDateTime.now();
        LocalDateTime endTime = time.truncatedTo(ChronoUnit.DAYS);
        LocalDateTime startTime = endTime.minusDays(1);
        List<String> sensorKeys = sensorService.getSensorKeys();
        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("yearAndMonth", yearAndMonth);
        List<String> macs = alarmMapper.getMacs(devices);
        devices.put("macs", macs);
        for (String mac : macs) {
            List<Map<String, Object>> resultList = new ArrayList<>();
            devices.put("mac", mac);
            List<Map<String, Object>> list = alarmMapper.getAlarmData(devices);
            List<Map<String, Object>> newList1 = list.stream().distinct().collect(Collectors.toList());
            for (Map<String, Object> map : newList1) {
                for (String key : map.keySet()) {
                    if (!"mac".equals(key)) {
                        String[] strings = map.get(key).toString().replace("$.", "")
                                .replace("[", "").replace("]", "")
                                .replace("\"", "").replace(" ", "").split(",");
 
                        List<String> sensors = new ArrayList<String>(Arrays.asList(strings));
                        map.put(key, sensors);
                    }
                }
            }
 
            Map<String, Object> hashMap = new HashMap<>();
            hashMap.put("mac", mac);
            ArrayList<String> stateList1 = new ArrayList<>();
            ArrayList<String> stateList2 = new ArrayList<>();
            ArrayList<String> stateList3 = new ArrayList<>();
            for (Map<String, Object> map : newList1) {
                if (map.get("1") != null) {
                    stateList1.addAll((ArrayList<String>) map.get("1"));
                    hashMap.put("1", stateList1);
                }
                if (map.get("2") != null) {
                    stateList2.addAll((ArrayList<String>) map.get("2"));
                    hashMap.put("2", stateList2);
                }
                if (map.get("3") != null) {
                    stateList3.addAll((ArrayList<String>) map.get("3"));
                    hashMap.put("3", stateList3);
                }
 
            }
 
            for (String key : hashMap.keySet()) {
                if (!"mac".equals(key)) {
                    ArrayList<String> arrayList = (ArrayList<String>) hashMap.get(key);
                    List<String> sensorsList = arrayList.stream().distinct().collect(Collectors.toList());
                    hashMap.put(key, sensorsList);
                }
            }
 
            for (String key : hashMap.keySet()) {
                if(!"mac".equals(key)){
                    Map<String, Object> jsonMap = new HashMap<>();
                    Map<String, Object> map = new HashMap<>();
                    map.put("mac",mac);
                    map.put("time",startTime);
                    map.put("state", key);
                    ArrayList<String> stateList = (ArrayList<String>) hashMap.get(key);
                    for (String state : stateList) {
                        jsonMap.put(state,1);
                    }
                    map.put("json",JSONUtils.toJSONString(jsonMap));
                    resultList.add(map);
                }
            }
            alarmMapper.insertAlarmDaily(resultList);
        }
    }
 
    @Override
    public void createTable(String yearAndMonth) {
        alarmMapper.createTable(yearAndMonth);
    }
}