kaiyu
2020-11-24 1d7e1b771160dd8b7f25765e402ce25cac676641
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?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.HangzhouAqiMapper">
    <resultMap id="BaseResultMap" type="com.moral.entity.HangzhouAqi">
        <id column="time" property="time" jdbcType="TIMESTAMP"/>
        <result column="aqi_json" property="aqiJson" jdbcType="OTHER"/>
        <result column="city_code" property="cityCode" jdbcType="VARCHAR"/>
    </resultMap>
    <sql id="Base_Column_List">
    time, aqi_json, city_code
  </sql>
    <!--<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.util.Date" >-->
    <!--select-->
    <!--<include refid="Base_Column_List" />-->
    <!--from hangzhou_aqi-->
    <!--where time = #{time,jdbcType=TIMESTAMP}-->
    <!--</select>-->
    <select id="selectAqisByCodeAndTimePeriod" resultType="map" parameterType="java.util.Date">
      select
        aqi_json->'$.AQI' as aqi,DATE_FORMAT(time,'${@com.moral.common.util.ReportTimeFormat@toMySqlTimeFormat(timePeriod.timeUnits)}') as 'time'
      from hangzhou_aqi
       where
          (
          time >= #{timePeriod.startTime}
          and time <![CDATA[<=]]> #{timePeriod.endTime}
          )
          and
          (
             city_code = #{code}
             or
             aqi_json->'$.city_code' = #{code}
          )
          order by time asc
    </select>
 
    <select id="getAqisByOrganizationId" resultType="map">
        SELECT
        h.aqi_json ->> '$.AQI' * 1 aqi,
        DATE_FORMAT( h.time, '%H' ) * 1 time
        FROM
        hangzhou_aqi h
        <where>
            h.city_code = #{cityCode}
            AND h.time <![CDATA[<]]> #{end}
        </where>
        ORDER BY
        h.time DESC
        LIMIT 24
    </select>
 
    <select id="queryCityCode" resultType="integer">
      select city_code from area where area_code = #{areaCode}
    </select>
 
    <select id="getAreaAvgDataByAreaCode" resultType="java.util.Map">
        SELECT
        DATE_FORMAT(time, #{typeFormat}) time,
        round(AVG(case when aqi_json->'$.${sensors}' is null then aqi_json->'$.${sensors1}' else aqi_json->'$.${sensors}' end),4)  as '${sensors2}'
        FROM
        hangzhou_aqi ha
        WHERE
        ha.time >= #{start}
        AND ha.time <![CDATA[<]]> #{end}
        AND ha.city_code = #{areaCode}
        GROUP BY
        DATE_FORMAT(time, #{typeFormat})
        ORDER BY
        time
    </select>
 
    <select id="getAqiDataByAreaCode" resultType="java.util.Map">
        SELECT
        DATE_FORMAT(time, #{typeFormat}) time,
        ifnull(aqi_json->'$.${sensor1}',aqi_json->'$.${sensor2}') as '${sensor2}'
        FROM
        hangzhou_aqi
        WHERE
        time >= #{start}
        AND time <![CDATA[<]]> #{end}
        AND city_code = #{cityCode}
        ORDER BY
        time
  </select>
 
    <select id="getHistoryO3" resultType="java.util.Map">
        SELECT
        DATE_FORMAT(time, #{typeFormat}) time,
        value->'$.${sensor2}' as '${sensor2}'
        FROM
        history_aqi_${timeUnits}
        WHERE
        time >= #{start}
        AND time <![CDATA[<]]> #{end}
        AND city_code = #{cityCode}
        ORDER BY
        time
  </select>
 
    <select id="getAqi" resultType="java.util.Map">
        select
        DATE_FORMAT(time, #{typeFormat}) time,
        aqi_json json
        FROM
        hangzhou_aqi
        WHERE
        time >= #{start}
        AND time <![CDATA[<]]> #{end}
        AND city_code = #{cityCode}
        ORDER BY
        time
    </select>
 
    <select id="getHistoryAqi" resultType="java.util.Map">
        select
        DATE_FORMAT(time, #{typeFormat}) time,
        value json
        FROM
        history_aqi_${timeUnits}
        WHERE
        time >= #{start}
        AND time <![CDATA[<]]> #{end}
        AND city_code = #{cityCode}
        ORDER BY
        time
    </select>
 
    <select id="getAvgO3EightHours" resultType="java.util.Map">
        select
        cast(AVG(case when aqi_json->'$.O3C' is null then aqi_json->'$.O3' else aqi_json->'$.O3C' end) as UNSIGNED  integer) 'O3_8H'
        FROM
        hangzhou_aqi
        WHERE
        time >= #{start}
        AND time <![CDATA[<]]> #{end}
        AND city_code = #{cityCode}
    </select>
 
    <select id="getHistoryO3EightHours" resultType="java.util.Map">
        select
        value->'$.O3_8h' 'O3_8H'
        FROM
        history_aqi_${timeUnits}
        WHERE
        time = #{time}
        and city_code=#{cityCode}
    </select>
 
    <select id="getSumO3" resultType="java.util.Map">
        select
        SUM(case when aqi_json->'$.O3C' is null then aqi_json->'$.O3' else aqi_json->'$.O3C' end) 'O3Sum'
        FROM
        hangzhou_aqi
        WHERE
        time >= #{start}
        AND time <![CDATA[<]]> #{end}
        AND city_code = #{cityCode}
    </select>
</mapper>