<?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.zehong.system.mapper.TDetectorReportDataMapper">
    
    <resultMap type="TDetectorReportData" id="TDetectorReportDataResult">
        <result property="id"    column="id"    />
        <result property="sourceId"    column="source_id"    />
        <result property="detectorCode"    column="detector_code"    />
        <result property="detectorStatusId"    column="detector_status_id"    />
        <result property="statusName"    column="status_name"    />
        <result property="reportValue"    column="report_value"    />
        <result property="zhHostStatusId"    column="zh_host_status_id"    />
        <result property="netPoint"    column="net_point"    />
        <result property="signalStrength"    column="signal_strength"    />
        <result property="ipAddress"    column="ip_address"    />
        <result property="createTime"    column="create_time"    />
        <result property="isCancelAlarm"    column="is_cancel_alarm"    />
        <result property="cancelTime"    column="cancel_time"    />
    </resultMap>

    <sql id="selectTDetectorReportDataVo">
        select id, source_id, detector_code, detector_status_id, status_name, report_value, zh_host_status_id, net_point, signal_strength, ip_address, create_time, is_cancel_alarm, cancel_time from t_detector_report_data
    </sql>

    <select id="selectTDetectorReportDataList" parameterType="TDetectorReportDataForm" resultType="TDetectorAlarmInfoVO">
    select * from(
        select rd.id,
               rd.cancel_time cancelTime,
               rd.detector_code detectorCode,
               CONCAT_WS("#",IFNULL(u.nick_name,IFNULL(di.linkman,di.detector_addr)),di.linkman) unitName,
               di.detector_type detectorType,
               rd.status_name statusName,
               rd.create_time alarmTime,
               rd.is_cancel_alarm handledStatus
        FROM t_detector_report_data rd
        LEFT JOIN t_detector_info di ON rd.detector_code = di.detector_code
        LEFT JOIN t_detector_user u ON u.user_id = di.user_id
        <where> u.is_del = '0' and di.is_del = '0'
            <if test="detectorCode != null  and detectorCode != ''"> and rd.detector_code = #{detectorCode}</if>
            <if test="statusName != null  and statusName != ''"> and rd.status_name = #{statusName}</if>
            <if test="createTimeStart != null "> and rd.create_time &gt;= #{createTimeStart}</if>
            <if test="createTimeEnd != null "> and rd.create_time &lt;= #{createTimeEnd}</if>
            <if test="isCancelAlarm != null  and isCancelAlarm != ''"> and is_cancel_alarm = #{isCancelAlarm}</if>
            <if test="cancelTimeStart != null "> and rd.cancel_time &gt;= #{cancelTimeStart}</if>
            <if test="cancelTimeEnd != null "> and rd.cancel_time &lt;= #{cancelTimeEnd}</if>
        </where>
        union
        SELECT rd.id,
               rd.cancel_time cancelTime,
               rd.detector_code detectorCode,
               CONCAT_WS("#",ti.device_name,ti.linkman) unitName,
               di.detector_type detectorType,
               rd.status_name statusName,
               rd.create_time alarmTime,
               rd.is_cancel_alarm handledStatus
        FROM t_detector_report_data rd
        LEFT JOIN t_detector_info di ON rd.detector_code = di.detector_code
        LEFT JOIN t_relation_device_detail_info ri ON rd.detector_code = ri.iot_no
        LEFT JOIN t_device_info ti ON ri.relation_device_id = ti.device_id
        <where> ri.relation_device_type = '1' and ri.is_del = '0' and ti.is_del = '0'
            <if test="detectorCode != null  and detectorCode != ''"> and rd.detector_code = #{detectorCode}</if>
            <if test="statusName != null  and statusName != ''"> and rd.status_name = #{statusName}</if>
            <if test="createTimeStart != null "> and rd.create_time &gt;= #{createTimeStart}</if>
            <if test="createTimeEnd != null "> and rd.create_time &lt;= #{createTimeEnd}</if>
            <if test="isCancelAlarm != null  and isCancelAlarm != ''"> and is_cancel_alarm = #{isCancelAlarm}</if>
            <if test="cancelTimeStart != null "> and rd.cancel_time &gt;= #{cancelTimeStart}</if>
            <if test="cancelTimeEnd != null "> and rd.cancel_time &lt;= #{cancelTimeEnd}</if>
        </where>
        union
        SELECT rd.id,
               rd.cancel_time cancelTime,
               rd.detector_code detectorCode,
               CONCAT_WS("#",si.site_station_name,si.build_unit) unitName,
               di.detector_type detectorType,
               rd.status_name statusName,
               rd.create_time alarmTime,
               rd.is_cancel_alarm handledStatus
        FROM t_detector_report_data rd
        LEFT JOIN t_detector_info di ON rd.detector_code = di.detector_code
        LEFT JOIN t_relation_device_detail_info ti ON rd.detector_code = ti.iot_no
        LEFT JOIN t_site_station_info si ON ti.relation_device_id = si.site_station_id
        <where> ti.relation_device_type = '2' and ti.is_del = '0' and si.is_del = '0'
            <if test="detectorCode != null  and detectorCode != ''"> and rd.detector_code = #{detectorCode}</if>
            <if test="statusName != null  and statusName != ''"> and rd.status_name = #{statusName}</if>
            <if test="createTimeStart != null "> and rd.create_time &gt;= #{createTimeStart}</if>
            <if test="createTimeEnd != null "> and rd.create_time &lt;= #{createTimeEnd}</if>
            <if test="isCancelAlarm != null  and isCancelAlarm != ''"> and is_cancel_alarm = #{isCancelAlarm}</if>
            <if test="cancelTimeStart != null "> and rd.cancel_time &gt;= #{cancelTimeStart}</if>
            <if test="cancelTimeEnd != null "> and rd.cancel_time &lt;= #{cancelTimeEnd}</if>
        </where>
        )t
        ORDER BY t.alarmTime DESC
    </select>
    
    <select id="selectTDetectorReportDataById" parameterType="Long" resultMap="TDetectorReportDataResult">
        <include refid="selectTDetectorReportDataVo"/>
        where id = #{id}
    </select>

    <select id="selectTDetectorReportDataByCode" parameterType="String" resultMap="TDetectorReportDataResult">
        <include refid="selectTDetectorReportDataVo"/>
        where detector_code = #{detectorCode}
        and is_cancel_alarm = '0'
        ORDER BY create_time DESC
    </select>

    <select id="selectTDetectorAlarm" resultType="TDetectorAlarmInfoVO">
    select * from(
        SELECT
            rd.detector_code detectorCode,
            CONCAT_WS("#",IFNULL(u.nick_name,IFNULL(di.linkman,di.detector_addr)),di.linkman) unitName,
            di.detector_type detectorType,
            rd.status_name statusName,
            rd.create_time alarmTime,
            IF (
                rd.is_cancel_alarm = 0,
                '未消除',
                '已消除'
            ) handledStatus
        FROM t_detector_report_data rd
        LEFT JOIN t_detector_info di ON rd.detector_code = di.detector_code
        LEFT JOIN t_detector_user u ON u.user_id = di.user_id
        WHERE u.is_del = '0' and di.is_del = '0' and rd.is_cancel_alarm = '0'
        union
		SELECT
            rd.detector_code detectorCode,
            CONCAT_WS("#",ti.device_name,ti.linkman) unitName,
            di.detector_type detectorType,
            rd.status_name statusName,
            rd.create_time alarmTime,
            IF (
                rd.is_cancel_alarm = 0,
                '未消除',
                '已消除'
            ) handledStatus
        FROM t_detector_report_data rd
        LEFT JOIN t_detector_info di ON rd.detector_code = di.detector_code
        LEFT JOIN t_relation_device_detail_info ri ON rd.detector_code = ri.iot_no
				LEFT JOIN t_device_info ti ON ri.relation_device_id = ti.device_id
        WHERE ri.relation_device_type = '1' and ri.is_del = '0' and ti.is_del = '0' and rd.is_cancel_alarm = '0'
        union
		SELECT
            rd.detector_code detectorCode,
            CONCAT_WS("#",si.site_station_name,si.build_unit) unitName,
            di.detector_type detectorType,
            rd.status_name statusName,
            rd.create_time alarmTime,
            IF (
                rd.is_cancel_alarm = 0,
                '未消除',
                '已消除'
            ) handledStatus
        FROM t_detector_report_data rd
        LEFT JOIN t_detector_info di ON rd.detector_code = di.detector_code
        LEFT JOIN t_relation_device_detail_info ti ON rd.detector_code = ti.iot_no
				LEFT JOIN t_site_station_info si ON ti.relation_device_id = si.site_station_id
        WHERE ti.relation_device_type = '2' and ti.is_del = '0' and si.is_del = '0' and rd.is_cancel_alarm = '0'
	)t
    ORDER BY t.alarmTime DESC LIMIT 50
    </select>


    <select id="selectRealtimeDataList" resultMap="TDetectorReportDataResult">
        SELECT * FROM
        (
            SELECT
            t.source_id,
            t.detector_code,
            t.detector_status_id,
            t.status_name,
            t.report_value,
            t.zh_host_status_id,
            t.net_point,
            t.signal_strength,
            t.ip_address,
            t.create_time,
            t.is_cancel_alarm,
            t.cancel_time,
            d.detector_id,
            d.detector_name,
            d.detector_type
            FROM
            t_detector_report_data t
            LEFT JOIN t_detector_info d ON t.detector_code = d.detector_code
            WHERE
            t.detector_status_id != '1'
            ORDER BY t.create_time DESC
        )data
        GROUP BY data.detector_id LIMIT 50
    </select>

    <select id="countDetectorByUserId" parameterType="TDetectorInfo" resultMap="TDetectorReportDataResult" >
        select r.id, r.source_id, r.detector_code, r.detector_status_id, r.is_cancel_alarm, r.cancel_time
        from t_detector_report_data r
        left join t_detector_info d on r.detector_code = d.detector_code
        where r.detector_status_id != '1' and d.user_id = #{userId} and d.is_del = #{isDel}
    </select>
        
    <insert id="insertTDetectorReportData" parameterType="TDetectorReportData" useGeneratedKeys="true" keyProperty="id">
        insert into t_detector_report_data
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="sourceId != null">source_id,</if>
            <if test="detectorCode != null and detectorCode != ''">detector_code,</if>
            <if test="detectorStatusId != null">detector_status_id,</if>
            <if test="statusName != null">status_name,</if>
            <if test="reportValue != null">report_value,</if>
            <if test="zhHostStatusId != null">zh_host_status_id,</if>
            <if test="netPoint != null">net_point,</if>
            <if test="signalStrength != null">signal_strength,</if>
            <if test="ipAddress != null">ip_address,</if>
            <if test="createTime != null">create_time,</if>
            <if test="isCancelAlarm != null">is_cancel_alarm,</if>
            <if test="cancelTime != null">cancel_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="sourceId != null">#{sourceId},</if>
            <if test="detectorCode != null and detectorCode != ''">#{detectorCode},</if>
            <if test="detectorStatusId != null">#{detectorStatusId},</if>
            <if test="statusName != null">#{statusName},</if>
            <if test="reportValue != null">#{reportValue},</if>
            <if test="zhHostStatusId != null">#{zhHostStatusId},</if>
            <if test="netPoint != null">#{netPoint},</if>
            <if test="signalStrength != null">#{signalStrength},</if>
            <if test="ipAddress != null">#{ipAddress},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="isCancelAlarm != null">#{isCancelAlarm},</if>
            <if test="cancelTime != null">#{cancelTime},</if>
         </trim>
    </insert>

    <update id="updateTDetectorReportData" parameterType="TDetectorReportData">
        update t_detector_report_data
        <trim prefix="SET" suffixOverrides=",">
            <if test="sourceId != null">source_id = #{sourceId},</if>
            <if test="detectorCode != null and detectorCode != ''">detector_code = #{detectorCode},</if>
            <if test="detectorStatusId != null">detector_status_id = #{detectorStatusId},</if>
            <if test="statusName != null">status_name = #{statusName},</if>
            <if test="reportValue != null">report_value = #{reportValue},</if>
            <if test="zhHostStatusId != null">zh_host_status_id = #{zhHostStatusId},</if>
            <if test="netPoint != null">net_point = #{netPoint},</if>
            <if test="signalStrength != null">signal_strength = #{signalStrength},</if>
            <if test="ipAddress != null">ip_address = #{ipAddress},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="isCancelAlarm != null">is_cancel_alarm = #{isCancelAlarm},</if>
            <if test="cancelTime != null">cancel_time = #{cancelTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTDetectorReportDataById" parameterType="Long">
        delete from t_detector_report_data where id = #{id}
    </delete>

    <delete id="deleteTDetectorReportDataByIds" parameterType="String">
        delete from t_detector_report_data where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <select id="selectAlarm" parameterType="integer" resultType="TDetectorAlarmInfoVO">
      SELECT d.*,u.nick_name as unitName  t_detector_report_data d
      LEFT JOIN t_detector_info i on i.detector_code = d.detector_code
      LEFT JOIN t_detector_user u on u.user_id = i.user_id
      WHERE d.id = #{id}
    </select>
</mapper>