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

    <resultMap type="cn.cecep.talroad.domain.BMainPollDisSelfMon" id="BMainPollDisSelfMonResult">
        <result property="monId"    column="mon_id"    />
        <result property="factoryId"    column="factory_id"    />
        <result property="pollDisId"    column="poll_dis_id"    />
        <result property="pollutantType"    column="pollutant_type"    />
        <result property="outletCode"    column="outlet_code"    />
        <result property="outletName"    column="outlet_name"    />
        <result property="monitoringContent"    column="monitoring_content"    />
        <result property="pollutantsName"    column="pollutants_name"    />
        <result property="monitoringFacility"    column="monitoring_facility"    />
        <result property="monitoringAutomaticNetworking"    column="monitoring_automatic_networking"    />
        <result property="monitoringAutomaticName"    column="monitoring_automatic_name"    />
        <result property="monitoringAutomaticAddress"    column="monitoring_automatic_address"    />
        <result property="monitoringAutomaticRequire"    column="monitoring_automatic_require"    />
        <result property="monitoringManualSampling"    column="monitoring_manual_sampling"    />
        <result property="monitoringManualFrequency"    column="monitoring_manual_frequency"    />
        <result property="monitoringManualMeans"    column="monitoring_manual_means"    />
        <result property="otherInformation"    column="other_information"    />
    </resultMap>


    <sql id="selectBMainPollDisSelfMonVo">
        select "mon_id", factory_id, poll_dis_id, pollutant_type, outlet_code, outlet_name, monitoring_content, pollutants_name, monitoring_facility, monitoring_automatic_networking, monitoring_automatic_name, monitoring_automatic_address, monitoring_automatic_require, monitoring_manual_sampling, monitoring_manual_frequency, monitoring_manual_means, other_information from b_main_poll_dis_self_mon
    </sql>

    <select id="selectBMainPollDisSelfMonList" parameterType="cn.cecep.talroad.domain.BMainPollDisSelfMon" resultMap="BMainPollDisSelfMonResult">
        <include refid="selectBMainPollDisSelfMonVo"/>
        <where>
            <if test="factoryId != null  and factoryId != ''"> and factory_id = #{factoryId}</if>
            <if test="pollDisId != null  and pollDisId != ''"> and poll_dis_id = #{pollDisId}</if>
            <if test="pollutantType != null  and pollutantType != ''"> and pollutant_type = #{pollutantType}</if>
            <if test="outletCode != null  and outletCode != ''"> and outlet_code = #{outletCode}</if>
            <if test="outletName != null  and outletName != ''"> and outlet_name like concat('%', #{outletName}, '%')</if>
            <if test="monitoringContent != null  and monitoringContent != ''"> and monitoring_content = #{monitoringContent}</if>
            <if test="pollutantsName != null  and pollutantsName != ''"> and pollutants_name like concat('%', #{pollutantsName}, '%')</if>
            <if test="monitoringFacility != null  and monitoringFacility != ''"> and monitoring_facility = #{monitoringFacility}</if>
            <if test="monitoringAutomaticNetworking != null  and monitoringAutomaticNetworking != ''"> and monitoring_automatic_networking = #{monitoringAutomaticNetworking}</if>
            <if test="monitoringAutomaticName != null  and monitoringAutomaticName != ''"> and monitoring_automatic_name like concat('%', #{monitoringAutomaticName}, '%')</if>
            <if test="monitoringAutomaticAddress != null  and monitoringAutomaticAddress != ''"> and monitoring_automatic_address = #{monitoringAutomaticAddress}</if>
            <if test="monitoringAutomaticRequire != null  and monitoringAutomaticRequire != ''"> and monitoring_automatic_require = #{monitoringAutomaticRequire}</if>
            <if test="monitoringManualSampling != null  and monitoringManualSampling != ''"> and monitoring_manual_sampling = #{monitoringManualSampling}</if>
            <if test="monitoringManualFrequency != null  and monitoringManualFrequency != ''"> and monitoring_manual_frequency = #{monitoringManualFrequency}</if>
            <if test="monitoringManualMeans != null  and monitoringManualMeans != ''"> and monitoring_manual_means = #{monitoringManualMeans}</if>
            <if test="otherInformation != null  and otherInformation != ''"> and other_information = #{otherInformation}</if>
        </where>
    </select>

    <select id="selectCountByPollutantsName" resultType="cn.cecep.talroad.vo.homepage.HpmEmissionRatioOverviewVo">
        SELECT
            SUM( CASE WHEN monitoring_facility = '手工' AND pollutants_name = 'SO2' THEN 1 ELSE 0 END ) permissibleDischargeSo2,
            SUM( CASE WHEN monitoring_facility = '手工' AND pollutants_name = 'NOx' THEN 1 ELSE 0 END ) permissibleDischargeNox,
            SUM( CASE WHEN monitoring_facility = '手工' AND pollutants_name = '颗粒物' THEN 1 ELSE 0 END ) permissibleDischargePm,
            SUM( CASE WHEN monitoring_facility = '手工' AND pollutants_name = 'VOCs' THEN 1 ELSE 0 END ) permissibleDischargeVocs,
            SUM( CASE WHEN monitoring_facility = '手工' AND pollutants_name = 'COD' THEN 1 ELSE 0 END ) permissibleDischargeCod,
            SUM( CASE WHEN monitoring_facility = '手工' AND pollutants_name = '氨氮' THEN 1 ELSE 0 END ) permissibleDischargeNhn,
            SUM( CASE WHEN monitoring_facility = '手工' AND pollutants_name = '总磷' THEN 1 ELSE 0 END ) permissibleDischargeTp,
            SUM( CASE WHEN monitoring_facility = '手工' AND pollutants_name = '总氮' THEN 1 ELSE 0 END ) permissibleDischargeTn,

            SUM( CASE WHEN monitoring_facility = '自动' AND pollutants_name = 'SO2' THEN 1 ELSE 0 END ) actualDischargeSo2,
            SUM( CASE WHEN monitoring_facility = '自动' AND pollutants_name = 'NOx' THEN 1 ELSE 0 END ) actualDischargeNox,
            SUM( CASE WHEN monitoring_facility = '自动' AND pollutants_name = '颗粒物' THEN 1 ELSE 0 END ) actualDischargePm,
            SUM( CASE WHEN monitoring_facility = '自动' AND pollutants_name = 'VOCs' THEN 1 ELSE 0 END ) actualDischargeVocs,
            SUM( CASE WHEN monitoring_facility = '自动' AND pollutants_name = 'COD' THEN 1 ELSE 0 END ) actualDischargeCod,
            SUM( CASE WHEN monitoring_facility = '自动' AND pollutants_name = '氨氮' THEN 1 ELSE 0 END ) actualDischargeNhn,
            SUM( CASE WHEN monitoring_facility = '自动' AND pollutants_name = '总磷' THEN 1 ELSE 0 END ) actualDischargeTp,
            SUM( CASE WHEN monitoring_facility = '自动' AND pollutants_name = '总氮' THEN 1 ELSE 0 END ) actualDischargeTn
        FROM
            ( <include refid="selectBMainPollDisSelfMonVo"/> UNION
            <include refid="cn.cecep.talroad.mapper.BMainPollDisSelfMonOtherMapper.selectBMainPollDisSelfMonOtherVo"/> ) tmp
        LEFT JOIN pc_factory pf ON pf.id=tmp.factory_id
        <where>
            <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>
        </where>
    </select>


    <select id="selectEmissionAndstatus" resultType="cn.cecep.talroad.vo.BMainEmissionAndstatusCountVo">
        SELECT
        SUM( CASE WHEN monitoring_facility = '手工' AND pollutant_type = '废气' THEN 1 ELSE 0 END ) permissibleDischargeAtmosphere,
        SUM( CASE WHEN monitoring_facility = '自动' AND pollutant_type = '废气' THEN 1 ELSE 0 END ) actualDischargeAtmosphere,
        SUM( CASE WHEN monitoring_facility = '手工' AND pollutant_type = '废水' THEN 1 ELSE 0 END ) permissibleDischargeWater,
        SUM( CASE WHEN monitoring_facility = '自动' AND pollutant_type = '废水' THEN 1 ELSE 0 END ) actualDischargeWater,
        SUM( CASE WHEN pollutant_type = '废气' AND monitoring_facility IS NOT NULL THEN 1 ELSE 0 END ) atmosphere,
        SUM( CASE WHEN pollutant_type = '废水' AND monitoring_facility IS NOT NULL THEN 1 ELSE 0 END ) water
        FROM
        ( <include refid="selectBMainPollDisSelfMonVo"/> UNION
        <include refid="cn.cecep.talroad.mapper.BMainPollDisSelfMonOtherMapper.selectBMainPollDisSelfMonOtherVo"/> ) tmp
        LEFT JOIN pc_factory pf ON pf.id=tmp.factory_id
        <where>
            <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>
        </where>
    </select>

    <select id="selectBMainPollDisMonList" resultType="cn.cecep.talroad.domain.dto.BMainPollMonDTO" >
        select tt.factory_id,tt.outlet_code,tt.outlet_name,tt.device_id,bdp.is_online  from
            (select
                 w.factory_id ,
                 w.outlet_code ,po.outlet_name,
                 regexp_split_to_table(w.pro_facility_id,E',')  as device_id
             from b_main_poll_dis_self_mon a
                      left join pc_outlet po on a.factory_id = po.factory_id and a.outlet_code = po.outlet_code
                      left join b_main_production_organiz_gas w on po.factory_id = w.factory_id and po.outlet_code = w.outlet_code
             where a.monitoring_facility ='自动' and a.pollutant_type =#{type} and w.id is not null and w.outlet_mon_type ='自动' and w.pro_facility_id is not null
            )TT
                left join bas_device_param bdp on tt.device_id = bdp.device_id and  bdp.create_time>=#{startTime} and bdp.create_time &lt;=#{endTime}
        group by tt.factory_id,tt.outlet_code,tt.device_id,tt.outlet_name,bdp.is_online
    </select>

    <select id="selectGasDataList" resultMap="BMainPollDisSelfMonResult">
        select * from b_main_poll_dis_self_mon
        where monitoring_facility='手工'
          and pollutant_type ='废气'
          and monitoring_manual_frequency = #{frequency}
    </select>
    <select id="selectWaterDataList" resultMap="BMainPollDisSelfMonResult">
        select * from b_main_poll_dis_self_mon
        where monitoring_facility='手工'
        and pollutant_type ='废水'
        and monitoring_manual_frequency = #{frequency}
    </select>
</mapper>
