<?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.prepare.harmonic.mapper.mysql.day.RStatDataVDMapper">

    <select id="getDayData" resultType="com.njcn.prepare.harmonic.pojo.mysql.po.line.RMpTargetDiffDPO">
        select
            A.time,
            A.lineId,
            coalesce(A.vUnbalance, 0) vUnbalance,
            coalesce(A.v, 0) vHarmonic,
            coalesce(B.vlDev, 0) vlDev,
            coalesce(B.freqDev, 0) freqDev,
            coalesce(C.plt, 0) plt,
            coalesce(D.sagTimes, 0) sagTimes,
            coalesce(D.interruptTimes, 0) interruptTimes
        from
            (
            select
                `time`,
                line_id lineId,
                max(v_unbalance) vUnbalance,
                max(greatest(v_thd, v_1, v_2, v_3, v_4, v_5, v_6, v_7, v_8, v_9, v_10, v_11, v_12, v_13, v_14, v_15, v_16, v_17, v_18, v_19, v_20, v_21, v_22, v_23, v_24, v_25, v_26, v_27, v_28, v_29, v_30, v_31, v_32, v_33, v_34, v_35, v_36, v_37, v_38, v_39, v_40, v_41, v_42, v_43, v_44, v_45, v_46, v_47, v_48, v_49, v_50)) v
            from
                r_stat_data_v_d t0
            where
                `time` = #{time}
                and value_type = 'CP95'
            group by
                `time`,
                line_id
            ) A
            left join
            (
                select
                    `time`,
                    line_id lineId,
                    max(abs(vl_dev)) vlDev,
                    max(abs(freq_dev)) freqDev
                from
                    r_stat_data_v_d t0
                where
                    `time` = #{time}
                    and value_type = 'MAX'
                group by
                    `time`,
                    line_id
            ) B
            on A.lineId = B.lineId
            left join
            (
                select
                    `time` ,
                    line_id lineId,
                    max(plt) plt
                from
                    r_stat_data_plt_d
                where
                    `time` = #{time}
                    and value_type = 'MAX'
                group by
                    `time`,
                    line_id
            ) C
            on A.lineId = C.lineId
            left join
            (
                select
                    measurement_point_id lineId,
                    data_date dataDate,
                    sag_times sagTimes,
                    interrupt_times interruptTimes
                from
                    r_mp_event_detail_d
                where
                    data_date = #{time}
            ) D
            on A.lineId = D.lineId
            <where>
                <if test="list != null and list.size() > 0">
                    A.lineId in
                    <foreach collection="list" item="item" open="(" close=")" separator=",">
                        #{item}
                    </foreach>
                </if>
            </where>
    </select>

    <select id="getAvgDayData" resultType="com.njcn.prepare.harmonic.pojo.dto.AlarmDetailDayDTO">
        select
            t0.lineId,
            coalesce(t0.vUnbalance, 0) vUnbalance,
            coalesce(t0.v, 0) vHarmonic,
            coalesce(t1.vlDev, 0) vlDev,
            coalesce(t1.freqDev, 0) freqDev,
            coalesce(t2.plt, 0) plt,
            coalesce(t3.sagTimes, 0) sagTimes,
            coalesce(t3.interruptTimes, 0) interruptTimes
        from
            (
            select
                A.lineId,
                avg(A.vUnbalance) vUnbalance,
                avg(A.v) v
            from
                (
                select
                    `time`,
                    line_id lineId,
                    max(v_unbalance) vUnbalance,
                    max(greatest(v_thd, v_1, v_2, v_3, v_4, v_5, v_6, v_7, v_8, v_9, v_10, v_11, v_12, v_13, v_14, v_15, v_16, v_17, v_18, v_19, v_20, v_21, v_22, v_23, v_24, v_25, v_26, v_27, v_28, v_29, v_30, v_31, v_32, v_33, v_34, v_35, v_36, v_37, v_38, v_39, v_40, v_41, v_42, v_43, v_44, v_45, v_46, v_47, v_48, v_49, v_50)) v
                from
                    r_stat_data_v_d
                where
                    `time` between #{startTime} and #{endTime}
                    and value_type = 'CP95'
                group by
                    `time`,
                    line_id
        ) A
            group by
                A.lineId
        ) t0
        left join
        (
            select
                B.lineId,
                avg(B.vlDev) vlDev,
                avg(B.freqDev) freqDev
            from
                (
                select
                    `time`,
                    line_id lineId,
                    max(abs(vl_dev)) vlDev,
                    max(abs(freq_dev)) freqDev
                from
                    r_stat_data_v_d
                where
                    `time` between #{startTime} and #{endTime}
                    and value_type = 'MAX'
                group by
                    `time`,
                    line_id
        ) B
            group by
                B.lineId
        ) t1
         on t0.lineId = t1.lineId
        left join
        (
            select
                C.lineId,
                avg(C.plt) plt
            from
                (
                select
                    `time` ,
                    line_id lineId,
                    max(plt) plt
                from
                    r_stat_data_plt_d
                where
                    `time` between #{startTime} and #{endTime}
                    and value_type = 'MAX'
                group by
                    `time`,
                    line_id
        ) C
            group by
                C.lineId
        ) t2
         on t0.lineId = t2.lineId
        left join
        (
            select
                measurement_point_id lineId,
                avg(sag_times) sagTimes,
                avg(interrupt_times) interruptTimes
            from
                r_mp_event_detail_d
            where
                data_date between #{startTime} and #{endTime}
            group by
                measurement_point_id
        ) t3
         on t0.lineId = t3.lineId
        <where>
            <if test="list != null and list.size() > 0">
                t0.lineId in
                <foreach collection="list" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="getTargetWarnD" resultType="com.njcn.prepare.harmonic.pojo.dto.RMpTargetWarnDDTO">
       select
            A.time,
            A.lineId,
            coalesce(A.vUnbalance, 0) vUnbalance,
            coalesce(A.v, 0) vHarmonic,
            coalesce(B.vlDev, 0) vlDev,
            coalesce(B.freqDev, 0) freqDev,
            coalesce(C.plt, 0) plt,
            coalesce(D.sagTimes, 0) sagTimes,
            coalesce(D.interruptTimes, 0) interruptTimes,
            case when (E.rate > 0.95 and E.isUnusual = 1) then 1 else 0 end isEffective,
            coalesce(F.isHarmonic, 0) isHarmonic,
            case when (G.swellTimes > 0 or G.sagTimes > 0 or G.interruptTimes > 0) then 1 else 0 end isEvent
        from
            (
            select
                `time`,
                line_id lineId,
                max(v_unbalance) vUnbalance,
                max(greatest(v_thd, v_1, v_2, v_3, v_4, v_5, v_6, v_7, v_8, v_9, v_10, v_11, v_12, v_13, v_14, v_15, v_16, v_17, v_18, v_19, v_20, v_21, v_22, v_23, v_24, v_25, v_26, v_27, v_28, v_29, v_30, v_31, v_32, v_33, v_34, v_35, v_36, v_37, v_38, v_39, v_40, v_41, v_42, v_43, v_44, v_45, v_46, v_47, v_48, v_49, v_50)) v
            from
                r_stat_data_v_d t0
            where
                `time` = #{time} and value_type = 'CP95'
            group by
                `time`,
                line_id
        ) A
        left join
        (
            select
                `time`,
                line_id lineId,
                max(abs(vl_dev)) vlDev,
                max(abs(freq_dev)) freqDev
            from
                r_stat_data_v_d t0
            where
                `time` = #{time} and value_type = 'MAX'
            group by
                `time`,
                line_id
        ) B
        on
            A.lineId = B.lineId
        left join
        (
            select
                `time` ,
                line_id lineId,
                max(plt) plt
            from
                r_stat_data_plt_d
            where
                `time` = #{time} and value_type = 'MAX'
            group by
                `time`,
                line_id
        ) C
        on
            A.lineId = C.lineId
        left join
        (
            select
                measurement_point_id lineId,
                data_date dataDate,
                sag_times sagTimes,
                interrupt_times interruptTimes
            from
                r_mp_event_detail_d
            where
                data_date = #{time}
        ) D
        on
            A.lineId = D.lineId
        left join
        (
        select
            data_date dataDate,
            measurement_point_id lineId,
            data_integrity_rate rate,
            is_unusual isUnusual
        from
            r_operating_monitor_d
        where
            data_date = #{time}
        ) E
        on A.lineId = E.lineId
        left join
        (
        select
            measurement_point_id lineId,
            is_harmonic isHarmonic
        from
            r_mp_part_harmonic_detail_d
        where
            data_date = #{time}
        ) F
        on A.lineId = F.lineId
        left join
        (
        select
            measurement_point_id lineId,
            swell_times swellTimes,
            sag_times sagTimes,
            interrupt_times interruptTimes
        from
            r_mp_event_detail_d
        where
            data_date = #{time}
        ) G
        on A.lineId = G.lineId
        <where>
            <if test="list != null and list.size() > 0">
                A.lineId in
                <foreach collection="list" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="getBenchmarkLevel" resultType="com.njcn.harmonic.pojo.po.day.RStatDataVDPO">
        select
            A.`time`,
            A.line_id lineId,
            A.rms,
            C.vUnbalance,
            B.vThd
        from
            (
                select
                    `time`,
                    line_id,
                    max(rms) rms
                from
                    r_stat_data_v_d
                where
                    `time` between #{startTime} and #{endTime}
                    and phasic_type in ('A', 'B', 'C')
                    and value_type in ('AVG')
                    <if test="list != null and list.size() > 0">
                        and line_id in
                        <foreach collection="list" item="item" open="(" close=")" separator=",">
                            #{item}
                        </foreach>
                    </if>
                group by `time`, line_id
            ) A
        inner join
            (
                select
                    `time`,
                    line_id,
                    max(v_thd) vThd
                from
                    r_stat_data_v_d
                where
                    `time` between #{startTime} and #{endTime}
                    and phasic_type in ('A', 'B', 'C')
                    and value_type in ('CP95')
                    <if test="list != null and list.size() > 0">
                        and line_id in
                        <foreach collection="list" item="item" open="(" close=")" separator=",">
                            #{item}
                        </foreach>
                    </if>
                group by `time`,line_id
            ) B
        on A.`time` = B.`time` and A.line_id = B.line_id
        inner join
            (
                select
                    `time`,
                    line_id,
                    max(v_unbalance) vUnbalance
                from
                    r_stat_data_v_d
                where
                    `time` between #{startTime} and #{endTime}
                    and phasic_type = 'T'
                    and value_type in ('CP95')
                    <if test="list != null and list.size() > 0">
                        and line_id in
                        <foreach collection="list" item="item" open="(" close=")" separator=",">
                            #{item}
                        </foreach>
                    </if>
                group by `time`,line_id
            ) C
        on A.`time` = C.`time` and A.line_id = C.line_id
    </select>
</mapper>