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

    <resultMap type="cn.cecep.talroad.domain.BMainPollDisGasMout" id="BMainPollDisGasMoutResult">
        <result property="moutId"    column="mout_id"    />
        <result property="factoryId"    column="factory_id"    />
        <result property="pollDisId"    column="poll_dis_id"    />
        <result property="outletCode"    column="outlet_code"    />
        <result property="outletName"    column="outlet_name"    />
        <result property="outletUseTotal"    column="outlet_use_total"    />
        <result property="pollutantsType"    column="pollutants_type"    />
        <result property="pollutantsMgHour"    column="pollutants_mg_hour"    />
        <result property="pollutantsMgDay"    column="pollutants_mg_day"    />
        <result property="pollutantsMgRate"    column="pollutants_mg_rate"    />
        <result property="pollutantsTotalFrist"    column="pollutants_total_frist"    />
        <result property="pollutantsTotalSecond"    column="pollutants_total_second"    />
        <result property="pollutantsTotalThird"    column="pollutants_total_third"    />
        <result property="pollutantsTotalFourth"    column="pollutants_total_fourth"    />
        <result property="pollutantsTotalFifth"    column="pollutants_total_fifth"    />
        <result property="exactDemandsPollutantsMg"    column="exact_demands_pollutants_mg"    />
    </resultMap>


    <sql id="selectBMainPollDisGasMoutVo">
        select mout_id, factory_id, poll_dis_id, outlet_code, outlet_name, outlet_use_total, pollutants_type, pollutants_mg_hour, pollutants_mg_day, pollutants_mg_rate, pollutants_total_frist, pollutants_total_second, pollutants_total_third, pollutants_total_fourth, pollutants_total_fifth, exact_demands_pollutants_mg from b_main_poll_dis_gas_mout
    </sql>

    <select id="selectBMainPollDisGasMoutList" parameterType="cn.cecep.talroad.domain.BMainPollDisGasMout" resultMap="BMainPollDisGasMoutResult">
        <include refid="selectBMainPollDisGasMoutVo"/>
        <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="outletCode != null  and outletCode != ''"> and outlet_code = #{outletCode}</if>
            <if test="outletName != null  and outletName != ''"> and outlet_name like concat('%', #{outletName}, '%')</if>
            <if test="outletUseTotal != null  and outletUseTotal != ''"> and outlet_use_total = #{outletUseTotal}</if>
            <if test="pollutantsType != null  and pollutantsType != ''"> and pollutants_type = #{pollutantsType}</if>
            <if test="pollutantsMgHour != null  and pollutantsMgHour != ''"> and pollutants_mg_hour = #{pollutantsMgHour}</if>
            <if test="pollutantsMgDay != null  and pollutantsMgDay != ''"> and pollutants_mg_day = #{pollutantsMgDay}</if>
            <if test="pollutantsMgRate != null  and pollutantsMgRate != ''"> and pollutants_mg_rate = #{pollutantsMgRate}</if>
            <if test="pollutantsTotalFrist != null  and pollutantsTotalFrist != ''"> and pollutants_total_frist = #{pollutantsTotalFrist}</if>
            <if test="pollutantsTotalSecond != null  and pollutantsTotalSecond != ''"> and pollutants_total_second = #{pollutantsTotalSecond}</if>
            <if test="pollutantsTotalThird != null  and pollutantsTotalThird != ''"> and pollutants_total_third = #{pollutantsTotalThird}</if>
            <if test="pollutantsTotalFourth != null  and pollutantsTotalFourth != ''"> and pollutants_total_fourth = #{pollutantsTotalFourth}</if>
            <if test="pollutantsTotalFifth != null  and pollutantsTotalFifth != ''"> and pollutants_total_fifth = #{pollutantsTotalFifth}</if>
            <if test="exactDemandsPollutantsMg != null  and exactDemandsPollutantsMg != ''"> and exact_demands_pollutants_mg = #{exactDemandsPollutantsMg}</if>
        </where>
    </select>
    <select id="selectPollutantGroupList" resultType="cn.cecep.talroad.vo.analysis.PollDisPollutantGroupVO">
        SELECT MIN
                   ( CAST ( pollutants_mg_hour  AS decimal ) ) AS pollutants_mg_hour,
               pollutants_type
        FROM
            b_main_poll_dis_gas_mout
        WHERE
            pollutants_mg_hour != '/'
            AND factory_id = #{factoryId}
            AND poll_dis_id = #{licenseId}
            AND outlet_code = #{outletCode}
            AND outlet_use_total = '0'
        GROUP BY
            pollutants_type
    </select>
    <select id="selectGasOutletPermitPollutant" resultType="cn.cecep.talroad.vo.analysis.OutletPermitPollutantVO">
        --查询排口的因子排污许可限值和监测形式
        SELECT
            mout.pollutants_type,
            mout.pollutants_total_frist,
            mout.pollutants_total_second,
            mout.pollutants_total_third,
            mout.pollutants_total_fourth,
            mout.pollutants_total_fifth,
            mout.factory_id,
            factory.factory_name,
            mout.outlet_code,
            mout.pollutant_code,
            mout.pollutants_mg_hour,
            1 as outlet_type
        FROM
            b_main_poll_dis_gas_mout mout
                left join pc_factory factory on mout.factory_id = factory.id
                where mout.outlet_code is not null AND mout.outlet_use_total = '0'
        UNION ALL
        SELECT
            mout.pollutants_type,
            mout.pollutants_total_frist,
            mout.pollutants_total_second,
            mout.pollutants_total_third,
            mout.pollutants_total_fourth,
            mout.pollutants_total_fifth,
            mout.factory_id,
            factory.factory_name,
            mout.outlet_code,
            mout.pollutant_code,
            mout.pollutants_mg_hour,
            2 as outlet_type
        FROM
            b_main_poll_dis_gas_oout AS mout
                left join pc_factory factory on mout.factory_id = factory.id
                where mout.outlet_code is not null AND mout.outlet_use_total = '0'

    </select>
    <select id="selectGasOnlineOutletPollutant" resultType="cn.cecep.talroad.vo.analysis.OutletPollutantVO">
        SELECT
        mout.pollutants_type,
        mout.pollutants_total_frist,
        mout.pollutants_total_second,
        mout.pollutants_total_third,
        mout.pollutants_total_fourth,
        mout.pollutants_total_fifth,
        mout.factory_id,
        factory.factory_name,
        mout.outlet_code,
        mout.outlet_name,
        mout.pollutant_code,
        mout.pollutants_mg_hour,
        1 as outlet_type
        FROM
        b_main_poll_dis_gas_mout mout
        left join pc_factory factory on mout.factory_id = factory.id
        where  mout.factory_id in
        <foreach collection="factoryIds" open="(" separator="," close=")" item="factoryId" index="i">
            #{factoryId}
        </foreach>
        and factory.id in
        <foreach collection="factoryIds" open="(" separator="," close=")" item="factoryId" index="i">
            #{factoryId}
        </foreach>
        and mout.outlet_code in
        <foreach collection="outletCodes" open="(" separator="," close=")" item="outletCode" index="i">
            #{outletCode}
        </foreach>

        UNION ALL
        SELECT
        mout.pollutants_type,
        mout.pollutants_total_frist,
        mout.pollutants_total_second,
        mout.pollutants_total_third,
        mout.pollutants_total_fourth,
        mout.pollutants_total_fifth,
        mout.factory_id,
        factory.factory_name,
        mout.outlet_code,
        mout.outlet_name,
        mout.pollutant_code,
        mout.pollutants_mg_hour,
        2 as outlet_type
        FROM
        b_main_poll_dis_gas_oout AS mout
        left join pc_factory factory on mout.factory_id = factory.id
        where  mout.factory_id in
        <foreach collection="factoryIds" open="(" separator="," close=")" item="factoryId" index="i">
            #{factoryId}
        </foreach>
        and factory.id in
        <foreach collection="factoryIds" open="(" separator="," close=")" item="factoryId" index="i">
            #{factoryId}
        </foreach>
        and mout.outlet_code in
        <foreach collection="outletCodes" open="(" separator="," close=")" item="outletCode" index="i">
            #{outletCode}
        </foreach>

    </select>

</mapper>
