<?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.line.RMpEventDetailDMapper">

    <select id="getSagTimes" resultType="int">
        SELECT IFNULL(MAX(sag_times), 0)
        FROM `r_mp_event_detail_d`
        WHERE measurement_point_id = #{lineId}
        <![CDATA[AND DATE_FORMAT(data_date, '%Y-%m-%d') = #{date}
        ]]>
    </select>

    <select id="getInterruptTimes" resultType="int">
        SELECT IFNULL(MAX(interrupt_times), 0)
        FROM `r_mp_event_detail_d`
        WHERE measurement_point_id = #{lineId}
        <![CDATA[AND DATE_FORMAT(data_date, '%Y-%m-%d') = #{date}
        ]]>
    </select>

    <select id="getSumAndCount" resultType="java.util.Map">
        SELECT
        count( CASE WHEN swell_times > 0 THEN 1 END ) "swellTimesCount",
        sum( swell_times ) "swellTimesSum" ,
        count( CASE WHEN sag_times > 0 THEN 1 END ) "sagTimesCount",
        sum( sag_times ) "sagTimesSum" ,
        count( CASE WHEN interrupt_times > 0 THEN 1 END ) "interruptTimesCount",
        sum( interrupt_times ) "interruptTimesSum",
        count( CASE WHEN swell_times > 0 || sag_times > 0 || interrupt_times > 0 THEN 1 END ) "resultCount"
        FROM
        r_mp_event_detail_d
        WHERE
        measurement_point_id in
        <foreach collection="lineIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        AND data_date between #{startTime} and #{endTime}
    </select>
</mapper>