jinpengyong
2020-09-08 adde1ed1a99622c80a95dea83e294711d295c55c
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
<?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">
    <select id="getPollutionSourceData" resultType="java.util.Map">
        SELECT
        <foreach collection="sensorKeys" separator="," item="sensorKey">
            json->'$.${sensorKey}[0]' AS '${sensorKey}'
        </foreach>
        FROM
        history_hourly
        <where>
            <if test="mac!=null">
                AND mac = #{mac}
            </if>
            <if test="time!=null">
                AND time = DATE_SUB(#{time}, INTERVAL 1 HOUR)
            </if>
        </where>
    </select>
 
    <select id="getPollutionSourceDataByHour" resultType="java.util.Map">
        SELECT
        DATE_FORMAT(time,'%Y-%m-%d %H') time,
        <foreach collection="sensorKeys" separator="," item="sensorKey">
            AVG(h.value ->'$.${sensorKey}') AS '${sensorKey}'
        </foreach>
        FROM
        <if test="mac!=null and time!=null">
            (select mac,value,time,version from history
            where mac = #{mac} and time BETWEEN DATE_SUB(#{time}, INTERVAL 1 HOUR) and #{time}) h
        </if>
        GROUP BY
        DATE_FORMAT(time,'%Y-%m-%d %H')
        <if test="time!=null">
            having time = DATE_FORMAT(DATE_SUB(#{time}, INTERVAL 1 HOUR),'%Y-%m-%d %H')
        </if>
    </select>
 
    <select id="getPollutionSourceDataAll" resultType="java.util.Map">
        SELECT
        DATE_FORMAT(time,'%Y-%m-%d %H') time,
        <foreach collection="sensorKeys" separator="," item="sensorKey">
            AVG(h.value ->'$.${sensorKey}') AS '${sensorKey}'
        </foreach>
        FROM
        <if test="mac!=null">
            (select mac,value,time,version from history
            where mac = #{mac} and time BETWEEN DATE_SUB(#{time}, INTERVAL 1 DAY) and #{time}) h
        </if>
        GROUP BY
        DATE_FORMAT(time,'%Y-%m-%d %H')
        order by time desc
        limit 0,1
    </select>
 
    <select id="getTVOCByMac" resultType="java.lang.String">
        select json->'$.${sensor}[0]' as e17 from history_hourly where mac=#{mac} and time=#{time}
    </select>
 
    <select id="getPressureByMac" resultType="java.lang.String">
        select json->'$.e28[0]' as e28 from history_hourly where mac=#{mac} and time=#{time}
    </select>
 
    <select id="getDataByMac" resultType="java.util.Map">
        select json  from history_hourly where mac=#{mac} and time=#{time}
    </select>
 
    <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.mac in
        <foreach  item="item" collection="macs" index="index"  open="(" separator="," close=")">#{item}</foreach>
        and h.time >= #{start}
        AND h.time <![CDATA[<]]> #{end}
        and h.json->'$.e12[0]' is not null
        group by DATE_FORMAT(h.time,'%Y-%m-%d %H:%i:%s')
    </select>
 
    <!-- 根据设备mac地址,时间,污染因子查询一天的平均值 -->
    <select id="getDataByTimeSlot" resultType="java.util.Map">
        SELECT *
        FROM history_hourly
        where mac=#{mac}
        AND time between #{startTime} and #{endTime}
    </select>
 
    <select id="getTVOCByMacs" resultType="java.util.Map">
        SELECT
        DATE_FORMAT(h.time,'%Y-%m-%d %H:%i:%s') time,
        avg(h.json->'$.e17[0]') 'TVOC'
        FROM history_hourly h
        WHERE h.mac in
        <foreach  item="item" collection="macs" index="index"  open="(" separator="," close=")">#{item}</foreach>
        and h.time >= #{start}
        AND h.time <![CDATA[<]]> #{end}
        and h.json->'$.e17[0]' is not null
        group by h.time
    </select>
</mapper>