<?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="cn.cecep.talroad.mapper.PcFactoryPollutantEmissionsRankMapper">

    <resultMap type="cn.cecep.talroad.domain.PcFactoryPollutantEmissionsRank" id="PcFactoryPollutantEmissionsRankResult">
        <result property="id"    column="id"    />
        <result property="factoryId"    column="factory_id"    />
        <result property="pollutantId"    column="pollutant_id"    />
        <result property="emissionsRank"    column="emissions_rank"    />
        <result property="emissionsActual"    column="emissions_actual"    />
        <result property="emissionsPermit"    column="emissions_permit"    />
        <result property="emissionsRatio"    column="emissions_ratio"    />
    </resultMap>


    <sql id="selectPcFactoryPollutantEmissionsRankVo">
        select id, factory_id, pollutant_id, emissions_rank, emissions_actual, emissions_permit, emissions_ratio from pc_factory_pollutant_emissions_rank
    </sql>

    <select id="selectPcFactoryPollutantEmissionsRankList" parameterType="cn.cecep.talroad.domain.PcFactoryPollutantEmissionsRank" resultMap="PcFactoryPollutantEmissionsRankResult">
        <include refid="selectPcFactoryPollutantEmissionsRankVo"/>
        <where>
            <if test="factoryId != null  and factoryId != ''"> and factory_id = #{factoryId}</if>
            <if test="pollutantId != null  and pollutantId != ''"> and pollutant_id = #{pollutantId}</if>
            <if test="emissionsRank != null  and emissionsRank != ''"> and emissions_rank = #{emissionsRank}</if>
            <if test="emissionsActual != null  and emissionsActual != ''"> and emissions_actual = #{emissionsActual}</if>
            <if test="emissionsPermit != null  and emissionsPermit != ''"> and emissions_permit = #{emissionsPermit}</if>
            <if test="emissionsRatio != null  and emissionsRatio != ''"> and emissions_ratio = #{emissionsRatio}</if>
        </where>
    </select>

    <select id="selectRankList" resultType="cn.cecep.talroad.vo.IndustryEmissionTrackingOverviewVo">
    <!--   此处对企业进行过滤  对行业的排名在 cn.cecep.talroad.mapper.PcFactoryMapper.selectIndustryEmissionTrackingOverviewVoNew中   -->
        SELECT
            factory_id,
            poll_name,
            COALESCE ( emissions_actual, 0) AS emissions_actual,
            COALESCE ( emissions_permit, 0) AS emissions_permit,
            CASE WHEN emissions_permit = 0 THEN 0 ELSE COALESCE ( emissions_actual / emissions_permit, 0) END AS calculate,
            rank

        FROM (
            SELECT
                pf.id	factory_id,
                p.poll_name,
                SUM ( CASE WHEN is_numeric (emissions_actual) THEN emissions_actual::NUMERIC ELSE 0 END ) AS emissions_actual,
                SUM ( CASE WHEN is_numeric (emissions_permit) THEN emissions_permit::NUMERIC ELSE 0 END ) AS emissions_permit,
                RANK() OVER ( PARTITION BY p.poll_name ORDER BY SUM ( CASE WHEN is_numeric (emissions_actual) THEN emissions_actual::NUMERIC ELSE 0 END ) DESC ) rank
            FROM (
                SELECT regexp_split_to_table('SO₂,颗粒物,COD,总氮,NOₓ,VOCs,氨氮,总磷', ',') as poll_name ) AS p
                CROSS JOIN  pc_factory pf
                LEFT JOIN (
                    SELECT 'SO₂'  poll_name, factory_id, actually_emitting_so2 AS emissions_actual, allowable_emission_so2 AS emissions_permit FROM b_main_emission_pollution_air WHERE data_source = #{emissionType}
                    UNION ALL
                    SELECT '颗粒物'  poll_name, factory_id, actually_emitting_pm AS emissions_actual, allowable_emission_pm AS emissions_permit FROM b_main_emission_pollution_air WHERE data_source = #{emissionType}
                    UNION ALL
                    SELECT 'VOCs'  poll_name, factory_id, actually_emitting_vocs AS emissions_actual, allowable_emission_vocs AS emissions_permit FROM b_main_emission_pollution_air WHERE data_source = #{emissionType}
                    UNION ALL
                    SELECT 'NOₓ'  poll_name, factory_id, actually_emitting_nox AS emissions_actual, allowable_emission_nox AS emissions_permit FROM b_main_emission_pollution_air WHERE data_source = #{emissionType}
                    UNION ALL
                    SELECT 'COD'  poll_name, factory_id, actually_emitting_cod AS emissions_actual, allowable_emission_cod AS emissions_permit FROM b_main_emission_pollution_water WHERE data_source = #{emissionType}
                    UNION ALL
                    SELECT '总氮'  poll_name, factory_id, actually_emitting_nhn AS emissions_actual, allowable_emission_nhn AS emissions_permit FROM b_main_emission_pollution_water WHERE data_source = #{emissionType}
                    UNION ALL
                    SELECT '总磷'  poll_name, factory_id, actually_emitting_tp AS emissions_actual, allowable_emission_tp AS emissions_permit FROM b_main_emission_pollution_water WHERE data_source = #{emissionType}
                    UNION ALL
                    SELECT '氨氮'  poll_name, factory_id, actually_emitting_tn AS emissions_actual, allowable_emission_tn AS emissions_permit FROM b_main_emission_pollution_water WHERE data_source = #{emissionType}
                ) pcper ON pf.id = pcper.factory_id AND p.poll_name = pcper.poll_name
                GROUP BY pf.id, p.poll_name
        ) tmp
        WHERE factory_id = #{factoryId}
        ORDER BY CASE poll_name
                WHEN 'SO₂' THEN 1
                WHEN '颗粒物' THEN 2
                WHEN 'COD' THEN 3
                WHEN '总氮' THEN 4
                WHEN 'NOₓ' THEN 5
                WHEN 'VOCs' THEN 6
                WHEN '氨氮' THEN 7
                WHEN '总磷' THEN 8
                END
    </select>
</mapper>
