From 1d25854b8dfdebe7b4d5153e932c0e74ed5f7453 Mon Sep 17 00:00:00 2001
From: ZhuDongming <773644075@qq.com>
Date: Thu, 15 Aug 2019 17:21:51 +0800
Subject: [PATCH] 添加查询最大值和最小值的service接口

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

diff --git a/src/main/java/com/moral/service/impl/HistoryServiceImpl.java b/src/main/java/com/moral/service/impl/HistoryServiceImpl.java
index 72410be..6628fe1 100644
--- a/src/main/java/com/moral/service/impl/HistoryServiceImpl.java
+++ b/src/main/java/com/moral/service/impl/HistoryServiceImpl.java
@@ -9,6 +9,7 @@
 import java.time.temporal.TemporalAdjusters;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Calendar;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.Date;
@@ -24,6 +25,8 @@
 
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
+import com.googlecode.aviator.AviatorEvaluator;
+import com.googlecode.aviator.Expression;
 import com.moral.common.util.ValidateUtil;
 import com.moral.entity.Device;
 import com.moral.entity.Profession;
@@ -31,9 +34,11 @@
 import com.moral.mapper.DeviceMapper;
 import com.moral.mapper.HistoryMapper;
 import com.moral.mapper.SensorMapper;
+import com.moral.mapper.SensorUnitMapper;
 import com.moral.service.AccountService;
 import com.moral.service.HistoryService;
 import com.moral.service.ProfessionService;
+import com.moral.util.AQICalculation;
 
 @Service
 public class HistoryServiceImpl implements HistoryService {
@@ -241,26 +246,55 @@
     		map = JSON.parseObject(values.get(0).get("value").toString());
 		}
     	Device device = deviceMapper.selectByPrimaryKey(Integer.valueOf((String) parameters.get("deviceId")));
+    	Map<String, Object> sensorUnits = getSensorUnitByDeviceMac(parameters);
     	for (Map<String, Object> sensor : sensors) {
+    		Object sensorKey = sensor.get("sensor_key");
+    		sensor.put("name", sensor.get("description"));
+    		sensor.put("state", device.getState());
     		if (!ObjectUtils.isEmpty(map)) {
     			sensor.put("value", map.remove(sensor.get("sensor_key")));
 			}
-    		sensor.put("state", device.getState());
-    		sensor.put("name", sensor.get("description"));
+    		if (sensorUnits.containsKey(sensorKey)) {
+    			Map<String, Object> sensorUnit  = (Map<String, Object>) sensorUnits.get(sensorKey);
+    			sensor.put("unit", sensorUnit.get("name"));
+    			if (sensor.containsKey("value")) {
+    				Object value = sensor.get("value");
+    				String rules = sensorUnit.get("rules").toString();
+    				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(value.toString()));
+    				value = expression.execute(env);
+    				sensor.put("value",String.format("%.2f", value));
+				}
+			}
     	}
 		return sensors;
 	}
 
 	@Override
 	public List<Map<String, Object>> getSensorDataBySensorKey(Map<String, Object> parameters) {
-		ValidateUtil.notNull(parameters.get("sensorKey"), "param.is.null");
+		Object sensorKey = parameters.get("sensorKey");
+		ValidateUtil.notNull(sensorKey, "param.is.null");
 		ValidateUtil.notNull(parameters.get("mac"), "param.is.null");
 		ValidateUtil.notNull(parameters.get("size"), "param.is.null");
 		parameters.put("size", Integer.valueOf(parameters.remove("size").toString()));
 		List<Map<String, Object>> values = historyMapper.getValueByMacAndSize(parameters);
+		Map<String, Object> sensorUnits = getSensorUnitByDeviceMac(parameters);
 		for (Map<String, Object> value : values) {
 			JSONObject json = JSON.parseObject(value.remove("value").toString());
-			value.put("value", json.get(parameters.get("sensorKey")));
+			Object sensorKeyValue = json.get(sensorKey);
+			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}", "value");
+				Expression expression = AviatorEvaluator.compile(rules);
+				Map<String, Object> env = new HashMap<String, Object>();
+				env.put("value", Double.valueOf(sensorKeyValue.toString()));
+				sensorKeyValue = expression.execute(env);
+				sensorKeyValue = String.format("%.2f", sensorKeyValue);
+			}
+			value.put("value", sensorKeyValue);
 		}
 		return values;
 	}
@@ -295,4 +329,51 @@
 		return result;
 	}
 
+	@Resource
+	private SensorUnitMapper sensorUnitMapper;
+	@Override
+	public Map<String, Object> getSensorUnitByDeviceMac(Map<String, Object> parameters){
+		Map<String, Object> resultMap = new HashMap<String, Object>();
+		List<Map<String, Object>> list = sensorUnitMapper.getSensorUnitByDeviceMac(parameters);
+		for (Map<String, Object> map : list) {
+			resultMap.put(map.remove("sensor_key").toString(), map);
+		}
+		return resultMap;
+		
+	}
+
+	@Override
+	public Map<String, Object> gitHourlyAQIByMacAndTimeslot(Map<String, Object> parameters) {
+		String mac = parameters.get("mac").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.getAVGValueByMacAndTimeslot(mac, 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