DeviceMapper.xml 19.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
<?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.common.data.yunteng.dto.DeviceDTO" 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="profile_id"/>
        <result property="deviceProfileId" column="device_profile_id"/>
        <result property="activeTime" column="active_time"/>
        <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="tenantId" column="tenant_id"/>
        <result property="tbDeviceId" column="tb_device_id"/>
        <result property="label" column="label"/>
        <result property="alarmStatus" column="alarm_status"/>
        <result property="deviceState" column="status"/>
        <result property="statusTime" column="status_time"/>
        <result property="lastOnlineTime" column="last_online_time"/>
        <result property="lastOfflineTime" column="last_offline_time"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="creator" column="creator"/>
        <result property="updater" column="updater"/>
        <result property="description" column="description"/>
        <result property="customerId" column="customer_id"/>
        <result property="customerName" column="cusotomer_name"/>
        <result property="organizationId" column="organization_id"/>
        <result property="gatewayId" column="gateway_id"/>
        <result property="gatewayName" column="gateway_name"/>
        <result property="gatewayAlias" column="gateway_alias"/>
        <association property="deviceProfile"
                     javaType="org.thingsboard.server.common.data.yunteng.dto.DeviceProfileDTO">
            <result property="name" column="profile_name"/>
            <result property="transportType" column="transport_type"/>
        </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="status"/>
        <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>

    <sql id="basicColumns">
        ifd.id
        ,ifd.sn,ifd.brand,ifd.name,ifd.alias,ifd.device_info,ifd.profile_id,ifd.device_profile_id,ifd.active_time,ifd.tenant_id,ifd.description
        ,ifd.tb_device_id,ifd.label,ifd.last_connect_time,ifd.device_type,ifd.device_state,ifd.create_time,ifd.update_time,ifd.creator,
        ifd.updater,ifd.organization_id,ifd.alarm_status
    </sql>
    <sql id="detailColumns">
        <include refid="pageColumns"/>
        ,ifd.gateway_id,idg.name gateway_name,idg.alias gateway_alias
    </sql>
    <sql id="pageColumns">
        <include refid="basicColumns"/>
        ,ifdp.name AS profile_name,ifdp.transport_type
        ,io.name AS organization_name
        ,a.bool_v status,b.long_v status_time,e.long_v last_online_time,c.long_v last_offline_time
    </sql>
    <select id="getDevicePage" resultMap="deviceMap">
        SELECT
        <include refid="pageColumns"/>,d.customer_id::TEXT AS customer_id,cus.title AS cusotomer_name
        FROM tk_device ifd
        LEFT JOIN device_profile ifdp ON ifd.profile_id = ifdp.id::TEXT
        LEFT JOIN tk_organization io ON io.id = ifd.organization_id
        LEFT JOIN attribute_kv a ON ifd.tb_device_id = a.entity_id::TEXT AND a.entity_type ='DEVICE' AND a.attribute_key='active'
        LEFT JOIN attribute_kv b ON ifd.tb_device_id = b.entity_id::TEXT AND b.entity_type ='DEVICE' AND b.attribute_key='lastActivityTime'
        LEFT JOIN attribute_kv c ON ifd.tb_device_id = c.entity_id::TEXT AND c.entity_type ='DEVICE' AND c.attribute_key='lastDisconnectTime'
        LEFT JOIN attribute_kv e ON ifd.tb_device_id = e.entity_id::TEXT AND e.entity_type ='DEVICE' AND e.attribute_key='lastConnectTime'
        LEFT JOIN device d ON d.id::TEXT = ifd.tb_device_id
        LEFT JOIN customer cus ON cus.id = d.customer_id
        <where>
            <if test="queryMap.tenantId !=null and queryMap.tenantId !=''">
                AND ifd.tenant_id = #{queryMap.tenantId}
            </if>
            <if test="queryMap.deviceProfileId !=null and queryMap.deviceProfileId !=''">
                AND ifd.device_profile_id = #{queryMap.deviceProfileId}
            </if>
            <if test="queryMap.name !=null and queryMap.name !=''">
                AND (
                ifd.name LIKE concat('%',#{queryMap.name}::TEXT,'%')
                or
                ifd.sn LIKE concat('%',#{queryMap.name}::TEXT,'%')
                or
                ifd.alias LIKE concat('%',#{queryMap.name}::TEXT,'%')
                )
            </if>
            <if test="queryMap.deviceType !=null and queryMap.deviceType !=''">
                AND ifd.device_type = #{queryMap.deviceType}
            </if>
            <if test="queryMap.deviceState !=null">
                AND a.bool_v = #{queryMap.deviceState}
                AND b.long_v IS NOT NULL
            </if>
            <if test="queryMap.activeState !=null">
                AND b.long_v IS NULL
            </if>
            <if test="queryMap.customerId !=null">
                AND d.customer_id::TEXT = #{queryMap.customerId}
            </if>
            <if test="queryMap.alarmStatus !=null">
                AND ifd.alarm_status = #{queryMap.alarmStatus}
            </if>
            <if test="queryMap.organizationIds !=null">
                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"/>
        FROM tk_device ifd
        LEFT JOIN device_profile ifdp ON ifd.profile_id = CAST (ifdp.id AS VARCHAR)
        LEFT JOIN tk_organization io ON io.id = ifd.organization_id
        LEFT JOIN attribute_kv a ON ifd.tb_device_id = a.entity_id::TEXT AND a.entity_type ='DEVICE' AND a.attribute_key='active'
        LEFT JOIN attribute_kv b ON ifd.tb_device_id = b.entity_id::TEXT AND b.entity_type ='DEVICE' AND b.attribute_key='lastActivityTime'
        LEFT JOIN attribute_kv c ON ifd.tb_device_id = c.entity_id::TEXT AND c.entity_type ='DEVICE' AND c.attribute_key='lastDisconnectTime'
        LEFT JOIN attribute_kv e ON ifd.tb_device_id = e.entity_id::TEXT AND e.entity_type ='DEVICE' AND e.attribute_key='lastConnectTime'
        LEFT JOIN device d ON d.id::TEXT = ifd.tb_device_id
        LEFT JOIN tk_device idg ON idg.tb_device_id = ifd.gateway_id
        <where>
            <if test="tenantId !=null and tenantId !=''">
                AND ifd.tenant_id = #{tenantId}
            </if>
            <if test="id !=null and id !=''">
                AND (ifd.id = #{id} OR ifd.tb_device_id = #{id})
            </if>

        </where>
    </select>
    <select id="findGateWayDeviceByTbDeviceId" resultMap="deviceMap">
        SELECT
        <include refid="basicColumns"/>
        FROM tk_device ifd
        LEFT JOIN relation rl ON ifd.tb_device_id = rl.from_id :: TEXT
        WHERE
        rl.to_id ::TEXT = #{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 tk_device
        SET alarm_status=#{created}
        WHERE tb_device_id = #{tbDeviceId}
    </update>

    <select id="findDevices" resultMap="deviceMap">
        SELECT
        ifd.ID,
        ifd.NAME,
        ifd.tenant_id,
        ifd.organization_id,
        ifd.create_time,
        ifdp.NAME AS profile_name,
        io.NAME AS organization_name,
        A.bool_v status,
        b.long_v last_online_time,
        d.customer_id :: TEXT AS customer_id
        FROM
        tk_device ifd
        LEFT JOIN device_profile ifdp ON ifd.profile_id = ifdp.ID ::
        TEXT LEFT JOIN tk_organization io ON io.ID = ifd.organization_id
        LEFT JOIN attribute_kv A ON ifd.tb_device_id = A.entity_id :: TEXT
        AND A.entity_type = 'DEVICE'
        AND A.attribute_key = 'active'
        LEFT JOIN attribute_kv b ON ifd.tb_device_id = b.entity_id :: TEXT
        AND b.entity_type = 'DEVICE'
        AND b.attribute_key = 'lastActivityTime'
        LEFT JOIN attribute_kv C ON ifd.tb_device_id = C.entity_id :: TEXT
        AND C.entity_type = 'DEVICE'
        AND C.attribute_key = 'inactivityAlarmTime'
        LEFT JOIN device d ON d.ID :: TEXT = ifd.tb_device_id
        <where>
            <if test="queryMap.tenantId !=null and queryMap.tenantId !=''">
                AND ifd.tenant_id = #{queryMap.tenantId}
            </if>
            <if test="queryMap.customerId !=null and queryMap.customerId !=''">
                AND d.customer_id :: TEXT = #{queryMap.customerId}
            </if>
        </where>
    </select>
    <select id="getRelationDevicePage" resultMap="relationDeviceMap">
        SELECT
        de.tb_device_id,
        de.name,
        de.alias,
        de.label,
        de.create_time as created_time,
        A.bool_v AS status,
        b.long_v AS last_online_time
        FROM
        tk_device de
        LEFT JOIN relation rt ON de.tb_device_id::uuid = rt.to_id
        LEFT JOIN attribute_kv A ON de.tb_device_id::uuid = A.entity_id
        AND A.entity_type = 'DEVICE'
        AND A.attribute_key = 'active'
        LEFT JOIN attribute_kv b ON de.tb_device_id::uuid = b.entity_id
        AND b.entity_type = 'DEVICE'
        AND b.attribute_key = 'lastActivityTime'
        <where>
            rt.from_id ::TEXT = #{queryMap.fromId}
            AND de.tenant_id  = #{queryMap.tenantId}

            <if test="queryMap.name !=null and queryMap.name !=''">
                AND de.name LIKE concat('%',#{queryMap.name}::TEXT,'%')
            </if>
            <if test="queryMap.deviceState !=null">
                AND A.bool_v = #{queryMap.deviceState}
                AND b.long_v IS NOT NULL
            </if>
            <if test="queryMap.activeState !=null">
                AND b.long_v IS NULL
            </if>
        </where>
    </select>
    <select id="findDeviceIdsByCustomerId" resultType="java.lang.String">
        SELECT idi.tb_device_id
        FROM tk_device idi
                 LEFT JOIN device d ON d.id::TEXT = idi.tb_device_id
        WHERE customer_id::TEXT = #{customerId}
    </select>
    <select id="findDeviceInfoByCustomerId" resultMap="deviceMap">
        SELECT <include refid="basicColumns"/>
        FROM tk_device ifd
        LEFT JOIN device d ON ifd.tb_device_id = d.id::TEXT
        WHERE ifd.tenant_id = #{tenantId} AND d.customer_id::TEXT = #{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::TEXT = #{customerId}
              GROUP BY tk.ts) ts;
    </select>

    <select id="findDeviceAlarmInfoByCustomer" resultMap="baseHomePageTop">
        SELECT COUNT(ID)                                                     AS sum_count,
               SUM(CASE WHEN created_time >= #{todayTime} THEN 1 ELSE 0 END) AS today_add
        FROM alarm
        WHERE customer_id ::TEXT = #{customerId}
          AND originator_type = 5;
    </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 ::TEXT = #{customerId}
                   AND tk.ts &gt;= #{startTime}
                   AND tk.ts
                  &lt; #{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 ::TEXT = #{customerId}
          AND created_time &gt;= #{startTime}
          AND created_time
         &lt; #{endTime}
          AND originator_type = 5;
    </select>

    <select id="countMsgs" resultType="int">
        SELECT COUNT(1) FROM ts_kv v LEFT JOIN device d ON d.id = v.entity_id
        <where>
            1=1
            <if test="queryMap.tenantId !=null">
                AND d.tenant_id= #{queryMap.tenantId}::uuid
            </if>
            <if test="queryMap.customerId !=null">
                AND d.customer_id= #{queryMap.customerId}::uuid
            </if>
            <if test="queryMap.startTime !=null">
                AND v.ts >= #{queryMap.startTime}
            </if>
            <if test="queryMap.endTime !=null">
                AND v.ts &lt; #{queryMap.endTime}
            </if>
        </where>
    </select>

    <select id="countDataPoints" resultType="int">
        SELECT COUNT(DISTINCT v.key) FROM ts_kv v LEFT JOIN device d ON d.id = v.entity_id
        <where>
            1=1
            <if test="queryMap.tenantId !=null">
                AND tenant_id= #{queryMap.tenantId}::uuid
            </if>
            <if test="queryMap.customerId !=null">
                AND customer_id= #{queryMap.customerId}::uuid
            </if>
            <if test="queryMap.startTime !=null">
                AND ts >= #{queryMap.startTime}
            </if>
            <if test="queryMap.endTime !=null">
                AND ts &lt; #{queryMap.endTime}
            </if>
        </where>
    </select>

    <select id="masterDevices" resultMap="listInform">
        SELECT
        base.tb_device_id as id,base.name,base.alias,base.device_type,base.device_profile_id
        FROM  tk_device base
        LEFT JOIN device tde ON tde.ID :: TEXT = base.tb_device_id
        <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 tde.customer_id :: TEXT = #{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.tb_device_id as 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 :: TEXT = #{masterId}
            </if>
        ) base
        LEFT JOIN device tde ON base.to_id = tde.id
        LEFT JOIN tk_device ide ON tde.ID :: TEXT = ide.tb_device_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 tde.customer_id :: TEXT = #{customerId}
            </if>
        </where>
    </select>

    <select id="slaveDevice" resultMap="deviceMap">
        SELECT
        <include refid="basicColumns"/>
        FROM tk_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 tb ON tb.ID  = latest.entity_id
        LEFT JOIN tk_device iot ON tb.ID :: TEXT = iot.tb_device_id
        <where>
            iot.tenant_id = #{tenantId}
            <if test="customerId !=null and customerId !=''">
                AND tb.customer_id :: TEXT = #{customerId}
            </if>
            <if test="organizationIds !=null and organizationIds.size() &gt; 0">
                AND iot.organization_id IN
                <foreach collection="organizationIds" item="orgId" open="(" separator="," close=")">
                    #{orgId}
                </foreach>
            </if>
            <if test="deviceIds !=null and deviceIds.size() &gt; 0">
                AND iot.tb_device_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 :: TEXT = #{deviceId}
        </if>
        <if test="isSlave !=null and isSlave == true">
          AND to_id :: TEXT = #{deviceId}
        </if>
    </select>
</mapper>