From e8ef361dc975aeaedeee6f28dfe679489a22f9d5 Mon Sep 17 00:00:00 2001
From: ZhuDongming <773644075@qq.com>
Date: Mon, 02 Sep 2019 17:42:06 +0800
Subject: [PATCH] Merge branch 'master' of http://blit.7drlb.com:8888/r/screen_api_v2

---
 src/main/java/com/moral/service/impl/HistoryServiceImpl.java |   99 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 98 insertions(+), 1 deletions(-)

diff --git a/src/main/java/com/moral/service/impl/HistoryServiceImpl.java b/src/main/java/com/moral/service/impl/HistoryServiceImpl.java
index 461bfea..6238443 100644
--- a/src/main/java/com/moral/service/impl/HistoryServiceImpl.java
+++ b/src/main/java/com/moral/service/impl/HistoryServiceImpl.java
@@ -1,5 +1,6 @@
 package com.moral.service.impl;
 
+import java.text.SimpleDateFormat;
 import java.time.Instant;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
@@ -33,6 +34,8 @@
 import com.moral.entity.Sensor;
 import com.moral.mapper.DeviceMapper;
 import com.moral.mapper.HistoryMapper;
+import com.moral.mapper.MonitorPointMapper;
+import com.moral.mapper.OrganizationRelationMapper;
 import com.moral.mapper.SensorMapper;
 import com.moral.mapper.SensorUnitMapper;
 import com.moral.service.AccountService;
@@ -57,6 +60,12 @@
 
     @Resource
     private ProfessionService professionService;
+    
+    @Resource
+    private OrganizationRelationMapper organizationRelationMapper;
+    
+    @Resource
+    private MonitorPointMapper monitorPointMapper;
 
 	@Override
 	public Map<String, Object> getAllSensorAverageByDevice(Map<String, Object> parameters) throws Exception {
@@ -287,7 +296,7 @@
 			if (sensorUnits.containsKey(sensorKey)) {
 				Map<String, Object> sensorUnit  = (Map<String, Object>) sensorUnits.get(sensorKey);
 				String rules = sensorUnit.get("rules").toString();
-				rules = rules.replace("d", "").replace("{0}", "sensorKeyValue");
+				rules = rules.replace("d", "").replace("{0}", "value");
 				Expression expression = AviatorEvaluator.compile(rules);
 				Map<String, Object> env = new HashMap<String, Object>();
 				env.put("value", Double.valueOf(sensorKeyValue.toString()));
@@ -376,4 +385,92 @@
 		
 		return returnMap;
 	}
+
+	@Override
+	public List<Map<String, Object>> getMointorPointRankByOrganizationId(Map<String, Object> parameters) {
+		//������������id
+		String organizationId = parameters.get("organization_id").toString();
+		//������������������������������������������������������id
+		List<Object> organizationIdList = new ArrayList<>();
+		organizationIdList.add(organizationId);
+		//���������������������������������id
+		List<Object> parentIdList = new ArrayList<>();
+		parentIdList.add(organizationId);
+		
+		//������������
+		for(int i=0;i<10;i++) {
+			if(!parentIdList.isEmpty()) {
+				//������������������������������
+				List<Map<String, Object>> childIdList = organizationRelationMapper.getChildIdByParentId(parentIdList);
+				//���������������id������
+				parentIdList.clear();
+				//������������id������������������
+				for (Map<String, Object> map : childIdList) {
+					organizationIdList.add(map.get("child_id"));
+					parentIdList.add(map.get("child_id"));
+				}
+			}else {
+				break;
+			}
+		}
+		System.out.println(organizationIdList);
+		
+		//������������id������������id
+		List<Map<String, Object>> monitorPointIdListMap = monitorPointMapper.getIdByOrganizationId(organizationIdList);
+		//���List<Map<String, Object>>���������List<Object>
+		List<Object> monitorPointIdList = new ArrayList<>();
+		for (Map<String, Object> map : monitorPointIdListMap) {
+			monitorPointIdList.add(map.get("id"));
+		}
+		
+		//������������id���������������������������
+		//������������������������
+		Calendar rightNow = Calendar.getInstance();
+		//���������������������������
+		Calendar beforeTime = Calendar.getInstance();
+		beforeTime.add(Calendar.MINUTE, -5);// 5���������������������
+		Date beforeD = beforeTime.getTime();
+		Date end = rightNow.getTime();
+		String endTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(end);
+		String before5Time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(beforeD);  // ������������������
+		
+		String sensor = parameters.get("sensor").toString();
+		List<Map<String, Object>> result = historyMapper.getAVGSensorRankByMonitorPointIdList(sensor, monitorPointIdList, before5Time, endTime);
+		return result;
+	}
+
+	@Override
+	public Map<String, Object> gitHourlyAQIByMonitorPointIdAndTimeslot(Map<String, Object> parameters) {
+		String monitor_point_id = parameters.get("monitor_point_id").toString();
+		//������������
+		LocalDate localDate = LocalDate.now();
+    	Calendar c = Calendar.getInstance();//������������������������������������
+    	int endHour = c.get(Calendar.HOUR_OF_DAY);
+    	String endTime = localDate+" "+endHour+":00:00";
+    	
+    	String startTime;
+    	if(endHour == 0) {
+    		LocalDate startDate = localDate.minusDays(1);
+    		startTime = startDate+" "+"23:00:00";
+    	}else {
+			int startHour = endHour-1;
+			startTime = localDate+" "+startHour+":00:00";
+		}
+		Map<String, Object> map = historyMapper.getMonitorPointAVGValueByMonitorPointIdAndTimeslot(monitor_point_id, startTime, endTime);
+		System.out.println(map);
+		Map<String, Object> returnMap = new HashMap<>();
+		if (map.isEmpty()) {
+			returnMap.put("AQI", "N/V");
+		} else {
+			Map<String, Double> AQIMap = new HashMap<>();
+			for (Map.Entry<String, Object> entry : map.entrySet()) {
+				String key = entry.getKey();
+				Double value = Double.parseDouble(entry.getValue().toString());
+				AQIMap.put(key, value);
+	        }
+			returnMap = AQICalculation.hourlyAQI(AQIMap);
+		}
+		
+		return returnMap;
+	}
 }

--
Gitblit v1.8.0