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
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
package com.moral.task;
 
import com.alibaba.fastjson.JSON;
import com.moral.service.*;
import com.moral.util.AlarmUtils_2;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
 
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;
 
@Component
public class RabbitMQInsertProvincialIndustrialPark {
    private static transient Logger logger = LoggerFactory.getLogger(RabbitMQInsertProvincialIndustrialPark.class);
 
    @Resource
    private DeviceService deviceService;
 
    @Resource
    private SensorService sensorService;
 
    @Resource
    private HistoryMinutelyService historyMinutelyService;
 
    @Resource
    private HistoryHourlyService historyHourlyService;
 
    @Resource
    private OrganizationRelationService organizationRelationService;
 
    @Resource
    private RabbitTemplate rabbitTemplate;
 
    @Resource
    @Qualifier("organization_data")
    private TopicExchange organization_data;
 
    //分钟数据,时间间隔一分钟
    @XxlJob("RabbitMQProvincialIndustrialParkMinutely")
    public ReturnT insertRabbitMQMinutely(String params) {
        LocalDateTime time = LocalDateTime.now();
        int year = time.getYear();
        int month = time.getMonthValue();
        int day = time.getDayOfMonth();
        int hour = time.getHour();
        int minute = time.getMinute();
        if (day == 1) {
            if (hour == 0 && minute == 0) {
                if (month == 1) {
                    month = 12;
                    year = year - 1;
                } else {
                    month = month - 1;
                }
            }
        }
        String monthStr = month < 10 ? ("0" + month) : month + "";
        String yearAndMonth = year + monthStr;
        Map organizationIdMap = JSON.parseObject(params);
        List<Integer> parentIdList = (List<Integer>) organizationIdMap.get("orgId");
        LocalDateTime endTime = time.truncatedTo(ChronoUnit.MINUTES);
        LocalDateTime startTime = endTime.minusMinutes(1);
        List<Object> organizationIdList = organizationRelationService.getChildIdByParentId(parentIdList);
        try {
            //List<String> macList = deviceService.getMacByOrganizationid(organizationIdList);
            List<String> macList = new ArrayList<>();
            macList.add("p5dnd7a0391989");
            List<String> sensorKeys = sensorService.getSensorKeyByMac(macList);
            List<Map<String, Object>> macAndOrganizationIdMap = deviceService.macAndOrganizationIdMap(macList);
            Map<String, Object> kv = new LinkedHashMap<>();
            for (Map<String, Object> map : macAndOrganizationIdMap) {
                kv.put(map.get("mac").toString(), map.get("organizationId"));
            }
            Map<String, Object> devices = new HashMap<>();
            devices.put("sensorKeys", sensorKeys);
            devices.put("start", startTime);
            devices.put("end", endTime);
            devices.put("macs", macList);
            devices.put("yearAndMonth", yearAndMonth);
            List<Map<String, Object>> minutelyData = historyMinutelyService.getMinutelySensorData(devices);
            XxlJobLogger.log("RabbitMQMinutelyData:" + minutelyData.size());
            if (!CollectionUtils.isEmpty(minutelyData)) {
                for (Map<String, Object> deviceData : minutelyData) {
                    if (!ObjectUtils.isEmpty(deviceData)) {
                        Iterator<String> iterator = deviceData.keySet().iterator();
                        while (iterator.hasNext()) {
                            String key = iterator.next();
                            if (key.startsWith("M")) {
                                iterator.remove();
                            }
                        }
                        deviceData.put("time", startTime.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
                        deviceData.put("timeType", "minutely");
                        String mac = deviceData.get("mac").toString();
                        Object o1 = kv.get(mac);
                        List<Integer> parentIds = organizationRelationService.getParentIdListByChildId((Integer) o1);
                        Map organizationIdMapNew = JSON.parseObject(params);
                        List<Integer> parentIdListNew = (List<Integer>) organizationIdMapNew.get("orgId");
                        List<Integer> intersection = parentIdListNew.stream().filter(item -> parentIds.contains(item)).collect(Collectors.toList());
                        intersection.stream().forEach(parentId -> rabbitTemplate.convertAndSend(organization_data.getName(), "ProvincialIndustrialPark" + "." + mac, JSON.toJSONString(deviceData)));
                    }
                }
                ReturnT returnT = new ReturnT(200, "RabbitMQ分钟数据存入成功");
                return returnT;
            }
        } catch (Exception e) {
            XxlJobLogger.log("RabbitMQMinutelyException:" + e.getMessage());
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        ReturnT returnT = new ReturnT(500, "RabbitMQ分钟数据存入失败");
        return returnT;
    }
 
    //小时数据时间间隔一小时
    @XxlJob("RabbitMQProvincialIndustrialParkHourly")
    public ReturnT insertRabbitMQHourly(String params) {
        LocalDateTime time = LocalDateTime.now();
        Map organizationIdMap = JSON.parseObject(params);
        List<Integer> parentIdList = (List<Integer>) organizationIdMap.get("orgId");
        LocalDateTime endTime = time.truncatedTo(ChronoUnit.HOURS);
        LocalDateTime startTime = endTime.minusHours(1);
        List<Object> organizationIdList = organizationRelationService.getChildIdByParentId(parentIdList);
        try {
            //List<String> macList = deviceService.getMacByOrganizationid(organizationIdList);
            List<String> macList = new ArrayList<>();
            macList.add("p5dnd7a0391989");
            List<String> sensorKeys = sensorService.getSensorKeyByMac(macList);
            List<Map<String, Object>> macAndOrganizationIdMap = deviceService.macAndOrganizationIdMap(macList);
            Map<String, Object> kv = new LinkedHashMap<>();
            for (Map<String, Object> map : macAndOrganizationIdMap) {
                kv.put(map.get("mac").toString(), map.get("organizationId"));
            }
            Map<String, Object> devices = new HashMap<>();
            devices.put("sensorKeys", sensorKeys);
            devices.put("start", startTime);
            devices.put("end", endTime);
            devices.put("macs", macList);
            List<Map<String, Object>> minutelyData = historyHourlyService.getHourlySensorData(devices);
            XxlJobLogger.log("RabbitMQHourlyData:" + minutelyData.size());
            if (!CollectionUtils.isEmpty(minutelyData)) {
                for (Map<String, Object> deviceData : minutelyData) {
                    if (!ObjectUtils.isEmpty(deviceData)) {
                        Iterator<String> iterator = deviceData.keySet().iterator();
                        while (iterator.hasNext()) {
                            String key = iterator.next();
                            if (key.startsWith("M")) {
                                iterator.remove();
                            }
                        }
                        deviceData.put("time", startTime.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
                        deviceData.put("timeType", "hourly");
                        String mac = deviceData.get("mac").toString();
                        Object o1 = kv.get(mac);
                        List<Integer> parentIds = organizationRelationService.getParentIdListByChildId((Integer) o1);
                        Map organizationIdMapNew = JSON.parseObject(params);
                        List<Integer> parentIdListNew = (List<Integer>) organizationIdMapNew.get("orgId");
                        List<Integer> intersection = parentIdListNew.stream().filter(item -> parentIds.contains(item)).collect(Collectors.toList());
                        intersection.stream().forEach(parentId -> rabbitTemplate.convertAndSend(organization_data.getName(), "ProvincialIndustrialPark" + "." + mac, JSON.toJSONString(deviceData)));
                    }
                }
                ReturnT returnT = new ReturnT(200, "RabbitMQ小时数据存入成功");
                return returnT;
            }
        } catch (Exception e) {
            XxlJobLogger.log("RabbitMQHourlyException:" + e.getMessage());
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        ReturnT returnT = new ReturnT(500, "RabbitMQ小时数据存入失败");
        return returnT;
    }
 
}