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

    <select id="selectEmissionsOverFactory" resultType="cn.cecep.talroad.vo.analysis.SRaEmissionsOverFactoryVo">
        select
           t2.factory_name,
           t3.arguments_name AS industry_name,
           t1.stat_scope,
           t1.stat_year,
           t1.pollutant_name,
           t1.analyse_type,
           t1.problem_desc
        from s_ra_emissions_over_factory t1
        LEFT JOIN pc_factory t2 ON t1.factory_id = t2.id
        LEFT JOIN (
            select
                plf.factory_id, plv.arguments_code, plv.arguments_name
            from pc_label_factory plf
            left join pc_label_value plv on plf.label_id = plv.label_id and plf.label_value = plv.arguments_code
            where plf.label_id = '4'
        ) t3 on t1.factory_id = t3.factory_id
        <where>
            <if test="query.factoryName != null  and query.factoryName != ''">
                and t2.factory_name LIKE CONCAT('%', #{query.factoryName}, '%')
            </if>
            <if test="query.industryCode != null  and query.industryCode != ''">
                and t3.arguments_code = #{query.industryCode}
            </if>
            <if test="query.regionCode != null  and query.regionCode != ''">
                and t2.country_code = #{query.regionCode}
            </if>
            <if test="query.regionCodeList != null and query.regionCodeList.size() > 0">
                AND t2.country_code IN
                <foreach collection="query.regionCodeList" open="(" separator="," close=")" item="item">
                    #{item}
                </foreach>
            </if>
            <if test="query.statScope != null  and query.statScope != ''">
                and t1.stat_scope LIKE CONCAT('%', #{query.statScope}, '%')
            </if>
            <if test="query.statYear != null  and query.statYear != ''">
                and t1.stat_year = #{query.statYear}
            </if>
            <if test="query.pollutantName != null  and query.pollutantName != ''">
                and t1.pollutant_name LIKE CONCAT('%', #{query.pollutantName}, '%')
            </if>
            <if test="query.analyseType != null  and query.analyseType != ''">
                and t1.analyse_type = #{query.analyseType}
            </if>
        </where>
        ORDER BY t2.factory_name, t1.id
    </select>
    <select id="selectStatScopeList" resultType="java.lang.String">
        SELECT stat_scope FROM s_ra_emissions_over_factory GROUP BY stat_scope ORDER BY stat_scope
    </select>

</mapper>
