<?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.securitycontrol.screen.mapper.TowerAssInspectMapper">

    <select id="getDeviceList" resultType="com.securitycontrol.entity.screen.vo.TowerAssInspectVo">
        SELECT tbd.device_id as deviceId,tbd.device_name as deviceName,
               CASE
                   WHEN tbd.`status` = '1' AND tdv.is_warn = '1' THEN '2'
                   WHEN tbd.`status` = '1' AND tdv.is_warn = '0' THEN '1'
                   WHEN tbd.`status` = '0' THEN '0'
                   END type
        FROM t_pro_gt tpg
         LEFT JOIN tb_bd_device tbd ON tpg.gt_id = tbd.gt_id
         LEFT JOIN tb_device_detail tdd ON  tdd.device_id = tbd.device_id
         LEFT JOIN tb_device_value tdv ON tdd.id = tdv.attribute_id
        WHERE tpg.gt_id = #{gtId}
    </select>

    <select id="getRealTimeDetection" resultType="com.securitycontrol.entity.screen.vo.TowerAssInspectVo">
        SELECT CASE WHEN tdd.data_type = '3331' THEN '1'
                WHEN tdd.data_type = '3332' THEN '2'
                WHEN tdd.data_type = '3333' THEN '3'
                END dataType,
            tdv.val AS val
        FROM
            tb_device_detail tdd
            LEFT JOIN tb_device_value tdv ON tdd.id = tdv.attribute_id
        WHERE device_id = #{deviceId}
    </select>

    <select id="getTrend" resultType="com.securitycontrol.entity.screen.vo.TowerAssInspectVo">
        SELECT thdv.create_time as time, thdv.change_val as changeVal FROM tb_device_detail tdd
            LEFT JOIN tb_his_device_value thdv ON tdd.id = thdv.attribute_id
        WHERE tdd.device_id = #{deviceId}
        ORDER BY thdv.create_time DESC
    </select>

    <select id="getRealTimeAlarmList" resultType="com.securitycontrol.entity.screen.vo.TowerAssInspectVo">
        SELECT tw.warn_content as warnContent,tw.warn_time as warnTime FROM tb_warn tw
        WHERE tw.device_id = #{deviceId}
    </select>

    <select id="getHistoryList" resultType="com.securitycontrol.entity.screen.vo.TowerAssInspectVo">
        SELECT ta.area_name as areaName,tdd.mode_name as modeName, thdv.val ,
               tdd.change_val as thisChangeVal, thdv.change_val as changeVal,tdd.update_time as updateTime,
               case when thdv.is_warn = '0' then '未告警' else '告警' end isWarn
        FROM tb_device_detail tdd
                 LEFT JOIN tb_bd_device tbd ON tdd.device_id = tbd.device_id
                 LEFT JOIN tb_area ta ON ta.area_id = tbd.area_id
                 LEFT JOIN tb_his_device_value thdv ON tdd.id = thdv.attribute_id
        WHERE tdd.device_id = #{deviceId} AND tdd.del_flag = '0'
    </select>

</mapper>