From c95b4b1890598b1e451fa593c173d37952cff58d Mon Sep 17 00:00:00 2001
From: xufenglei <xufenglei>
Date: Thu, 16 Nov 2017 11:14:21 +0800
Subject: [PATCH] 接口拆分

---
 src/main/java/com/moral/monitor/service/impl/ScreenServiceImpl.java |  106 ++++++++++++++++++++++++++
 src/main/resources/mongodb/mongodb-config.properties                |    6 
 src/main/java/com/moral/monitor/service/ScreenService.java          |    4 +
 src/main/java/com/moral/monitor/controller/ScreenController.java    |   67 ++++++++++++++++
 4 files changed, 176 insertions(+), 7 deletions(-)

diff --git a/src/main/java/com/moral/monitor/controller/ScreenController.java b/src/main/java/com/moral/monitor/controller/ScreenController.java
index 4f6fe2d..1376544 100644
--- a/src/main/java/com/moral/monitor/controller/ScreenController.java
+++ b/src/main/java/com/moral/monitor/controller/ScreenController.java
@@ -9,25 +9,27 @@
 
 import javax.servlet.http.HttpServletRequest;
 
-import com.alibaba.fastjson.JSON;
-import com.moral.monitor.util.RedisUtil;
 import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.lang.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.core.io.Resource;
 import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.util.ObjectUtils;
 import org.springframework.web.bind.annotation.CrossOrigin;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RestController;
 
+import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONReader;
 import com.alibaba.fastjson.TypeReference;
 import com.moral.monitor.entity.AccountEntity;
 import com.moral.monitor.service.ScreenService;
 import com.moral.monitor.util.BusinessException;
 import com.moral.monitor.util.Crypto;
+import com.moral.monitor.util.RedisUtil;
+import com.moral.monitor.util.ResourceUtil;
 import com.moral.monitor.util.WebUtils;
 
 @RestController
@@ -201,4 +203,65 @@
 		}
 		return result;
 	}
+	
+	@RequestMapping(value = "month-sensor-average", method = RequestMethod.GET)
+	public Map<String, Object> getMonthAverageBySensor(HttpServletRequest request) {
+		Map<String, Object> result = new HashMap<String, Object>();
+		try {
+			Map<String, Object> parameters = WebUtils.getParametersStartingWith(request, null);
+			if (!(parameters.containsKey("mac") && parameters.containsKey("macKey"))) {
+				result.put("msg", "���������������������");
+			} else {
+				result = screenService.getMonthAverageBySensor(parameters);
+			}
+		} catch (BusinessException be) {
+			be.printStackTrace();
+			result.put("msg", be.getMessage());
+		} catch (Exception e) {
+			e.printStackTrace();
+			result.put("msg", "���������������������������������������������������"+e.getMessage());
+		}
+		return result;
+	}
+
+	@RequestMapping(value = "day-aqi", method = RequestMethod.GET)
+	public Map<String, Object> getDayAQIBySensor(HttpServletRequest request) {
+		Map<String, Object> result = new HashMap<String, Object>();
+		try {
+			Map<String, Object> parameters = WebUtils.getParametersStartingWith(request, null);
+			if (!parameters.containsKey("mac")) {
+				result.put("msg", "���������������������");
+			} else {
+				result = screenService.getDayAQIBySensor(parameters);
+			}
+		} catch (BusinessException be) {
+			be.printStackTrace();
+			result.put("msg", be.getMessage());
+		} catch (Exception e) {
+			e.printStackTrace();
+			result.put("msg", "���������������������������������������������������"+e.getMessage());
+		}
+		return result;
+	}
+
+	@RequestMapping(value = "sensor-standard", method = RequestMethod.GET)
+	public Map<String, Object> getStandardBySensor(HttpServletRequest request) {
+		Map<String, Object> result = new HashMap<String, Object>();
+		try {
+			String macKey = request.getParameter("macKey");
+			if (ObjectUtils.isEmpty(macKey)) {
+				result.put("msg", "���������������������");
+			} else {
+				result.put("standard", ResourceUtil.getValue(macKey + "-standard"));
+			}
+		} catch (BusinessException be) {
+			be.printStackTrace();
+			result.put("msg", be.getMessage());
+		} catch (Exception e) {
+			e.printStackTrace();
+			result.put("msg", "���������������������������������������������������"+e.getMessage());
+		}
+		return result;
+	}
+
 }
