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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.njcn.event.pojo.po.RStatSubstationQ">
        <id column="data_date" property="dataDate" />
        <result column="substation_id" property="substationId" />
        <result column="effective_measurement_average" property="effectiveMeasurementAverage" />
        <result column="effective_measurement_accrued" property="effectiveMeasurementAccrued" />
        <result column="event_freq" property="harmonicAverageOverDay" />
        <result column="freq_average_over_day" property="freqAverageOverDay" />
        <result column="v_dev_average_over_day" property="vDevAverageOverDay" />
        <result column="v_average_over_day" property="vAverageOverDay" />
        <result column="i_average_over_day" property="iAverageOverDay" />
        <result column="unbalance_average_over_day" property="unbalanceAverageOverDay" />
        <result column="i_neg_average_over_day" property="iNegAverageOverDay" />
        <result column="flicker_average_over_day" property="flickerAverageOverDay" />
        <result column="inuharm_average_over_day" property="inuharmAverageOverDay" />
        <result column="event_count" property="eventCount" />
        <result column="event_freq" property="eventFreq" />
        <result column="sag_count" property="sagCount" />
        <result column="sag_freq" property="sagFreq" />
        <result column="swell_count" property="swellCount" />
        <result column="swell_freq" property="swellFreq" />
        <result column="interrupt_count" property="interruptCount" />
        <result column="interrupt_freq" property="interruptFreq" />
    </resultMap>
    <select id="getStatSubstationIconQ" resultType="com.njcn.event.pojo.vo.RSubstationIconVO">
        SELECT
            COALESCE( sum( CASE WHEN X.event_freq BETWEEN 0 AND 10 THEN 1 ELSE 0 END ), 0 ) "num_1",
            COALESCE ( sum( CASE WHEN X.event_freq BETWEEN 11 AND 20 THEN 1 ELSE 0 END ), 0 ) "num_2" ,
            COALESCE ( sum( CASE WHEN X.event_freq BETWEEN 21 AND 30 THEN 1 ELSE 0 END ), 0 ) "num_3" ,
            COALESCE ( sum( CASE WHEN X.event_freq BETWEEN 31 AND 40 THEN 1 ELSE 0 END ), 0 ) "num_4" ,
            COALESCE ( sum( CASE WHEN X.event_freq BETWEEN 41 AND 50 THEN 1 ELSE 0 END ), 0 ) "num_5" ,
            COALESCE ( sum( CASE WHEN X.event_freq BETWEEN 51 AND 60 THEN 1 ELSE 0 END ), 0 ) "num_6" ,
            COALESCE ( sum( CASE WHEN X.event_freq BETWEEN 61 AND 70 THEN 1 ELSE 0 END ), 0 ) "num_7" ,
            COALESCE ( sum( CASE WHEN X.event_freq BETWEEN 71 AND 80 THEN 1 ELSE 0 END ), 0 ) "num_8" ,
            COALESCE ( sum( CASE WHEN X.event_freq BETWEEN 81 AND 90 THEN 1 ELSE 0 END ), 0 ) "num_9" ,
            COALESCE ( sum( CASE WHEN X.event_freq BETWEEN 91 AND 100 THEN 1 ELSE 0 END ), 0 ) "num_10" ,
            COALESCE ( round(sum(CASE WHEN X.event_freq BETWEEN 0 AND 10 THEN 1 ELSE 0 END) / COUNT(X.substation_id),4)*100 , 0 )   "upRatio_1",
            COALESCE ( round(sum(CASE WHEN X.event_freq BETWEEN 11 AND 20 THEN 1 ELSE 0 END) / COUNT(X.substation_id),4)*100 , 0 )  "upRatio_2",
            COALESCE ( round(sum(CASE WHEN X.event_freq BETWEEN 21 AND 30  THEN 1 ELSE 0 END) / COUNT(X.substation_id),4)*100 , 0 ) "upRatio_3",
            COALESCE ( round(sum(CASE WHEN X.event_freq BETWEEN 31 AND 40  THEN 1 ELSE 0 END) / COUNT(X.substation_id),4)*100 , 0 ) "upRatio_4",
            COALESCE ( round(sum(CASE WHEN X.event_freq BETWEEN 41 AND 50  THEN 1 ELSE 0 END) / COUNT(X.substation_id),4)*100 , 0 ) "upRatio_5",
            COALESCE ( round(sum(CASE WHEN X.event_freq BETWEEN 51 AND 60  THEN 1 ELSE 0 END) / COUNT(X.substation_id),4)*100 , 0 ) "upRatio_6",
            COALESCE ( round(sum(CASE WHEN X.event_freq BETWEEN 61 AND 70  THEN 1 ELSE 0 END) / COUNT(X.substation_id),4)*100 , 0 ) "upRatio_7",
            COALESCE ( round(sum(CASE WHEN X.event_freq BETWEEN 71 AND 80  THEN 1 ELSE 0 END) / COUNT(X.substation_id),4)*100 , 0 ) "upRatio_8",
            COALESCE ( round(sum(CASE WHEN X.event_freq BETWEEN 81 AND 90  THEN 1 ELSE 0 END) / COUNT(X.substation_id),4)*100 , 0 ) "upRatio_9",
            COALESCE ( round(sum(CASE WHEN X.event_freq BETWEEN 91 AND 100 THEN 1 ELSE 0 END) / COUNT(X.substation_id),4)*100 , 0 ) "upRatio_10"
        FROM
        (
                    SELECT
                        substation_id,
                        event_freq
                    FROM
                        r_stat_substation_q
                <where>
                    <if test="param != null and  param.ids != null and param.ids.size > 0">
                        AND substation_id IN
                        <foreach collection='param.ids' item='item' index="index" open='(' separator=',' close=')'>
                            #{item}
                        </foreach>
                    </if>
                    <if test="param!=null and param.startTime != null and param.startTime !=''">
                        AND data_date >= #{param.startTime}
                    </if>
                    <if test="param!=null and param.endTime != null and param.endTime != ''">
                        AND #{param.endTime}>= data_date
                    </if>
                </where>

        ) X

    </select>
    <select id="getStatSubstationIcon2Q" resultType="com.njcn.event.pojo.vo.RSubstationIcon2VO">
        SELECT
            substation_id,
            interrupt_freq,
            swell_freq,
            sag_freq
        FROM
            r_stat_substation_q
        <where>
            <if test="param != null and  param.ids != null and param.ids.size > 0">
                AND substation_id IN
                <foreach collection='param.ids' item='item' index="index" open='(' separator=',' close=')'>
                    #{item}
                </foreach>
            </if>
            <if test="param!=null and param.startTime != null and param.startTime !=''">
                AND data_date >= #{param.startTime}
            </if>
            <if test="param!=null and param.endTime != null and param.endTime != ''">
                AND #{param.endTime}>= data_date
            </if>
        </where>
    </select>

</mapper>
