<?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.specialanalysis.RStatOrgPvPowerQualityMMapper">

    <resultMap id="BaseResultMap" type="com.njcn.harmonic.pojo.po.RStatOrgPvPowerQualityMPO">
            <id property="dataDate" column="data_date" jdbcType="DATE"/>
            <id property="orgNo" column="org_no" jdbcType="VARCHAR"/>
            <id property="monitoringObject" column="monitoring_object" jdbcType="VARCHAR"/>
            <id property="areaPqEventType" column="area_pq_event_type" jdbcType="VARCHAR"/>
            <result property="avgMonitorNum" column="avg_monitor_num" jdbcType="INTEGER"/>
            <result property="totalMonitorNum" column="total_monitor_num" jdbcType="INTEGER"/>
            <result property="monitorValidNum" column="monitor_valid_num" jdbcType="INTEGER"/>
            <result property="avgDateNum" column="avg_date_num" jdbcType="INTEGER"/>
            <result property="totalDateNum" column="total_date_num" jdbcType="INTEGER"/>
    </resultMap>

    <sql id="Base_Column_List">
        data_date,org_no,monitoring_object,
        area_pq_event_type,avg_monitor_num,total_monitor_num,
        monitor_valid_num,avg_date_num,total_date_num
    </sql>
    <select id="getStreamReverseByOrg" resultType="com.njcn.harmonic.pojo.vo.StreamReverseOrgVO">
        SELECT
            org_no as orgId,
            monitoring_object as monitoringObject,
            SUM(IF(`area_pq_event_type` = #{condMap.Event_Type_u}, total_monitor_num, 0)) as streamNum,
                SUM(IF(`area_pq_event_type` = #{condMap.Event_Type_a}, total_monitor_num, 0)) as streamReloadNum,
                SUM(IF(`area_pq_event_type` = #{condMap.Event_Type_s}, total_monitor_num, 0)) as streamOverloadNum,
                SUM(IF(`area_pq_event_type` = #{condMap.Event_Type_u}, total_monitor_num / monitor_valid_num, 0)) as streamRate
        FROM r_stat_org_pv_power_quality_m
        where org_no in
        <foreach collection="condMap.orgIdList" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
        <if test="condMap.startTime != null and condMap.startTime != ''">
            and DATE_FORMAT(data_date, '%Y-%m-%d') &gt;= DATE_FORMAT(#{condMap.startTime}, '%Y-%m-%d')
        </if>
        <if test="condMap.endTime != null and condMap.endTime != ''">
            and DATE_FORMAT(data_date, '%Y-%m-%d') &lt;= DATE_FORMAT(#{condMap.endTime}, '%Y-%m-%d')
        </if>

        GROUP BY org_no,monitoring_object
    </select>
    <select id="selectVoltage" resultType="com.njcn.harmonic.pojo.vo.OverviewVO$VoltageVO">
        SELECT
            monitoring_object,
            area_pq_event_type,
            TRUNCATE ( SUM( total_monitor_num )/ count( total_monitor_num ), 0 ) AS totalMonitorNum
        FROM
            r_stat_org_pv_power_quality_m
        <where>
            <if test="deptCodes != null and deptCodes.size > 0">
                AND org_no IN
                <foreach collection='deptCodes' item='item' index="index" open='(' separator=',' close=')'>
                    #{item}
                </foreach>
            </if>
            <if test="ids != null and ids.size > 0">
                AND area_pq_event_type IN
                <foreach collection='ids' item='item' index="index" open='(' separator=',' close=')'>
                    #{item}
                </foreach>
            </if>
            <if test="param!=null and param.year != null and param.year != ''">
                and  YEAR(data_date)=#{param.year}
            </if>
            <if test="param != null and param.month!=null and param.month.size > 0">
                AND MONTH(data_date) IN
                <foreach collection='param.month' item='item' index="index" open='(' separator=',' close=')'>
                    #{item}
                </foreach>
            </if>
        </where>
        GROUP BY
            monitoring_object,
            area_pq_event_type
    </select>
</mapper>
