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
<?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.HistoryDailyMapper">
  <resultMap id="BaseResultMap" type="com.moral.entity.HistoryDaily">
    <result column="mac" jdbcType="VARCHAR" property="mac" />
    <result column="time" jdbcType="TIMESTAMP" property="time" />
    <result column="json" jdbcType="OTHER" property="json" />
  </resultMap>
    <select id="getDailyData" resultType="java.util.Map">
        SELECT
            <foreach  collection="sensorKeys" separator="," item="sensorKey">
                json->'$.${sensorKey}' AS '${sensorKey}'
            </foreach>
        FROM
            history_daily hd
        WHERE
            hd.time >= #{start}
        AND hd.time <![CDATA[<]]> #{end}
        AND    hd.mac = #{mac}
    </select>
 
    <insert id="insertHistoryDaily">
        insert into
        history_daily
        values
        <foreach collection="list" item="map" separator=",">
            (#{map.mac},#{map.time},#{map.json})
        </foreach>
    </insert>
 
    <select id="getDailySensorData" 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_daily 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>
 
</mapper>