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

    <sql id="selectSRaEmissionsDiffVo">
        select
        t1.id as "id",
        t1.factory_id as "factoryId",
        t1.pollutant_name as "pollutantName",
        t1.sys_emissions as "sysEmissions",
        t1.report_emissions as "reportEmissions",
        t1.diff_rate as "diffRate",
        t1.analyse_date as "analyse_date",
        t1.create_by as "createBy",
        t1.create_time as "createTime",
        t1.update_by as "updateBy",
        t1.update_time as "updateTime",
        t3.factory_name as "factoryName",
        t3.country_code as "regionCode",
        t3.country_name as "regionName",
        t4.arguments_code as "industryCode",
        t4.arguments_name as "industryName"
        from s_ra_emissions_diff t1
        left join pc_factory t3 on t1.factory_id = t3.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'
        ) t4 on t1.factory_id = t4.factory_id
    </sql>
    <!--left join pc_pollutant t5 on t1.pollutant_type = t5.poll_code-->

    <select id="selectEmissionsDiffList" resultType="cn.cecep.talroad.vo.analysis.SRaEmissionsDiffVo">
        <include refid="selectSRaEmissionsDiffVo"/>
        <where>
            <if test="query.factoryName != null  and query.factoryName != ''">
                and t3.factory_name like concat('%', #{query.factoryName}, '%')
            </if>
            <if test="query.regionName != null  and query.regionName != ''">
                and t3.countryName = #{query.regionName}
            </if>
            <if test="query.regionCode != null  and query.regionCode != ''">
                and t3.country_code = #{query.regionCode}
            </if>
            <if test="query.regionCodeList != null and query.regionCodeList.size() > 0">
                AND t3.country_code IN
                <foreach collection="query.regionCodeList" open="(" separator="," close=")" item="item">
                    #{item}
                </foreach>
            </if>
            <if test="query.pollutantName != null  and query.pollutantName != ''">
                and t1.pollutant_name like concat('%', #{query.pollutantName}, '%')
            </if>
            <if test="query.industryName != null  and query.industryName != ''">
                and t4.arguments_name = #{query.industryName}
            </if>
            <if test="query.industryCode != null  and query.industryCode != ''">
                and t4.arguments_code = #{query.industryCode}
            </if>
            <if test="query.analyseTimeStart != null and query.analyseTimeStart != ''">
                and t1.analyse_date >= #{query.analyseTimeStart}
            </if>
            <if test="query.analyseTimeEnd != null and query.analyseTimeEnd != ''">
                and t1.analyse_date &lt;= #{query.analyseTimeEnd}
            </if>
        </where>
        ORDER BY t3.factory_name, t1.id
    </select>

</mapper>
