From 881e13105ca2d99be13ef6f06137c0e4763e3a78 Mon Sep 17 00:00:00 2001
From: xufenglei <xufenglei>
Date: Fri, 03 Nov 2017 15:29:07 +0800
Subject: [PATCH] sql 改进

---
 src/main/resources/mapper/HistoryEntityMapper.xml |   68 +++++++++++++--------------------
 1 files changed, 27 insertions(+), 41 deletions(-)

diff --git a/src/main/resources/mapper/HistoryEntityMapper.xml b/src/main/resources/mapper/HistoryEntityMapper.xml
index 21e40f4..cddc593 100644
--- a/src/main/resources/mapper/HistoryEntityMapper.xml
+++ b/src/main/resources/mapper/HistoryEntityMapper.xml
@@ -238,55 +238,39 @@
   </select>
 
 	<select id="getAverageByAll" resultType="map">
-<!-- 
 		SELECT
-			h.mac_key,
+			<if test="macKey != null and macKey != 'all'">
+				e.name,
+			</if>	
+			<if test="macKey == 'all'">
+				h.mac_key,
+			</if>	
 			AVG(h.mac_value) avg
 		FROM
 			history h,
 			monitorpoint m,
 			equipment e
-		<if test="orgIds != null and orgIds.size > 0">
-			JOIN org_equ oe ON e.id = oe.equid
-			AND oe.orgid IN
-			<foreach collection="orgIds" item="listItem" open="(" separator="," close=")" >
-				#{listItem}
-			</foreach>
-		</if>
 		WHERE
 			m.areacode = #{areaCode}
 		AND m.id = e.monitorpoint
 		AND e.mac = h.mac
 		AND h.time &gt; #{start}
 		AND h.time &lt; #{end}
-		GROUP BY
-			h.mac_key
- -->
-
-		SELECT
-			mac_key,
-			AVG(mac_value) avg
-		FROM
-			history h
-			<if test="orgIds!=null and orgIds.size > 0">
-				JOIN (
-					SELECT
-						e.mac
-					FROM
-						equipment e,
-						org_equ oe
-					WHERE
-						e.id = oe.equid
-					AND oe.orgid IN
-					<foreach collection="orgIds" item="listItem" open="(" separator="," close=")" >
-						#{listItem}
-					</foreach>
-				) e ON h.mac = e.mac
-			</if>
-			WHERE h.time &gt; #{start}
-			AND h.time &lt; #{end}
-		GROUP BY
-			mac_key
+		<if test="orgIds != null and orgIds.size > 0">
+			<!-- JOIN org_equ oe ON e.id = oe.equid -->
+			AND e.owner_id IN
+			<foreach collection="orgIds" item="listItem" open="(" separator="," close=")" >
+				#{listItem}
+			</foreach>
+		</if>
+		<if test="macKey != null and macKey != 'all'">
+			AND	h.mac_key = #{macKey}
+			GROUP BY e.id
+			ORDER BY avg
+		</if>	
+		<if test="macKey == 'all'">
+			GROUP BY h.mac_key
+		</if>	
 	</select>
 
 	<select id="getEquipmentStates" resultType="map">
@@ -296,14 +280,16 @@
 		FROM
 			equipment
 		<if test="orgIds != null and orgIds.size > 0">
-			JOIN org_equ oe ON e.id = oe.equid
-			AND oe.orgid IN
+			<!-- JOIN org_equ oe ON e.id = oe.equid -->
+			where owner_id IN
 			<foreach collection="orgIds" item="listItem" open="(" separator="," close=")" >
 				#{listItem}
 			</foreach>
 		</if>
-		GROUP BY
-			state
+		GROUP BY state
 	</select>
 
+	<select id="getMacLogByLast" resultType="string">
+		SELECT mac_log FROM logger ORDER BY time DESC LIMIT 1
+	</select>
 </mapper>
\ No newline at end of file

--
Gitblit v1.8.0