<?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.njcn.device.pq.mapper.TopMsgMapper">

    <select id="dailyDeviceAbnormalStatistics" resultType="com.njcn.device.pq.pojo.po.TopMsgPO">
        SELECT
                ptm.Time_Id AS time,
                IFNULL(COUNT(ptm.Dev_Id),3.14159)AS deviceAbnormalNum
        FROM pqs_top_msg ptm
        INNER JOIN pq_line device ON ptm.Dev_Id = device.Id
        INNER JOIN pq_line vol ON vol.pid = device.Id
        INNER JOIN pq_line line ON line.pid = vol.id
        INNER JOIN pq_line_detail pld ON pld.Id=line.Id
        WHERE
            ptm.State=1
        AND
            ptm.Time_Id between #{startTime} and #{endTime}
        <if test="lineGrade!=null and lineGrade!=''">
            AND pld.Line_Grade=#{lineGrade}
        </if>
        GROUP BY ptm.Time_Id
        ORDER BY ptm.Time_Id DESC
    </select>

    <select id="dailyDeviceAbnormal" resultType="com.njcn.device.pq.pojo.po.TopMsgPO">
        SELECT
            device.Id AS devId,
            city.Name AS cityName,
            sub.Name AS subName,
            device.Name AS deviceName,
            CAST(ptm.Integrity_Value as DECIMAL(13,2)) AS integrityValue,
            ptm.Integrity_Flag AS integrityFlag,
            ptm.Comout_Count AS comOutCount,
            ptm.Comout_Desc AS comOutDesc,
            ptm.Alarm_Count AS alarmCount,
            ptm.Alarm_Desc AS alarmDesc,
            CAST(ptm.Flow_Value as DECIMAL(13,2)) AS flowValue,
            ptm.OnlineRate_Flag AS onlineRateFlag,
            CAST(ptm.OnlineRate_Value as DECIMAL(13,2)) AS onlineRateValue
        FROM pq_line line
        INNER JOIN pq_line vol ON line.pid=vol.id
        INNER JOIN pq_line device ON vol.pid=device.id
        INNER JOIN pq_line sub ON device.pid=sub.id
        INNER JOIN pq_line city ON sub.pid=city.id
        INNER JOIN pqs_top_msg ptm ON ptm.Dev_Id = device.Id
        LEFT JOIN pq_line_detail pld ON pld.Id=line.Id
        WHERE
            ptm.State=1
        AND
            ptm.Time_Id = #{startTime}
        <if test="lineGrade!=null and lineGrade!=''">
            AND pld.Line_Grade=#{lineGrade}
        </if>
    </select>
</mapper>