<?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.GetDistortionDataMapper">
    <select id="getDistortionDataV" resultType="ReportValue">
    SELECT PHASIC_TYPE PHASETYPE,MAX(V_THD_MAX) FMAXVALUE,MIN(V_THD_MIN) MINVALUE,ROUND(AVG(V_THD) ,2)
    MEANVALUE,MAX(CASE WHEN VTHDCP95 =#{condition.count} THEN V_THD ELSE NULL END)
    CP95VALUE FROM ( SELECT T.*,ROW_NUMBER() OVER (PARTITION BY PHASIC_TYPE ORDER BY V_THD DESC) VTHDCP95
    from
    <choose>
        <when test="condition.b == true">
            Data_V
        </when>
        <otherwise>
            Day_V
        </otherwise>
    </choose>
    t where lineid=#{condition.pointIndex} and phasic_type in ('A','B','C') and timeid between
    #{condition.start} and #{condition.end}
    <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
    group by phasic_type
</select>


    <select id="getDistortionDataI" resultType="ReportValue">
        SELECT PHASIC_TYPE PHASETYPE,MAX(I_THD_MAX) FMAXVALUE,MIN(I_THD_MIN) MINVALUE,ROUND(AVG(I_THD) ,2)
        MEANVALUE,MAX(CASE WHEN ITHDCP95 =#{condition.count} THEN I_THD ELSE NULL END)
        CP95VALUE FROM ( SELECT T.*,ROW_NUMBER() OVER (PARTITION BY PHASIC_TYPE ORDER BY I_THD DESC) ITHDCP95
        from
        <choose>
            <when test="condition.b == true">
                Data_I
            </when>
            <otherwise>
                Day_I
            </otherwise>
        </choose>
        t where lineid=#{condition.pointIndex} and phasic_type in ('A','B','C') and timeid between
        #{condition.start} and #{condition.end}
        <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
        group by phasic_type
    </select>

    <select id="getHDistortionData" resultType="ReportValue">
        SELECT PHASIC_TYPE PHASETYPE,V_THD_MAX FMAXVALUE,V_THD_MIN MINVALUE,V_THD MEANVALUE,V_THD_CP95 CP95VALUE,TIMEID 
        from day_v where lineid=#{condition.pointIndex} and phasic_type in ('A','B','C') and timeid between
        #{condition.start} and #{condition.end}
    </select>
</mapper>