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

    <resultMap id="BaseResultMap" type="com.njcn.device.pms.pojo.po.RDnOperatingIndexM">
            <id property="orgNo" column="org_no" jdbcType="VARCHAR"/>
            <id property="dataDate" column="data_date" jdbcType="TIMESTAMP"/>
            <id property="measurementPointType" column="measurement_point_type" jdbcType="VARCHAR"/>
            <result property="measurementRunPoints" column="measurement_run_points" jdbcType="INTEGER"/>
            <result property="transitMeasurementPoints" column="transit_measurement_points" jdbcType="INTEGER"/>
            <result property="effectiveAccessMeasurementCount" column="effective_access_measurement_count" jdbcType="INTEGER"/>
            <result property="effectiveAccessRate" column="effective_access_rate" jdbcType="VARCHAR"/>
            <result property="shouldCount" column="should_count" jdbcType="INTEGER"/>
            <result property="shouldPointCoverage" column="should_point_coverage" jdbcType="INTEGER"/>
            <result property="dataIntegrityRate" column="data_integrity_rate" jdbcType="FLOAT"/>
            <result property="dataRightRate" column="data_right_rate" jdbcType="FLOAT"/>
            <result property="indexIntegrityRate" column="index_integrity_rate" jdbcType="FLOAT"/>
            <result property="isUnusual" column="is_unusual" jdbcType="INTEGER"/>
    </resultMap>

    <sql id="Base_Column_List">
        org_no,data_date,measurement_point_type,
        measurement_run_points,transit_measurement_points,effective_access_measurement_count,
        effective_access_rate,should_count,should_point_coverage,
        data_integrity_rate,data_right_rate,index_integrity_rate,
        is_unusual
    </sql>
    <select id="getOperatingList" resultType="com.njcn.device.pms.pojo.vo.PwRDnOperatingIndexCommonVO" parameterType="map">
        select
            rdim1.org_no,
            rdim1.data_date,

            rdim1.measurement_run_points as measurementPointsOne,
            rdim1.should_count as shouldCountOne,

            rdim2.measurement_run_points as measurementPointsTwo,
            rdim2.should_count as shouldCountTwo,

            rdim3.measurement_run_points as measurementPointsThree,
            rdim3.should_count as shouldCountThree,
            -- 应设点覆盖率
            truncate((rdim1.should_point_coverage + rdim2.should_point_coverage + rdim3.should_point_coverage) / 3, 2) as shouldPointCoverage,

            -- 有效接入率
            truncate(
                (rdim1.effective_access_measurement_count + rdim2.effective_access_measurement_count + rdim3.effective_access_measurement_count)
                   / (rdim1.measurement_run_points + rdim2.measurement_run_points + rdim3.measurement_run_points) * 100, 2
                ) as effectiveAccessRate,

            -- 数据完整率
            truncate((rdim1.data_integrity_rate + rdim2.data_integrity_rate + rdim3.data_integrity_rate) / 3, 2) as dataIntegrityRate,
            -- 数据准确率
            truncate((rdim1.data_right_rate + rdim2.data_right_rate + rdim3.data_right_rate) / 3, 2) as dataRightRate

        from r_dn_operating_index_m  rdim1

        left join r_dn_operating_index_m  rdim2
          on rdim1.org_no = rdim2.org_no
        left join r_dn_operating_index_m rdim3
          on rdim1.org_no = rdim3.org_no
        where  1 = 1
        <if test="startTime != null and startTime != ''">
            and DATE_FORMAT(rdim1.data_date, '%Y-%m-%d') >= DATE_FORMAT(#{startTime}, '%Y-%m-%d')
        </if>
        <if test="endTime != null and endTime != ''">
            and DATE_FORMAT(rdim1.data_date, '%Y-%m-%d') &lt;= DATE_FORMAT(#{endTime}, '%Y-%m-%d')
        </if>
          and rdim1.org_no in
        <foreach collection="orgIdList" item="orgId" separator="," open="(" close=")">
            #{orgId}
        </foreach>
        and DATE_FORMAT(rdim1.data_date, '%Y-%m-%d') = DATE_FORMAT(rdim2.data_date, '%Y-%m-%d')
        and DATE_FORMAT(rdim2.data_date, '%Y-%m-%d') = DATE_FORMAT(rdim3.data_date, '%Y-%m-%d')
        and rdim1.measurement_point_type = #{monitorSortOne}
        and rdim2.measurement_point_type = #{monitorSortTwo}
        and rdim3.measurement_point_type = #{monitorSortThree}

    </select>
</mapper>
