<?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.tofly.xrtymis.mapper.NoiseLoggerInfoMapper">

    <resultMap id="noiseLoggerInfoMap" type="com.tofly.xrtymis.entity.NoiseLoggerInfo">
        <id property="id" column="ID"/>
        <result property="mobileNumber" column="MOBILE_NUMBER"/>
        <result property="channelName" column="CHANNEL_NAME"/>
        <result property="monitoringState" column="MONITORING_STATE"/>
        <result property="disposeState" column="DISPOSE_STATE"/>
        <result property="lastValueDate" column="LAST_VALUE_DATE"/>
        <result property="synchronisedTime" column="SYNCHRONISED_TIME"/>
        <result property="numberCode" column="NUMBER_CODE"/>
        <result property="fileId" column="FILE_ID"/>
        <result property="gdinfoId" column="GDINFO_ID"/>
        <result property="listenNum" column="LISTEN_NUM"/>
        <association property="mobileNumberName" column="MOBILE_NUMBER" select="getMobileNumberName"/>
        <association property="address" column="MOBILE_NUMBER" select="getAddress"/>
        <association property="monitoringStateName" column="MONITORING_STATE" select="getMonitoringStateName"/>
        <association property="disposeStateName" column="DISPOSE_STATE" select="getDisposeStateName"/>
        <association property="filePath" column="FILE_ID" select="getFileIdsList"/>
    </resultMap>


    <select id="getFileIdsList" resultType="string" databaseId="oracle">
        select FILE_PATH as filePath
        from TF_YWPN_FILE_W
        <if test="fileIds != null and fileIds != ''  ">where id in
            <foreach item="nodeCodestr" collection="fileIds.split(',')" open="(" separator="," close=")">
                #{nodeCodestr}
            </foreach>
        </if>
        <if test="fileIdsOne == null ">
            where id = ''
        </if>

    </select>
    <select id="getMobileNumberName" resultType="string">
        select name as mobileNumberName
        from TF_DEVICE_ARCHIVE_MANAGE
        where code = #{mobileNumber}
    </select>
    <select id="getAddress" resultType="string">
        select address
        from TF_DEVICE_ARCHIVE_MANAGE
        where code = #{mobileNumber}
    </select>
    <select id="getMonitoringStateName" resultType="string">
        SELECT CNAME
        FROM TF_YWPN_SYSCHILDCODE_D
        WHERE PCID = 53200
          AND CCODE = #{monitoringState}
    </select>
    <select id="getDisposeStateName" resultType="string">
        SELECT CNAME
        FROM TF_YWPN_SYSCHILDCODE_D
        WHERE PCID = 53300
          AND CCODE = #{monitoringState}
    </select>

    <select id="getNoises" resultType="string">
        SELECT wm_concat(ID)
        FROM TF_NOISE_LOGGER_INFO_D
        WHERE GDINFO_ID = #{id}
    </select>

    <select id="getLeakDeviceArchivePage" resultType="com.tofly.xrtymis.custom.entity.LeakDeviceArchiveVo">
        SELECT Q1.CODE                                          deviceCode,
               Q1.name                                          deviceName,
               Q1.ADDRESS                                       address,
               Q1.CREATE_TIME                                   nowTime,
               Q1.LEADER                                        principalId,
               (select REAL_NAME createUserName
                from TF_SMPN_USER_X
                where id = Q1.LEADER)                           principalIdName,
               Q1.STATUS                                        deviceState,
               (SELECT CNAME
                FROM TF_YWPN_SYSCHILDCODE_D
                WHERE PCID = 53005
                  AND CCODE = Q1.STATUS)                        deviceStateName,
               Q1.WARNINGNUMBERS,
               Q1.LOSTTIME,
               decode(Q2.MONITORINGSTATE, '0', '正常', '1', '异常') monitoringState,
               q1.latitude,
               q1.longitude
        FROM (SELECT t2.CODE,
                     t2.name,
                     T2.ADDRESS,
                     T2.CREATE_TIME,
                     t2.LEADER,
                     t2.STATUS,
                     t2.LONGITUDE longitude,
                     t2.LATITUDE latitude,
                     count(CASE WHEN T1.MONITORING_STATE = '1' THEN 1 ELSE 0 END) warningNumbers,
                     max(t1.LAST_VALUE_DATE)                                      lostTime
              FROM TF_NOISE_LOGGER_INFO_D t1
                       LEFT JOIN TF_DEVICE_ARCHIVE_MANAGE t2
                                 on T1.MOBILE_NUMBER = t2.CODE
              WHERE T2.DEVICE_TYPE = '4'
                and T1.DISPOSE_STATE = '0'
              GROUP BY t2.CODE, t2.name, T2.ADDRESS, T2.CREATE_TIME, t2.LEADER, t2.STATUS,t2.LONGITUDE,t2.LATITUDE
            ) q1
                 LEFT JOIN

             (SELECT MOBILE_NUMBER, (CASE WHEN MONITORING_STATE = '1' THEN 1 ELSE 0 END) monitoringState
              FROM TF_NOISE_LOGGER_INFO_D
              WHERE id in (SELECT max(id)
                           FROM TF_NOISE_LOGGER_INFO_D
                           GROUP BY MOBILE_NUMBER)) q2
             on q1.CODE = q2.MOBILE_NUMBER
            where 1=1
        <if test="monitoringState != null and monitoringState.trim() != ''  ">
            and Q2.MONITORINGSTATE = #{monitoringState}
        </if>
        <if test="likeStr != null and likeStr.trim() != ''">
            and Q1.CODE || Q1.name like '%' || #{likeStr} || '%'
        </if>
    </select>
</mapper>
