<?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 *,case  WHEN aa.`status` = '0' THEN '0' when aa.is_warn like '%1%' then '2' else '1' end  type from (
        SELECT tbd.device_id as deviceId,tbd.device_name as deviceName,
          GROUP_CONCAT(tdv.is_warn ) as is_warn,tbd.`status`
        FROM tb_pro_bd tpb
            LEFT join tb_bd_device tbd on tbd.bd_id=tpb.id and tbd.del_flag=0
            left join tb_device_detail tdd on tdd.device_id=tbd.device_id and tdd.del_flag=0
            LEFT JOIN t_pro_gt tpg ON tpb.gt_id = tpg.gt_id
            LEFT JOIN tb_device_value tdv ON tdd.id = tdv.attribute_id
        WHERE tpg.gt_id = #{gtId} and tbd.devic_type = '1901'
        group by tpb.id,tbd.device_id
        ) aa
    </select>

    <select id="getRealTimeDetection" resultType="com.securitycontrol.entity.screen.vo.TowerAssInspectVo">
        SELECT tdd.id as id,  tdd.mode_name as modeName , 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} and tdd.del_flag = '0'
    </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} AND  tdd.id = #{id}  and tdd.del_flag = '0'
        ORDER BY thdv.create_time ASC
    </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 ,
        tbd.device_name deviceName,
                thdv.change_val as changeVal,tdd.update_time as updateTime,
                thdv.create_time createTime,
               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'
        AND    thdv.val IS NOT NULL
    </select>

</mapper>