<?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.pqs9000.mapper.report.GetThreephaseDataMapper">
    <select id="getThreephaseV" resultType="java.util.HashMap">
        SELECT MAX(V_UNBALANCE_MAX) V_UNBALANCE_MAX,MIN(V_UNBALANCE_MIN)
        V_UNBALANCE_MIN,ROUND(AVG(V_UNBALANCE) ,2) V_UNBALANCE,
        MAX(CASE WHEN VUNBALANCECP95 = #{condition.count}
        THEN V_UNBALANCE
        ELSE NULL END) V_UNBALANCE_CP95
        ,MAX(V_POS_MAX) V_POS_MAX,MIN(V_POS_MIN) V_POS_MIN,ROUND(AVG(V_POS) ,2)
        V_POS,
        MAX(CASE WHEN VPOSCP95 = #{condition.count}
        THEN V_POS
        ELSE NULL END) V_POS_CP95
        ,MAX(V_NEG_MAX) V_NEG_MAX,MIN(V_NEG_MIN) V_NEG_MIN,ROUND(AVG(V_NEG) ,2)
        V_NEG,
        MAX(CASE WHEN VNEGCP95 = #{condition.count}
        THEN V_NEG
        ELSE NULL END) V_NEG_CP95
        ,MAX(V_ZERO_MAX) V_ZERO_MAX,MIN(V_ZERO_MIN) V_ZERO_MIN,ROUND(AVG(V_ZERO) ,2)
        V_ZERO,
        MAX(CASE WHEN VZEROCP95 = #{condition.count}
        THEN V_ZERO
        ELSE NULL END) V_ZERO_CP95
        FROM (SELECT
        t.*,
        row_number() over (partition BY phasic_type ORDER BY V_Unbalance DESC) Vunbalancecp95,
        row_number() over (partition BY phasic_type ORDER BY V_Pos DESC) Vposcp95,
        row_number() over (partition BY phasic_type ORDER BY V_Neg DESC) Vnegcp95,
        row_number() over (partition BY phasic_type ORDER BY V_Zero DESC) Vzerocp95
        from
        <choose>
            <when test="condition.b == true">
                data_V
            </when>
            <otherwise>
                day_V
            </otherwise>
        </choose>
        t
        where lineid=#{condition.pointIndex} and timeid between
        #{condition.start} and #{condition.end} and phasic_type = 'T'
        <if test="condition.b == true">
            and QualityFlag = 0
        </if>
        <foreach collection="condition.exceptTime" index="index" item="item" open=" " separator=" " close="">
            and timeid not between #{item.start} and #{item.end}
        </foreach>) a
    </select>

    <select id="getThreephaseI" resultType="java.util.HashMap">
        SELECT MAX(I_UNBALANCE_MAX) I_UNBALANCE_MAX,MIN(I_UNBALANCE_MIN)
        I_UNBALANCE_MIN,ROUND(AVG(I_UNBALANCE) ,2) I_UNBALANCE,
        MAX(CASE WHEN IUNBALANCECP95 = #{condition.count}
        THEN I_UNBALANCE
        ELSE NULL END) I_UNBALANCE_CP95
        ,MAX(I_POS_MAX) I_POS_MAX,MIN(I_POS_MIN) I_POS_MIN,ROUND(AVG(I_POS) ,2)
        I_POS,
        MAX(CASE WHEN IPOSCP95 = #{condition.count}
        THEN I_POS
        ELSE NULL END) I_POS_CP95
        ,MAX(I_NEG_MAX) I_NEG_MAX,MIN(I_NEG_MIN) I_NEG_MIN,ROUND(AVG(I_NEG) ,2)
        I_NEG,
        MAX(CASE WHEN INEGCP95 = #{condition.count}
        THEN I_NEG
        ELSE NULL END) I_NEG_CP95
        ,MAX(I_ZERO_MAX) I_ZERO_MAX,MIN(I_ZERO_MIN) I_ZERO_MIN,ROUND(AVG(I_ZERO) ,2)
        I_ZERO,
        MAX(CASE WHEN IZEROCP95 = #{condition.count}
        THEN I_ZERO
        ELSE NULL END) I_ZERO_CP95
        FROM (SELECT
        t.*,
        row_number() over (partition BY phasic_type ORDER BY I_Unbalance DESC) Iunbalancecp95,
        row_number() over (partition BY phasic_type ORDER BY I_Pos DESC) Iposcp95,
        row_number() over (partition BY phasic_type ORDER BY I_Neg DESC) Inegcp95,
        row_number() over (partition BY phasic_type ORDER BY I_Zero DESC) Izerocp95
        from
        <choose>
            <when test="condition.b == true">
                data_I
            </when>
            <otherwise>
                day_I
            </otherwise>
        </choose>
        t
        where lineid=#{condition.pointIndex} and timeid between
        #{condition.start} and #{condition.end} and phasic_type = 'T'
        <if test="condition.b == true">
            and QualityFlag = 0
        </if>
        <foreach collection="condition.exceptTime" index="index" item="item" open=" " separator=" " close="">
            and timeid not between #{item.start} and #{item.end}
        </foreach>) a
    </select>

    <select id="getHThreephase" resultType="java.util.HashMap">
        SELECT V_UNBALANCE_MAX,V_UNBALANCE_MIN,TIMEID
        ,V_UNBALANCE,
        V_UNBALANCE_CP95
        FROM DAY_V
        where lineid=#{condition.pointIndex} and timeid between
        #{condition.start} and #{condition.end} and phasic_type = 'T'
    </select>
</mapper>