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

    <resultMap type="cn.cecep.talroad.domain.analysis.SRaEmissionsMonitoringVariance" id="SRaEmissionsMonitoringVarianceResult">
        <result property="id"    column="id"    />
        <result property="factoryId"    column="factory_id"    />
        <result property="stationTime"    column="station_time"    />
        <result property="stationId"    column="station_id"    />
        <result property="pollId"    column="poll_id"    />
        <result property="concentrationOnline"    column="concentration_online"    />
        <result property="concentrationPm"    column="concentration_pm"    />
        <result property="concentrationEtl"    column="concentration_etl"    />
        <result property="differenceRatio"    column="difference_ratio"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>


    <sql id="selectSRaEmissionsMonitoringVarianceVo">
        select id, factory_id, station_time, station_id, poll_id, concentration_online, concentration_pm, concentration_etl, difference_ratio, create_time, update_time from s_ra_emissions_monitoring_variance
    </sql>

    <select id="selectSRaEmissionsMonitoringVarianceList" parameterType="cn.cecep.talroad.domain.analysis.SRaEmissionsMonitoringVariance" resultMap="SRaEmissionsMonitoringVarianceResult">
        <include refid="selectSRaEmissionsMonitoringVarianceVo"/>
        <where>
            <if test="factoryId != null  and factoryId != ''"> and factory_id = #{factoryId}</if>
            <if test="stationTime != null  and stationTime != ''"> and station_time = #{stationTime}</if>
            <if test="stationId != null "> and station_id = #{stationId}</if>
            <if test="pollId != null  and pollId != ''"> and poll_id = #{pollId}</if>
            <if test="concentrationOnline != null  and concentrationOnline != ''"> and concentration_online = #{concentrationOnline}</if>
            <if test="concentrationPm != null  and concentrationPm != ''"> and concentration_pm = #{concentrationPm}</if>
            <if test="concentrationEtl != null  and concentrationEtl != ''"> and concentration_etl = #{concentrationEtl}</if>
            <if test="differenceRatio != null  and differenceRatio != ''"> and difference_ratio = #{differenceRatio}</if>
        </where>
    </select>

    <select id="selectList" resultType="cn.cecep.talroad.vo.analysis.SRaEmissionsMonitoringVarianceVo">
        select
            t1.*,
            t2.factory_name,
            t2.industry_permission_name,
            t2.country_code,
            t2.country_name,
            t3.outlet_code,
            t3.outlet_name,
            t4.poll_name,
            t4.poll_code
        from s_ra_emissions_monitoring_variance t1
             left join pc_factory t2 on t1.factory_id = t2.id
             left join pc_outlet t3 on t1.station_id::VARCHAR = t3.id
             left JOIN pc_pollutant t4 on t1.poll_id = t4.id
        <where>
            <if test="query.factoryName != null and query.factoryName != ''"> and t2.factory_name like concat('%', #{query.factoryName}, '%')</if>
            <if test="query.industryPermissionName != null and query.industryPermissionName != ''"> and t2.industry_permission_name = #{query.industryPermissionName}</if>
            <if test="query.countryCode != null and query.countryCode != ''"> and t2.country_code = #{query.countryCode}</if>
            <if test="query.startTime != null and query.startTime != ''">
                and t1.station_time >= to_timestamp(#{query.startTime}, 'yyyy-mm-dd hh24:MI:SS')
            </if>
            <if test="query.endTime != null and query.endTime != ''">
                and t1.station_time &lt;= to_timestamp(#{query.endTime}, 'yyyy-mm-dd hh24:MI:SS')
            </if>
            <if test="query.outletName != null and query.outletName != ''"> and t3.outlet_name like concat('%', #{query.outletName}, '%')</if>
            <if test="query.pollId != null and query.pollId != ''"> and t1.poll_id = #{query.pollId}</if>
        </where>
        ORDER BY t2.factory_name, t1.id
    </select>

    <select id="pollList" resultType="cn.cecep.talroad.domain.PcPollutant">
        SELECT
            DISTINCT t2.*
        FROM s_ra_emissions_monitoring_variance t1
            INNER JOIN pc_pollutant t2 ON t1.poll_id = t2.id
        ORDER BY t2.poll_name
    </select>

</mapper>
