jinpengyong
2022-01-06 45ece9986a39a01081fab9b224966e1f297d383d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?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.HistoryFiveMinutelyMapper">
 
    <select id="getFiveMinutesSensorData" resultType="java.util.LinkedHashMap">
        select
        h.mac
        <foreach collection="sensorKeys" open="," separator="," item="sensorKey">
            <choose >
              <when test="sensorKey=='e23'">
                  ROUND((CASE    WHEN AVG(json->'$.e18[0]'*SIN((json->'$.e23[0]'/180)*PI()))<![CDATA[>]]>0 AND AVG(json->'$.e18[0]'*COS((json->'$.e23[0]'/180)*PI()))<![CDATA[>]]>0
                  THEN ATAN(AVG(json->'$.e18[0]'*SIN((json->'$.e23[0]'/180)*PI()))/AVG(json->'$.e18[0]'*COS((json->'$.e23[0]'/180)*PI())))*180/PI()
                  WHEN AVG(json->'$.e18[0]'*SIN((json->'$.e23[0]'/180)*PI()))>0 AND AVG(json->'$.e18[0]'*COS((json->'$.e23[0]'/180)*PI()))<![CDATA[<]]>0
                  THEN (ATAN(AVG(json->'$.e18[0]'*SIN((json->'$.e23[0]'/180)*PI()))/AVG(json->'$.e18[0]'*COS((json->'$.e23[0]'/180)*PI())))*180/PI())+180
                  WHEN AVG(json->'$.e18[0]'*SIN((json->'$.e23[0]'/180)*PI()))<![CDATA[<]]>0 AND AVG(json->'$.e18[0]'*COS((json->'$.e23[0]'/180)*PI()))<![CDATA[<]]>0
                  THEN (ATAN(AVG(json->'$.e18[0]'*SIN((json->'$.e23[0]'/180)*PI()))/AVG(json->'$.e18[0]'*COS((json->'$.e23[0]'/180)*PI())))*180/PI())+180
                  ELSE (ATAN(AVG(json->'$.e18[0]'*SIN((json->'$.e23[0]'/180)*PI()))/AVG(json->'$.e18[0]'*COS((json->'$.e23[0]'/180)*PI())))*180/PI())+360
                  END),3) AS 'AVG${sensorKey}',
                  ROUND(MIN(json->'$.${sensorKey}[1]'),3) AS 'MIN${sensorKey}',
                  ROUND(MAX(json->'$.${sensorKey}[2]'),3) AS 'MAX${sensorKey}'
              </when>
                <otherwise>
                    ROUND(AVG(json->'$.${sensorKey}[0]'),3) AS 'AVG${sensorKey}',
                    ROUND(MIN(json->'$.${sensorKey}[1]'),3) AS 'MIN${sensorKey}',
                    ROUND(MAX(json->'$.${sensorKey}[2]'),3) AS 'MAX${sensorKey}'
                </otherwise>
            </choose>
        </foreach>
        FROM
        history_minutely_${yearAndMonth} h
        where  h.time<![CDATA[>=]]>#{start} and h.time <![CDATA[<]]>  #{end}
        group by h.mac
    </select>
 
    <insert id="insertHistoryFiveMinutely">
        insert into
        history_five_minutely_${yearAndMonth}
        values
        <foreach collection="list" item="map" separator=",">
            (#{map.mac},#{map.time},#{map.json})
        </foreach>
    </insert>
 
    <update id="createHistoryFiveMinutely" parameterType="String">
        CREATE TABLE  IF NOT EXISTS `history_five_minutely_${yearAndMonth}` (
        `mac` varchar(30) CHARACTER SET latin1 DEFAULT NULL,
        `time` datetime DEFAULT NULL,
        `json` json DEFAULT NULL,
         KEY `idx_time` (`time`),
         KEY `idx_mac` (`mac`)
        ) ENGINE=INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
    </update>
 
</mapper>