<?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.harmonic.mapper.majornetwork.RStatSubstationMMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.njcn.harmonic.pojo.po.RStatSubstationM">
        <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="harmonic_over_day" property="harmonicOverDay" />
        <result column="harmonic_count" property="harmonicCount" />
        <result column="freq_over_day" property="freqOverDay" />
        <result column="freq_count" property="freqCount" />
        <result column="v_dev_over_day" property="vDevOverDay" />
        <result column="v_dev_count" property="vDevCount" />
        <result column="v_over_day" property="vOverDay" />
        <result column="v_count" property="vCount" />
        <result column="i_over_day" property="iOverDay" />
        <result column="i_count" property="iCount" />
        <result column="unbalance_over_day" property="unbalanceOverDay" />
        <result column="unbalance_count" property="unbalanceCount" />
        <result column="i_neg_over_day" property="iNegOverDay" />
        <result column="i_neg_count" property="iNegCount" />
        <result column="flicker_over_day" property="flickerOverDay" />
        <result column="flicker_count" property="flickerCount" />
        <result column="inuharm_over_day" property="inuharmOverDay" />
        <result column="inuharm_count" property="inuharmCount" />
        <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="getStatSubstationIconM" resultType="com.njcn.harmonic.pojo.vo.RSubstationIconVO">
        SELECT
            COALESCE( sum( CASE WHEN X.harmonic_over_day BETWEEN 0 AND 10 THEN 1 ELSE 0 END ), 0 ) "num_1",
            COALESCE ( sum( CASE WHEN X.harmonic_over_day BETWEEN 11 AND 20 THEN 1 ELSE 0 END ), 0 ) "num_2" ,
            COALESCE ( sum( CASE WHEN X.harmonic_over_day BETWEEN 21 AND 30 THEN 1 ELSE 0 END ), 0 ) "num_3" ,
            COALESCE ( sum( CASE WHEN X.harmonic_over_day BETWEEN 31 AND 40 THEN 1 ELSE 0 END ), 0 ) "num_4" ,
            COALESCE ( sum( CASE WHEN X.harmonic_over_day BETWEEN 41 AND 50 THEN 1 ELSE 0 END ), 0 ) "num_5" ,
            COALESCE ( sum( CASE WHEN X.harmonic_over_day BETWEEN 51 AND 60 THEN 1 ELSE 0 END ), 0 ) "num_6" ,
            COALESCE ( sum( CASE WHEN X.harmonic_over_day BETWEEN 61 AND 70 THEN 1 ELSE 0 END ), 0 ) "num_7" ,
            COALESCE ( sum( CASE WHEN X.harmonic_over_day BETWEEN 71 AND 80 THEN 1 ELSE 0 END ), 0 ) "num_8" ,
            COALESCE ( sum( CASE WHEN X.harmonic_over_day BETWEEN 81 AND 90 THEN 1 ELSE 0 END ), 0 ) "num_9" ,
            COALESCE ( sum( CASE WHEN X.harmonic_over_day BETWEEN 91 AND 100 THEN 1 ELSE 0 END ), 0 ) "num_10" ,
            COALESCE ( round(sum(CASE WHEN X.harmonic_over_day BETWEEN 0 AND 10 THEN 1 ELSE 0 END) / COUNT(X.substation_id),4)*100 , 0 )   "upRatio_1",
            COALESCE ( round(sum(CASE WHEN X.harmonic_over_day BETWEEN 11 AND 20 THEN 1 ELSE 0 END) / COUNT(X.substation_id),4)*100 , 0 )  "upRatio_2",
            COALESCE ( round(sum(CASE WHEN X.harmonic_over_day BETWEEN 21 AND 30  THEN 1 ELSE 0 END) / COUNT(X.substation_id),4)*100 , 0 ) "upRatio_3",
            COALESCE ( round(sum(CASE WHEN X.harmonic_over_day BETWEEN 31 AND 40  THEN 1 ELSE 0 END) / COUNT(X.substation_id),4)*100 , 0 ) "upRatio_4",
            COALESCE ( round(sum(CASE WHEN X.harmonic_over_day BETWEEN 41 AND 50  THEN 1 ELSE 0 END) / COUNT(X.substation_id),4)*100 , 0 ) "upRatio_5",
            COALESCE ( round(sum(CASE WHEN X.harmonic_over_day BETWEEN 51 AND 60  THEN 1 ELSE 0 END) / COUNT(X.substation_id),4)*100 , 0 ) "upRatio_6",
            COALESCE ( round(sum(CASE WHEN X.harmonic_over_day BETWEEN 61 AND 70  THEN 1 ELSE 0 END) / COUNT(X.substation_id),4)*100 , 0 ) "upRatio_7",
            COALESCE ( round(sum(CASE WHEN X.harmonic_over_day BETWEEN 71 AND 80  THEN 1 ELSE 0 END) / COUNT(X.substation_id),4)*100 , 0 ) "upRatio_8",
            COALESCE ( round(sum(CASE WHEN X.harmonic_over_day BETWEEN 81 AND 90  THEN 1 ELSE 0 END) / COUNT(X.substation_id),4)*100 , 0 ) "upRatio_9",
            COALESCE ( round(sum(CASE WHEN X.harmonic_over_day BETWEEN 91 AND 100 THEN 1 ELSE 0 END) / COUNT(X.substation_id),4)*100 , 0 ) "upRatio_10"
        FROM
            (
                SELECT
                        substation_id,
                        harmonic_over_day
                FROM
                        r_stat_substation_m
                        <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="getStatSubstationIcon2M" resultType="com.njcn.harmonic.pojo.vo.RSubstationIcon2VO">
        SELECT
            substation_id,
            effective_measurement_accrued,
            ROUND(freq_over_day / freq_count,2) AS 'freqAverageOverDay',
            ROUND(v_dev_over_day / v_dev_count,2) AS 'vDevAverageOverDay',
            ROUND(v_over_day / v_count,2) AS 'vAverageOverDay',
            ROUND(i_over_day / i_count,2) AS 'iAverageOverDay',
            ROUND(unbalance_over_day / unbalance_count,2) AS 'unbalanceAverageOverDay',
            ROUND(i_neg_over_day / i_neg_count,2) AS 'iNegAverageOverDay',
            ROUND(flicker_over_day / flicker_count,2) AS 'flickerAverageOverDay',
            ROUND(inuharm_over_day / inuharm_count,2) AS 'inuharmAverageOverDay'
        FROM
            r_stat_substation_m
        <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>
