DeviceMapper.xml
27.5 KB
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
<?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="org.thingsboard.server.dao.yunteng.mapper.DeviceMapper">
<resultMap type="org.thingsboard.server.common.data.yunteng.dto.SelectItemDTO" id="listInform">
<result property="id" column="id" />
<result property="name" column="name"/>
<result property="alias" column="alias"/>
<result property="deviceType" column="device_type" typeHandler="org.apache.ibatis.type.EnumTypeHandler"/>
</resultMap>
<resultMap type="org.thingsboard.server.dao.yunteng.entities.TkDeviceEntity" id="deviceMap">
<result property="id" column="id" />
<result property="name" column="name"/>
<result property="alias" column="alias"/>
<result property="deviceInfo" column="device_info"
typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/>
<result property="profileId" column="device_profile_id"
/>
<result property="deviceProfileId" column="device_profile_id"
/>
<result property="deviceType" column="device_type" typeHandler="org.apache.ibatis.type.EnumTypeHandler"/>
<result property="brand" column="brand"/>
<result property="sn" column="sn"/>
<result property="code" column="code"/>
<result property="codeType" column="code_type" typeHandler="org.apache.ibatis.type.EnumTypeHandler"/>
<result property="tenantId" column="tenant_id"
/>
<result property="tbDeviceId" column="id"
/>
<result property="label" column="label"/>
<result property="alarmStatus" column="alarm_status"/>
<result property="deviceState" column="device_state" typeHandler="org.apache.ibatis.type.EnumTypeHandler"/>
<result property="lastOnlineTime" column="last_online_time"/>
<result property="lastOfflineTime" column="last_offline_time"/>
<result property="createTime" column="created_time"/>
<result property="updateTime" column="update_time"/>
<result property="creator" column="creator"
/>
<result property="updater" column="updater"
/>
<result property="customerId" column="customer_id" />
<result property="customerName" column="customer_name"/>
<result property="customerAdditionalInfo" column="customer_additional_info"
typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/>
<result property="organizationId" column="organization_id"/>
<result property="gatewayId" column="gateway_id"
/>
<result property="gatewayName" column="gateway_name"/>
<result property="gatewayAlias" column="gateway_alias"/>
<result property="transportType" column="transport_type"/>
<result property="firmwareId" column="firmware_id"
/>
<result property="softwareId" column="software_id"
/>
<result property="profileName" column="profile_name"/>
<result property="organizationName" column="organization_name"/>
<result property="description" column="description"/>
<result property="isEdge" column="is_edge"/>
<result property="createTime" column="createTime"/>
<result property="updateTime" column="updateTime"/>
<!-- <association property="deviceProfile" column="device_profile_id"-->
<!-- javaType="org.thingsboard.server.dao.yunteng.entities.TkDeviceProfileEntity">-->
<!-- <result property="name" column="profile_name"/>-->
<!-- <result property="transportType" column="transport_type"/>-->
<!-- <!– <result property="profileData" column="profile_data" typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/>–>-->
<!-- </association>-->
<!-- <association property="organizationDTO"-->
<!-- javaType="org.thingsboard.server.common.data.yunteng.dto.OrganizationDTO">-->
<!-- <result property="name" column="organization_name"/>-->
<!-- </association>-->
</resultMap>
<resultMap id="relationDeviceMap" type="org.thingsboard.server.common.data.yunteng.dto.RelationDeviceDTO">
<result property="tbDeviceId" column="tb_device_id"/>
<result property="tbDeviceName" column="name"/>
<result property="alias" column="alias"/>
<result property="label" column="label"/>
<result property="deviceState" column="device_state" typeHandler="org.apache.ibatis.type.EnumTypeHandler"/>
<result property="createdTime" column="created_time"/>
<result property="lastOnlineTime" column="last_online_time"/>
</resultMap>
<resultMap id="baseHomePageTop" type="org.thingsboard.server.common.data.yunteng.dto.BaseHomePageTop">
<result property="sumCount" column="sum_count"/>
<result property="todayAdd" column="today_add"/>
</resultMap>
<resultMap id="kvDictionaryMap" type="org.thingsboard.server.common.data.yunteng.dto.KvDictionaryValueDTO">
<result property="key" column="key"></result>
<result property="value" column="msg_count"></result>
</resultMap>
<sql id="basicColumns">
ifd.id,ifd.id tb_device_id,ifd.gateway_id,ifd.code,ifd.code_type,ifd.device_profile_id
profile_id,ifd.device_profile_id
,ifd.sn,ifd.brand,ifd.name,ifd.alias,ifd.device_info,ifd.tenant_id
,ifd.label,ifd.device_type,ifd.created_time,ifd.update_time,ifd.creator,
ifd.updater,ifd.organization_id,ifd.alarm_status,ifd.description
<!-- ,attrAt.long_v last_activity_time,attrCt.long_v last_connect_time-->
</sql>
<sql id="detailColumns">
<include refid="pageColumns"/>
,idg.name gateway_name,idg.alias gateway_alias,ifdp.profile_data
</sql>
<sql id="pageColumns">
<include refid="basicColumns"/>
,ifdp.name AS profile_name,ifdp.transport_type
,io.name AS organization_name
,attrCt.long_v last_online_time,attrDt.long_v last_offline_time
</sql>
<select id="getDevicePage" resultMap="deviceMap">
SELECT
<include refid="pageColumns"/>,
ifd.customer_id,
cus.title AS customer_name,
cus.additional_info AS customer_additional_info,
ifd.firmware_id,
ifd.software_id,
attrAt.long_v last_activity_time,
attrDt.long_v last_disconnect_time,
attrCt.long_v last_connect_time,
CASE
WHEN attrAt.long_v is null THEN 'INACTIVE'
WHEN attrA0.bool_v =true THEN 'ONLINE'
WHEN attrA0.bool_v =false and attrAt.long_v is not null THEN 'OFFLINE'
END AS device_state,
CASE
WHEN re.to_id is not null THEN 1
ELSE 0
END AS is_edge
FROM device ifd
LEFT JOIN device_profile ifdp ON ifd.device_profile_id = ifdp.id
LEFT JOIN tk_organization io ON io.id = ifd.organization_id
LEFT JOIN attribute_kv attrA0 ON ifd.id = attrA0.entity_id AND attrA0.entity_type ='DEVICE' AND attrA0.attribute_key='active'
LEFT JOIN attribute_kv attrAt ON ifd.id = attrAt.entity_id AND attrAt.entity_type ='DEVICE' AND attrAt.attribute_key='lastActivityTime'
LEFT JOIN attribute_kv attrDt ON ifd.id = attrDt.entity_id AND attrDt.entity_type ='DEVICE' AND attrDt.attribute_key='lastDisconnectTime'
LEFT JOIN attribute_kv attrCt ON ifd.id = attrCt.entity_id AND attrCt.entity_type ='DEVICE' AND attrCt.attribute_key='lastConnectTime'
LEFT JOIN customer cus ON cus.id = ifd.customer_id
LEFT JOIN relation re on re.to_id = ifd.id and from_type = 'EDGE' and re.relation_type = 'ManagedByEdge'
<where>
<if test="queryMap.tenantId !=null ">
AND ifd.tenant_id = #{queryMap.tenantId}
</if>
<if test="queryMap.selected !=null and queryMap.convertDevices !=null and queryMap.convertDevices.size>0">
<if test="queryMap.selected == true">
AND ifd.id IN
</if>
<if test="queryMap.selected == false">
AND ifd.id NOT IN
</if>
<foreach collection="queryMap.convertDevices" item="convertDevice" open="(" separator="," close=")">
#{convertDevice}
</foreach>
</if>
<if test="queryMap.deviceProfileIds !=null and queryMap.deviceProfileIds.size>0">
AND ifd.device_profile_id IN
<foreach collection="queryMap.deviceProfileIds" item="deviceProfileId" open="(" separator="," close=")">
#{deviceProfileId}
</foreach>
</if>
<if test="queryMap.name !=null and queryMap.name !=''">
AND (
ifd.name LIKE concat('%',#{queryMap.name},'%')
or
ifd.alias LIKE concat('%',#{queryMap.name},'%')
)
</if>
<if test="queryMap.deviceType !=null and queryMap.deviceType !=''">
AND ifd.device_type = #{queryMap.deviceType}
</if>
<if test="queryMap.deviceState !=null">
<if test="queryMap.deviceState =='INACTIVE'">
and attrAt.long_v is null
</if>
<if test="queryMap.deviceState =='ONLINE'">
and attrA0.bool_v =true
</if>
<if test="queryMap.deviceState =='OFFLINE'">
and attrA0.bool_v =false and attrAt.long_v is not null
</if>
</if>
<if test="queryMap.activeState !=null">
AND attrA.long_v IS NULL
</if>
<if test="queryMap.customerId !=null">
AND ifd.customer_id = #{queryMap.customerId}
</if>
<if test="queryMap.isCollect !=null ">
and ifd.id::TEXT
<if test="queryMap.isCollect==1">
in
</if>
<if test="queryMap.isCollect==0">
not in
</if>
( SELECT business_id
FROM tk_user_collect
WHERE user_id = #{queryMap.userId}
AND business_type = 'DEVICE_COLLECT')
</if>
<if test="queryMap.alarmStatus !=null and queryMap.alarmStatus == 1">
AND ifd.alarm_status = 1
</if>
<if test="queryMap.alarmStatus !=null and queryMap.alarmStatus == 0">
AND (ifd.alarm_status = 0 or ifd.alarm_status is null)
</if>
<if test="queryMap.label !=null">
AND ifd.label like concat('%',#{queryMap.label},'%')
</if>
<if test="queryMap.organizationIds !=null and queryMap.organizationIds.size>0">
AND ifd.organization_id IN
<foreach collection="queryMap.organizationIds" item="organizationId" open="(" separator="," close=")">
#{organizationId}
</foreach>
</if>
</where>
</select>
<select id="selectDetail" resultMap="deviceMap">
SELECT
<include refid="detailColumns"/>,
CASE
WHEN attrAt.long_v is null THEN 'INACTIVE'
WHEN attrA0.bool_v =true THEN 'ONLINE'
WHEN attrA0.bool_v =false and attrAt.long_v is not null THEN 'OFFLINE'
END AS device_state
FROM device ifd
LEFT JOIN device_profile ifdp ON ifd.device_profile_id = ifdp.id
LEFT JOIN tk_organization io ON io.id = ifd.organization_id
LEFT JOIN attribute_kv attrA0 ON ifd.id = attrA0.entity_id AND attrA0.entity_type ='DEVICE' AND attrA0.attribute_key='active'
LEFT JOIN attribute_kv attrAt ON ifd.id = attrAt.entity_id AND attrAt.entity_type ='DEVICE' AND attrAt.attribute_key='lastActivityTime'
LEFT JOIN attribute_kv attrDt ON ifd.id = attrDt.entity_id AND attrDt.entity_type ='DEVICE' AND attrDt.attribute_key='lastDisconnectTime'
LEFT JOIN attribute_kv attrCt ON ifd.id = attrCt.entity_id AND attrCt.entity_type ='DEVICE' AND attrCt.attribute_key='lastConnectTime'
LEFT JOIN device idg ON idg.id = ifd.gateway_id
<where>
<if test="tenantId !=null">
AND ifd.tenant_id = #{tenantId}
</if>
<if test="id !=null ">
AND ifd.id = #{id}
</if>
</where>
</select>
<select id="findGateWayDeviceByTbDeviceId" resultMap="deviceMap">
SELECT
<include refid="basicColumns"/>
FROM device ifd
LEFT JOIN relation rl ON ifd.id = rl.from_id
WHERE
rl.to_id = #{tbDeviceId}
AND rl.relation_type_group = 'COMMON' AND rl.relation_type = 'Created'
AND rl.from_type = 'DEVICE' AND rl.to_type = 'DEVICE'
</select>
<update id="freshAlarmStatus">
UPDATE device
SET alarm_status=#{created}
WHERE id = #{tbDeviceId}
</update>
<select id="findDevices" resultMap="deviceMap">
SELECT
ifd.ID,
ifd.NAME,
ifd.tenant_id,
ifd.organization_id,
ifd.created_time,
ifd.device_type,
ifdp.NAME AS profile_name,
io.NAME AS organization_name,
attrAt.long_v last_online_time,
ifd.customer_id,
ifd.created_time as createTime,
ifd.update_time as updateTime,
CASE
WHEN attrAt.long_v is null THEN 'INACTIVE'
WHEN attrA0.bool_v =true THEN 'ONLINE'
WHEN attrA0.bool_v =false and attrAt.long_v is not null THEN 'OFFLINE'
END AS device_state
FROM device ifd
LEFT JOIN device_profile ifdp ON ifd.device_profile_id = ifdp.ID
LEFT JOIN tk_organization io ON io.ID = ifd.organization_id
LEFT JOIN attribute_kv attrA0 ON ifd.id = attrA0.entity_id AND attrA0.entity_type ='DEVICE' AND attrA0.attribute_key='active'
LEFT JOIN attribute_kv attrAt ON ifd.id = attrAt.entity_id AND attrAt.entity_type ='DEVICE' AND attrAt.attribute_key='lastActivityTime'
LEFT JOIN attribute_kv attrIt ON ifd.id = attrIt.entity_id AND attrIt.entity_type ='DEVICE' AND attrIt.attribute_key='inactivityAlarmTime'
LEFT JOIN attribute_kv attrCt ON ifd.id = attrCt.entity_id AND attrCt.entity_type ='DEVICE' AND attrCt.attribute_key='lastConnectTime'
LEFT JOIN attribute_kv attrDt ON ifd.id = attrDt.entity_id AND attrDt.entity_type ='DEVICE' AND attrDt.attribute_key='lastDisconnectTime'
<where>
<if test="queryMap.tenantId !=null">
AND ifd.tenant_id = #{queryMap.tenantId}
</if>
<if test="queryMap.customerId !=null">
AND ifd.customer_id = #{queryMap.customerId}
</if>
<if test="queryMap.organizationIds !=null">
AND ifd.organization_id IN
<foreach collection="queryMap.organizationIds" item="orgId" open="(" separator="," close=")">
#{orgId}
</foreach>
</if>
</where>
</select>
<select id="getRelationDevicePage" resultMap="relationDeviceMap">
SELECT
de.id,
de.id as tb_device_id,
de.name,
de.alias,
de.label,
de.created_time,
attrCt.long_v AS last_online_time,
CASE
WHEN attrAt.long_v is null THEN 'INACTIVE'
WHEN attrA0.bool_v =true THEN 'ONLINE'
WHEN attrA0.bool_v =false and attrAt.long_v is not null THEN 'OFFLINE'
END AS device_state
FROM device de
LEFT JOIN relation rt ON de.id = rt.to_id
LEFT JOIN attribute_kv attrA0 ON de.id = attrA0.entity_id AND attrA0.entity_type ='DEVICE' AND attrA0.attribute_key='active'
LEFT JOIN attribute_kv attrAt ON de.id = attrAt.entity_id AND attrAt.entity_type ='DEVICE' AND attrAt.attribute_key='lastActivityTime'
LEFT JOIN attribute_kv attrDt ON de.id = attrDt.entity_id AND attrDt.entity_type ='DEVICE' AND attrDt.attribute_key='lastDisconnectTime'
LEFT JOIN attribute_kv attrCt ON de.id = attrCt.entity_id AND attrCt.entity_type ='DEVICE' AND attrCt.attribute_key='lastConnectTime'
<where>
rt.from_id = #{queryMap.fromId}
AND de.tenant_id = #{queryMap.tenantId}
<if test="queryMap.name !=null and queryMap.name !=''">
AND de.name LIKE concat('%',#{queryMap.name},'%')
</if>
<if test="queryMap.deviceState !=null">
<if test="queryMap.deviceState =='INACTIVE'">
and attrAt.long_v is null
</if>
<if test="queryMap.deviceState =='ONLINE'">
and attrA0.bool_v =true
</if>
<if test="queryMap.deviceState =='OFFLINE'">
and attrA0.bool_v =false and attrAt.long_v is not null
</if>
</if>
<if test="queryMap.organizationIds !=null and queryMap.organizationIds.size>0">
AND de.organization_id IN
<foreach collection="queryMap.organizationIds" item="organizationId" open="(" separator="," close=")">
#{organizationId}
</foreach>
</if>
</where>
</select>
<select id="findDeviceIdsByCustomerId" resultType="java.lang.String">
SELECT idi.id
FROM device idi
WHERE customer_id = #{customerId}
</select>
<select id="findDeviceInfoByCustomerId" resultMap="deviceMap">
SELECT
<include refid="basicColumns"/>
FROM device ifd
WHERE ifd.tenant_id = #{tenantId} AND ifd.customer_id = #{customerId}
</select>
<select id="findDeviceMessageInfo" resultMap="baseHomePageTop">
SELECT COUNT(ts.ts) AS sum_count,
SUM(CASE WHEN ts.ts >= #{todayTime} THEN 1 ELSE 0 END) AS today_add
FROM (SELECT tk.ts
FROM ts_kv tk
LEFT JOIN device d ON tk.entity_id = d.ID
WHERE d.customer_id = #{customerId}
GROUP BY tk.ts) ts;
</select>
<select id="findDeviceAlarmInfoByCustomer" resultMap="baseHomePageTop">
SELECT COUNT(m.ID) AS sum_count,
SUM(CASE WHEN m.created_time >= #{todayTime} THEN 1 ELSE 0 END) AS today_add
FROM alarm m
LEFT JOIN device d ON m.originator_id = d.id
WHERE m.originator_type = 5
<if test="customerId !=null">
AND d.customer_id = #{customerId}
</if>
<if test="organizationIds !=null">
AND d.organization_id IN
<foreach collection="organizationIds" item="orgId" open="(" separator="," close=")">
#{orgId}
</foreach>
</if>
</select>
<select id="findDeviceMessageInfoByTs" resultType="java.lang.Integer">
SELECT COUNT
(ts.ts) AS sum_count
FROM (SELECT tk.ts
FROM ts_kv tk
LEFT JOIN device d ON tk.entity_id = d.ID
WHERE d.customer_id = #{customerId}
AND tk.ts >= #{startTime}
AND tk.ts
< #{endTime}
GROUP BY tk.ts) ts;
</select>
<select id="findDeviceAlarmInfoByCreatedTime" resultType="java.lang.Integer">
SELECT COUNT
(ID) AS sum_count
FROM alarm
WHERE customer_id = #{customerId}
AND created_time >= #{startTime}
AND created_time
< #{endTime}
AND originator_type = 5;
</select>
<select id="masterDevices" resultMap="listInform">
SELECT
base.id,base.name,base.alias,base.device_type,base.device_profile_id
FROM device base
<where>
base.device_type != 'SENSOR'
<if test="tenantId !=null and tenantId !=''">
AND base.tenant_id = #{tenantId}
</if>
<if test="deviceProfileId !=null and deviceProfileId !=''">
AND base.device_profile_id = #{deviceProfileId}
</if>
<if test="customerId !=null and customerId !=''">
AND base.customer_id = #{customerId}
</if>
<if test="organizationIds !=null">
AND base.organization_id IN
<foreach collection="organizationIds" item="orgId" open="(" separator="," close=")">
#{orgId}
</foreach>
</if>
</where>
</select>
<select id="slaveDevices" resultMap="listInform">
SELECT
ide.id,ide.name,base.alias,ide.device_type,ide.device_profile_id
FROM (select * from relation where relation_type_group = 'COMMON' AND relation_type = 'Created' AND from_type =
'DEVICE' AND to_type = 'DEVICE'
<if test="masterId !=null and masterId !=''">
AND from_id = #{masterId}
</if>
) base
LEFT JOIN device ide ON base.to_id = ide.id
<where>
ide.device_type = 'SENSOR'
<if test="tenantId !=null and tenantId !=''">
AND ide.tenant_id = #{tenantId}
</if>
<if test="organizationIds !=null">
AND ide.organization_id IN
<foreach collection="organizationIds" item="orgId" open="(" separator="," close=")">
#{orgId}
</foreach>
</if>
<if test="customerId !=null and customerId !=''">
AND ide.customer_id = #{customerId}
</if>
</where>
</select>
<select id="slaveDevice" resultMap="deviceMap">
SELECT
<include refid="basicColumns"/>
FROM device ifd
<where>
ifd.tenant_id = #{tenantId}
AND ifd.gateway_id = #{masterId}
AND ifd.code = #{code}
</where>
</select>
<select id="findDeviceKeys" resultType="string">
SELECT
DISTINCT base.key as keyName
FROM ts_kv_dictionary base
LEFT JOIN ts_kv_latest latest ON latest.key = base.key_id
LEFT JOIN device iot ON iot.ID = latest.entity_id
<where>
iot.tenant_id = #{tenantId}
<if test="customerId !=null and customerId !=''">
AND tb.customer_id = #{customerId}
</if>
<if test="organizationIds !=null and organizationIds.size() > 0">
AND iot.organization_id IN
<foreach collection="organizationIds" item="orgId" open="(" separator="," close=")">
#{orgId}
</foreach>
</if>
<if test="deviceIds !=null and deviceIds.size() > 0">
AND iot.id IN
<foreach collection="deviceIds" item="devId" open="(" separator="," close=")">
#{devId}
</foreach>
</if>
</where>
</select>
<select id="getDeviceRelation" resultType="java.lang.String">
SELECT
<if test="isSlave !=null and isSlave == true">
from_id
</if>
<if test="isSlave !=null and isSlave == false">
to_id
</if>
FROM relation
WHERE
from_type = 'DEVICE'
AND relation_type_group = 'COMMON'
<if test="isSlave !=null and isSlave == false">
AND from_id = #{deviceId}
</if>
<if test="isSlave !=null and isSlave == true">
AND to_id = #{deviceId}
</if>
</select>
<select id="findDeviceInfo" resultMap="deviceMap">
SELECT
<include refid="basicColumns"/>,tdp.name AS profile_name,io.name AS organization_name
FROM device ifd
LEFT JOIN device_profile tdp ON ifd.device_profile_id = tdp.id
LEFT JOIN tk_organization io ON io.id = ifd.organization_id
LEFT JOIN attribute_kv attrCt ON ifd.id = attrCt.entity_id AND attrCt.entity_type ='DEVICE' AND attrCt.attribute_key='lastConnectTime'
LEFT JOIN attribute_kv attrAt ON ifd.id = attrAt.entity_id AND attrAt.entity_type ='DEVICE' AND attrAt.attribute_key='lastActivityTime'
WHERE ifd.id = #{tbDeviceId}
AND ifd.tenant_id = #{tenantId}
</select>
<select id="getMsgSumByTenantIdAndDictionary" resultMap="kvDictionaryMap">
SELECT tkd."key",SUM ( long_v ) AS msg_count FROM
ts_kv tk
LEFT JOIN api_usage_state aus ON tk.entity_id = aus.ID
LEFT JOIN ts_kv_dictionary tkd ON tkd.key_id = tk."key"
WHERE
aus.tenant_id = #{tenantId}
AND tkd."key" IN
<foreach collection="dictionaries" item="dictionary" open="(" separator="," close=")">
#{dictionary}
</foreach>
GROUP BY
tkd."key";
</select>
<select id="findDevicesByOrganizationIds" resultMap="deviceMap">
SELECT
<include refid="basicColumns"/>,dev.transport_type
FROM device ifd
left join device_profile dev on ifd.device_profile_id = dev.id
where
1=1
and ifd.device_profile_id in
<foreach collection="deviceProfileIds" item="deviceProfileId" open="(" separator="," close=")">
#{deviceProfileId}
</foreach>
and ifd.organization_id in
<foreach collection="orgIds" item="orgId" open="(" separator="," close=")">
#{orgId}
</foreach>
</select>
<select id="findDevicesByDeviceTypeAndOrganizationId" resultMap="deviceMap">
SELECT
<include refid="basicColumns"/>,dev.transport_type
FROM device ifd
left join device_profile dev on ifd.device_profile_id = dev.id
<if test="isSceneLinkage == true">
<!-- 此设备是边端创建或者已经分配给边端-->
LEFT JOIN relation re on re.to_id = ifd.id and re.from_type = 'EDGE' and( re.relation_type = 'ManagedByEdge' or re.relation_type = 'Contains')
</if>
where
1=1
<if test="isSceneLinkage == true">
and re.to_id is null
</if>
<if test="deviceType !=null and deviceType !=''">
AND ifd.device_type = #{deviceType}
</if>
<if test="deviceLabel !=null and deviceLabel !=''">
AND ifd.label = #{deviceLabel}
</if>
<if test="deviceProfileId !=null ">
AND ifd.device_profile_id = #{deviceProfileId}
</if>
<if test="transportType !=null">
AND dev.transport_type = #{transportType}
</if>
and ifd.organization_id in
<foreach collection="orgIds" item="orgId" open="(" separator="," close=")">
#{orgId}
</foreach>
</select>
<select id="findDeviceByTbDeviceIdAndCustomerId"
resultType="org.thingsboard.server.common.data.yunteng.dto.DeviceDTO">
SELECT
<include refid="basicColumns"/>
FROM device ifd
WHERE ifd.customer_id = #{customerId}
AND ifd.tenant_id = #{tenantId}
AND ifd.id = #{tbDeviceId}
</select>
<select id="findDevicesByProfileIdAndOrganizationId" resultMap="deviceMap">
SELECT
<include refid="basicColumns"/>,dev.transport_type
FROM device ifd
LEFT JOIN device_profile dev ON ifd.profile_id = dev.id
WHERE
1=1
<if test="profileId !=null and profileId !=''">
AND ifd.device_profile_id = #{profileId}
</if>
AND ifd.organization_id IN
<foreach collection="orgIds" item="orgId" open="(" separator="," close=")">
#{orgId}
</foreach>
</select>
</mapper>