cjl
2024-08-09 a022ae9804d0c2f402711b6b5202319d853919cf
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?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.api.mapper.DeviceMapper">
 
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.moral.api.entity.Device">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="mac" property="mac"/>
        <result column="address" property="address"/>
        <result column="longitude" property="longitude"/>
        <result column="latitude" property="latitude"/>
        <result column="state" property="state"/>
        <result column="operate_ids" property="operateIds"/>
        <result column="monitor_point_id" property="monitorPointId"/>
        <result column="organization_id" property="organizationId"/>
        <result column="guid" property="guid"/>
        <result column="device_version_id" property="deviceVersionId"/>
        <result column="profession" property="profession"/>
        <result column="tech" property="tech"/>
        <result column="detector" property="detector"/>
        <result column="purchaser" property="purchaser"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="install_time" property="installTime"/>
        <result column="is_delete" property="isDelete"/>
        <result column="extend" property="extend"/>
        <result column="town_code" property="townCode"/>
        <result column="dev_num" property="devNum"/>
    </resultMap>
 
    <!--监测因子趋势图数据-->
    <select id="getTrendChartData" resultType="java.util.Map">
        SELECT mac,
        DATE_FORMAT(`time`,#{dateFormat}) AS `time`,
        `value`->'$.${sensorCode}' AS '${sensorCode}'
        FROM history_${timeUnits}
        WHERE mac IN
            <foreach collection="macs" item="mac" index="index" open="(" close=")" separator=",">
                #{mac}
            </foreach>
        AND DATE_FORMAT(`time`,'%Y-%m-%d %H') <![CDATA[>=]]> #{start}
        AND DATE_FORMAT(`time`,'%Y-%m-%d %H') <![CDATA[<]]> #{end}
        ORDER BY `time`
    </select>
    <select id="deviceList" resultType="java.util.Map">
        SELECT
            t.latitude as latitude,
            t.longitude as longitude,
            t.mac as mac,
            t.state,(hf.value ->> #{type} )+0 as ${sensorCode}
        FROM
            device t
        inner JOIN monitor_point p on p.id = t.monitor_point_id and p.is_delete = 0
                and p.organization_id in (
                    SELECT id FROM (
                    SELECT id, parent_id, name FROM organization  WHERE id = #{organizationId}
                    UNION ALL
                    SELECT i.id, i.parent_id, i.name
                    FROM organization  i
                    INNER JOIN (
                    SELECT * FROM (
                    SELECT id, parent_id, name FROM organization  WHERE id = #{organizationId}
                    UNION ALL
                    SELECT id, parent_id, name FROM organization WHERE parent_id = #{organizationId}
                    ) t1
                    ) t2 ON i.parent_id = t2.id
                    where i.is_delete = 0
                    ) t3
                    )
        <if test="region != null">
            and p.${region} = #{regionCode}
        </if>
        <if test="endHourlyTime !=null and endHourlyTime != ''">
            left join history_hourly_${times} hf on hf.time =#{endHourlyTime}  and t.mac = hf.mac
        </if>
        <if test="endHourlyTime ==null">
            left join history_five_minutely_${times} hf on hf.time = (select max(time) from history_five_minutely_${times} LIMIT 1
            ) and t.mac = hf.mac
        </if>
 
 
        WHERE
            t.state <![CDATA[ <> ]]> '0'  and t.is_delete = 0
        order by t.id desc
    </select>
 
    <select id="deviceIdList" resultType="java.lang.Integer">
        SELECT
            t.id
        FROM
            device t
                INNER JOIN monitor_point p ON p.id = t.monitor_point_id
                AND p.is_delete = 0
                AND p.organization_id IN (
                    SELECT
                        id
                    FROM
                        (
                            SELECT
                                id,
                                parent_id,
                                NAME
                            FROM
                                organization
                            WHERE
                                id = #{organizationId} UNION ALL
                            SELECT
                                i.id,
                                i.parent_id,
                                i.NAME
                            FROM
                                organization i
                                    INNER JOIN (
                                    SELECT
                                        *
                                    FROM
                                        (
                                            SELECT
                                                id,
                                                parent_id,
                                                NAME
                                            FROM
                                                organization
                                            WHERE
                                                id = #{organizationId} UNION ALL
                                            SELECT
                                                id,
                                                parent_id,
                                                NAME
                                            FROM
                                                organization
                                            WHERE
                                                parent_id = #{organizationId}
                                        ) t1
                                ) t2 ON i.parent_id = t2.id
                            WHERE
                                i.is_delete = 0
                        ) t3
                )
        WHERE
            t.is_delete = 0
    </select>
 
    <select id="deviceOrgIdList" resultType="java.lang.Integer">
        SELECT
            id
        FROM
            (
                SELECT
                    id,
                    parent_id,
                    NAME
                FROM
                    organization
                WHERE
                    id = #{organizationId} UNION ALL
                SELECT
                    i.id,
                    i.parent_id,
                    i.NAME
                FROM
                    organization i
                        INNER JOIN (
                        SELECT
                            *
                        FROM
                            (
                                SELECT
                                    id,
                                    parent_id,
                                    NAME
                                FROM
                                    organization
                                WHERE
                                    id = #{organizationId} UNION ALL
                                SELECT
                                    id,
                                    parent_id,
                                    NAME
                                FROM
                                    organization
                                WHERE
                                    parent_id = #{organizationId}
                            ) t1
                    ) t2 ON i.parent_id = t2.id
                WHERE
                    i.is_delete = 0
            ) t3
    </select>
    <select id="deviceMacs" resultType="java.lang.String">
        select mac from device where organization_id = #{organizationId} order by id desc
 
    </select>
 
    <select id="getDevices" resultType="com.moral.api.pojo.vo.device.AppDeviceVo">
        SELECT d.id,d.`name`,d.mac,d.special_type FROM device as d
        where 1=1
        <if test="organizationId !=null">
            AND organization_id = #{organizationId}
        </if>
           and mac LIKE "%"#{mac}"%" or `name` like "%"#{mac}"%" AND is_delete = 0
        union
        SELECT sd.id,sd.`name`,sd.mac,sd.special_type FROM special_device  as sd
        WHERE 1=1
        <if test="organizationId !=null">
            AND organization_id = #{organizationId}
        </if>
          and mac LIKE "%"#{mac}"%" AND is_delete = 0
    </select>
 
 
    <select id="getHeatMap" resultType="com.moral.api.pojo.dto.dataDisplay.HeatMapDTO">
        SELECT
        d.mac as mac,
        d.latitude as lat,
        d.longitude as lng,
        d.name as name,
        (hd.value ->> #{type} ) as count,
        hd.time
        FROM
        device as d
        inner JOIN organization o on o.id = d.organization_id and o.is_delete = 0
        <if test="organizationIds != null and organizationIds.size !=0">
            and organization_id in
            <foreach collection="organizationIds" item="id" index="index" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>
        <if test="tableName !=null">
            RIGHT join history_hourly${tableName} hd on hd.time = #{start}  and d.mac = hd.mac
        </if>
        <if test="tableName ==null ">
            RIGHT join history_daily hd on hd.time = #{start}   and d.mac = hd.mac
        </if>
        WHERE
         d.is_delete = 0
        order by d.mac
    </select>
 
 
    <select id="getHeatMapV1" resultType="com.moral.api.pojo.dto.dataDisplay.HeatMapDTO">
        SELECT
        d.mac as mac,
        d.latitude as lat,
        d.longitude as lng,
        d.name as name,
        (hd.value ->> #{type} ) as count,
        hd.time,
        dm.group_id AS groupId
        FROM
        device as d
        inner JOIN organization o on o.id = d.organization_id and o.is_delete = 0
        <if test="monitorId !=null and monitorId !=''">
            and monitor_point_id =#{monitorId}
        </if>
        <if test="organizationIds != null and organizationIds.size !=0">
            and organization_id in
            <foreach collection="organizationIds" item="id" index="index" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>
        left JOIN device_map_hierarchy as dm on d.id = dm.device_id
        <if test="tableName !=null">
            left join history_hourly${tableName} hd on hd.time = #{start}  and d.mac = hd.mac
        </if>
        <if test="tableName ==null ">
            left join history_daily hd on hd.time = #{start}   and d.mac = hd.mac
        </if>
        WHERE
        d.is_delete = 0
        order by d.mac
    </select>
 
 
    <select id="getPage" resultType="com.moral.api.vo.OnlineRateVo">
        select d.name,d.state,d.mac from device as d where organization_id=#{organizationId}
    </select>
 
 
    <select id="getLists" resultType="com.moral.api.vo.OnlineRateVo">
        select d.name,d.state,d.mac from device as d where  is_delete=0
        <if test="macs != null and macs.size !=0">
            and mac in
            <foreach collection="macs" item="mac" index="index" open="(" close=")" separator=",">
                #{mac}
            </foreach>
        </if>
        <if test="state !=null and state == 0" >
            and  state=#{state}
        </if>
        <if test="state !=null and state != 0">
            and  state<![CDATA[>=]]>#{state}
        </if>
    </select>
 
    <select id="getListVo" resultType="com.moral.api.pojo.vo.device.DeviceVO">
        select * from device  where monitor_point_id=#{monitorPointId} and is_delete=0
    </select>
</mapper>