From ed8c07e1870d8cd2151355ea34019a3f02087568 Mon Sep 17 00:00:00 2001
From: lizijie <lzjiiie@163.com>
Date: Thu, 26 May 2022 16:05:10 +0800
Subject: [PATCH] 常熟中隆汽车设备转发,千灯设备数据转发

---
 src/main/java/com/moral/task/RabbitMQInsertQiandengPark.java             |  214 ++++++++++++++++++++++++++++++
 src/main/java/com/moral/task/RabbitMQInsertProvincialIndustrialPark.java |  189 +++++++++++++++++++++++++++
 src/main/java/com/moral/mapper/HistoryMapper.java                        |    2 
 src/main/resources/mapper/HistoryMapper.xml                              |    7 +
 4 files changed, 412 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/moral/mapper/HistoryMapper.java b/src/main/java/com/moral/mapper/HistoryMapper.java
index 08e6a4c..16557f3 100644
--- a/src/main/java/com/moral/mapper/HistoryMapper.java
+++ b/src/main/java/com/moral/mapper/HistoryMapper.java
@@ -37,4 +37,6 @@
 
     void dropHistoryTable(@Param("yearMonthDay") String yearMonthDay);
 
+    History selectLastDataByMac(@Param("yearMonthDay") String yearMonthDay, @Param("mac") String mac);
+
 }
\ No newline at end of file
diff --git a/src/main/java/com/moral/task/RabbitMQInsertProvincialIndustrialPark.java b/src/main/java/com/moral/task/RabbitMQInsertProvincialIndustrialPark.java
new file mode 100644
index 0000000..2efb827
--- /dev/null
+++ b/src/main/java/com/moral/task/RabbitMQInsertProvincialIndustrialPark.java
@@ -0,0 +1,189 @@
+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;
+    }
+
+}
+
+
diff --git a/src/main/java/com/moral/task/RabbitMQInsertQiandengPark.java b/src/main/java/com/moral/task/RabbitMQInsertQiandengPark.java
new file mode 100644
index 0000000..ccaa6d5
--- /dev/null
+++ b/src/main/java/com/moral/task/RabbitMQInsertQiandengPark.java
@@ -0,0 +1,214 @@
+package com.moral.task;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.moral.entity.History;
+import com.moral.mapper.HistoryMapper;
+import com.moral.service.*;
+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.text.SimpleDateFormat;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.time.temporal.ChronoUnit;
+import java.util.*;
+import java.util.stream.Collectors;
+
+/**
+ * @program: screen_job
+ * @description: ������������������������������������
+ * @author: lizijie
+ * @create: 2022-05-13 08:58
+ **/
+@Component
+public class RabbitMQInsertQiandengPark {
+    private static transient Logger logger = LoggerFactory.getLogger(RabbitMQInsertQiandengPark.class);
+
+    @Resource
+    private DeviceService deviceService;
+
+    @Resource
+    private SensorService sensorService;
+
+    @Resource
+    private HistoryMinutelyService historyMinutelyService;
+
+    @Resource
+    private HistoryHourlyService historyHourlyService;
+
+    @Resource
+    private HistoryMapper historyMapper;
+
+    @Resource
+    private OrganizationRelationService organizationRelationService;
+
+    @Resource
+    private RabbitTemplate rabbitTemplate;
+
+    @Resource
+    @Qualifier("organization_data")
+    private TopicExchange organization_data;
+
+    //������������������������������������
+    @XxlJob("RabbitMQQiandengParkMinutely")
+    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> 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);
+            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
+            String yearMonthDay = sdf.format(new Date());
+            History guodu = historyMapper.selectLastDataByMac(yearMonthDay, "jsxlqxpc000001");
+            History baihua = historyMapper.selectLastDataByMac(yearMonthDay, "jsxlqxpc000002");
+            Map<String,Object> guoduValue = JSONObject.parseObject(guodu.getValue().toString());
+            JSONObject baihuaValue = JSONObject.parseObject(baihua.getValue().toString());
+            guoduValue.put("mac","jsxlqxpc000001");
+            baihuaValue.put("mac","jsxlqxpc000002");
+            minutelyData.add(guoduValue);
+            minutelyData.add(baihuaValue);
+            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(), "QiandengPark" + "." + 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("RabbitMQQiandengParkHourly")
+    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> 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);
+            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
+            String yearMonthDay = sdf.format(new Date());
+            History guodu = historyMapper.selectLastDataByMac(yearMonthDay, "jsxlqxpc000001");
+            History baihua = historyMapper.selectLastDataByMac(yearMonthDay, "jsxlqxpc000002");
+            Map<String,Object> guoduValue = JSONObject.parseObject(guodu.getValue().toString());
+            JSONObject baihuaValue = JSONObject.parseObject(baihua.getValue().toString());
+            guoduValue.put("mac","jsxlqxpc000001");
+            baihuaValue.put("mac","jsxlqxpc000002");
+            minutelyData.add(guoduValue);
+            minutelyData.add(baihuaValue);
+            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(), "QiandengPark" + "." + 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;
+    }
+}
diff --git a/src/main/resources/mapper/HistoryMapper.xml b/src/main/resources/mapper/HistoryMapper.xml
index 1cd78ce..aa4967d 100644
--- a/src/main/resources/mapper/HistoryMapper.xml
+++ b/src/main/resources/mapper/HistoryMapper.xml
@@ -263,4 +263,11 @@
     <delete id="dropHistoryTable" parameterType="String">
         drop table history_${yearMonthDay}
     </delete>
+
+    <select id="selectLastDataByMac" resultType="com.moral.entity.History">
+        select * from history_${yearMonthDay}
+        where mac = #{mac}
+        order by time desc
+        limit 1
+    </select>
 </mapper>
\ No newline at end of file

--
Gitblit v1.8.0