<?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.prepare.harmonic.mapper.mysql.device.DeviceAbnormaStatisticsMapper">
    <insert id="insertTopMsg">
        INSERT INTO pqs_top_msg(
        `Time_Id`,
        `Dev_Id`,
        `Comout_Count`,
        `Comout_Desc`,
        `Integrity_Flag`,
        `Integrity_Value`,
        `Alarm_Count`,
        `Alarm_Desc`,
        `Flow_Flag`,
        `Flow_Value`,
        `OnlineRate_Flag`,
        `OnlineRate_Value`,
        `State`
        ) VALUES
        <foreach collection="list" separator="," item="item">
            (
            DATE_FORMAT(date_sub(now(), interval 1 day),'%Y-%m-%d'),
            #{item.devId},
            #{item.comOutCount},
            #{item.comOutDescription},
            #{item.integrityFlag},
            #{item.integrityValue},
            IFNULL(#{item.alarmCount},0),
            #{item.alarmDesc},
            #{item.flowFlag},
            #{item.flowValue},
            #{item.onlineRateFlag},
            #{item.onlineRateValue},
            1
            )
        </foreach>
    </insert>

    <select id="getLines" resultType="com.njcn.prepare.harmonic.pojo.mysql.po.line.AlarmPO">
        SELECT  line.Id AS id,
                device.Id AS deviceId,
                sdd.Algo_Describe AS level,
                DATE_FORMAT(pd.Update_Time, "%Y-%m-%d %H:%i:%s") AS updateTime
        FROM pq_line line
        INNER JOIN pq_line vol ON line.pid=vol.id
        INNER JOIN pq_line device ON vol.pid=device.id
        LEFT JOIN pq_device pd ON device.id=pd.id
        LEFT JOIN pq_line_detail pld ON pld.id=line.id
        LEFT JOIN sys_dict_data sdd ON sdd.Id = pld.Line_Grade
        WHERE
                pd.Dev_Model=1
        AND
                pd.Run_Flag=0
        AND
                pld.Line_Grade is NOT NULL
    </select>

    <select id="getIntegrityByLineIds" resultType="com.njcn.prepare.harmonic.pojo.mysql.po.line.AlarmPO">
       SELECT
            device.id AS deviceId,
            IFNULL(SUM(integrity.due_time),3.14159) AS due,
            IFNULL(SUM(integrity.real_time),0) AS `real`,
            CONVERT(SUM(integrity.real_time)*100/SUM(integrity.due_time),DECIMAL(15,2)) AS integrity
        FROM r_stat_integrity_d integrity
        INNER JOIN pq_line line ON integrity.line_index = line.id
        INNER JOIN pq_line vol ON line.pid=vol.id
        INNER JOIN pq_line device ON vol.pid=device.id
        WHERE integrity.line_index IN
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        AND
            integrity.time_id BETWEEN #{startTime} AND #{endTime}
        GROUP BY device.Id
    </select>

    <select id="getFlowByDevice" resultType="com.njcn.prepare.harmonic.pojo.mysql.po.line.AlarmPO">
        SELECT
            t.*,
            CONVERT((t.actualValue)/(t.flowMeal)*100,DECIMAL(15,2)) flowProportion
		FROM
            (
                SELECT
                        cmf.Dev_Id deviceId,
                        DATE_FORMAT(cmf.Time_Id,'%Y-%m') updateTime,
                        IFNULL(d.flow, (select flow from cld_flow_meal where type = 0 and flag = 1)) +  IFNULL(d1.flow, 0) flowMeal,
                        IFNULL(cmf.Actual_Value/1024/1024,0) actualValue
                FROM  cld_month_flow cmf
                LEFT JOIN cld_dev_meal c ON cmf.Dev_Id = c.Line_Id
                LEFT JOIN cld_flow_meal d ON c.Base_Meal_Id = d.id
                LEFT JOIN cld_flow_meal d1 ON c.Ream_Meal_Id = d1.id
                WHERE cmf.Dev_Id IN
                    <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                AND
                    DATE_FORMAT(cmf.Time_Id,'%Y-%m') = #{time}
            ) t
        ORDER BY flowProportion DESC
    </select>

    <select id="getAlarmByLines" resultType="com.njcn.prepare.harmonic.pojo.mysql.po.line.AlarmPO">
       (SELECT  GROUP_CONCAT(line.Id) AS Id,
                device.id AS deviceId,
                COUNT(ca.Id) AS alarmCount,
				GROUP_CONCAT(ca.Remark) AS alarmDesc
        FROM cld_alarm ca
        INNER JOIN  pq_line line ON ca.Line_Id = line.id
        INNER JOIN pq_line vol ON line.pid=vol.id
        INNER JOIN pq_line device ON vol.pid=device.id
        WHERE ca.Line_Id IN
        <foreach collection="monitorIds" index="index" item="item1" open="(" separator="," close=")">
            #{item1}
        </foreach>
        AND
            ca.Occurred_Time BETWEEN #{startTime} AND #{endTime}
        GROUP BY device.Id
        ORDER BY alarmCount DESC)
        UNION ALL
        (SELECT
                ca.Line_Id AS Id,
                ca.Line_Id AS deviceId,
                COUNT(ca.Id) AS alarmCount,
                GROUP_CONCAT(ca.Remark) AS alarmDesc
        FROM cld_alarm ca
        WHERE ca.Line_Id IN
        <foreach collection="devIds" index="index" item="item2" open="(" separator="," close=")">
            #{item2}
        </foreach>
        AND
        ca.Occurred_Time BETWEEN #{startTime} AND #{endTime}
        GROUP BY ca.Line_Id
        ORDER BY alarmCount DESC)
    </select>

    <select id="selectAlarmStrategy" resultType="com.njcn.prepare.harmonic.pojo.mysql.po.line.AlarmStrategyVO">
       SELECT
        A.`Id` id,
        B.`Name` name,
        A.`Integrity_Value` integrityValue,
        A.`Online_Value` onlineValue,
        A.`Offtime_Value` offTimeValue,
        A.`Warn_Value` warnValue,
        A.`Update_Time` updateTime,
        B.Algo_Describe algoDesc
        FROM
        pqs_alarm_strategy A
        LEFT JOIN sys_dict_data B ON A.Id = B.Id
        WHERE A.State = 1
    </select>

    <select id="getOnlineRateByDevice" resultType="com.njcn.prepare.harmonic.pojo.mysql.po.line.AlarmPO">
        SELECT
        rsod.dev_index deviceId,
        rsod.online_min/(rsod.online_min+rsod.offline_min) onlineRate
        FROM  r_stat_onlinerate_d rsod
        WHERE rsod.dev_index IN
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        AND
        rsod.time_id BETWEEN #{startTime} AND #{endTime}
    </select>

</mapper>