<?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.NormLimitMapper">
    <select id="getDyPc" resultType="HistoryHarmOverLimitVO">
        SELECT
        ab.Id lineId,
        sub.NAME subName,
        line.NAME lineName,
        date_format( ab.Time_Id, '%Y-%m-%d' ) timeId,
        IF
        (
        detail.PT_Type IN ( 1, 2 ),
        IF (ab.Phasic_Type = 'A','AB',IF(ab.Phasic_Type = 'B','BC','CA')),ab.Phasic_Type
        ) phaseType,
        dic.NAME scale,
        sum( CASE WHEN Value_Type = 'AVG' THEN Voltage_Dev ELSE 0 END ) avgData,
        sum( CASE WHEN Value_Type = 'MAX' THEN Voltage_Dev ELSE 0 END ) maxData,
        sum( CASE WHEN Value_Type = 'MIN' THEN Voltage_Dev ELSE 0 END ) minData,
        sum( CASE WHEN Value_Type = 'CP95' THEN Voltage_Dev ELSE 0 END ) cp95Data
        FROM
        ( SELECT * FROM r_mp_surplus_abnormal_d WHERE Id IN
        <foreach collection="lineIndex" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        ) ab
        LEFT JOIN pq_line line ON line.Id = ab.Id
        LEFT JOIN pq_voltage vo ON vo.Id = line.Pid
        LEFT JOIN sys_dict_data dic ON dic.Id = vo.scale
        LEFT JOIN pq_line subv ON subv.Id = vo.Id
        LEFT JOIN pq_line dev ON dev.Id = subv.Pid
        LEFT JOIN pq_line sub ON sub.Id = dev.Pid
        LEFT JOIN pq_line_detail detail ON detail.Id = line.Id
        WHERE
        ab.phasic_type!='T'
        AND ab.Time_Id between #{startTime} and #{endTime}
        GROUP BY
        lineId,
        timeid,
        phaseType
        ORDER BY
        timeid
    </select>

    <select id="getSxBpHd" resultType="HistoryHarmOverLimitVO">
        SELECT
        ab.Id lineId,
        sub.NAME subName,
        line.NAME lineName,
        date_format( ab.Time_Id, '%Y-%m-%d' ) timeId,
        "/" phaseType,
        dic.NAME scale,
        sum( CASE WHEN Value_Type = 'AVG' THEN Ubalance ELSE 0 END ) avgData,
        sum( CASE WHEN Value_Type = 'MAX' THEN Ubalance ELSE 0 END ) maxData,
        sum( CASE WHEN Value_Type = 'MIN' THEN Ubalance ELSE 0 END ) minData,
        sum( CASE WHEN Value_Type = 'CP95' THEN Ubalance ELSE 0 END ) cp95Data
        FROM
        ( SELECT * FROM r_mp_surplus_abnormal_d WHERE Id IN
        <foreach collection="lineIndex" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        ) ab
        LEFT JOIN pq_line line ON line.Id = ab.Id
        LEFT JOIN pq_voltage vo ON vo.Id = line.Pid
        LEFT JOIN sys_dict_data dic ON dic.Id = vo.scale
        LEFT JOIN pq_line subv ON subv.Id = vo.Id
        LEFT JOIN pq_line dev ON dev.Id = subv.Pid
        LEFT JOIN pq_line sub ON sub.Id = dev.Pid
        LEFT JOIN pq_line_detail detail ON detail.Id = line.Id
        WHERE
        ab.phasic_type ='T'
        AND ab.Time_Id between #{startTime} and #{endTime}
        GROUP BY
        lineId,
        timeid,
        phaseType
        ORDER BY
        timeid
    </select>

    <select id="getDyZjBl" resultType="HistoryHarmOverLimitVO">
        SELECT
        ab.Id lineId,
        sub.NAME subName,
        line.NAME lineName,
        date_format( ab.Time_Id, '%Y-%m-%d' ) timeId,
        IF
        (
        detail.PT_Type IN ( 1, 2 ),
        IF (ab.Phasic_Type = 'A','AB',IF(ab.Phasic_Type = 'B','BC','CA')),ab.Phasic_Type
        ) phaseType,
        dic.NAME scale,
        sum( CASE WHEN Value_Type = 'AVG' THEN Uaberrance ELSE 0 END ) avgData,
        sum( CASE WHEN Value_Type = 'MAX' THEN Uaberrance ELSE 0 END ) maxData,
        sum( CASE WHEN Value_Type = 'MIN' THEN Uaberrance ELSE 0 END ) minData,
        sum( CASE WHEN Value_Type = 'CP95' THEN Uaberrance ELSE 0 END ) cp95Data
        FROM
        ( SELECT * FROM r_mp_surplus_abnormal_d WHERE Id IN
        <foreach collection="lineIndex" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        ) ab
        LEFT JOIN pq_line line ON line.Id = ab.Id
        LEFT JOIN pq_voltage vo ON vo.Id = line.Pid
        LEFT JOIN sys_dict_data dic ON dic.Id = vo.scale
        LEFT JOIN pq_line subv ON subv.Id = vo.Id
        LEFT JOIN pq_line dev ON dev.Id = subv.Pid
        LEFT JOIN pq_line sub ON sub.Id = dev.Pid
        LEFT JOIN pq_line_detail detail ON detail.Id = line.Id
        WHERE
        ab.phasic_type!='T'
        AND ab.Time_Id between #{startTime} and #{endTime}
        GROUP BY
        lineId,
        timeid,
        phaseType
        ORDER BY
        timeid
    </select>

    <select id="getFxDl" resultType="HistoryHarmOverLimitVO">
        SELECT
        ab.Id lineId,
        sub.NAME subName,
        line.NAME lineName,
        date_format( ab.Time_Id, '%Y-%m-%d' ) timeId,
        "/" phaseType,
        dic.NAME scale,
        sum( CASE WHEN Value_Type = 'AVG' THEN I_Neg ELSE 0 END ) avgData,
        sum( CASE WHEN Value_Type = 'MAX' THEN I_Neg ELSE 0 END ) maxData,
        sum( CASE WHEN Value_Type = 'MIN' THEN I_Neg ELSE 0 END ) minData,
        sum( CASE WHEN Value_Type = 'CP95' THEN I_Neg ELSE 0 END ) cp95Data
        FROM
        ( SELECT * FROM r_mp_surplus_abnormal_d WHERE Id IN
        <foreach collection="lineIndex" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        ) ab
        LEFT JOIN pq_line line ON line.Id = ab.Id
        LEFT JOIN pq_voltage vo ON vo.Id = line.Pid
        LEFT JOIN sys_dict_data dic ON dic.Id = vo.scale
        LEFT JOIN pq_line subv ON subv.Id = vo.Id
        LEFT JOIN pq_line dev ON dev.Id = subv.Pid
        LEFT JOIN pq_line sub ON sub.Id = dev.Pid
        LEFT JOIN pq_line_detail detail ON detail.Id = line.Id
        WHERE
        ab.phasic_type='T'
        AND ab.Time_Id between #{startTime} and #{endTime}
        GROUP BY
        lineId,
        timeid,
        phaseType
        ORDER BY
        timeid
    </select>

    <select id="getPl" resultType="HistoryHarmOverLimitVO">
        SELECT
        ab.Id lineId,
        sub.NAME subName,
        line.NAME lineName,
        date_format( ab.Time_Id, '%Y-%m-%d' ) timeId,
        "/" phaseType,
        dic.NAME scale,
        sum( CASE WHEN Value_Type = 'AVG' THEN Freq ELSE 0 END ) avgData,
        sum( CASE WHEN Value_Type = 'MAX' THEN Freq ELSE 0 END ) maxData,
        sum( CASE WHEN Value_Type = 'MIN' THEN Freq ELSE 0 END ) minData,
        sum( CASE WHEN Value_Type = 'CP95' THEN Freq ELSE 0 END ) cp95Data
        FROM
        ( SELECT * FROM r_mp_surplus_abnormal_d WHERE Id IN
        <foreach collection="lineIndex" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        ) ab
        LEFT JOIN pq_line line ON line.Id = ab.Id
        LEFT JOIN pq_voltage vo ON vo.Id = line.Pid
        LEFT JOIN sys_dict_data dic ON dic.Id = vo.scale
        LEFT JOIN pq_line subv ON subv.Id = vo.Id
        LEFT JOIN pq_line dev ON dev.Id = subv.Pid
        LEFT JOIN pq_line sub ON sub.Id = dev.Pid
        LEFT JOIN pq_line_detail detail ON detail.Id = line.Id
        WHERE
        ab.phasic_type='T'
        AND ab.Time_Id between #{startTime} and #{endTime}
        GROUP BY
        lineId,
        timeid,
        phaseType
        ORDER BY
        timeid
    </select>

    <select id="getXbDyHyl" resultType="HistoryHarmOverLimitVO">
        SELECT
        ab.Id lineId,
        sub.NAME subName,
        line.NAME lineName,
        date_format( ab.Time_Id, '%Y-%m-%d' ) timeId,
        IF
        (
        detail.PT_Type IN ( 1, 2 ),
        IF (ab.Phasic_Type = 'A','AB',IF(ab.Phasic_Type = 'B','BC','CA')),ab.Phasic_Type
        ) phaseType,
        dic.NAME scale,
        sum( CASE WHEN Value_Type = 'AVG' THEN Uharm_${number} ELSE 0 END ) avgData,
        sum( CASE WHEN Value_Type = 'MAX' THEN Uharm_${number} ELSE 0 END ) maxData,
        sum( CASE WHEN Value_Type = 'MIN' THEN Uharm_${number} ELSE 0 END ) minData,
        sum( CASE WHEN Value_Type = 'CP95' THEN Uharm_${number} ELSE 0 END ) cp95Data
        FROM
        ( SELECT * FROM r_mp_surplus_abnormal_d WHERE Id IN
        <foreach collection="lineIndex" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        ) ab
        LEFT JOIN pq_line line ON line.Id = ab.Id
        LEFT JOIN pq_voltage vo ON vo.Id = line.Pid
        LEFT JOIN sys_dict_data dic ON dic.Id = vo.scale
        LEFT JOIN pq_line subv ON subv.Id = vo.Id
        LEFT JOIN pq_line dev ON dev.Id = subv.Pid
        LEFT JOIN pq_line sub ON sub.Id = dev.Pid
        LEFT JOIN pq_line_detail detail ON detail.Id = line.Id
        WHERE
        ab.phasic_type!='T'
        AND ab.Time_Id between #{startTime} and #{endTime}
        GROUP BY
        lineId,
        timeid,
        phaseType
        ORDER BY
        timeid
    </select>

    <select id="getXbDlFz" resultType="HistoryHarmOverLimitVO">
        SELECT
        ab.Id lineId,
        sub.NAME subName,
        line.NAME lineName,
        date_format( ab.Time_Id, '%Y-%m-%d' ) timeId,
        IF
        (
        detail.PT_Type IN ( 1, 2 ),
        IF (ab.Phasic_Type = 'A','AB',IF(ab.Phasic_Type = 'B','BC','CA')),ab.Phasic_Type
        ) phaseType,
        dic.NAME scale,
        sum( CASE WHEN Value_Type = 'AVG' THEN Iharm_${number} ELSE 0 END ) avgData,
        sum( CASE WHEN Value_Type = 'MAX' THEN Iharm_${number} ELSE 0 END ) maxData,
        sum( CASE WHEN Value_Type = 'MIN' THEN Iharm_${number} ELSE 0 END ) minData,
        sum( CASE WHEN Value_Type = 'CP95' THEN Iharm_${number} ELSE 0 END ) cp95Data
        FROM
        ( SELECT * FROM r_mp_surplus_abnormal_d WHERE Id IN
        <foreach collection="lineIndex" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        ) ab
        LEFT JOIN pq_line line ON line.Id = ab.Id
        LEFT JOIN pq_voltage vo ON vo.Id = line.Pid
        LEFT JOIN sys_dict_data dic ON dic.Id = vo.scale
        LEFT JOIN pq_line subv ON subv.Id = vo.Id
        LEFT JOIN pq_line dev ON dev.Id = subv.Pid
        LEFT JOIN pq_line sub ON sub.Id = dev.Pid
        LEFT JOIN pq_line_detail detail ON detail.Id = line.Id
        WHERE
        ab.phasic_type!='T'
        AND ab.Time_Id between #{startTime} and #{endTime}
        GROUP BY
        lineId,
        timeid,
        phaseType
        ORDER BY
        timeid
    </select>

    <select id="getJxbHyl" resultType="HistoryHarmOverLimitVO">
        SELECT
        ab.Id lineId,
        sub.NAME subName,
        line.NAME lineName,
        date_format( ab.Time_Id, '%Y-%m-%d' ) timeId,
        IF
        (
        detail.PT_Type IN ( 1, 2 ),
        IF (ab.Phasic_Type = 'A','AB',IF(ab.Phasic_Type = 'B','BC','CA')),ab.Phasic_Type
        ) phaseType,
        dic.NAME scale,
            sum( CASE WHEN Value_Type = 'AVG' THEN Inuharm_${number} ELSE 0 END ) avgData,
        sum( CASE WHEN Value_Type = 'MAX' THEN Inuharm_${number} ELSE 0 END ) maxData,
        sum( CASE WHEN Value_Type = 'MIN' THEN Inuharm_${number} ELSE 0 END ) minData,
        sum( CASE WHEN Value_Type = 'CP95' THEN Inuharm_${number} ELSE 0 END ) cp95Data
        FROM
        ( SELECT * FROM r_mp_surplus_abnormal_d WHERE Id IN
        <foreach collection="lineIndex" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        ) ab
        LEFT JOIN pq_line line ON line.Id = ab.Id
        LEFT JOIN pq_voltage vo ON vo.Id = line.Pid
        LEFT JOIN sys_dict_data dic ON dic.Id = vo.scale
        LEFT JOIN pq_line subv ON subv.Id = vo.Id
        LEFT JOIN pq_line dev ON dev.Id = subv.Pid
        LEFT JOIN pq_line sub ON sub.Id = dev.Pid
        LEFT JOIN pq_line_detail detail ON detail.Id = line.Id
        WHERE
        ab.phasic_type!='T'
        AND ab.Time_Id between #{startTime} and #{endTime}
        GROUP BY
        lineId,
        timeid,
        phaseType
        ORDER BY
        timeid
    </select>

    <select id="getCsSb" resultType="HistoryHarmOverLimitVO">
        SELECT
        ab.Id lineId,
        sub.NAME subName,
        line.NAME lineName,
        date_format( ab.Time_Id, '%Y-%m-%d' ) timeId,
        IF
        (
        detail.PT_Type IN ( 1, 2 ),
        IF (ab.Phasic_Type = 'A','AB',IF(ab.Phasic_Type = 'B','BC','CA')),ab.Phasic_Type
        ) phaseType,
        dic.NAME scale,
        sum( CASE WHEN Value_Type = 'AVG' THEN Flicker ELSE 0 END ) avgData
        FROM
        ( SELECT * FROM r_mp_surplus_abnormal_d WHERE Id IN
        <foreach collection="lineIndex" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        ) ab
        LEFT JOIN pq_line line ON line.Id = ab.Id
        LEFT JOIN pq_voltage vo ON vo.Id = line.Pid
        LEFT JOIN sys_dict_data dic ON dic.Id = vo.scale
        LEFT JOIN pq_line subv ON subv.Id = vo.Id
        LEFT JOIN pq_line dev ON dev.Id = subv.Pid
        LEFT JOIN pq_line sub ON sub.Id = dev.Pid
        LEFT JOIN pq_line_detail detail ON detail.Id = line.Id
        WHERE
        ab.phasic_type!='T'
        AND ab.Time_Id between #{startTime} and #{endTime}
        GROUP BY
        lineId,
        timeid,
        phaseType
        ORDER BY
        timeid
    </select>
</mapper>
