<?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.event.mapper.majornetwork.LargeScreenMapper">

    <select id="selectDownCount" resultType="java.util.Map">
        select
        count(ed.event_id) "count"
        from r_mp_event_detail ed
        where
        ed.measurement_point_id in
        <foreach collection="lineIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        <if test="startTime != null and startTime != ''">
            and date_format(ed.start_time,'%y%m%d') &gt;= date_format(#{startTime},'%y%m%d')
        </if>
        <if test="endTime != null and endTime != ''">
            and date_format(ed.start_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
        </if>
    </select>


    <select id="selectLoadTypeCount" resultType="java.util.Map">
        select count(ed.event_id) "count",
        ed.measurement_point_id "id"
        from r_mp_event_detail ed
        where
        ed.measurement_point_id in
        <foreach collection="lineIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        <if test="startTime != null and startTime != ''">
            and date_format(ed.start_time,'%y%m%d') &gt;= date_format(#{startTime},'%y%m%d')
        </if>
        <if test="endTime != null and endTime != ''">
            and date_format(ed.start_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
        </if>
        group by ed.measurement_point_id
    </select>

    <select id="getTimeCount" resultType="java.util.Map">
        select
        date(ed.start_time) "day", count(*) "count"
        from r_mp_event_detail ed where ed.measurement_point_id in
        <foreach collection="lineIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        <if test="startTime != null and startTime != ''">
            and date_format(ed.start_time,'%y%m%d') &gt;= date_format(#{startTime},'%y%m%d')
        </if>
        <if test="endTime != null and endTime != ''">
            and date_format(ed.start_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
        </if>
        group by day
    </select>



    <select id="getFlagCount" resultType="com.njcn.device.pq.pojo.vo.AllDataVO">
            select
            count(case when pd.Run_Flag	= 0 then 1 end)  running,
            count(case when pd.Run_Flag	= 1 then 1 end)  hotSpare,
            count(case when pd.Run_Flag	= 2 then 1 end)  shutdown,
            count(case when pd.Com_Flag	= 0 then 1 end)  breakdown,
            count(case when pd.Com_Flag	= 1 then 1 end)  normal
            from pq_device pd
            where pd.id in
            <foreach collection="lineIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
    </select>

    <select id="getDownCount" resultType="java.util.Map">
        select
        count(ed.event_id) "count"
        from r_mp_event_detail ed
        left join sys_dict_data sdd on ed.event_type = sdd.id and sdd.code = 'Voltage_Dip'
        where
        ed.measurement_point_id in
        <foreach collection="lineIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        <if test="startTime != null and startTime != ''">
            and date_format(ed.start_time,'%y%m%d') &gt;= date_format(#{startTime},'%y%m%d')
        </if>
        <if test="endTime != null and endTime != ''">
            and date_format(ed.start_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
        </if>
    </select>

    <select id="getMiddleDown" resultType="com.njcn.device.pq.pojo.vo.EventVO">
        SELECT
        ed.start_time  "time",
        pl.`Name` "name",
        ed.advance_reason reason,
        ed.advance_type "type",
        ed.feature_amplitude amplitude,
        ed.duration
        from r_mp_event_detail ed
        left join pq_line pl on pl.id = ed.measurement_point_id
        where
        ed.measurement_point_id in
        <foreach collection="lineIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        <if test="startTime != null and startTime != ''">
            and date_format(ed.start_time,'%y%m%d') &gt;= date_format(#{startTime},'%y%m%d')
        </if>
        <if test="endTime != null and endTime != ''">
            and date_format(ed.start_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
        </if>
        order by ed.start_time desc
    </select>

    <select id="getMiddleTerminal" resultType="com.njcn.device.pq.pojo.vo.MiddleChildVO">
        SELECT
        pd.id,
        IFNULL(
        cfm1.flow,(
        SELECT
        cfm.flow
        FROM
        cld_flow_meal cfm
        WHERE
        cfm.type = 0
        AND cfm.flag = 1
        )) base,
        IFNULL( cfm2.flow, 0 ) ream,
        IFNULL( cmf.Actual_Value, 0 ) statusValue,
        pl1.`Name` "name",
        pd.IP ip,
        pd.`Port` "port",
        case
        when pd.Com_Flag = '0' then '通讯中断'
        when pd.Com_Flag = '1' then '通讯正常'
        end comFlag
        FROM
        pq_line pl
        LEFT JOIN pq_device pd ON pl.id = pd.id
        LEFT JOIN cld_dev_meal cdm ON cdm.Line_Id = pd.id
        LEFT JOIN cld_flow_meal cfm1 ON cfm1.Id = cdm.Base_Meal_Id
        LEFT JOIN cld_flow_meal cfm2 ON cfm2.Id = cdm.Ream_Meal_Id
        LEFT JOIN cld_month_flow cmf ON cmf.Dev_Id = pd.id
        LEFT JOIN pq_line pl1 ON pl1.id = pl.pid
        WHERE
        pd.id IN
        <foreach collection="deviceIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        <if test="startTime != null and startTime != ''">
            and date_format(cmf.Time_Id,'%y%m%d') &gt;= date_format(#{startTime},'%y%m%d')
        </if>
        <if test="endTime != null and endTime != ''">
            and date_format(cmf.Time_Id,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
        </if>
        and pd.Com_Flag = '0'
    </select>

    <select id="getMiddleLimitRate" resultType="com.njcn.device.pq.pojo.vo.MiddleLimitRateVO">
            select pl.`Name` mName,
            pl3.`Name` subName,
            t.count from pq_line pl
            inner join(select rslr.my_index,
            sum(rslr.all_time)  count
            from r_stat_limit_rate_d rslr
            WHERE
            rslr.my_index IN
            <foreach collection="lineIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
            <if test="startTime != null and startTime != ''">
                and date_format(rslr.Time_Id,'%y%m%d') &gt;= date_format(#{startTime},'%y%m%d')
            </if>
            <if test="endTime != null and endTime != ''">
                and date_format(rslr.Time_Id,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
            </if>
            group by rslr.my_index)t on pl.Id = t.my_index

            left join pq_line pl1 on pl.Pid = pl1.Id
            left join pq_line pl2 on pl1.Pid = pl2.Id
            left join pq_line pl3 on pl2.Pid = pl3.Id
            where t.count != 0
    </select>

    <select id="getHomeostasisArea" resultType="java.util.Map">
        select
        rshom.org_no "id",
        round(avg(rshom.over_limit_measurement_ratio_accrued),2) avg
        from r_stat_harmonic_org_m rshom
        left join sys_dict_data sdd on rshom.data_type = sdd.Id and sdd.`Code` = 'Mainnet_Point'
        WHERE
        rshom.org_no IN
        <foreach collection="indexIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        <if test="startTime != null and startTime != ''">
            and date_format(rshom.data_date,'%y%m%d') &gt;= date_format(#{startTime},'%y%m%d')
        </if>
        <if test="endTime != null and endTime != ''">
            and date_format(rshom.data_date,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
        </if>
        group by rshom.org_no
    </select>

    <select id="getAvgCount" resultType="com.njcn.harmonic.pojo.po.PQSComAssesPO">
        SELECT
        line_id,
        AVG ( vu_dev1 ) AS vu_dev1,
        AVG ( vu_dev2 ) AS vu_dev2,
        AVG ( vu_dev3 ) AS vu_dev3,
        AVG ( vu_dev4 ) AS vu_dev4,
        AVG ( vu_dev5 ) AS vu_dev5,
        AVG ( freq_dev1 ) AS freq_dev1,
        AVG ( freq_dev2 ) AS freq_dev2,
        AVG ( freq_dev3 ) AS freq_dev3,
        AVG ( freq_dev4 ) AS freq_dev4,
        AVG ( freq_dev5 ) AS freq_dev5,
        AVG ( data_plt1 ) AS data_plt1,
        AVG ( data_plt2 ) AS data_plt2,
        AVG ( data_plt3 ) AS data_plt3,
        AVG ( data_plt4 ) AS data_plt4,
        AVG ( data_plt5 ) AS data_plt5,
        AVG ( v_unbalance1 ) AS v_unbalance1,
        AVG ( v_unbalance2 ) AS v_unbalance2,
        AVG ( v_unbalance3 ) AS v_unbalance3,
        AVG ( v_unbalance4 ) AS v_unbalance4,
        AVG ( v_unbalance5 ) AS v_unbalance5,
        AVG ( v_thd1 ) AS v_thd1,
        AVG ( v_thd2 ) AS v_thd2,
        AVG ( v_thd3 ) AS v_thd3,
        AVG ( v_thd4 ) AS v_thd4,
        AVG ( v_thd5 ) AS v_thd5,
        AVG ( event1 ) AS event1,
        AVG ( event2 ) AS event2,
        AVG ( event3 ) AS event3,
        AVG ( event4 ) AS event4,
        AVG ( event5 ) AS event5
        FROM
        r_stat_comasses_d
        <where>
            <if test=" ids != null and ids.size > 0">
                AND line_id IN
                <foreach collection='ids' item='item' index="index" open='(' separator=',' close=')'>
                    #{item}
                </foreach>
            </if>
            <if test=" statTime != null and statTime !=''">
                AND  time_id >= #{statTime}
            </if>
            <if test="endTime != null and endTime != ''">
                AND  time_id &lt;= #{endTime}
            </if>
        </where>
        GROUP BY line_id

    </select>

    <select id="getMiddleDownChind" resultType="com.njcn.device.pq.pojo.vo.EventVO">
        SELECT
        ed.start_time  "time",
               pl.`Name` "name",
               ed.advance_reason reason,
               ed.advance_type "type",
               ed.feature_amplitude amplitude,
               ed.duration
        from r_mp_event_detail ed
        left join pq_line pl on pl.id = ed.measurement_point_id
        where
        ed.measurement_point_id in
        <foreach collection="lineIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        <if test="startTime != null and startTime != ''">
            and date_format(ed.start_time,'%y%m%d') &gt;= date_format(#{startTime},'%y%m%d')
        </if>
        <if test="endTime != null and endTime != ''">
            and date_format(ed.start_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
        </if>
        order by ed.start_time desc
    </select>

    <select id="getMiddleLimitRateChild" resultType="com.njcn.device.pq.pojo.vo.MiddleLimitRateVO">
        select pl.`Name` mName,
        pl3.`Name` subName,
        t.count from pq_line pl
        inner join(select rslr.my_index,
        sum(rslr.all_time)  count
        from r_stat_limit_rate_d rslr
        WHERE
        rslr.my_index IN
        <foreach collection="lineIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        <if test="startTime != null and startTime != ''">
            and date_format(rslr.Time_Id,'%y%m%d') &gt;= date_format(#{startTime},'%y%m%d')
        </if>
        <if test="endTime != null and endTime != ''">
            and date_format(rslr.Time_Id,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
        </if>
        group by rslr.my_index)t on pl.Id = t.my_index

        left join pq_line pl1 on pl.Pid = pl1.Id
        left join pq_line pl2 on pl1.Pid = pl2.Id
        left join pq_line pl3 on pl2.Pid = pl3.Id
    </select>

    <select id="getMiddleTerminalChild" resultType="com.njcn.device.pq.pojo.vo.MiddleChildVO">
        SELECT
        pd.id,
        IFNULL(
        cfm1.flow,(
        SELECT
        cfm.flow
        FROM
        cld_flow_meal cfm
        WHERE
        cfm.type = 0
        AND cfm.flag = 1
        )) base,
        IFNULL( cfm2.flow, 0 ) ream,
        IFNULL( cmf.Actual_Value, 0 ) statusValue,
        pl1.`Name` "name",
        pd.IP ip,
        pd.`Port` "port",
        case
        when pd.Com_Flag = '0' then '通讯中断'
        when pd.Com_Flag = '1' then '通讯正常'
        end comFlag
        FROM
        pq_line pl
        LEFT JOIN pq_device pd ON pl.id = pd.id
        LEFT JOIN cld_dev_meal cdm ON cdm.Line_Id = pd.id
        LEFT JOIN cld_flow_meal cfm1 ON cfm1.Id = cdm.Base_Meal_Id
        LEFT JOIN cld_flow_meal cfm2 ON cfm2.Id = cdm.Ream_Meal_Id
        LEFT JOIN cld_month_flow cmf ON cmf.Dev_Id = pd.id
        LEFT JOIN pq_line pl1 ON pl1.id = pl.pid
        WHERE
        pd.id IN
        <foreach collection="deviceIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        <if test="startTime != null and startTime != ''">
            and date_format(cmf.Time_Id,'%y%m%d') &gt;= date_format(#{startTime},'%y%m%d')
        </if>
        <if test="endTime != null and endTime != ''">
            and date_format(cmf.Time_Id,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
        </if>
    </select>

    <select id="eventLists" resultType="com.njcn.device.pq.pojo.vo.EventVO">
            SELECT
            ed.event_id as eventId, ed.start_time  "time",pl.`Name` "name",ed.advance_reason reason,ed.advance_type "type",ed.feature_amplitude amplitude,ed.duration
            from pq_line pl
            left join r_mp_event_detail ed on pl.id = ed.measurement_point_id
            <where>
                ed.measurement_point_id in
                <foreach collection="lineIds" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
                <if test="startTime != null and startTime != ''">
                    and date_format(ed.start_time,'%y%m%d') &gt;= date_format(#{startTime},'%y%m%d')
                </if>
                <if test="endTime != null and endTime != ''">
                    and date_format(ed.start_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
                </if>
                    and ed.event_type = #{typeId}
            </where>
            order by time desc
            limit 50
    </select>

</mapper>
