jinpengyong
2020-10-09 c60311f48e7ec07c56a4df31536f795ba62751ce
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
<?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.HistoryMinutelyMapper">
 
    <insert id="insertHistoryMinutely">
        insert into
        history_minutely_${yearAndMonth}
        values
        <foreach collection="list" item="map" separator=",">
            (#{map.mac},#{map.time},#{map.json})
        </foreach>
    </insert>
    <update id="createHistoryMinutelyTable">
    CREATE TABLE  IF NOT EXISTS `history_minutely_${yearAndMonth}` (
        `mac` varchar(20) CHARACTER SET latin1 DEFAULT NULL,
        `time` datetime DEFAULT NULL,
        `json` json DEFAULT NULL,
        KEY `_idx_mac` (`mac`) USING BTREE,
        KEY `_idx_time` (`time`) USING BTREE,
        KEY `_idx_mac_time` (`mac`,`time`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    </update>
 
    <select id="getMinutelySensorData" 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_minutely_${yearAndMonth} 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>