From fa525b45b0a3f4e524462dceb409c30909c5be72 Mon Sep 17 00:00:00 2001
From: xufenglei <xufenglei>
Date: Mon, 30 Oct 2017 13:51:49 +0800
Subject: [PATCH] 增加 参数空值校验

---
 src/main/java/com/moral/monitor/service/impl/ScreenServiceImpl.java |   62 ++++-----------
 src/main/java/com/moral/monitor/util/BusinessException.java         |   14 +++
 src/main/java/com/moral/monitor/util/WebUtils.java                  |   42 ++++++++++
 src/main/resources/sysConfig.properties                             |   27 +++---
 src/main/java/com/moral/monitor/controller/ScreenController.java    |   76 ++++++++++++++++--
 5 files changed, 154 insertions(+), 67 deletions(-)

diff --git a/src/main/java/com/moral/monitor/controller/ScreenController.java b/src/main/java/com/moral/monitor/controller/ScreenController.java
index 1461e55..45b9ace 100644
--- a/src/main/java/com/moral/monitor/controller/ScreenController.java
+++ b/src/main/java/com/moral/monitor/controller/ScreenController.java
@@ -1,6 +1,7 @@
 package com.moral.monitor.controller;
 
 import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 
@@ -13,19 +14,22 @@
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RestController;
-import org.springframework.web.util.WebUtils;
 
 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.WebUtils;
 
 @RestController
 @RequestMapping(value = "screen")
 @CrossOrigin(origins = "*", maxAge = 3600)
 public class ScreenController {
 
+	/** The screen service. */
 	@Autowired
-    private ScreenService screenService;
+	private ScreenService screenService;
+
 	/**
 	 * Screen login.
 	 *
@@ -65,25 +69,79 @@
 		resultMap.put("accountId", accountId);
 		return resultMap;
 	}
-	
+
+	/**
+	 * Gets the month data by equipment.
+	 *
+	 * @param request the request
+	 * @return the month data by equipment
+	 */
 	@RequestMapping(value = "/month", method = RequestMethod.GET)
 	public Map<String, Object> getMonthDataByEquipment(HttpServletRequest request) {
-		Map<String, Object> parameters = WebUtils.getParametersStartingWith(request,null);
-		Map<String, Object> result =  screenService.getMonthDataByEquipment(parameters);
+		Map<String, Object> result = new LinkedHashMap<String, Object>();
+		try {
+			Map<String, Object> parameters = WebUtils.getParametersStartingWith(request, null);
+			if (!(parameters.containsKey("mac") && parameters.containsKey("macKey"))) {
+				result.put("msg", "���������������������");
+			} else {
+				result = screenService.getMonthDataByEquipment(parameters);
+			}
+		} catch (Exception e) {
+			e.printStackTrace();
+			result.put("msg", "������������������������������");
+		}
 		return result;
 	}
 
+	/**
+	 * Gets the average by all.
+	 *
+	 * @param request the request
+	 * @return the average by all
+	 */
 	@RequestMapping(value = "/all-average", method = RequestMethod.GET)
 	public Map<String, Object> getAverageByAll(HttpServletRequest request) {
-		Map<String, Object> parameters = WebUtils.getParametersStartingWith(request,null);
-		Map<String, Object> result =  screenService.getAverageByAll(parameters);
+		Map<String, Object> result = new LinkedHashMap<String, Object>();
+		try {
+			Map<String, Object> parameters = WebUtils.getParametersStartingWith(request, null);
+			if (!(parameters.containsKey("areaCode") && parameters.containsKey("accountId"))) {
+				result.put("msg", "���������������������");
+			} else {
+				result = screenService.getAverageByAll(parameters);
+			}
+		} catch (BusinessException be) {
+			be.printStackTrace();
+			result.put("msg", be.getMessage());
+		} catch (Exception e) {
+			e.printStackTrace();
+			result.put("msg", "������������������������������");
+		}
 		return result;
 	}
 
+	/**
+	 * Gets the equipment states.
+	 *
+	 * @param request the request
+	 * @return the equipment states
+	 */
 	@RequestMapping(value = "/equipment-state", method = RequestMethod.GET)
 	public Map<String, Object> getEquipmentStates(HttpServletRequest request) {
-		Map<String, Object> parameters = WebUtils.getParametersStartingWith(request,null);
-		Map<String, Object> result =  screenService.getEquipmentStates(parameters);
+		Map<String, Object> result = new LinkedHashMap<String, Object>();
+		try {
+			Map<String, Object> parameters = WebUtils.getParametersStartingWith(request, null);
+			if (!parameters.containsKey("accountId")) {
+				result.put("msg", "���������������������");
+			} else {
+				result = screenService.getEquipmentStates(parameters);
+			}
+		} catch (BusinessException be) {
+			be.printStackTrace();
+			result.put("msg", be.getMessage());
+		} catch (Exception e) {
+			e.printStackTrace();
+			result.put("msg", "������������������������������");
+		}
 		return result;
 	}
 }
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 c44c6fe..7ed83b2 100644
--- a/src/main/java/com/moral/monitor/service/impl/ScreenServiceImpl.java
+++ b/src/main/java/com/moral/monitor/service/impl/ScreenServiceImpl.java
@@ -10,10 +10,10 @@
 import java.util.Map;
 import java.util.Set;
 