diff --git a/src/main/java/com/moral/monitor/service/ScreenService.java b/src/main/java/com/moral/monitor/service/ScreenService.java
index c6c5bce..5b7f81c 100644
--- a/src/main/java/com/moral/monitor/service/ScreenService.java
+++ b/src/main/java/com/moral/monitor/service/ScreenService.java
@@ -17,4 +17,8 @@
 
 	Map<String, Object> getAverageBySensor(Map<String, Object> parameters);
 
+	Map<String, Object> getMonthAverageBySensor(Map<String, Object> parameters);
+
+	Map<String, Object> getDayAQIBySensor(Map<String, Object> parameters) throws Exception;
+
 }
diff --git a/src/main/java/com/moral/monitor/service/impl/ScreenServiceImpl.java b/src/main/java/com/moral/monitor/service/impl/ScreenServiceImpl.java
index 28cfbfb..338230a 100644
--- a/src/main/java/com/moral/monitor/service/impl/ScreenServiceImpl.java
+++ b/src/main/java/com/moral/monitor/service/impl/ScreenServiceImpl.java
@@ -5,6 +5,7 @@
 import java.util.Collections;
 import java.util.Date;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
@@ -18,9 +19,14 @@
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.time.DateUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.mongodb.core.MongoTemplate;
+import org.springframework.data.mongodb.core.aggregation.Aggregation;
+import org.springframework.data.mongodb.core.aggregation.AggregationResults;
+import org.springframework.data.mongodb.core.query.Criteria;
 import org.springframework.stereotype.Service;
 import org.springframework.util.ObjectUtils;
 
+import com.mongodb.BasicDBObject;
 import com.moral.monitor.dao.AccountEntityMapper;
 import com.moral.monitor.dao.HistoryEntityMapper;
 import com.moral.monitor.entity.AccountEntity;
@@ -43,6 +49,9 @@
 	@Autowired
 	private OrganizationService organizationService;
 
+	@Autowired
+	private MongoTemplate mongoTemplate;
+	
 	public List<AccountEntity> getAccountLists(String account, String password) {
 		AccountEntityExample example = new AccountEntityExample();
 		example.or().andAccountEqualTo(account).andPasswordEqualTo(password);
@@ -120,11 +129,11 @@
 			resultMap.put("average", String.format("%.2f", average.get("average")));
 		}
 		
-		List<Double> IAQIs = new ArrayList<Double>();
+		Set<Double> IAQIs = new HashSet<Double>();
 		for (String macKey : macKeys) {
 			IAQIs.add(cs.take().get());
 		}
-		
+		IAQIs.remove(null);
 		if (ObjectUtils.isEmpty(IAQIs)) {
 			resultMap.put("AQI", "N/A");
 		} else {
@@ -224,4 +233,97 @@
 		return result;
 	}
 
