From ed8c07e1870d8cd2151355ea34019a3f02087568 Mon Sep 17 00:00:00 2001
From: lizijie <lzjiiie@163.com>
Date: Thu, 26 May 2022 16:05:10 +0800
Subject: [PATCH] 常熟中隆汽车设备转发,千灯设备数据转发

---
 src/main/resources/mapper/HistoryMapper.xml |  296 ++++++++++++++++++++++++++++++++++++++++------------------
 1 files changed, 204 insertions(+), 92 deletions(-)

diff --git a/src/main/resources/mapper/HistoryMapper.xml b/src/main/resources/mapper/HistoryMapper.xml
index 823b530..aa4967d 100644
--- a/src/main/resources/mapper/HistoryMapper.xml
+++ b/src/main/resources/mapper/HistoryMapper.xml
@@ -1,97 +1,113 @@
 <?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>
-  <insert id="insert" parameterType="com.moral.entity.History">
+    <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>
+    <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="getAvgData" resultType="java.util.Map">
-		SELECT
-			<foreach  collection="sensorKeys" separator="," item="sensorKey">
-				AVG(value->'$.${sensorKey}') AS '${sensorKey}'
-			</foreach>
-		FROM
-			history h
-		WHERE
-			h.time >= #{start}
-		AND h.time <![CDATA[<]]> #{end}
-		AND	h.mac = #{mac}
-	</select>
-	<select id="getMaxAndMinData" resultType="java.util.Map">
-		SELECT
-			<foreach  collection="sensorKeys" separator="," item="sensorKey">
-				max(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'max${sensorKey}',
-            	MIN(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'min${sensorKey}'
-			</foreach>
-		FROM
-			history h
-		WHERE
-			h.time >= #{start}
-		AND h.time <![CDATA[<]]> #{end}
-		AND	h.mac = #{mac}
-	</select>
-	<select id="getMinData" resultType="java.util.Map">
-		SELECT
-			<foreach  collection="sensorKeys" separator="," item="sensorKey">
-				MIN(value->'$.${sensorKey}') AS '${sensorKey}'
-			</foreach>
-		FROM
-			history h
-		WHERE
-			h.time >= #{start}
-		AND h.time <![CDATA[<]]> #{end}
-		AND	h.mac = #{mac}
-	</select>
+    <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="getAvgData" resultType="java.util.Map">
+        SELECT
+        <foreach collection="sensorKeys" separator="," item="sensorKey">
+            AVG(value->'$.${sensorKey}') AS '${sensorKey}'
+        </foreach>
+        FROM
+        history h
+        WHERE
+        h.time >= #{start}
+        AND h.time <![CDATA[<]]> #{end}
+        AND h.mac = #{mac}
+    </select>
+    <select id="getMaxAndMinData" resultType="java.util.Map">
+        SELECT
+        <foreach collection="sensorKeys" separator="," item="sensorKey">
+            max(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'max${sensorKey}',
+            MIN(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'min${sensorKey}'
+        </foreach>
+        FROM
+        history h
+        WHERE
+        h.time >= #{start}
+        AND h.time <![CDATA[<]]> #{end}
+        AND h.mac = #{mac}
+    </select>
+    <select id="getMinData" resultType="java.util.Map">
+        SELECT
+        <foreach collection="sensorKeys" separator="," item="sensorKey">
+            MIN(value->'$.${sensorKey}') AS '${sensorKey}'
+        </foreach>
+        FROM
+        history h
+        WHERE
+        h.time >= #{start}
+        AND h.time <![CDATA[<]]> #{end}
+        AND h.mac = #{mac}
+    </select>
 
     <select id="getSensorData" resultType="java.util.LinkedHashMap">
         SELECT
         h.mac,
-        <foreach  collection="sensorKeys" separator="," item="sensorKey">
-            AVG(value->'$.${sensorKey}') AS '${sensorKey}',
-            MAX(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'MAX${sensorKey}',
-            MIN(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'MIN${sensorKey}'
+        <foreach collection="sensorKeys" separator="," item="sensorKey">
+            <choose >
+            <when test="sensorKey=='e23'">
+            ROUND((CASE    WHEN AVG(value->'$.e18'*SIN((value->'$.e23'/180)*PI()))<![CDATA[>]]>0 AND AVG(value->'$.e18'*COS((value->'$.e23'/180)*PI()))<![CDATA[>]]>0
+                THEN ATAN(AVG(value->'$.e18'*SIN((value->'$.e23'/180)*PI()))/AVG(value->'$.e18'*COS((value->'$.e23'/180)*PI())))*180/PI()
+                WHEN AVG(value->'$.e18'*SIN((value->'$.e23'/180)*PI()))>0 AND AVG(value->'$.e18'*COS((value->'$.e23'/180)*PI()))<![CDATA[<]]>0
+                THEN (ATAN(AVG(value->'$.e18'*SIN((value->'$.e23'/180)*PI()))/AVG(value->'$.e18'*COS((value->'$.e23'/180)*PI())))*180/PI())+180
+                WHEN AVG(value->'$.e18'*SIN((value->'$.e23'/180)*PI()))<![CDATA[<]]>0 AND AVG(value->'$.e18'*COS((value->'$.e23'/180)*PI()))<![CDATA[<]]>0
+                THEN (ATAN(AVG(value->'$.e18'*SIN((value->'$.e23'/180)*PI()))/AVG(value->'$.e18'*COS((value->'$.e23'/180)*PI())))*180/PI())+180
+                ELSE (ATAN(AVG(value->'$.e18'*SIN((value->'$.e23'/180)*PI()))/AVG(value->'$.e18'*COS((value->'$.e23'/180)*PI())))*180/PI())+360
+                END),3) AS '${sensorKey}',
+                MAX(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'MAX${sensorKey}',
+                MIN(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'MIN${sensorKey}'
+            </when>
+            <otherwise>
+                AVG(value->'$.${sensorKey}') AS '${sensorKey}',
+                MAX(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'MAX${sensorKey}',
+                MIN(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'MIN${sensorKey}'
+            </otherwise>
+            </choose>
         </foreach>
         FROM
-        history h
+        history_${yearAndMonthDay} h
         WHERE
         h.time >= #{start}
         AND h.time <![CDATA[<]]> #{end}
@@ -102,10 +118,26 @@
     <select id="getSensorDataByMac" resultType="java.util.LinkedHashMap">
         SELECT
         h.mac,
-        <foreach  collection="sensorKeys" separator="," item="sensorKey">
-            AVG(value->'$.${sensorKey}') AS '${sensorKey}',
-            MAX(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'MAX${sensorKey}',
-            MIN(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'MIN${sensorKey}'
+        <foreach collection="sensorKeys" separator="," item="sensorKey">
+            <choose >
+                <when test="sensorKey=='e23'">
+                    ROUND((CASE    WHEN AVG(value->'$.e18'*SIN((value->'$.e23'/180)*PI()))<![CDATA[>]]>0 AND AVG(value->'$.e18'*COS((value->'$.e23'/180)*PI()))<![CDATA[>]]>0
+                    THEN ATAN(AVG(value->'$.e18'*SIN((value->'$.e23'/180)*PI()))/AVG(value->'$.e18'*COS((value->'$.e23'/180)*PI())))*180/PI()
+                    WHEN AVG(value->'$.e18'*SIN((value->'$.e23'/180)*PI()))>0 AND AVG(value->'$.e18'*COS((value->'$.e23'/180)*PI()))<![CDATA[<]]>0
+                    THEN (ATAN(AVG(value->'$.e18'*SIN((value->'$.e23'/180)*PI()))/AVG(value->'$.e18'*COS((value->'$.e23'/180)*PI())))*180/PI())+180
+                    WHEN AVG(value->'$.e18'*SIN((value->'$.e23'/180)*PI()))<![CDATA[<]]>0 AND AVG(value->'$.e18'*COS((value->'$.e23'/180)*PI()))<![CDATA[<]]>0
+                    THEN (ATAN(AVG(value->'$.e18'*SIN((value->'$.e23'/180)*PI()))/AVG(value->'$.e18'*COS((value->'$.e23'/180)*PI())))*180/PI())+180
+                    ELSE (ATAN(AVG(value->'$.e18'*SIN((value->'$.e23'/180)*PI()))/AVG(value->'$.e18'*COS((value->'$.e23'/180)*PI())))*180/PI())+360
+                    END),3) AS '${sensorKey}',
+                    MAX(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'MAX${sensorKey}',
+                    MIN(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'MIN${sensorKey}'
+                </when>
+                <otherwise>
+                    AVG(value->'$.${sensorKey}') AS '${sensorKey}',
+                    MAX(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'MAX${sensorKey}',
+                    MIN(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'MIN${sensorKey}'
+                </otherwise>
+            </choose>
         </foreach>
         FROM
         history h
@@ -123,10 +155,26 @@
     <select id="getSensorDataByMacOnce" resultType="java.util.LinkedHashMap">
         SELECT
         h.mac,DATE_FORMAT(time,'%Y-%m-%dT%H:%i') time,
-        <foreach  collection="sensorKeys" separator="," item="sensorKey">
-            AVG(value->'$.${sensorKey}') AS '${sensorKey}',
-            MAX(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'MAX${sensorKey}',
-            MIN(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'MIN${sensorKey}'
+        <foreach collection="sensorKeys" separator="," item="sensorKey">
+            <choose >
+                <when test="sensorKey=='e23'">
+                    ROUND((CASE    WHEN AVG(value->'$.e18'*SIN((value->'$.e23'/180)*PI()))<![CDATA[>]]>0 AND AVG(value->'$.e18'*COS((value->'$.e23'/180)*PI()))<![CDATA[>]]>0
+                    THEN ATAN(AVG(value->'$.e18'*SIN((value->'$.e23'/180)*PI()))/AVG(value->'$.e18'*COS((value->'$.e23'/180)*PI())))*180/PI()
+                    WHEN AVG(value->'$.e18'*SIN((value->'$.e23'/180)*PI()))>0 AND AVG(value->'$.e18'*COS((value->'$.e23'/180)*PI()))<![CDATA[<]]>0
+                    THEN (ATAN(AVG(value->'$.e18'*SIN((value->'$.e23'/180)*PI()))/AVG(value->'$.e18'*COS((value->'$.e23'/180)*PI())))*180/PI())+180
+                    WHEN AVG(value->'$.e18'*SIN((value->'$.e23'/180)*PI()))<![CDATA[<]]>0 AND AVG(value->'$.e18'*COS((value->'$.e23'/180)*PI()))<![CDATA[<]]>0
+                    THEN (ATAN(AVG(value->'$.e18'*SIN((value->'$.e23'/180)*PI()))/AVG(value->'$.e18'*COS((value->'$.e23'/180)*PI())))*180/PI())+180
+                    ELSE (ATAN(AVG(value->'$.e18'*SIN((value->'$.e23'/180)*PI()))/AVG(value->'$.e18'*COS((value->'$.e23'/180)*PI())))*180/PI())+360
+                    END),3) AS '${sensorKey}',
+                    MAX(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'MAX${sensorKey}',
+                    MIN(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'MIN${sensorKey}'
+                </when>
+                <otherwise>
+                    AVG(value->'$.${sensorKey}') AS '${sensorKey}',
+                    MAX(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'MAX${sensorKey}',
+                    MIN(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'MIN${sensorKey}'
+                </otherwise>
+            </choose>
         </foreach>
         FROM
         history h
@@ -144,10 +192,26 @@
     <select id="getSensorDataOnce" resultType="java.util.LinkedHashMap">
         SELECT
         h.mac,DATE_FORMAT(time,'%Y-%m-%dT%H:%i') time,
-        <foreach  collection="sensorKeys" separator="," item="sensorKey">
-            AVG(value->'$.${sensorKey}') AS '${sensorKey}',
-            MAX(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'MAX${sensorKey}',
-            MIN(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'MIN${sensorKey}'
+        <foreach collection="sensorKeys" separator="," item="sensorKey">
+            <choose >
+                <when test="sensorKey=='e23'">
+                    ROUND((CASE    WHEN AVG(value->'$.e18'*SIN((value->'$.e23'/180)*PI()))<![CDATA[>]]>0 AND AVG(value->'$.e18'*COS((value->'$.e23'/180)*PI()))<![CDATA[>]]>0
+                    THEN ATAN(AVG(value->'$.e18'*SIN((value->'$.e23'/180)*PI()))/AVG(value->'$.e18'*COS((value->'$.e23'/180)*PI())))*180/PI()
+                    WHEN AVG(value->'$.e18'*SIN((value->'$.e23'/180)*PI()))>0 AND AVG(value->'$.e18'*COS((value->'$.e23'/180)*PI()))<![CDATA[<]]>0
+                    THEN (ATAN(AVG(value->'$.e18'*SIN((value->'$.e23'/180)*PI()))/AVG(value->'$.e18'*COS((value->'$.e23'/180)*PI())))*180/PI())+180
+                    WHEN AVG(value->'$.e18'*SIN((value->'$.e23'/180)*PI()))<![CDATA[<]]>0 AND AVG(value->'$.e18'*COS((value->'$.e23'/180)*PI()))<![CDATA[<]]>0
+                    THEN (ATAN(AVG(value->'$.e18'*SIN((value->'$.e23'/180)*PI()))/AVG(value->'$.e18'*COS((value->'$.e23'/180)*PI())))*180/PI())+180
+                    ELSE (ATAN(AVG(value->'$.e18'*SIN((value->'$.e23'/180)*PI()))/AVG(value->'$.e18'*COS((value->'$.e23'/180)*PI())))*180/PI())+360
+                    END),3) AS '${sensorKey}',
+                    MAX(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'MAX${sensorKey}',
+                    MIN(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'MIN${sensorKey}'
+                </when>
+                <otherwise>
+                    AVG(value->'$.${sensorKey}') AS '${sensorKey}',
+                    MAX(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'MAX${sensorKey}',
+                    MIN(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'MIN${sensorKey}'
+                </otherwise>
+            </choose>
         </foreach>
         FROM
         history h
@@ -158,4 +222,52 @@
         h.mac,DATE_FORMAT(time,'%Y-%m-%dT%H:%i')
     </select>
 
+    <select id="selectByMacAndTime" resultType="com.moral.entity.History">
+        SELECT *
+        from history
+        WHERE time = #{time} and mac in
+        <foreach collection="macList" index="index" item="mac" open="(" separator="," close=")">
+            #{mac}
+        </foreach>
+    </select>
+
+    <insert id="insertHistorySpecialTable">
+        insert into
+        history_special(mac, value, time,version)
+        SELECT * from history WHERE time>=#{startTime} and time<![CDATA[<]]>#{endTime} and mac in
+        <foreach collection="macList" index="index" item="mac" open="(" separator="," close=")">
+            #{mac}
+        </foreach>
+    </insert>
+
+    <delete id="deleteHistoryData" parameterType="java.lang.String">
+        delete from history where  time &lt; #{oldTime};
+    </delete>
+
+    <update id="deletePartition">
+        ALTER table history drop PARTITION ${p};
+    </update>
+
+    <update id="createHistoryTable" parameterType="String">
+        CREATE TABLE history_${yearMonthDay} (
+        `mac` varchar(30) DEFAULT NULL,
+        `value` json DEFAULT NULL,
+        `time` datetime DEFAULT NULL,
+        `version` int(11) DEFAULT NULL,
+        KEY `_idx_mac_time` (`mac`,`time`) USING BTREE,
+        KEY `_idx_mac` (`mac`) USING BTREE,
+        KEY `_idx_time` (`time`) USING BTREE
+        ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC
+    </update>
+
+    <delete id="dropHistoryTable" parameterType="String">
+        drop table history_${yearMonthDay}
+    </delete>
+
+    <select id="selectLastDataByMac" resultType="com.moral.entity.History">
+        select * from history_${yearMonthDay}
+        where mac = #{mac}
+        order by time desc
+        limit 1
+    </select>
 </mapper>
\ No newline at end of file

--
Gitblit v1.8.0