<?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.area.RMpTargetWarnDMapper">
    <resultMap id="BaseResultMap" type="com.njcn.harmonic.pojo.po.RMpTargetWarnDPO">
        <!--@mbg.generated-->
        <!--@Table r_mp_target_warn_d-->
        <id column="measurement_point_id" jdbcType="VARCHAR" property="measurementPointId"/>
        <id column="data_date" jdbcType="DATE" property="dataDate"/>
        <result column="is_effective" jdbcType="TINYINT" property="isEffective"/>
        <result column="is_harmonic" jdbcType="TINYINT" property="isHarmonic"/>
        <result column="is_event" jdbcType="TINYINT" property="isEvent"/>
        <result column="is_warn" jdbcType="TINYINT" property="isWarn"/>
        <result column="is_v_dev_warn" jdbcType="TINYINT" property="isVDevWarn"/>
        <result column="is_freq_warn" jdbcType="TINYINT" property="isFreqWarn"/>
        <result column="is_unbalance_warn" jdbcType="TINYINT" property="isUnbalanceWarn"/>
        <result column="is_v_warn" jdbcType="TINYINT" property="isVWarn"/>
        <result column="is_flicker_warn" jdbcType="TINYINT" property="isFlickerWarn"/>
        <result column="is_sag_warn" jdbcType="TINYINT" property="isSagWarn"/>
        <result column="is_interrupt_warn" jdbcType="TINYINT" property="isInterruptWarn"/>
    </resultMap>
    <sql id="Base_Column_List">
        <!--@mbg.generated-->
        measurement_point_id, data_date, is_effective, is_harmonic, is_event, is_warn, is_v_dev_warn,
        is_freq_warn, is_unbalance_warn, is_v_warn, is_flicker_warn, is_sag_warn, is_interrupt_warn
    </sql>

    <select id="getDate" resultType="com.njcn.harmonic.pojo.dto.RMpMonitorAlarmCountMDTO">
        select
        #{startTime} dataDate,
        measurement_point_id measurementPointId,
        case when sum(is_v_dev_warn) > 0 then 1 else 0 end vdevAlarmInfo,
        sum(is_v_dev_warn)/count(distinct data_date) vdevSeverity,
        case when sum(is_freq_warn) > 0 then 1 else 0 end freqAlarmInfo,
        sum(is_freq_warn)/count(distinct data_date) freqSeverity,
        case when sum(is_unbalance_warn) > 0 then 1 else 0 end unbalanceAlarmInfo,
        sum(is_unbalance_warn)/count(distinct data_date) unbalanceSeverity,
        case when sum(is_v_warn) > 0 then 1 else 0 end vAlarmInfo,
        sum(is_v_warn)/count(distinct data_date) vSeverity,
        case when sum(is_flicker_warn) > 0 then 1 else 0 end flickerAlarmInfo,
        sum(is_flicker_warn)/count(distinct data_date) flickerSeverity,
        case when sum(is_sag_warn) > 0 then 1 else 0 end sagAlarmInfo,
        sum(is_sag_warn)/count(distinct data_date) sagSeverity,
        case when sum(is_interrupt_warn) > 0 then 1 else 0 end interruptAlarmInfo,
        sum(is_interrupt_warn)/count(distinct data_date) interruptSeverity,
        sum(is_v_dev_warn)/count(distinct data_date)+sum(is_freq_warn)/count(distinct
        data_date)+sum(is_unbalance_warn)/count(distinct data_date)+sum(is_v_warn)/count(distinct
        data_date)+sum(is_flicker_warn)/count(distinct data_date) harmonicTypeGradeTotal,
        sum(is_sag_warn)/count(distinct data_date)+sum(is_interrupt_warn)/count(distinct data_date) eventTypeGradeTotal
        from
        r_mp_target_warn_d
        where
        data_date between #{startTime} and #{endTime}
        <where>
            <if test="list != null and list.size() > 0">
                and measurement_point_id in
                <foreach collection="list" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
        group by measurement_point_id
        order by harmonicTypeGradeTotal desc,eventTypeGradeTotal desc
    </select>

    <select id="getSumDate" resultType="java.util.Map">
        SELECT
        count(*) "iECount"
        FROM
        r_mp_target_warn_d
        WHERE
        measurement_point_id in
        <foreach collection="lineIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        AND data_date between #{startTime} and #{endTime}
        AND is_effective = 1
    </select>


</mapper>