<?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.majornetwork.RStatOrgQMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.njcn.harmonic.pojo.po.RStatOrgQ">
        <id column="org_no" property="orgNo" />
        <result column="data_date" property="dataDate" />
        <result column="measurement_type_class" property="measurementTypeClass" />
        <result column="all_count" property="allCount" />
        <result column="effective_measurement_average" property="effectiveMeasurementAverage" />
        <result column="effective_measurement_accrued" property="effectiveMeasurementAccrued" />
        <result column="harmonic_measurement_average" property="harmonicMeasurementAverage" />
        <result column="harmonic_measurement_accrued" property="harmonicMeasurementAccrued" />
        <result column="harmonic_measurement_ratio_average" property="harmonicMeasurementRatioAverage" />
        <result column="average_over_day" property="averageOverDay" />
        <result column="freq_pass_rate" property="freqPassRate" />
        <result column="v_dev_pass_rate" property="vDevPassRate" />
        <result column="flicker_pass_rate" property="flickerPassRate" />
        <result column="event_measurement_average" property="eventMeasurementAverage" />
        <result column="event_measurement_accrued" property="eventMeasurementAccrued" />
        <result column="event_measurement_ratio_average" property="eventMeasurementRatioAverage" />
        <result column="event_freq" property="eventFreq" />
        <result column="event_count" property="eventCount" />
        <result column="should_count" property="shouldCount" />
        <result column="warn_count" property="warnCount" />
        <result column="data_type" property="dataType" />
    </resultMap>
    <select id="getQuarterRStatHarmonicOrgInfo" resultType="com.njcn.harmonic.pojo.vo.RStatSubstationOrgVO">
        SELECT
            rso.org_no AS orgNo,
            rso.measurement_type_class,
            rso.data_date AS dataDate,
            rso.effective_measurement_average AS effectiveMeasurementAverage,
            rso.effective_measurement_accrued AS effectiveMeasurementAccrued,
            rso.harmonic_measurement_average AS harmonicMeasurementAverage,
            rso.harmonic_measurement_accrued AS harmonicMeasurementAccrued,
            rso.harmonic_measurement_ratio_average as harmonicMeasurementRatioAverage,
            TRUNCATE(rso.event_measurement_accrued/rso.effective_measurement_accrued ,2)as harmonicMeasurementRatioAccrued,
            c.harmonic_type,
            c.over_limit_measurement_average,
            c.over_limit_measurement_accrued,
            c.over_limit_measurement_ratio_average,
            c.over_limit_measurement_ratio_accrued,
        (
            SELECT
            over_limit_measurement_average
            FROM
            r_stat_harmonic_org_q a
            WHERE
            a.org_no=c.org_no
            and a.data_Type = #{dataType}
              and
            a.harmonic_type=c.harmonic_type
            <if test="param!=null and param.startTime != null and param.startTime !=''">
                AND (data_date >= date(
                DATE_ADD( #{param.startTime}, INTERVAL - 1 YEAR )))
            </if>
            <if test="param!=null and param.endTime != null and param.endTime !=''">
                AND (data_date &lt;= date(
                DATE_ADD( #{param.endTime}, INTERVAL - 1 YEAR )))
            </if>

        ) chainAverage ,
        (
            SELECT
            over_limit_measurement_accrued
            FROM
            r_stat_harmonic_org_q a
            WHERE
            a.org_no=c.org_no
             and a.data_Type = #{dataType}
            and
            a.harmonic_type=c.harmonic_type
            <if test="param!=null and param.startTime != null and param.startTime !=''">
                AND (data_date >= date(
                DATE_ADD( #{param.startTime}, INTERVAL - 1 YEAR )))
            </if>
            <if test="param!=null and param.endTime != null and param.endTime !=''">
                AND (data_date &lt;= date(
                DATE_ADD( #{param.endTime}, INTERVAL - 1 YEAR )))
            </if>
        ) chainAccrued ,
        (
            SELECT
            over_limit_measurement_average
            FROM
            r_stat_harmonic_org_q a
            WHERE
            a.org_no=c.org_no
            and a.data_Type = #{dataType}
            and
            a.harmonic_type=c.harmonic_type
            <if test="param!=null and param.startTime != null and param.startTime !=''">
                AND (data_date >= date(
                DATE_ADD( #{param.startTime}, INTERVAL - 3 MONTH )))
            </if>
            <if test="param!=null and param.endTime != null and param.endTime !=''">
                AND (data_date &lt;= date(
                DATE_ADD( #{param.endTime}, INTERVAL - 3 MONTH )))
            </if>
        ) sameAverage,
        (
            SELECT
            over_limit_measurement_accrued
            FROM
            r_stat_harmonic_org_q a
            WHERE
            a.org_no=c.org_no
            and a.data_Type = #{dataType}
            and
            a.harmonic_type=c.harmonic_type
            <if test="param!=null and param.startTime != null and param.startTime !=''">
                AND (data_date >= date(
                DATE_ADD( #{param.startTime}, INTERVAL - 3 MONTH )))
            </if>
            <if test="param!=null and param.endTime != null and param.endTime !=''">
                AND (data_date &lt;= date(
                DATE_ADD( #{param.endTime}, INTERVAL - 3 MONTH )))
            </if>

        ) sameAccrued,
        c.average_over_day
        FROM
            r_stat_harmonic_org_q c
        RIGHT JOIN r_stat_org_q AS rso ON rso.org_no = c.org_no
        <where>
            c.data_Type=#{dataType} and rso.data_Type=#{dataType}
            <if test="param != null and  param.ids != null and param.ids.size > 0">
                AND rso.measurement_type_class IN
                <foreach collection='param.ids' item='item' index="index" open='(' separator=',' close=')'>
                    #{item}
                </foreach>
            </if>
            <if test="deptIds != null and deptIds.size > 0">
                AND rso.org_no IN
                <foreach collection='deptIds' item='item' index="index" open='(' separator=',' close=')'>
                    #{item}
                </foreach>
            </if>
            <if test="param!=null and param.startTime != null and param.startTime !=''">
                AND  rso.data_date >= #{param.startTime} AND c.data_date >= #{param.startTime}
            </if>
            <if test="param!=null and param.endTime != null and param.endTime != ''">
                AND   rso.data_date &lt;= #{param.endTime}  AND c.data_date &lt;= #{param.endTime}
            </if>
        </where>
    </select>
    <select id="getQuarterRStatOrgIndex" resultType="com.njcn.harmonic.pojo.vo.RStatOrgIndexVO$RStatOrgIndexInfoVO">
        select
        org_no,
        sum(effective_measurement_accrued) as allCount,
        avg(freq_pass_rate) freq_pass_rate,
        avg(v_dev_pass_rate) v_dev_pass_rate,
        avg(flicker_pass_rate) flicker_pass_rate
        from
            r_stat_org_q rso
        <where>
            and rso.data_Type=#{dataType}
            <if test="param != null and  param.ids != null and param.ids.size > 0">
                AND rso.org_no IN
                <foreach collection='param.ids' item='item' index="index" open='(' separator=',' close=')'>
                    #{item}
                </foreach>
            </if>
            <if test="param!=null and param.startTime != null and param.startTime !=''">
                AND  rso.data_date >= #{param.startTime}
            </if>
            <if test="param!=null and param.endTime != null and param.endTime != ''">
                AND   rso.data_date &lt;= #{param.endTime}
            </if>
            group by org_no
        </where>
    </select>
    <select id="getPwQuarterRStatOrgIndex" resultType="com.njcn.harmonic.pojo.vo.PwRStatOrgVO">
        select
            org_no,
            measurement_type_class as monitorSort,
            effective_measurement_accrued as allCount,
            freq_pass_rate,
            v_dev_pass_rate,
            flicker_pass_rate
        from
            r_stat_org_y
        <where>
            and data_Type=#{dataType}
            <if test="param != null and  param.ids != null and param.ids.size > 0">
                AND org_no IN
                <foreach collection='param.ids' 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>
