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

    <resultMap type="cn.cecep.talroad.domain.SEnvWaterMonDay" id="SEnvWaterMonDayResult">
        <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="sewage"    column="sewage"    />
        <result property="sewageEmission"    column="sewage_emission"    />
        <result property="cod"    column="cod"    />
        <result property="codConcentration"    column="cod_concentration"    />
        <result property="codEmission"    column="cod_emission"    />
        <result property="ammoniaNitrogen"    column="ammonia_nitrogen"    />
        <result property="ammoniaNitrogenEmission"    column="ammonia_nitrogen_emission"    />
        <result property="nitrogenConcentration"    column="nitrogen_concentration"    />
        <result property="nitrogenConcentrationEmission"    column="nitrogen_concentration_emission"    />
        <result property="phosphorusConcentration"    column="phosphorus_concentration"    />
        <result property="phosphorusConcentrationEmission"    column="phosphorus_concentration_emission"    />
        <result property="phValue"    column="ph_value"    />
        <result property="wastewaterFlow"    column="wastewater_flow"    />
        <result property="codFlag" column="cod_flag" />
        <result property="ammoniaNitrogenFlag" column="ammonia_nitrogen_flag" />
        <result property="nitrogenConcentrationFlag" column="nitrogen_concentration_flag" />
        <result property="phosphorusConcentrationFlag" column="phosphorus_concentration_flag" />

    </resultMap>


    <sql id="selectSEnvWaterMonDayVo">
        select id, factory_id, outlet_id, data_type, monitoring_time, sewage, sewage_emission, cod, cod_concentration, cod_emission, ammonia_nitrogen, ammonia_nitrogen_emission, nitrogen_concentration, nitrogen_concentration_emission, phosphorus_concentration, phosphorus_concentration_emission, ph_value, wastewater_flow,cod_flag,ammonia_nitrogen_flag,phosphorus_concentration_flag,nitrogen_concentration_flag from s_env_water_mon_hour
    </sql>

    <select id="selectMaxByDate" resultMap="SEnvWaterMonDayResult">
        select factory_id,outlet_id,
               max(cod_emission) as cod_emission,
               max(ammonia_nitrogen_emission) as ammonia_nitrogen_emission,
               max(nitrogen_concentration_emission) as nitrogen_concentration_emission,
               max(phosphorus_concentration_emission) as phosphorus_concentration_emission
        from
            s_env_water_mon_day
        where
                monitoring_time &gt;= #{startTime}
            and monitoring_time &lt; #{endTime}
        GROUP BY
            factory_id,
            outlet_id
    </select>
    <select id="selectLastDayCountData"
        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_water_mon_real 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 ='2'
          and po.monitoring_point_name not like '%入口%'
          and po.monitoring_point_name not like '%进口%'

    </select>
    <select id="selectHourDataByTime" resultMap="SEnvWaterMonDayResult">
        select * from s_env_water_mon_day where factory_id = #{factoryId} and outlet_id = #{outletId} and data_type ='H' and monitoring_time = #{monitorTime}
    </select>


    <select id="selectEmissionAvgAndMax" resultType="cn.cecep.talroad.vo.analysis.FactoryOutletStaticsVo$EmissionAvgAndMax">
        SELECT t1.outlet_id,
               round(avg(t1.sewage_mission::numeric), 6)                   sewageEmissionAvg,
               round(MAX(t1.sewage_mission::numeric), 6)                   sewageEmissionMax,
               round(avg(t1.cod_emission::numeric), 6)                     codEmissionAvg,
               round(MAX(t1.cod_emission::numeric), 6)                     codEmissionMax,
               round(avg(t1.ammonia_nitrogen_emission::numeric), 6)        ammoniaNitrogenEmissionAvg,
               round(MAX(t1.ammonia_nitrogen_emission::numeric), 6)        ammoniaNitrogenEmissionMax,
               round(avg(t1.nitrogen_oncentration_emission::numeric), 6)  nitrogenConcentrationEmissionAvg,
               round(MAX(t1.nitrogen_oncentration_emission::numeric), 6)   nitrogenConcentrationEmissionMax,
               round(avg(t1.phosphorus_oncentration_emission::numeric), 6) phosphorusConcentrationEmissionAvg,
               round(MAX(t1.phosphorus_oncentration_emission::numeric), 6)phosphorusConcentrationEmissionMax
                FROM (
                SELECT
                   outlet_id,
                   sewage_mission,
                   cod_emission,
                   ammonia_nitrogen_emission,
                   nitrogen_oncentration_emission,
                   phosphorus_oncentration_emission
                FROM s_env_water_mon_day
                WHERE data_type = 'd'
                  AND data_flag not in ('C', 'M', 'D', 'MD', 'K', 'P', 'VGD', 'TD', 'F')
                  AND monitoring_time >= to_timestamp(#{startTime}, 'yyyy-mm-dd hh24:MI:SS')
                  AND monitoring_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>
</mapper>