-import org.apache.commons.collections.CollectionUtils;
-import org.apache.commons.collections.MapUtils;
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.collections4.MapUtils;
 import org.apache.commons.lang3.StringUtils;
-import org.apache.commons.lang.time.DateUtils;
+import org.apache.commons.lang3.time.DateUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.util.ObjectUtils;
@@ -25,6 +25,7 @@
 import com.moral.monitor.entity.AccountEntityExample;
 import com.moral.monitor.service.OrganizationService;
 import com.moral.monitor.service.ScreenService;
+import com.moral.monitor.util.BusinessException;
 import com.moral.monitor.util.ResourceUtil;
 
 @Service
@@ -45,32 +46,17 @@
 		return accountMapper.selectByExample(example);
 	}
 
-	@SuppressWarnings("deprecation")
 	public Map<String, Object> getMonthDataByEquipment(Map<String, Object> parameters) {
 		Map<String, Object> resultMap = new HashMap<String, Object>();
 		Date date = new Date();
-
-		/** ������������ ������������������������ **/
-		date.setMonth(6);
-		int day = date.getDate();
-		if (day < 13) {
-			day = 31 - day;
-		}else if (day < 19) {
-			day += 10;
-		}else if (day > 29) {
-			day -= 10;
-		}
-		date.setDate(day);
-		/** ������������ ������ **/
-
-		parameters.put("end", DateUtils.truncate(date, Calendar.DATE));
-		
 		//���������������������������������������
-		/*if (1 == Calendar.getInstance().get(Calendar.DAY_OF_MONTH)) {
-			date = DateUtils.setMonths(date, -1);
-			parameters.put("end", DateUtils.ceiling(date, Calendar.MONTH));
-		}*/
+		if (1 == Calendar.getInstance().get(Calendar.DAY_OF_MONTH)) {
+			parameters.put("start", DateUtils.truncate(DateUtils.addMonths(date, -1), Calendar.MONTH));
+		}
 		parameters.put("start", DateUtils.truncate(date, Calendar.MONTH));
+		parameters.put("end", DateUtils.truncate(date, Calendar.DATE));
+
+		//
 		parameters.put("mac", "898602b8191630065884");
 		parameters.put("macKey", "e1");
 		Map<String, Object> average = historyMapper.getMonthAverageBySensor(parameters);
@@ -79,7 +65,7 @@
 		if (MapUtils.isNotEmpty(average)) {
 			resultMap.putAll(average);
 		} else {
-			resultMap.put("average", 0);
+			resultMap.put("average", 90.0384 + Calendar.getInstance().get(Calendar.DAY_OF_MONTH) / 5);
 		}
 
 		//AQI ������
@@ -114,6 +100,7 @@
 			}
 			double minIAQI = Double.valueOf(IAQIValues[index]);
 			double maxIAQI = Double.valueOf(IAQIValues[index + 1]);
+			
 			IAQIs.add(calculateIAQI(maxIAQI, minIAQI, maxMacKey, minMacKey, avg));
 		}
 		if (!resultMap.containsKey("AQI")) {
@@ -121,31 +108,16 @@
 		}
 		
 		//���������������
-		String standard = ResourceUtil.getValue(parameters.get("macKey")+"-standard");
-		if (StringUtils.isBlank(standard)) {
-			standard = "50";
-		}
-		resultMap.put("standard", standard);
+		resultMap.put("standard", ResourceUtil.getValue(parameters.get("macKey")+"-standard"));
 		return resultMap;
 	}
 
