<?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.device.pms.mapper.distribution.PwROperatingIndexMapper">



    <!--获取配网数据质量-监测指标数据质量统计（年）-->
    <select id="getYearInfo" resultType="com.njcn.device.pms.pojo.vo.PwRDnOperatingVO">
        SELECT
        ta.org_no,
        concat( ta.yy, '-', ta.mm ) dateTime,
        ta.mrp AS measurementCount,
        ta.eamc AS effectiveAccessMeasurementCount,
        ifnull( round(( ta.eamc - tb.eamc )/ tb.eamc * 100, 2 ), 3.14159 ) AS effectiveAccessMeasurementCountYearOnYear,
        ifnull( round(( ta.eamc - tc.eamc )/ tc.eamc * 100, 2 ), 3.14159 ) AS effectiveAccessMeasurementCountRingRatio,
        ta.ear AS effectiveAccessRate,
        ifnull( round(( ta.ear - tb.ear )/ tb.ear * 100, 2 ), 3.14159 ) AS effectiveAccessRateYearOnYear,
        ifnull( round(( ta.ear - tc.ear )/ tc.ear * 100, 2 ), 3.14159 ) AS effectiveAccessRateRingRatio,
        ta.drr AS dataIntegrityRate,
        ifnull( round(( ta.drr - tb.drr )/ tb.drr * 100, 2 ), 3.14159 ) AS dataIntegrityRateYearOnYear,
        ifnull( round(( ta.drr - tc.drr )/ tc.drr * 100, 2 ), 3.14159 ) AS dataIntegrityRateRingRatio,
        ta.iir AS indexIntegrityRate,
        ta.is_unusual AS isUnusual
        FROM
        (
            SELECT
            t.org_no,
            YEAR ( t.data_date ) AS yy,
            MONTH ( t.data_date ) AS mm,
            sum( measurement_run_points ) mrp,
            sum( transit_measurement_points ) tmp,
            sum( effective_access_measurement_count ) eamc,
            TRUNCATE ( sum( effective_access_rate ) / count( effective_access_rate ), 2 ) ear,
            TRUNCATE ( sum( data_right_rate ) / count( data_right_rate ), 2 ) drr,
            TRUNCATE ( sum( index_integrity_rate ) / count( index_integrity_rate ), 2 ) iir,
            is_unusual
            FROM
            r_dn_operating_index_y t
            <where>
                t.org_no IN
                <foreach collection="deptIdList" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </where>
            GROUP BY
            t.org_no,
            yy,
            mm,
            is_unusual
        ) ta -- 同比：上年同月
        LEFT JOIN (
            SELECT
            t.org_no,
            YEAR ( t.data_date ) AS yy,
            MONTH ( t.data_date ) AS mm,
            sum( measurement_run_points ) mrp,
            sum( transit_measurement_points ) tmp,
            sum( effective_access_measurement_count ) eamc,
            TRUNCATE ( sum( effective_access_rate ) / count( effective_access_rate ), 2 ) ear,
            TRUNCATE ( sum( data_right_rate ) / count( data_right_rate ), 2 ) drr,
            TRUNCATE ( sum( index_integrity_rate ) / count( index_integrity_rate ), 2 ) iir,
            is_unusual
            FROM
            r_dn_operating_index_y t
            <where>
                t.org_no IN
                <foreach collection="deptIdList" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </where>
            GROUP BY
            t.org_no,
            yy,
            mm,
            is_unusual
        ) tb ON tb.mm = ta.mm
        AND tb.yy = ta.yy - 1
        AND tb.org_no = ta.org_no
        AND tb.is_unusual = ta.is_unusual -- 环比：上月
        LEFT JOIN (
            SELECT
            t.org_no,
            YEAR ( t.data_date ) AS yy,
            MONTH ( t.data_date ) AS mm,
            sum( measurement_run_points ) mrp,
            sum( transit_measurement_points ) tmp,
            sum( effective_access_measurement_count ) eamc,
            TRUNCATE ( sum( effective_access_rate ) / count( effective_access_rate ), 2 ) ear,
            TRUNCATE ( sum( data_right_rate ) / count( data_right_rate ), 2 ) drr,
            TRUNCATE ( sum( index_integrity_rate ) / count( index_integrity_rate ), 2 ) iir,
            is_unusual
            FROM
            r_dn_operating_index_y t
            <where>
                t.org_no IN
                <foreach collection="deptIdList" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </where>
            GROUP BY
            t.org_no,
            yy,
            mm,
            is_unusual
        ) tc ON (
        (
        tc.yy = ta.yy
        AND tc.mm = ta.mm - 1
        AND tb.org_no = ta.org_no
        AND tb.is_unusual = ta.is_unusual
        )
        OR (
        tc.yy = ta.yy - 1
        AND tc.mm = 12
        AND ta.mm = 1
        AND tb.org_no = ta.org_no
        AND tb.is_unusual = ta.is_unusual
        )
        )
        <where>
            ta.yy = year(#{param.startTime})
        </where>
    </select>

    <!--获取配网数据质量-监测指标数据质量统计（季）-->
    <select id="getSeasonInfo" resultType="com.njcn.device.pms.pojo.vo.PwRDnOperatingVO">
        SELECT
        ta.org_no,
        concat( ta.yy, '-', ta.mm ) dateTime,
        ta.mrp AS measurementCount,
        ta.eamc AS effectiveAccessMeasurementCount,
        ifnull( round(( ta.eamc - tb.eamc )/ tb.eamc * 100, 2 ), 3.14159 ) AS effectiveAccessMeasurementCountYearOnYear,
        ifnull( round(( ta.eamc - tc.eamc )/ tc.eamc * 100, 2 ), 3.14159 ) AS effectiveAccessMeasurementCountRingRatio,
        ta.ear AS effectiveAccessRate,
        ifnull( round(( ta.ear - tb.ear )/ tb.ear * 100, 2 ), 3.14159 ) AS effectiveAccessRateYearOnYear,
        ifnull( round(( ta.ear - tc.ear )/ tc.ear * 100, 2 ), 3.14159 ) AS effectiveAccessRateRingRatio,
        ta.drr AS dataIntegrityRate,
        ifnull( round(( ta.drr - tb.drr )/ tb.drr * 100, 2 ), 3.14159 ) AS dataIntegrityRateYearOnYear,
        ifnull( round(( ta.drr - tc.drr )/ tc.drr * 100, 2 ), 3.14159 ) AS dataIntegrityRateRingRatio,
        ta.iir AS indexIntegrityRate,
        ta.is_unusual AS isUnusual
        FROM
        (
        SELECT
        t.org_no,
        YEAR ( t.data_date ) AS yy,
        MONTH ( t.data_date ) AS mm,
        sum( measurement_run_points ) mrp,
        sum( transit_measurement_points ) tmp,
        sum( effective_access_measurement_count ) eamc,
        TRUNCATE ( sum( effective_access_rate ) / count( effective_access_rate ), 2 ) ear,
        TRUNCATE ( sum( data_right_rate ) / count( data_right_rate ), 2 ) drr,
        TRUNCATE ( sum( index_integrity_rate ) / count( index_integrity_rate ), 2 ) iir,
        is_unusual
        FROM
        r_dn_operating_index_q t
        <where>
            t.org_no IN
            <foreach collection="deptIdList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </where>
        GROUP BY
        t.org_no,
        yy,
        mm,
        is_unusual
        ) ta -- 同比：上年同月
        LEFT JOIN (
        SELECT
        t.org_no,
        YEAR ( t.data_date ) AS yy,
        MONTH ( t.data_date ) AS mm,
        sum( measurement_run_points ) mrp,
        sum( transit_measurement_points ) tmp,
        sum( effective_access_measurement_count ) eamc,
        TRUNCATE ( sum( effective_access_rate ) / count( effective_access_rate ), 2 ) ear,
        TRUNCATE ( sum( data_right_rate ) / count( data_right_rate ), 2 ) drr,
        TRUNCATE ( sum( index_integrity_rate ) / count( index_integrity_rate ), 2 ) iir,
        is_unusual
        FROM
        r_dn_operating_index_q t
        <where>
            t.org_no IN
            <foreach collection="deptIdList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </where>
        GROUP BY
        t.org_no,
        yy,
        mm,
        is_unusual
        ) tb ON tb.mm = ta.mm
        AND tb.yy = ta.yy - 1
        AND tb.org_no = ta.org_no
        AND tb.is_unusual = ta.is_unusual -- 环比：上月
        LEFT JOIN (
        SELECT
        t.org_no,
        YEAR ( t.data_date ) AS yy,
        MONTH ( t.data_date ) AS mm,
        sum( measurement_run_points ) mrp,
        sum( transit_measurement_points ) tmp,
        sum( effective_access_measurement_count ) eamc,
        TRUNCATE ( sum( effective_access_rate ) / count( effective_access_rate ), 2 ) ear,
        TRUNCATE ( sum( data_right_rate ) / count( data_right_rate ), 2 ) drr,
        TRUNCATE ( sum( index_integrity_rate ) / count( index_integrity_rate ), 2 ) iir,
        is_unusual
        FROM
        r_dn_operating_index_q t
        <where>
            t.org_no IN
            <foreach collection="deptIdList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </where>
        GROUP BY
        t.org_no,
        yy,
        mm,
        is_unusual
        ) tc ON (
        (
        tc.yy = ta.yy
        AND tc.mm = ta.mm - 1
        AND tb.org_no = ta.org_no
        AND tb.is_unusual = ta.is_unusual
        )
        OR (
        tc.yy = ta.yy - 1
        AND tc.mm = 12
        AND ta.mm = 1
        AND tb.org_no = ta.org_no
        AND tb.is_unusual = ta.is_unusual
        )
        )
        <where>
            ta.yy = year(#{param.startTime})
            <if test="dates!=null and dates.size()!=0">
                AND ta.mm IN
                <foreach collection="dates" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <!--获取配网数据质量-监测指标数据质量统计（月）-->
    <select id="getMonthInfo" resultType="com.njcn.device.pms.pojo.vo.PwRDnOperatingVO">
        SELECT
        ta.org_no,
        concat( ta.yy, '-', ta.mm ) dateTime,
        ta.mrp AS measurementCount,
        ta.eamc AS effectiveAccessMeasurementCount,
        ifnull( round(( ta.eamc - tb.eamc )/ tb.eamc * 100, 2 ), 3.14159 ) AS effectiveAccessMeasurementCountYearOnYear,
        ifnull( round(( ta.eamc - tc.eamc )/ tc.eamc * 100, 2 ), 3.14159 ) AS effectiveAccessMeasurementCountRingRatio,
        ta.ear AS effectiveAccessRate,
        ifnull( round(( ta.ear - tb.ear )/ tb.ear * 100, 2 ), 3.14159 ) AS effectiveAccessRateYearOnYear,
        ifnull( round(( ta.ear - tc.ear )/ tc.ear * 100, 2 ), 3.14159 ) AS effectiveAccessRateRingRatio,
        ta.drr AS dataIntegrityRate,
        ifnull( round(( ta.drr - tb.drr )/ tb.drr * 100, 2 ), 3.14159 ) AS dataIntegrityRateYearOnYear,
        ifnull( round(( ta.drr - tc.drr )/ tc.drr * 100, 2 ), 3.14159 ) AS dataIntegrityRateRingRatio,
        ta.iir AS indexIntegrityRate,
        ta.is_unusual AS isUnusual
        FROM
        (
        SELECT
        t.org_no,
        YEAR ( t.data_date ) AS yy,
        MONTH ( t.data_date ) AS mm,
        sum( measurement_run_points ) mrp,
        sum( transit_measurement_points ) tmp,
        sum( effective_access_measurement_count ) eamc,
        TRUNCATE ( sum( effective_access_rate ) / count( effective_access_rate ), 2 ) ear,
        TRUNCATE ( sum( data_right_rate ) / count( data_right_rate ), 2 ) drr,
        TRUNCATE ( sum( index_integrity_rate ) / count( index_integrity_rate ), 2 ) iir,
        is_unusual
        FROM
        r_dn_operating_index_m t
        <where>
            t.org_no IN
            <foreach collection="deptIdList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </where>
        GROUP BY
        t.org_no,
        yy,
        mm,
        is_unusual
        ) ta -- 同比：上年同月
        LEFT JOIN (
        SELECT
        t.org_no,
        YEAR ( t.data_date ) AS yy,
        MONTH ( t.data_date ) AS mm,
        sum( measurement_run_points ) mrp,
        sum( transit_measurement_points ) tmp,
        sum( effective_access_measurement_count ) eamc,
        TRUNCATE ( sum( effective_access_rate ) / count( effective_access_rate ), 2 ) ear,
        TRUNCATE ( sum( data_right_rate ) / count( data_right_rate ), 2 ) drr,
        TRUNCATE ( sum( index_integrity_rate ) / count( index_integrity_rate ), 2 ) iir,
        is_unusual
        FROM
        r_dn_operating_index_m t
        <where>
            t.org_no IN
            <foreach collection="deptIdList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </where>
        GROUP BY
        t.org_no,
        yy,
        mm,
        is_unusual
        ) tb ON tb.mm = ta.mm
        AND tb.yy = ta.yy - 1
        AND tb.org_no = ta.org_no
        AND tb.is_unusual = ta.is_unusual -- 环比：上月
        LEFT JOIN (
        SELECT
        t.org_no,
        YEAR ( t.data_date ) AS yy,
        MONTH ( t.data_date ) AS mm,
        sum( measurement_run_points ) mrp,
        sum( transit_measurement_points ) tmp,
        sum( effective_access_measurement_count ) eamc,
        TRUNCATE ( sum( effective_access_rate ) / count( effective_access_rate ), 2 ) ear,
        TRUNCATE ( sum( data_right_rate ) / count( data_right_rate ), 2 ) drr,
        TRUNCATE ( sum( index_integrity_rate ) / count( index_integrity_rate ), 2 ) iir,
        is_unusual
        FROM
        r_dn_operating_index_m t
        <where>
            t.org_no IN
            <foreach collection="deptIdList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </where>
        GROUP BY
        t.org_no,
        yy,
        mm,
        is_unusual
        ) tc ON (
        (
        tc.yy = ta.yy
        AND tc.mm = ta.mm - 1
        AND tb.org_no = ta.org_no
        AND tb.is_unusual = ta.is_unusual
        )
        OR (
        tc.yy = ta.yy - 1
        AND tc.mm = 12
        AND ta.mm = 1
        AND tb.org_no = ta.org_no
        AND tb.is_unusual = ta.is_unusual
        )
        )
        <where>
            ta.yy = year(#{param.startTime})

                AND ta.mm = MONTH(#{param.startTime})

        </where>
    </select>

</mapper>














