lizijie
2021-11-29 d8310f6bc609440fa352c0e5a9436c7c78caa287
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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.HistoryHourlyMapper">
 
    <insert id="insertHistoryHourly">
        insert into
        history_hourly
        values
        <foreach collection="list" item="map" separator=",">
            (#{map.mac},#{map.time},#{map.json})
        </foreach>
    </insert>
 
    <select id="getHourlySensorData" resultType="java.util.LinkedHashMap">
        SELECT
        h.mac,
        <foreach collection="sensorKeys" 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 '${sensorKey}',
                    MIN(json->'$.${sensorKey}[1]') AS 'MIN${sensorKey}',
                    MAX(json->'$.${sensorKey}[2]') AS 'MAX${sensorKey}'
                </when>
                <otherwise>
                    AVG(json->'$.${sensorKey}[0]') AS '${sensorKey}',
                    MIN(json->'$.${sensorKey}[1]') AS 'MIN${sensorKey}',
                    MAX(json->'$.${sensorKey}[2]') AS 'MAX${sensorKey}'
                </otherwise>
            </choose>
        </foreach>
        FROM
        history_hourly h
        WHERE
        h.time >= #{start}
        AND h.time <![CDATA[<]]> #{end}
        AND h.mac in
        <foreach collection="macs" open="(" separator="," close=")"
                 item="mac">
            #{mac}
        </foreach>
        group by
        h.mac
    </select>
 
    <select id="getHourlySensorDataToCalculateO3" resultType="java.util.LinkedHashMap">
        SELECT
        h.mac,
        <foreach collection="sensorKeys" separator="," item="sensorKey">
            AVG(json->'$.${sensorKey}[0]') AS '${sensorKey}',
            MIN(json->'$.${sensorKey}[1]') AS 'MIN${sensorKey}',
            MAX(json->'$.${sensorKey}[2]') AS 'MAX${sensorKey}'
        </foreach>
        FROM
        history_hourly h
        WHERE
        h.time = #{time}
        AND h.mac in
        <foreach collection="macList" open="(" separator="," close=")"
                 item="mac">
            #{mac}
        </foreach>
        group by
        h.mac
    </select>
 
    <insert id="insertHistoryO3Hourly">
        insert into
        history_hourly_o3
        values
        <foreach collection="list" item="map" separator=",">
            (#{map.mac},#{map.time},#{map.json})
        </foreach>
    </insert>
 
    <select id="getBeamByMacs" resultType="java.util.Map">
        SELECT
        DATE_FORMAT(h.time,#{typeFormat}) time,
        avg(h.json->'$.e12[0]') 'beam'
        FROM history_hourly h
        WHERE h.json->'$.e12[0]' is not null
        and h.mac in
        <foreach collection="macs" open="(" separator="," close=")" item="mac">
            #{mac}
        </foreach>
        and h.time in
        <foreach collection="times" open="(" separator="," close=")" item="time">
            #{time}
        </foreach>
        group by h.time
    </select>
 
    <select id="selectCountByMac" resultType="java.lang.Integer">
        SELECT count(1) FROM history_hourly
        WHERE mac = #{mac}
        AND `time` >= #{start}
        AND `time` <![CDATA[<]]> #{end}
    </select>
 
    <select id="selectDataByMac" resultType="java.lang.String">
        select
        json
        from history_hourly
        where mac = #{mac}
        and `time` = #{time}
    </select>
</mapper>