jinpengyong
2020-11-05 88b14b3c7446f63a66dca1b3d4a5cff892739487
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?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">
    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>
 
    <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>
        FROM
        history h
        WHERE
        h.time >= #{start}
        AND h.time <![CDATA[<]]> #{end}
        group by
        h.mac
    </select>
 
    <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>
        FROM
        history h
        WHERE
        h.time >= #{start}
        AND h.time <![CDATA[<]]> #{end}
        and h.mac in
        <foreach collection="macList" index="index" item="mac" open="(" separator="," close=")">
            #{mac}
        </foreach>
        group by
        h.mac
    </select>
 
    <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>
        FROM
        history h
        WHERE
        h.time >= #{start}
        AND h.time <![CDATA[<]]> #{end}
        and h.mac in
        <foreach collection="macList" index="index" item="mac" open="(" separator="," close=")">
            #{mac}
        </foreach>
        group by
        h.mac,DATE_FORMAT(time,'%Y-%m-%dT%H:%i')
    </select>
 
    <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>
        FROM
        history h
        WHERE
        h.time >= #{start}
        AND h.time <![CDATA[<]]> #{end}
        group by
        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>
</mapper>