From 1830f5429730a40f7d2f17ca748a80a4b1046c21 Mon Sep 17 00:00:00 2001
From: xufenglei <xufenglei>
Date: Mon, 25 Mar 2019 09:12:22 +0800
Subject: [PATCH] update

---
 src/main/resources/mapper/HistoryMapper.xml |  535 ++++++++++++++++++-----------------------------------------
 1 files changed, 162 insertions(+), 373 deletions(-)

diff --git a/src/main/resources/mapper/HistoryMapper.xml b/src/main/resources/mapper/HistoryMapper.xml
index 5f805da..8cc81ab 100644
--- a/src/main/resources/mapper/HistoryMapper.xml
+++ b/src/main/resources/mapper/HistoryMapper.xml
@@ -1,376 +1,165 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
-<mapper namespace="com.moral.mapper.HistoryMapper">
-  <resultMap id="BaseResultMap" type="com.moral.entity.History">
-    <result column="mac" jdbcType="VARCHAR" property="mac" />
-    <result column="value" jdbcType="OTHER" property="value" />
-    <result column="time" jdbcType="TIMESTAMP" property="time" />
-    <result column="version" jdbcType="INTEGER" property="version" />
-  </resultMap>
-  <sql id="Example_Where_Clause">
-    <where>
-      <foreach collection="oredCriteria" item="criteria" separator="or">
-        <if test="criteria.valid">
-          <trim prefix="(" prefixOverrides="and" suffix=")">
-            <foreach collection="criteria.criteria" item="criterion">
-              <choose>
-                <when test="criterion.noValue">
-                  and ${criterion.condition}
-                </when>
-                <when test="criterion.singleValue">
-                  and ${criterion.condition} #{criterion.value}
-                </when>
-                <when test="criterion.betweenValue">
-                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
-                </when>
-                <when test="criterion.listValue">
-                  and ${criterion.condition}
-                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
-                    #{listItem}
-                  </foreach>
-                </when>
-              </choose>
-            </foreach>
-          </trim>
-        </if>
-      </foreach>
-    </where>
-  </sql>
-  <sql id="Update_By_Example_Where_Clause">
-    <where>
-      <foreach collection="example.oredCriteria" item="criteria" separator="or">
-        <if test="criteria.valid">
-          <trim prefix="(" prefixOverrides="and" suffix=")">
-            <foreach collection="criteria.criteria" item="criterion">
-              <choose>
-                <when test="criterion.noValue">
-                  and ${criterion.condition}
-                </when>
-                <when test="criterion.singleValue">
-                  and ${criterion.condition} #{criterion.value}
-                </when>
-                <when test="criterion.betweenValue">
-                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
-                </when>
-                <when test="criterion.listValue">
-                  and ${criterion.condition}
-                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
-                    #{listItem}
-                  </foreach>
-                </when>
-              </choose>
-            </foreach>
-          </trim>
-        </if>
-      </foreach>
-    </where>
-  </sql>
-  <sql id="Base_Column_List">
-    mac, value, time, version
-  </sql>
-  <select id="selectByExample" parameterType="com.moral.entity.HistoryExample" resultMap="BaseResultMap">
-    select
-    <if test="distinct">
-      distinct
-    </if>
-    <include refid="Base_Column_List" />
-    from history
-    <if test="_parameter != null">
-      <include refid="Example_Where_Clause" />
-    </if>
-    <if test="orderByClause != null">
-      order by ${orderByClause}
-    </if>
-  </select>
-  <delete id="deleteByExample" parameterType="com.moral.entity.HistoryExample">
-    delete from history
-    <if test="_parameter != null">
-      <include refid="Example_Where_Clause" />
-    </if>
-  </delete>
-  <insert id="insert" parameterType="com.moral.entity.History">
-    insert into history (mac, value, time, 
-      version)
-    values (#{mac,jdbcType=VARCHAR}, #{value,jdbcType=OTHER}, #{time,jdbcType=TIMESTAMP}, 
-      #{version,jdbcType=INTEGER})
-  </insert>
-  <insert id="insertSelective" parameterType="com.moral.entity.History">
-    insert into history
-    <trim prefix="(" suffix=")" suffixOverrides=",">
-      <if test="mac != null">
-        mac,
-      </if>
-      <if test="value != null">
-        value,
-      </if>
-      <if test="time != null">
-        time,
-      </if>
-      <if test="version != null">
-        version,
-      </if>
-    </trim>
-    <trim prefix="values (" suffix=")" suffixOverrides=",">
-      <if test="mac != null">
-        #{mac,jdbcType=VARCHAR},
-      </if>
-      <if test="value != null">
-        #{value,jdbcType=OTHER},
-      </if>
-      <if test="time != null">
-        #{time,jdbcType=TIMESTAMP},
-      </if>
-      <if test="version != null">
-        #{version,jdbcType=INTEGER},
-      </if>
-    </trim>
-  </insert>
-  <select id="countByExample" parameterType="com.moral.entity.HistoryExample" resultType="java.lang.Integer">
-    select count(*) from history
-    <if test="_parameter != null">
-      <include refid="Example_Where_Clause" />
-    </if>
-  </select>
-  <update id="updateByExampleSelective" parameterType="map">
-    update history
-    <set>
-      <if test="record.mac != null">
-        mac = #{record.mac,jdbcType=VARCHAR},
-      </if>
-      <if test="record.value != null">
-        value = #{record.value,jdbcType=OTHER},
-      </if>
-      <if test="record.time != null">
-        time = #{record.time,jdbcType=TIMESTAMP},
-      </if>
-      <if test="record.version != null">
-        version = #{record.version,jdbcType=INTEGER},
-      </if>
-    </set>
-    <if test="_parameter != null">
-      <include refid="Update_By_Example_Where_Clause" />
-    </if>
-  </update>
-  <update id="updateByExample" parameterType="map">
-    update history
-    set mac = #{record.mac,jdbcType=VARCHAR},
-      value = #{record.value,jdbcType=OTHER},
-      time = #{record.time,jdbcType=TIMESTAMP},
-      version = #{record.version,jdbcType=INTEGER}
-    <if test="_parameter != null">
-      <include refid="Update_By_Example_Where_Clause" />
-    </if>
-  </update>
-  <resultMap id="BaseResultMap" type="com.moral.entity.History">
-    <result column="device_mac" jdbcType="VARCHAR" property="deviceMac" />
-    <result column="value" jdbcType="OTHER" property="value" />
-    <result column="time" jdbcType="TIMESTAMP" property="time" />
-    <result column="version" jdbcType="VARCHAR" property="version" />
-  </resultMap>
-  <sql id="Example_Where_Clause">
-    <where>
-      <foreach collection="oredCriteria" item="criteria" separator="or">
-        <if test="criteria.valid">
-          <trim prefix="(" prefixOverrides="and" suffix=")">
-            <foreach collection="criteria.criteria" item="criterion">
-              <choose>
-                <when test="criterion.noValue">
-                  and ${criterion.condition}
-                </when>
-                <when test="criterion.singleValue">
-                  and ${criterion.condition} #{criterion.value}
-                </when>
-                <when test="criterion.betweenValue">
-                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
-                </when>
-                <when test="criterion.listValue">
-                  and ${criterion.condition}
-                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
-                    #{listItem}
-                  </foreach>
-                </when>
-              </choose>
-            </foreach>
-          </trim>
-        </if>
-      </foreach>
-    </where>
-  </sql>
-  <sql id="Update_By_Example_Where_Clause">
-    <where>
-      <foreach collection="example.oredCriteria" item="criteria" separator="or">
-        <if test="criteria.valid">
-          <trim prefix="(" prefixOverrides="and" suffix=")">
-            <foreach collection="criteria.criteria" item="criterion">
-              <choose>
-                <when test="criterion.noValue">
-                  and ${criterion.condition}
-                </when>
-                <when test="criterion.singleValue">
-                  and ${criterion.condition} #{criterion.value}
-                </when>
-                <when test="criterion.betweenValue">
-                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
-                </when>
-                <when test="criterion.listValue">
-                  and ${criterion.condition}
-                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
-                    #{listItem}
-                  </foreach>
-                </when>
-              </choose>
-            </foreach>
-          </trim>
-        </if>
-      </foreach>
-    </where>
-  </sql>
-  <sql id="Base_Column_List">
-    device_mac, value, time, version
-  </sql>
-  <select id="selectByExample" parameterType="com.moral.entity.HistoryExample" resultMap="BaseResultMap">
-    select
-    <if test="distinct">
-      distinct
-    </if>
-    <include refid="Base_Column_List" />
-    from history
-    <if test="_parameter != null">
-      <include refid="Example_Where_Clause" />
-    </if>
-    <if test="orderByClause != null">
-      order by ${orderByClause}
-    </if>
-  </select>
-  <delete id="deleteByExample" parameterType="com.moral.entity.HistoryExample">
-    delete from history
-    <if test="_parameter != null">
-      <include refid="Example_Where_Clause" />
-    </if>
-  </delete>
-  <insert id="insert" parameterType="com.moral.entity.History">
-    insert into history (device_mac, value, time, 
-      version)
-    values (#{deviceMac,jdbcType=VARCHAR}, #{value,jdbcType=OTHER}, #{time,jdbcType=TIMESTAMP}, 
-      #{version,jdbcType=VARCHAR})
-  </insert>
-  <insert id="insertSelective" parameterType="com.moral.entity.History">
-    insert into history
-    <trim prefix="(" suffix=")" suffixOverrides=",">
-      <if test="deviceMac != null">
-        device_mac,
-      </if>
-      <if test="value != null">
-        value,
-      </if>
-      <if test="time != null">
-        time,
-      </if>
-      <if test="version != null">
-        version,
-      </if>
-    </trim>
-    <trim prefix="values (" suffix=")" suffixOverrides=",">
-      <if test="deviceMac != null">
-        #{deviceMac,jdbcType=VARCHAR},
-      </if>
-      <if test="value != null">
-        #{value,jdbcType=OTHER},
-      </if>
-      <if test="time != null">
-        #{time,jdbcType=TIMESTAMP},
-      </if>
-      <if test="version != null">
-        #{version,jdbcType=VARCHAR},
-      </if>
-    </trim>
-  </insert>
-  <select id="countByExample" parameterType="com.moral.entity.HistoryExample" resultType="java.lang.Integer">
-    select count(*) from history
-    <if test="_parameter != null">
-      <include refid="Example_Where_Clause" />
-    </if>
-  </select>
-  <update id="updateByExampleSelective" parameterType="map">
-    update history
-    <set>
-      <if test="record.deviceMac != null">
-        device_mac = #{record.deviceMac,jdbcType=VARCHAR},
-      </if>
-      <if test="record.value != null">
-        value = #{record.value,jdbcType=OTHER},
-      </if>
-      <if test="record.time != null">
-        time = #{record.time,jdbcType=TIMESTAMP},
-      </if>
-      <if test="record.version != null">
-        version = #{record.version,jdbcType=VARCHAR},
-      </if>
-    </set>
-    <if test="_parameter != null">
-      <include refid="Update_By_Example_Where_Clause" />
-    </if>
-  </update>
-  <update id="updateByExample" parameterType="map">
-    update history
-    set device_mac = #{record.deviceMac,jdbcType=VARCHAR},
-      value = #{record.value,jdbcType=OTHER},
-      time = #{record.time,jdbcType=TIMESTAMP},
-      version = #{record.version,jdbcType=VARCHAR}
-    <if test="_parameter != null">
-      <include refid="Update_By_Example_Where_Clause" />
-    </if>
-  </update>
-
-  <select id="getDayAQIBySensor" resultType="map">
-		SELECT
-			AVG(value -&gt; '$.e1') e1,
-			AVG(value -&gt; '$.e2') e2,
-			AVG(value -&gt; '$.e10') e10,
-			AVG(value -&gt; '$.e11') e11,
-			AVG(value -&gt; '$.e15') e15,
-			AVG(value -&gt; '$.e16') e16
-		FROM
-			history
-		WHERE
-			device_mac = #{mac}
-		AND time &gt; #{start}
-		AND time &lt; #{end}
-  </select>
-
-	<select id="getAverageByAll" resultType="map">
-		SELECT
-			<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
-		WHERE
-			m.areacode = #{areaCode}
-		AND m.id = epoint
-		AND e.mac = h.mac
-		AND h.time &gt; #{start}
-		AND h.time &lt; #{end}
-		<if test="orgIds != null and orgIds.size &gt; 0">
-			<!-- JOIN org_equ oe ON e.id = oe.equid -->
-			AND e.owner_id IN
-			<foreach close=")" collection="orgIds" item="listItem" open="(" separator=",">
-				#{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>
+<mapper namespace="com.moral.mapper.HistoryMapper">
+	<select id="selectValueByMacAndTime" resultType="java.lang.String">
+		select `value` from history
+		where  time = #{time} and mac = #{mac}
+		limit 0,1
+	</select>
+
+	<select id="getAreaAllDataByAccount" resultType="java.util.LinkedHashMap">
+		SELECT
+		<if test="macKey != null">
+			d.`name`,
+		</if>
+		<foreach collection="sensorKeys" separator="," item="sensorKey">
+			AVG(h.`value` ->'$.${sensorKey}[0]') AS '${sensorKey}'
+		</foreach>
+		FROM
+			history h,
+			device d,
+			monitor_point mp
+		WHERE
+			h.mac = d.mac
+			AND d.monitor_point_id = mp.id
+			<if test="areaCode != null">
+				AND mp.area_code = #{areaCode}
+			</if>
+			<if test="start != null">
+				AND h.time >= #{start}
+			</if>
+			<if test="end != null">
+				AND h.time &lt; #{end}
+			</if>
+			<if test="monitorPointId != null">
+				AND mp.id = #{monitorPointId}
+			</if>
+			<if test="orgIds != null and orgIds.size &gt; 0">
+				AND mp.organization_id IN
+				<foreach collection="orgIds" open="(" separator="," close=")"
+					item="listItem">
+					#{listItem}
+				</foreach>
+			</if>
+		<if test="macKey != null">
+			GROUP BY d.id
+			ORDER BY ${macKey} desc
+		</if>
+	</select>
+
+	<select id="getRegionRankingData" resultType="java.util.Map">
+		SELECT
+			AVG( h.${column} -> '$.${sensorKey}[0]' ) avg ,
+			<if test="dimension == 'profession'">
+				p.`name`
+			</if>
+			<if test="dimension == 'monitorPoint'">
+				mp.`name`
+			</if>
+			<if test="dimension == 'region'">
+				r.${nextLevel}_name AS 'name'
+			</if>
+		FROM
+			<if test="dimension == 'profession'">
+				profession p , 
+			</if>
+			<if test="dimension == 'region'">
+				${nextLevel} r,
+			</if>
+			device d,
+			${table} h,
+			monitor_point mp
+		WHERE
+			d.mac = h.mac 
+			AND d.is_delete = '0'
+			AND mp.is_delete = '0'
+			AND d.monitor_point_id = mp.id 
+			AND h.time >= #{start}
+			AND mp.${regionType}_code = #{regionCode}
+			<if test="dimension == 'profession'">
+				AND d.profession_id = p.id 
+			</if>
+			<if test="dimension == 'region'">
+				AND r.${nextLevel}_code = mp.${nextLevel}_code
+			</if>
+			<if test="orgIds != null and orgIds.size &gt; 0">
+				AND mp.organization_id IN
+				<foreach  collection="orgIds" open="(" separator="," close=")" item="listItem">
+					#{listItem}
+				</foreach>
+			</if>
+		GROUP BY
+			<if test="dimension == 'profession'">
+				p.id
+			</if>
+			<if test="dimension == 'monitorPoint'">
+				mp.id
+			</if>
+			<if test="dimension == 'region'">
+				mp.${nextLevel}_code
+			</if>
+		ORDER BY
+			avg DESC
+		<if test="dimension == 'monitorPoint'">
+		LIMIT 0,50
+		</if>
+	</select>
+	
+	<select id="selectNextLevelRegion" resultType="string">
+		SELECT
+			${nextLevel}_name AS 'name' 
+		FROM
+			${nextLevel}
+		WHERE
+			${regionType}_code = #{regionCode}
+	</select>
+
+	<select id="getDischargeByRegion" resultType="java.util.Map">
+		SELECT
+			mp.`name`,
+			dis.`value` -> '$.${sensorKey}' 'value'
+		FROM
+			monitor_point mp,
+			discharge dis 
+		WHERE
+			dis.monitor_point_id = mp.id 
+			AND mp.${regionType}_code = #{regionCode}
+			AND dis.`year` = #{year}
+	</select>
+	
+	<select id="getValueByMacAndSize" resultType="java.util.Map">
+		SELECT 
+			value,
+			time
+		FROM
+			history 
+		WHERE
+			mac = #{mac} 
+		ORDER BY
+			time DESC 
+		LIMIT #{size}
+	</select>
+	
+	<select id="getActualDataByRegion" resultType="java.util.Map">
+		SELECT
+		<foreach collection="sensorKeys" separator="," item="sensorKey">
+			MAX( `value` ->> '$.${sensorKey}' ) * 1 AS '${sensorKey}' 
+		</foreach>
+		FROM
+			history h ,
+			device d,
+			monitor_point mp
+		<where>
+			h.mac = d.mac 
+			AND d.monitor_point_id = mp.id 
+		<if test="orgIds != null and orgIds.size &gt; 0">
+			AND mp.organization_id IN
+			<foreach  collection="orgIds" open="(" separator="," close=")" item="listItem">
+				#{listItem}
+			</foreach>
+		</if>
+			AND h.time >= #{start}			
+			AND h.time &lt; #{end}	
+		</where>
+	</select>
 </mapper>
\ No newline at end of file

--
Gitblit v1.8.0