<?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.analysis.SRaEmissionsOverMapper">

    <select id="selectRegionEmissions" resultType="cn.cecep.talroad.vo.analysis.SRaEmissionsOverVo$RegionVo">
        select
            t1.region_name,
            t1.pollutant_name,
            t1.stat_year,
            t1.permit_emmisons,
            t1.reality_emmisons,
            t1.emmisons_ratio,
            t1.analyse_type
        from s_ra_emissions_over t1
        <where>
            AND t1.region_code IN
            <foreach collection="query.regionCodeList" open="(" separator="," close=")" item="item">
                #{item}
            </foreach>
            <choose>
                <when test="query.industryId != null and query.industryId != ''">
                    and t1.industry_id = #{query.industryId}
                </when>
                <otherwise>
                    <!-- 未指定行业，则查询整个区域的排放量，industry_id为null的记录表示整个区域的排放量记录 -->
                    and t1.industry_id is null
                </otherwise>
            </choose>
            <if test="query.pollutantName != null and query.pollutantName != ''">
                and t1.pollutant_name ilike concat('%', #{query.pollutantName}, '%')
            </if>
            <if test="query.statYear != null and query.statYear != ''">
                and t1.stat_year = #{query.statYear}
            </if>
            <if test="query.analyseType != null and query.analyseType != ''">
                and t1.analyse_type = #{query.analyseType}
            </if>
        </where>
        order by t1.region_code, t1.pollutant_name, t1.stat_year, t1.id
    </select>
    <select id="selectIndustryEmissions" resultType="cn.cecep.talroad.vo.analysis.SRaEmissionsOverVo$IndustryVo">
        select
            t1.industry_name,
            t1.pollutant_name,
            t1.stat_year,
            t1.permit_emmisons,
            t1.reality_emmisons,
            t1.emmisons_ratio,
            t1.analyse_type
        from s_ra_emissions_over t1
        <where>
            AND t1.industry_id IS NOT NULL
            <choose>
                <when test="query.regionCode != null and query.regionCode != ''">
                    and t1.region_code = #{query.regionCode}
                </when>
                <otherwise>
                    <!-- 未指定区域，则查询整个行业的排放量，region_code为null的记录表示整个行业的排放量记录 -->
                    and t1.region_code is null
                </otherwise>
            </choose>
            <if test="query.pollutantName != null and query.pollutantName != ''">
                and t1.pollutant_name ilike concat('%', #{query.pollutantName}, '%')
            </if>
            <if test="query.statYear != null and query.statYear != ''">
                and t1.stat_year = #{query.statYear}
            </if>
            <if test="query.analyseType != null and query.analyseType != ''">
                and t1.analyse_type = #{query.analyseType}
            </if>
        </where>
        order by t1.industry_id, t1.pollutant_name, t1.stat_year, t1.id
    </select>

</mapper>