+	@Override
+	public Map<String, Object> getMonthAverageBySensor(Map<String, Object> parameters) {
+		Map<String, Object> result = new HashMap<String, Object>();
+		Date date = new Date();
+		Long end = DateUtils.truncate(date, Calendar.DATE).getTime(),start;
+		// ���������������������������������������
+		if (1 == Calendar.getInstance().get(Calendar.DAY_OF_MONTH)) {
+			// ���������1���00:00:00
+			start = DateUtils.truncate(DateUtils.addMonths(date, -1), Calendar.MONTH).getTime();
+		} else {
+			// ���������1���00:00:00
+			start =  DateUtils.truncate(date, Calendar.MONTH).getTime();
+		}
+		Aggregation aggregation = Aggregation.newAggregation(
+				Aggregation.match(Criteria.where("mac").is(parameters.get("mac"))),
+				Aggregation.match(Criteria.where("time").gte(start)),
+				Aggregation.match(Criteria.where("time").lt(end)),
+				Aggregation.group("mac").avg((String) parameters.get("macKey")).as("avg")
+			);
+		AggregationResults<BasicDBObject> results = mongoTemplate.aggregate(aggregation, "data",BasicDBObject.class);
+		List<BasicDBObject> list = results.getMappedResults();
+		if (!ObjectUtils.isEmpty(list)) {
+			result = list.get(0);
+		}
+		return result;
+	}
+
+	@Override
+	public Map<String, Object> getDayAQIBySensor(final Map<String, Object> parameters) throws Exception {
+		Map<String, Object> resultMap = new HashMap<String, Object>();
+		Date date = new Date();
+		// ������00:00:00
+		parameters.put("start", DateUtils.truncate(DateUtils.addDays(date, -1), Calendar.DATE));
+		// ������00:00:00
+		parameters.put("end", DateUtils.truncate(date, Calendar.DATE));
+		String[] macKeys = { "e1", "e2", "e10", "e11", "e15", "e16" };
+		final String[] IAQIValues = ResourceUtil.getArrValue("IAQI");
+		ExecutorService threadPool = Executors.newCachedThreadPool();
+		CompletionService<Double> cs = new ExecutorCompletionService<Double>(threadPool);
+		for (final String macKey : macKeys) {
+			cs.submit(new Callable<Double>() {
+				@Override
+				public Double call() throws Exception {
+					Double result = null;
+					double avg = 0, minMacKey = 0, maxMacKey = 0, minIAQI = 0, maxIAQI = 0;
+					Map<String, Object> map = new HashMap<String, Object>(parameters);
+					map.put("macKey", macKey);
+					Map<String, Object> average = historyMapper.getMonthAverageBySensor(map);
+					if (!ObjectUtils.isEmpty(average)) {
+						avg = (Double) average.get("average");
+						String[] macKeyValues = ResourceUtil.getArrValue(macKey);
+						int index = -1;
+						for (int i = 0; i < macKeyValues.length; i++) {
+							if (avg <= Double.valueOf(macKeyValues[i])) {
+								if (i == 0) {
+									index = i;
+								} else {
+									index = i - 1;
+								}
+								break;
+							}
+						}
+						if (index == -1) {
+							result = Double.MAX_VALUE;
+						} else {
+							minMacKey = Double.valueOf(macKeyValues[index]);
+							maxMacKey = Double.valueOf(macKeyValues[index + 1]);
+							minIAQI = Double.valueOf(IAQIValues[index]);
+							maxIAQI = Double.valueOf(IAQIValues[index + 1]);
+							result = calculateIAQI(maxIAQI, minIAQI, maxMacKey, minMacKey, avg);
+						}
+					}
+					return result;
+				}
+			});
+		}
+		Set<Double> IAQIs = new HashSet<Double>();
+		for (String macKey : macKeys) {
+			IAQIs.add(cs.take().get());
+		}
+		IAQIs.remove(null);
+		if (ObjectUtils.isEmpty(IAQIs)) {
+			resultMap.put("AQI", "N/A");
+		} else {
+			Double AQI = Collections.max(IAQIs);
+			if (AQI == Double.MAX_VALUE) {
+				resultMap.put("AQI", IAQIValues[IAQIValues.length - 1]);
+			} else {
+				resultMap.put("AQI", String.format("%.0f", AQI));
+			}
+		}
+		return resultMap;
+	}
 }
diff --git a/src/main/resources/mongodb/mongodb-config.properties b/src/main/resources/mongodb/mongodb-config.properties
index 288872a..0c2c52c 100644
--- a/src/main/resources/mongodb/mongodb-config.properties
+++ b/src/main/resources/mongodb/mongodb-config.properties
@@ -7,10 +7,10 @@
 #���������������connection���������������������������connection������������N���������������������
 mongo.threadsAllowedToBlockForConnectionMultiplier=4
 #���������������������������������������������������������ms���������������0
-mongo.connectTimeout=1000
+mongo.connectTimeout=10000
 #���������������������������������������������������������������ms���
-mongo.maxWaitTime=1500
+mongo.maxWaitTime=15000
 #������������socket���������������
 mongo.socketKeepAlive=true
 #���������������������;���������������������Socket.setSoTimeout(int)������������0(������)
-mongo.socketTimeout=1500
\ No newline at end of file
+mongo.socketTimeout=15000
\ No newline at end of file

--
Gitblit v1.8.0