xufenglei
2020-03-26 ac4de7623b87a6826d3078f35cf3da13a43af3cf
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
<?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="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>
 
</mapper>