xufenglei
2018-04-18 b52c1e137107abbaee8cdea45964def25c952a7f
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
<?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.AlarmMapper">
    <select id="getAlarmData" resultType="java.util.LinkedHashMap">
        SELECT
            <if test="dimension=='monitorPoint'">
                `name`,
            </if>
            <foreach collection="sensorKeys" item="sensorKey" separator=",">
                SUM( ${sensorKey} ) AS '${sensorKey}'
            </foreach>
            <foreach close=" AS 'sum' " collection="sensorKeys" item="sensorKey" open="," separator="+">
                SUM( ${sensorKey} )
            </foreach>
        FROM
            (
                SELECT 
                    <if test="dimension=='monitorPoint'">
                        mp.`name`,
                    </if>
                    <foreach collection="sensorKeys" item="sensorKey" separator=",">
                        CASE COUNT( json -> '$.${sensorKey}' ) WHEN 0 THEN 0 ELSE 1 END AS '${sensorKey}'
                    </foreach>
                FROM
                    alarm h, 
                    device d,
                    monitor_point mp 
                WHERE d.monitor_point_id = mp.id 
                    AND h.mac = d.mac 
                    AND h.time >= #{start}
                    AND h.time <![CDATA[<]]> #{end}
                    <if test="provinceCode != null">
                    AND mp.province_code = #{provinceCode}
                    </if>
                    <if test="cityCode != null">
                    AND mp.city_code = #{cityCode}
                    </if>
                    <if test="areaCode != null">
                    AND mp.area_code = #{areaCode}
                    </if>
                    <if test="monitorPointId != null">
                    AND mp.id =    #{monitorPointId}
                    </if>
                    <if test="mac != null">
                    AND d.mac =    #{mac}
                    </if>
                GROUP BY
                    <if test="dimension=='monitorPoint'">
                        mp.id,
                    </if>
                    DATE_FORMAT( h.time, '%Y-%m-%d' )
            ) a
        <if test="dimension=='monitorPoint'">
            GROUP BY name
        </if>
            
    </select>
    
</mapper>