<?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.RStatOrgPvPowerQualityYMapper">

    <resultMap id="BaseResultMap" type="com.njcn.harmonic.pojo.po.RStatOrgPvPowerQualityYPO">
            <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="selectListPage" resultType="com.njcn.harmonic.pojo.vo.RStatOrgPvPowerStreamVO">
        SELECT
            org_no,
            monitoring_object,
            SUM(IF(`area_pq_event_type`=#{param.mapId.eventTypeL,javaType=string},total_monitor_num,0)) as lowPowerFactor89,
            SUM(IF(`area_pq_event_type`=#{param.mapId.eventTypeP,javaType=string},total_monitor_num,0)) as lowPowerFactor78,
            SUM(IF(`area_pq_event_type`=#{param.mapId.eventTypeO,javaType=string},total_monitor_num,0)) as lowPowerFactor7,
            IF(`monitoring_object`=`monitoring_object`,TRUNCATE(SUM(total_monitor_num)/SUM(monitor_valid_num),2),0) as lowPowerFactorRatio
        FROM
            <if test="param != null and  param.type == 1">
                r_stat_org_pv_power_quality_y
            </if>
            <if test="param != null and  param.type == 2">
                r_stat_org_pv_power_quality_q
            </if>
            <if test="param != null and  param.type == 3">
                r_stat_org_pv_power_quality_m
            </if>
        <where>
            `area_pq_event_type` in (
                #{param.mapId.eventTypeL,javaType=string},
                #{param.mapId.eventTypeP,javaType=string},
                #{param.mapId.eventTypeO,javaType=string}
            )
            <if test="param.OrgNos != null and  param.OrgNos.size > 0">
                AND org_no IN
                <foreach collection='param.OrgNos' item='item' index="index" open='(' separator=',' close=')'>
                    #{item}
                </foreach>
            </if>
            <if test="param!=null and param.startTime != null and param.startTime !=''">
                AND  data_date >= #{param.startTime}
            </if>
            <if test="param!=null and param.endTime != null and param.endTime != ''">
                AND   data_date &lt;= #{param.endTime}
            </if>
        </where>

        GROUP BY
            org_no,monitoring_object

    </select>
    <select id="selectDetailListPage" resultType="com.njcn.harmonic.pojo.vo.RStatOrgPvDetailVO">
        SELECT
            data_date,
            vol_over_max,
            vol_over_min,
            measurement_point_id,
            stream_electricity,
            stream_load_rate,
            permeability,
            low_power_factor
        FROM
        <if test="param != null and  param.type == 1">
            r_mp_pv_power_detail_y
        </if>
        <if test="param != null and  param.type == 2">
            r_mp_pv_power_detail_q
        </if>
        <if test="param != null and  param.type == 3">
            r_mp_pv_power_detail_m
        </if>
        <where>
            <if test="param.OrgNos != null and  param.OrgNos.size > 0">
                AND measurement_point_id IN
                <foreach collection='param.OrgNos' item='item' index="index" open='(' separator=',' close=')'>
                    #{item}
                </foreach>
            </if>
            <if test="param!=null and param.startTime != null and param.startTime !=''">
                AND  data_date >= #{param.startTime}
            </if>
            <if test="param!=null and param.endTime != null and param.endTime != ''">
                AND   data_date &lt;= #{param.endTime}
            </if>
        </where>

    </select>
</mapper>
