<?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.SEnvGasMonDayMapper">
    <sql id="Base_Column_List">
        id,
        factory_id,
        outlet_id,
        data_type,
        monitoring_time,
        so2,
        so2_convert,
        so2_emission,
        nox,
        nox_convert,
        nox_emission,
        "no",
        no_convert,
        no_emission,
        no2,
        no2_convert,
        no2_emission,
        smoke_dust,
        smoke_dust_convert,
        smoke_dust_emission,
        o3,
        oxygen,
        smoke_dust_tem,
        smoke_dust_pressure,
        smoke_dust_flow_velocity,
        smoke_dust_humidity,
        pm,
        pm_convert,
        pm_emission
    </sql>
    <resultMap type="cn.cecep.talroad.domain.SEnvGasMonDay" id="SEnvGasMonDayResult">
        <result property="id"    column="id"    />
        <result property="factoryId"    column="factory_id"    />
        <result property="outletId"    column="outlet_id"    />
        <result property="dataType"    column="data_type"    />
        <result property="monitoringTime"    column="monitoring_time"    />
        <result property="so2"    column="so2"    />
        <result property="so2Convert"    column="so2_convert"    />
        <result property="so2Emission"    column="so2_emission"    />
        <result property="nox"    column="nox"    />
        <result property="noxConvert"    column="nox_convert"    />
        <result property="noxEmission"    column="nox_emission"    />
        <result property="no"    column="no"    />
        <result property="noConvert"    column="no_convert"    />
        <result property="noEmission"    column="no_emission"    />
        <result property="no2"    column="no2"    />
        <result property="no2Convert"    column="no2_convert"    />
        <result property="no2Emission"    column="no2_emission"    />
        <result property="smokeDust"    column="smoke_dust"    />
        <result property="smokeDustConvert"    column="smoke_dust_convert"    />
        <result property="smokeDustEmission"    column="smoke_dust_emission"    />
        <result property="pm"    column="pm"    />
        <result property="pmConvert"    column="pm_convert"    />
        <result property="pmEmission"    column="pm_emission"    />
        <result property="vocs"    column="vocs"    />
        <result property="vocsConvert"    column="vocs_convert"    />
        <result property="vocsEmission"    column="vocs_emission"    />
        <result property="o3"    column="o3"    />
        <result property="oxygen"    column="oxygen"    />
        <result property="smokeDustTem"    column="smoke_dust_tem"    />
        <result property="smokeDustPressure"    column="smoke_dust_pressure"    />
        <result property="smokeDustFlowVelocity"    column="smoke_dust_flow_velocity"    />
        <result property="smokeDustHumidity"    column="smoke_dust_humidity"    />
        <result property="so2Flag"    column="so2_flag"    />
        <result property="noxFlag"    column="nox_flag"    />
        <result property="smokeDustFlag"    column="smoke_dust_flag"    />
        <result property="dataFlag"    column="data_flag"    />
        <result property="vocsFlag"    column="vocs_flag"    />
        <result property="so2CorrectEmission" column="so2_correct_emission" />
        <result property="noxCorrectEmission" column="nox_correct_emission" />
        <result property="vocsCorrectEmission" column="vocs_correct_emission" />
        <result property="smokeDustCorrectEmission" column="smoke_dust_correct_emission" />
    </resultMap>


    <sql id="selectSEnvGasMonDayVo">
        select id, factory_id, outlet_id, data_type, monitoring_time, so2,
               so2_convert, nox, nox_convert, "no", no_convert, no2, no2_convert, smoke_dust,
               smoke_dust_convert, o3, oxygen, smoke_dust_tem, smoke_dust_pressure,
               smoke_dust_flow_velocity, smoke_dust_humidity, pm, pm_convert,
               vocs, vocs_convert,so2_flag,nox_flag,smoke_dust_flag,vocs_falg,data_flag,
               nox_correct_emission,so2_correct_emission,vocs_correct_emission,smoke_dust_correct_emission
        from s_env_gas_mon_hour
    </sql>


    <select id="selectSEnvGasMonDayListGroupByYear" resultType="cn.cecep.talroad.vo.SEnvGasMonDayVo">
        SELECT
            series.generate_date                                AS monitoringTime,
            ROUND ( COALESCE( tmp.so2_emissions, 0)::numeric, 2)                        AS so2Emission,
            ROUND ( COALESCE( tmp.nox_emissions, 0)::numeric, 2)                        AS noxEmission,
            ROUND ( COALESCE( tmp.pm_emissions, 0)::numeric, 2)                         AS pmEmission,
            ROUND ( COALESCE( tmp.vocs_emissions, 0)::numeric, 2)                       AS vocsEmission
        FROM
        ( SELECT to_char(generate_series(#{monitoringStartTime}::date, #{monitoringEndTime}::date, '1 month'), 'yyyy-mm') generate_date ) series
        LEFT JOIN (
            SELECT
                to_char( monitor_time, 'YYYY-MM' ) monitoringTime,
                SUM ( CASE pollutant_code WHEN 'A21026' THEN CAST ( pollutant_emissions AS DECIMAL ) ELSE 0 END )     so2_emissions,
                SUM ( CASE pollutant_code WHEN 'A21002' THEN CAST ( pollutant_emissions AS DECIMAL ) ELSE 0 END )     nox_emissions,
                SUM ( CASE pollutant_code WHEN 'A99911' THEN CAST ( pollutant_emissions AS DECIMAL ) ELSE 0 END )     pm_emissions,
                SUM ( CASE pollutant_code WHEN 'A99901' THEN CAST ( pollutant_emissions AS DECIMAL ) ELSE 0 END )     vocs_emissions
            FROM pc_factory pf
                LEFT JOIN s_ra_online_emissions_gas sroeg ON sroeg.factory_id = pf.ID
            <where>
                <if test="industry != null and industry != ''">
                    AND pf.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>
                AND monitor_time BETWEEN #{monitoringStartTime}::TIMESTAMP AND #{monitoringEndTime}::TIMESTAMP
            </where>
            GROUP BY monitoringTime
        ) tmp ON series.generate_date = tmp.monitoringTime
        ORDER BY monitoringTime
    </select>

    <select id="selectEmissionAvg" resultType="cn.cecep.talroad.vo.SEnvGasMonDayVo$OxygenEmissionAvg">
        SELECT
            t1.outlet_id,
            round(avg(oxygen_emission::numeric), 6) oxygen_emission_avg
        FROM (
            SELECT
                outlet_id, date_time, oxygen_emission
            FROM s_env_gas_mon_hour
            WHERE data_type = 'H'
            AND date_time >= to_timestamp(#{startTime}, 'yyyy-mm-dd hh24:MI:SS')
            AND date_time &lt;= to_timestamp(#{endTime}, 'yyyy-mm-dd hh24:MI:SS')
            AND outlet_Id IN
            <foreach collection="outletIds" open="(" separator="," close=")" item="item">
                #{item}
            </foreach>
        ) t1
        <!-- 关联查询监测点停产记录 -->
        LEFT JOIN (
            SELECT t2.id , t3.start_time , t3.end_time
            FROM pc_outlet t2
            INNER JOIN ods_online_ol_stopinfo_a t3 ON t2.outlet_code = t3.port_num AND t2.factory_id = t3.factory_id
            WHERE t3.start_time >= to_timestamp(#{startTime}, 'yyyy-mm-dd hh24:MI:SS')
            and t3.end_time &lt;= to_timestamp(#{endTime}, 'yyyy-mm-dd hh24:MI:SS')
        ) t2 ON t1.outlet_id = t2."id" AND t1.date_time >= t2.start_time AND t1.date_time &lt;= t2.end_time

        <!-- 关联查询监测点白名单记录 -->
        LEFT JOIN (
            SELECT
                t2.id,
                t3.start_time,
                t3.end_time
            FROM pc_outlet t2
            INNER JOIN ods_online_ol_ps_whitelist_a t3 ON t2.outlet_code = t3.port_num AND t2.factory_id = t3.factory_id
            WHERE t3.start_time >= to_timestamp(#{startTime}, 'yyyy-mm-dd hh24:MI:SS')
            and t3.end_time &lt;= to_timestamp(#{endTime}, 'yyyy-mm-dd hh24:MI:SS')
            AND upper(t3.pollutant_code) IN ( SELECT upper(poll_code) FROM pc_pollutant WHERE is_gas_type = '1')
        ) t3 ON t1.outlet_id = t3."id" AND t1.date_time >= t3.start_time AND t1.date_time &lt;= t3.end_time
        WHERE t2."id" IS NULL
        AND t3."id" IS NULL
        GROUP BY t1.outlet_id
    </select>
    <select id="selectLastDayData"
        resultType="cn.cecep.talroad.vo.analysis.FactoryOutletStaticsVo">
        select a.* from
            (select
                 factory_id as factoryId,
                 outlet_id as outletId ,
                 count(1) as actualCount
             from
                 s_env_gas_mon_day segmh
             WHERE
                 data_type = 'D'
               AND monitoring_time between #{lastDayBegin} and #{lastDayEnd}
             group by factory_id , outlet_id
            ) a
                inner join pc_outlet po on a.factoryId = po.factory_id and a.outletId = po.id
        where po.pollutant_type ='4'
          and po.pollutant_outlet_type ='1'
          and po.monitoring_point_name not like '%入口%'
          and po.monitoring_point_name not like '%进口%'

    </select>


</mapper>
