lizijie
2019-07-25 30d1267479ad9cb076cb2692befc6fe3a53fa7d9
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
<?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">
    <select id="getEmissionsData" resultType="java.util.Map">
        SELECT
            SUM(hd.`json` ->'$.${sensorKey}[0]') AS '${sensorKey}',
            vdi.mac,
            mp.`name` 'name' 
        FROM
            history_daily hd,
            view_device_info vdi,
            monitor_point mp
        <where>
            hd.mac = vdi.mac
            AND vdi.monitor_point_id = mp.id
            AND vdi.device_tech = 1 
            <if test="start != null">
                AND hd.time >= #{start}
            </if>
            <if test="end != null">
                AND hd.time &lt; #{end}
            </if>
            <if test="regionCode != null">
                AND mp.${regionType}_code = #{regionCode}
            </if>
            <if test="orgIds != null and orgIds.size &gt; 0">
                AND mp.organization_id IN
                <foreach collection="orgIds" open="(" separator="," close=")"
                    item="listItem">
                    #{listItem}
                </foreach>
            </if>
        </where>
        GROUP BY mp.`name`, vdi.mac
    </select>
 
 
    <select id="getOverproofData" resultType="java.util.Map">
        SELECT
            AVG( hd.`json` -> '$.${sensorKey}[0]' ) avg,
            ( SELECT DISTINCT limit_val ->> '$.${sensorKey}' FROM view_device_density WHERE monitor_point_id = vdd.monitor_point_id AND device_tech = vdd.device_tech ) `limit`,
            ( SELECT mp.`name` FROM monitor_point mp WHERE mp.id = vdd.monitor_point_id ) `name`,
            vdd.device_tech 
        FROM    
            history_daily hd,
            view_device_density vdd,
            (
                SELECT
                    vdd.monitor_point_id 
                FROM
                    history_daily hd,
                    device vdd,
                    monitor_point mp 
                <where>
                    hd.mac = vdd.mac 
                    AND mp.id = vdd.monitor_point_id 
                    <if test="start != null">
                        AND hd.time >= #{start}
                    </if>
                    <if test="end != null">
                        AND hd.time &lt; #{end}
                    </if>
                    <if test="regionCode != null">
                        AND mp.${regionType}_code = #{regionCode}
                    </if>
                    <if test="orgIds != null and orgIds.size &gt; 0">
                        AND mp.organization_id IN
                        <foreach collection="orgIds" open="(" separator="," close=")"
                            item="listItem">
                            #{listItem}
                        </foreach>
                    </if>
                </where>
                GROUP BY
                    vdd.mac,
                    vdd.monitor_point_id 
                HAVING
                    AVG( hd.`json` -> '$.${sensorKey}[0]' ) >= ( SELECT limit_val ->> '$.${sensorKey}' FROM view_device_density WHERE mac = vdd.mac ) * (1 + #{overproofRatio} * 1) 
                    <if test="overproofRatio != 0 and overproofRatio != 3">
                    AND AVG( hd.`json` -> '$.${sensorKey}[0]' ) &lt; ( SELECT limit_val ->> '$.${sensorKey}' FROM view_device_density WHERE mac = vdd.mac ) * (2 + #{overproofRatio} * 1) 
                    </if>
            ) ta 
        <where>
            hd.mac = vdd.mac 
            AND vdd.monitor_point_id = ta.monitor_point_id 
            <if test="start != null">
                AND hd.time >= #{start}
            </if>
            <if test="end != null">
                AND hd.time &lt; #{end}
            </if>
        </where>
        GROUP BY
            vdd.monitor_point_id,
            vdd.device_tech
        </select>
        
    <!-- 根据设备mac地址,时间,污染因子查询一天的平均值 -->
    <select id="getTraceabilityData" resultType="java.util.Map">
        SELECT
            json ->'$.${sensorKey}[0]' AS '${sensorKey}'
        FROM
            history_daily
        <where>
            <if test="mac!=null and mac!=''">
                AND mac=#{mac}
            </if>
            <if test="time!=null and time!=''">
                AND time=#{time}
            </if>
        </where>
    </select>
</mapper>