<?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.ConstructionQualityMapper">

    <!--施工质量-检测记录对比-->
    <select id="getByMonth" resultType="com.securitycontrol.entity.screen.vo.ConstructionQualityVo">
        SELECT
            COUNT(*) as detectionNumber,
            DATE_FORMAT(thdv.create_time,'%Y-%m') as month,
            SUM(CASE WHEN thdv.is_warn = 1 THEN 1 ELSE 0 END) as qualificationNumber
        FROM tb_his_device_value thdv
        LEFT JOIN tb_device_detail tdd ON thdv.attribute_id = tdd.id
        LEFT JOIN tb_bd_device tbd ON tdd.device_id = tbd.device_id
        LEFT JOIN tb_pro_bd tpb ON tbd.bd_id = tpb.id
        WHERE tpb.bid_code = #{bidCode} and DATE_FORMAT(thdv.create_time,'%Y-%m') = #{month}
        <choose>
            <when test="construction != null and construction != ''">
                AND tbd.device_code = #{construction}
            </when>
            <otherwise>
                AND tbd.device_code IN ('1906002', '1906003', '1906004')
            </otherwise>
        </choose>
        GROUP BY DATE_FORMAT(thdv.create_time,'%Y-%m')
    </select>
    <!--施工质量-告警内容-->
    <select id="getConstructionAlarm" resultType="com.securitycontrol.entity.screen.vo.ConstructionQualityVo">
        select   tbd.device_name deviceName,
        tw.warn_content AS warningContent,
        tw.warn_time warnTime
        FROM tb_warn tw
        LEFT JOIN tb_bd_device tbd ON tw.device_id = tbd.device_id
        where tw.bid_code = #{bidCode} and tbd.devic_type = 1906
            <if test="deviceId!=null and deviceId!=''">
                AND tbd.device_id=#{deviceId}
            </if>
    </select>

    <!--施工质量-质量检测设备-->
    <select id="getQualityInspection" resultType="com.securitycontrol.entity.screen.vo.ConstructionQualityVo">
        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 as status
        FROM
            tb_bd_device tbd
                LEFT JOIN tb_pro_bd tpb ON tpb.id = tbd.bd_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
            tbd.devic_type =1906 and  tpb.bid_code = #{bidCode}

    </select>
    <select id="getConstructionQualityList"
            resultType="com.securitycontrol.entity.screen.vo.ConstructionQualityVo">
        SELECT tbd.device_name deviceName,tdd.mode_name moduleName,
            concat(tdv.val,'(',tdv.unit,')') val,
            tdv.change_val changeVal,ta.area_name areaName,
                tpb.bd_name ,tdv.create_time createTime,tdv.is_warn isWarn
                FROM tb_bd_device tbd
                LEFT JOIN tb_area ta on tbd.area_id=ta.area_id
                left join tb_device_detail tdd on tbd.device_id=tdd.device_id
                left join  tb_device_value  tdv on tdd.id=tdv.attribute_id
                left join tb_pro_bd  tpb on tpb.id=tbd.bd_id
              WHERE  tpb.bid_code = #{bidCode}
                        and tbd.devic_type = 1906
                <if test="deviceId!=null and deviceId!=''">
                    AND tbd.device_id=#{deviceId}
                </if>
                <if test="moduleName!=null and moduleName!=''">
                    AND   tdd.mode_name like concat('%',#{moduleName},'%')
                </if>

    </select>
    <select id="getHistoryList" resultType="com.securitycontrol.entity.screen.vo.ConstructionQualityVo">
            SELECT tbd.device_name deviceName,tdd.mode_name moduleName,tdv.val,tdv.change_val,ta.area_name areaName,
            tpb.bd_name ,tdv.create_time createTime,tdv.is_warn isWarn,tdd.id,unit
            FROM tb_bd_device tbd
            LEFT JOIN tb_area ta on tbd.area_id=ta.area_id
            left join tb_device_detail tdd on tbd.device_id=tdd.device_id
            left join  tb_his_device_value  tdv on tdd.id=tdv.attribute_id
            left join tb_pro_bd  tpb on tpb.id=tbd.bd_id
            WHERE
             tbd.devic_type = 1906 and   tdv.val is not null  and  tpb.bid_code = #{bidCode}
                <if test="deviceName!=null and deviceName!=''">
                      AND tbd.device_name like concat('%',#{deviceName},'%')
                </if>
                <if test="times!=null and times!=''">
                    and    tdv.create_time   BETWEEN #{startTime} AND #{endTime}
                </if>
    </select>


</mapper>