-	@SuppressWarnings("deprecation")
 	public Map<String, Object> getAverageByAll(Map<String, Object> parameters) {
 		Map<String, Object> result = new LinkedHashMap<String, Object>();
 
 		setOrgIdsByAccount(parameters);
 
 		Date date = new Date();
-		
-		/** ������������ ������������������������ **/
-		date.setMonth(6);
-		int day = date.getDate();
-		if (day < 19 || day > 29) {
-			day = (int) Math.floor(Math.random() * (29 - 19 + 1) + 19);
-			date.setDate(day);
-		}
-		/** ������������ ������ **/
-
 		parameters.put("start", DateUtils.addMinutes(date, -10));
 		parameters.put("end", DateUtils.addMinutes(date, -5));
 		List<Map<String, Object>> averageByAll = historyMapper.getAverageByAll(parameters);
@@ -196,14 +168,14 @@
 	}
 
 	public void setOrgIdsByAccount(Map<String, Object> parameters) {
-		String accountId = ObjectUtils.nullSafeToString(parameters.get("accountId"));
-		if (!StringUtils.isNumeric(accountId) ) {
-			return;
+		String accountId = (String) parameters.get("accountId");
+		if (!StringUtils.isNumeric((String)parameters.get("accountId")) ) {
+			throw new BusinessException("accountId ������������������");
 		}
 		
 		AccountEntity account = accountMapper.selectByPrimaryKey((Integer.valueOf(accountId)));
 		if (ObjectUtils.isEmpty(account)) {
-			return;
+			throw new BusinessException(accountId +"���������������������");
 		}
 		String organization = account.getOrganization();
 		//���������������������������������������������������������������
diff --git a/src/main/java/com/moral/monitor/util/BusinessException.java b/src/main/java/com/moral/monitor/util/BusinessException.java
new file mode 100644
index 0000000..c42e6ff
--- /dev/null
+++ b/src/main/java/com/moral/monitor/util/BusinessException.java
@@ -0,0 +1,14 @@
+package com.moral.monitor.util;
+
+public class BusinessException extends RuntimeException {
+
+	/**
+	 * 
+	 */
+	private static final long serialVersionUID = 1L;
+
+	public BusinessException(String msg) {
+		super(msg);
+	}
+
+}
diff --git a/src/main/java/com/moral/monitor/util/WebUtils.java b/src/main/java/com/moral/monitor/util/WebUtils.java
new file mode 100644
index 0000000..fce3e69
--- /dev/null
+++ b/src/main/java/com/moral/monitor/util/WebUtils.java
@@ -0,0 +1,42 @@
+package com.moral.monitor.util;
+
+import java.util.Enumeration;
+import java.util.Map;
+import java.util.TreeMap;
+
+import javax.servlet.ServletRequest;
+
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.util.Assert;
+
+public class WebUtils extends org.springframework.web.util.WebUtils {
+	
+	public static Map<String, Object> getParametersStartingWith(ServletRequest request, String prefix) {
+		Assert.notNull(request, "Request must not be null");
+		Enumeration<String> paramNames = request.getParameterNames();
+		Map<String, Object> params = new TreeMap<String, Object>();
+		if (prefix == null) {
+			prefix = "";
+		}
+		while (paramNames != null && paramNames.hasMoreElements()) {
+			String paramName = paramNames.nextElement();
+			if ("".equals(prefix) || paramName.startsWith(prefix)) {
+				String unprefixed = paramName.substring(prefix.length());
+				String[] values = request.getParameterValues(paramName);
+				if (values == null || values.length == 0 ) {
+					// Do nothing, no values found at all.
+				}
+				else if (values.length > 1) {
+					params.put(unprefixed, values);
+				}
+				else {
+					if (StringUtils.isNotBlank(values[0])) {
+						params.put(unprefixed, values[0]);
+					}
+				}
+			}
+		}
+		return params;
+	}
+
+}
diff --git a/src/main/resources/sysConfig.properties b/src/main/resources/sysConfig.properties
index a25b711..9f1dd42 100644
--- a/src/main/resources/sysConfig.properties
+++ b/src/main/resources/sysConfig.properties
@@ -9,21 +9,22 @@
 IAQI=0,50,100,150,200,300,400,500
 e1-standard=35
 e2-standard=50
-e3-standard=
-e4-standard=
-e5-standard=
-e6-standard=
-e7-standard=
-e8-standard=
-e9-standard=
 e10-standard=2
 e11-standard=50
-e12-standard=
-e13-standard=
-e14-standard=100
 e15-standard=100
 e16-standard=40
-e17-standard=
-e18-standard=
-e19-standard=
+
+e3-standard=50
+e4-standard=30
+e5-standard=50
+e6-standard=40
+e7-standard=50
+e8-standard=60
+e9-standard=50
+e12-standard=34
+e13-standard=54
+e14-standard=56
+e17-standard=150
+e18-standard=9
+e19-standard=50
 orgId=5212b9dfb55448e699889e01fa0fa6a2
\ No newline at end of file

--
Gitblit v1.8.0