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

    <resultMap type="cn.cecep.talroad.domain.SEnvWaterMonHour" id="SEnvWaterMonHourResult">
        <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="selectSEnvWaterMonHourVo">
        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="selectSEnvWaterMonHourList" parameterType="cn.cecep.talroad.domain.SEnvWaterMonHour" resultType="cn.cecep.talroad.vo.SEnvWaterMonHourVo">
        select
            sewmr.monitoring_time               monitoringTime,
            sewmr.sewage                        ,
            sewmr.sewage_emission               sewageEmission,
            sewmr.ph_value                      phValue,
            sewmr.cod,
            sewmr.cod_emission                  codEmission,
            sewmr.ammonia_nitrogen              ammoniaNitrogen,
            sewmr.ammonia_nitrogen_emission     ammoniaNitrogenEmission,
            sewmr.cod_concentration             codConcentration,
            sewmr.wastewater_flow               wastewaterFlow,
            sewmr.nitrogen_concentration        nitrogenConcentration,
            sewmr.nitrogen_concentration_emission        nitrogenConcentrationEmission,
            sewmr.phosphorus_concentration      phosphorusConcentration,
            sewmr.phosphorus_concentration_emission      phosphorusConcentrationEmission,
            polet.outlet_name              outletName,
            polet.outlet_code              outletCode,
            pf.factory_name             factoryName,
            po.monitoring_point_name    monitoringPointName,
            polet.mark
        from s_env_water_mon_hour sewmr
            left join pc_outlet po ON sewmr.outlet_id = po."id"
            LEFT JOIN pc_outlet polet ON polet."id" = po."parent_outlet_id"
            left join pc_factory pf ON sewmr.factory_id = pf."id"
        <where>
            po.pollutant_type = '4'
            <if test="sEnvWaterMonHour.factoryId != null  and sEnvWaterMonHour.factoryId != ''"> and sewmr.factory_id = #{sEnvWaterMonHour.factoryId}</if>
            <if test="sEnvWaterMonHour.outletId != null  and sEnvWaterMonHour.outletId != ''"> and outlet_id = #{sEnvWaterMonHour.outletId}</if>
            <if test="sEnvWaterMonHour.dataType != null  and sEnvWaterMonHour.dataType != ''"> and data_type = #{sEnvWaterMonHour.dataType}</if>
            <if test="sEnvWaterMonHour.monitoringStartTime != null  and sEnvWaterMonHour.monitoringStartTime != '' and sEnvWaterMonHour.monitoringEndTime != null  and sEnvWaterMonHour.monitoringEndTime != ''">
                and monitoring_time BETWEEN #{sEnvWaterMonHour.monitoringStartTime} and #{sEnvWaterMonHour.monitoringEndTime}
            </if>

            <if test="sEnvWaterMonHour.monitoringPointName != null  and sEnvWaterMonHour.monitoringPointName != ''"> and (po.monitoring_point_name = #{sEnvWaterMonHour.monitoringPointName} or polet.monitoring_point_name = #{sEnvWaterMonHour.monitoringPointName})</if>
            <if test="sEnvWaterMonHour.mark != null  and sEnvWaterMonHour.mark != ''"> and mark = #{sEnvWaterMonHour.mark}</if>
            <if test="sEnvWaterMonHour.outletIdArr != null and sEnvWaterMonHour.outletIdArr.length != 0">
                <foreach collection="sEnvWaterMonHour.outletIdArr" item="item" separator="," open="and outlet_id in (" close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        ${ew.customSqlSegment}
    </select>


    <select id="selectSEnvWaterMonHourListGroupByYear" resultType="cn.cecep.talroad.vo.SEnvWaterMonHourVo">
        SELECT
            series.generate_date                                                            AS monitoringTime,
            ROUND ( COALESCE( cod_emissions, 0 )::numeric, 2 )                              AS cod,
            ROUND ( COALESCE( totalNitrogen_emissions, 0 )::numeric, 2 )                    AS codEmission,
            ROUND ( COALESCE( totalPhosphorus_emissions, 0 )::numeric, 2 )                  AS ammoniaNitrogen,
            ROUND ( COALESCE( ammoniaNitrogen_emissions, 0 )::numeric, 2 )                  AS ammoniaNitrogenEmission
        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 'W01018' THEN CAST ( pollutant_emissions AS DECIMAL ) ELSE 0 END )     cod_emissions,
                SUM ( CASE pollutant_code WHEN 'W21001' THEN CAST ( pollutant_emissions AS DECIMAL ) ELSE 0 END )     totalNitrogen_emissions,
                SUM ( CASE pollutant_code WHEN 'W21011' THEN CAST ( pollutant_emissions AS DECIMAL ) ELSE 0 END )     totalPhosphorus_emissions,
                SUM ( CASE pollutant_code WHEN 'W21003' THEN CAST ( pollutant_emissions AS DECIMAL ) ELSE 0 END )     ammoniaNitrogen_emissions
            FROM pc_factory pf
                LEFT JOIN s_ra_online_emissions_water sroew ON sroew.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="selectAvgDataForDay" resultType="cn.cecep.talroad.domain.SEnvWaterMonHour">
        --查询小时内分钟数据的平均浓度
        select     round(avg(cast(phosphorus_concentration as decimal)),2) as phosphorus_concentration,
                   round(avg(cast(nitrogen_concentration as decimal)),2) as nitrogen_concentration,
                   round(avg(cast(cod_concentration as decimal)),2) as cod_concentration,
                   round(avg(cast(ammonia_nitrogen as decimal)),2) as ammonia_nitrogen,
                   outlet_id,
                   factory_id
        from s_env_water_mon_hour
        where
            phosphorus_concentration != '/' and nitrogen_concentration != '/' and cod_concentration != '/' and ammonia_nitrogen != '/'
            and data_type = 'H'
            and monitoring_time >= #{dateStr}
        group by outlet_id,factory_id
    </select>
    <select id="selectLastDayData" resultType="cn.cecep.talroad.domain.SEnvWaterMonHour">
        select r.* from pc_outlet po
        left join s_env_water_mon_hour r on po.factory_id =  r.factory_id and po.id = r.outlet_id
        where r.data_type ='H' and r.monitoring_time between #{lastDayBegin} and #{lastDayEnd} and po.pollutant_type ='4' and po.pollutant_outlet_type = #{pollutantOutletType}
          and (r.cod_flag in ('C','M','D','MD','K','P','Vgd','VGD','TD') or r.ammonia_nitrogen_flag in ('C','M','D','MD','K','P','Vgd','VGD','TD') or r.nitrogen_concentration_flag in ('C','M','D','MD','K','P','VGD','TD') or r.phosphorus_concentration_flag in ('C','M','D','MD','K','P','VGD','TD'))
    </select>

    <select id="selectSRaWaterPermittedOnlineListBy" resultMap="SEnvWaterMonHourResult">
        select * from s_env_water_mon_hour
        where data_type ='H'
        <if test="monitorStartTime != null  and monitorStartTime != ''"> and monitoring_time >=#{monitorStartTime} </if>
        <if test="monitorEndTime != null  and monitorEndTime != ''"> and monitoring_time &lt;=#{monitorEndTime} </if>
        <if test="monitors != null and monitors.size() != 0">
            <foreach collection="monitors" item="chile" close=" )" open="  and outlet_id in (" separator=",">
                #{chile}
            </foreach>
        </if>
        order by outlet_id,monitoring_time asc
    </select>

    <select id="selectSRaWaterSourceListBy" resultMap="SEnvWaterMonHourResult">
        select water.* from s_env_water_mon_hour as water
        inner  join pc_outlet as po on po.id=water.outlet_id
        where data_type ='H'
        <if test='type=="1"'>
            and po.source_online='1'
        </if>
        <if test='type=="2"'>
            and po.source_permission='1'
        </if>
        <if test="monitorStartTime != null  and monitorStartTime != ''"> and water.monitoring_time >=#{monitorStartTime} </if>
        <if test="monitorEndTime != null  and monitorEndTime != ''"> and water.monitoring_time &lt;=#{monitorEndTime} </if>
        <if test="monitors != null and monitors.size() != 0">
            <foreach collection="monitors" item="chile" close=" )" open="  and po.factory_id in (" separator=",">
                #{chile}
            </foreach>
        </if>
        order by water.factory_id,water.outlet_id,water.monitoring_time asc
    </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 = 'H'
               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="selectManualSRaWaterPermittedOnlineListBy" resultMap="SEnvWaterMonHourResult">
        select * from s_env_water_self_day
        where data_type ='D'
        <if test="monitorStartTime != null  and monitorStartTime != ''"> and monitoring_time >=#{monitorStartTime} </if>
        <if test="monitorEndTime != null  and monitorEndTime != ''"> and monitoring_time &lt;=#{monitorEndTime} </if>
        <if test="monitors != null and monitors.size() != 0">
            <foreach collection="monitors" item="chile" close=" )" open="  and outlet_id in (" separator=",">
                #{chile}
            </foreach>
        </if>
        order by outlet_id,monitoring_time asc
    </select>

    <select id="selectManualSRaWaterSourceListBy" resultMap="SEnvWaterMonHourResult">
        select water.* from s_env_water_self_day as water
        inner  join pc_outlet as po on po.id=water.outlet_id
        where data_type ='H'
        <if test='type=="1"'>
            and po.source_online='1'
        </if>
        <if test='type=="2"'>
            and po.source_permission='1'
        </if>
        <if test="monitorStartTime != null  and monitorStartTime != ''"> and water.monitoring_time >=#{monitorStartTime} </if>
        <if test="monitorEndTime != null  and monitorEndTime != ''"> and water.monitoring_time &lt;=#{monitorEndTime} </if>
        <if test="monitors != null and monitors.size() != 0">
            <foreach collection="monitors" item="chile" close=" )" open="  and po.factory_id in (" separator=",">
                #{chile}
            </foreach>
        </if>
        order by water.factory_id,water.outlet_id,water.monitoring_time asc
    </select>
</mapper>
