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

    <resultMap type="cn.cecep.talroad.domain.BMainEmissionPollutionAir" id="BMainEmissionPollutionAirResult">
        <result property="id"    column="id"    />
        <result property="factoryId"    column="factory_id"    />
        <result property="pollYear"    column="poll_year"    />
        <result property="pollutionCategory"    column="pollution_category"    />
        <result property="dataSource"    column="data_source"    />
        <result property="actuallyEmittingSo2"    column="actually_emitting_so2"    />
        <result property="allowableEmissionSo2"    column="allowable_emission_so2"    />
        <result property="actuallyEmittingNox"    column="actually_emitting_nox"    />
        <result property="allowableEmissionNox"    column="allowable_emission_nox"    />
        <result property="actuallyEmittingPm"    column="actually_emitting_pm"    />
        <result property="allowableEmissionPm"    column="allowable_emission_pm"    />
        <result property="actuallyEmittingVocs"    column="actually_emitting_vocs"    />
        <result property="allowableEmissionVocs"    column="allowable_emission_vocs"    />
        <result property="groupByField"    column="groupByField"    />
        <result property="indusName"    column="indus_name"    />
        <result property="industryCategory"    column="industry_category"    />
    </resultMap>


    <sql id="selectBMainEmissionPollutionAirVo">
        select id, factory_id, poll_year, pollution_category, data_source, actually_emitting_so2, allowable_emission_so2, actually_emitting_nox, allowable_emission_nox, actually_emitting_pm, allowable_emission_pm, actually_emitting_vocs, allowable_emission_vocs from b_main_emission_pollution_air
    </sql>

    <select id="selectBMainEmissionPollutionAirList" parameterType="cn.cecep.talroad.domain.BMainEmissionPollutionAir" resultMap="BMainEmissionPollutionAirResult">
        <include refid="selectBMainEmissionPollutionAirVo"/>
        <where>
            <if test="factoryId != null  and factoryId != ''"> and factory_id = #{factoryId}</if>
            <if test="pollYear != null  and pollYear != ''"> and poll_year = #{pollYear}</if>
            <if test="pollutionCategory != null  and pollutionCategory != ''"> and pollution_category = #{pollutionCategory}</if>
            <if test="dataSource != null  and dataSource != ''"> and data_source = #{dataSource}</if>
            <if test="actuallyEmittingSo2 != null  and actuallyEmittingSo2 != ''"> and actually_emitting_so2 = #{actuallyEmittingSo2}</if>
            <if test="allowableEmissionSo2 != null  and allowableEmissionSo2 != ''"> and allowable_emission_so2 = #{allowableEmissionSo2}</if>
            <if test="actuallyEmittingNox != null  and actuallyEmittingNox != ''"> and actually_emitting_nox = #{actuallyEmittingNox}</if>
            <if test="allowableEmissionNox != null  and allowableEmissionNox != ''"> and allowable_emission_nox = #{allowableEmissionNox}</if>
            <if test="actuallyEmittingPm != null  and actuallyEmittingPm != ''"> and actually_emitting_pm = #{actuallyEmittingPm}</if>
            <if test="allowableEmissionPm != null  and allowableEmissionPm != ''"> and allowable_emission_pm = #{allowableEmissionPm}</if>
            <if test="actuallyEmittingVocs != null  and actuallyEmittingVocs != ''"> and actually_emitting_vocs = #{actuallyEmittingVocs}</if>
            <if test="allowableEmissionVocs != null  and allowableEmissionVocs != ''"> and allowable_emission_vocs = #{allowableEmissionVocs}</if>
        </where>
    </select>

    <select id="selectEmissionDataList" resultMap="BMainEmissionPollutionAirResult">
        SELECT
            pf."id"   AS  factory_id,
            MAX ( pi.indus_name )       AS indus_name,
            <if test="groupByIndustry"> pf.industry_category    industry_category, </if>
            <if test="groupByField != null and groupByField != ''"> ${groupByField} AS groupByField, </if>
            SUM ( CASE WHEN is_numeric (actually_emitting_so2) THEN actually_emitting_so2::NUMERIC ELSE 0 END )    AS actually_emitting_so2,
            SUM ( CASE WHEN is_numeric (allowable_emission_so2) THEN allowable_emission_so2::NUMERIC ELSE 0 END )    AS allowable_emission_so2,
            SUM ( CASE WHEN is_numeric (actually_emitting_nox) THEN actually_emitting_nox::NUMERIC ELSE 0 END )    AS actually_emitting_nox,
            SUM ( CASE WHEN is_numeric (allowable_emission_nox) THEN allowable_emission_nox::NUMERIC ELSE 0 END )    AS allowable_emission_nox,
            SUM ( CASE WHEN is_numeric (actually_emitting_pm) THEN actually_emitting_pm::NUMERIC ELSE 0 END )    AS actually_emitting_pm,
            SUM ( CASE WHEN is_numeric (allowable_emission_pm) THEN allowable_emission_pm::NUMERIC ELSE 0 END )    AS allowable_emission_pm,
            SUM ( CASE WHEN is_numeric (actually_emitting_vocs) THEN actually_emitting_vocs::NUMERIC ELSE 0 END )    AS actually_emitting_vocs,
            SUM ( CASE WHEN is_numeric (allowable_emission_vocs) THEN allowable_emission_vocs::NUMERIC ELSE 0 END )    AS allowable_emission_vocs
        FROM
            pc_factory pf
            LEFT JOIN pc_industry pi ON pf.industry_category = pi.indus_code
            LEFT JOIN b_main_emission_pollution_air bmepa on bmepa.factory_id = pf.id
        <where>
            data_source = #{dataSource}
            AND poll_year = #{year}
            <if test="factoryId != null  and factoryId != ''"> AND factory_id = #{factoryId}</if>
            <if test="industry != null and industry != ''"> AND industry_category = #{industry} </if>
            <if test="regionCodes != null and regionCodes.size() != 0">
                and pf.country_code IN
                <foreach collection="regionCodes" separator="," item="item" close=")" open="(">
                    #{item,jdbcType=VARCHAR}
                </foreach>
            </if>
        </where>
        GROUP BY
            pf."id"
            <if test="groupByIndustry"> , pf.industry_category </if>
            <if test="groupByField != null and groupByField != ''">, ${groupByField} </if>
    </select>
    <select id="selectEmissioinDataByYear" resultMap="BMainEmissionPollutionAirResult">
        select * from b_main_emission_pollution_air where poll_year = #{lastYear} and data_source  ='系统核算'
    </select>
</mapper>
