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

    <resultMap type="cn.cecep.talroad.domain.BMainProductionWater" id="BMainProductionWaterResult">
        <result property="id"    column="id"    />
        <result property="factoryId"    column="factory_id"    />
        <result property="outletCode"    column="outlet_code"    />
        <result property="outletName"    column="outlet_name"    />
        <result property="outletType"    column="outlet_type"    />
        <result property="outletMonType"    column="outlet_mon_type"    />
        <result property="cepFacilityCode"    column="cep_facility_code"    />
        <result property="cepFacilityName"    column="cep_facility_name"    />
        <result property="cepFacilityCraft"    column="cep_facility_craft"    />
        <result property="pollutionType"    column="pollution_type"    />
        <result property="waterType"    column="water_type"    />
    </resultMap>


    <sql id="selectBMainProductionWaterVo">
        select id, factory_id, outlet_code, outlet_name, outlet_type, outlet_mon_type, cep_facility_code, cep_facility_name, cep_facility_craft, pollution_type, water_type from b_main_production_water
    </sql>

    <select id="selectBMainProductionWaterList" parameterType="cn.cecep.talroad.domain.BMainProductionWater" resultMap="BMainProductionWaterResult">
        <include refid="selectBMainProductionWaterVo"/>
        <where>
            <if test="factoryId != null "> and factory_id = #{factoryId}</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="outletType != null  and outletType != ''"> and outlet_type = #{outletType}</if>
            <if test="outletMonType != null  and outletMonType != ''"> and outlet_mon_type = #{outletMonType}</if>
            <if test="cepFacilityCode != null  and cepFacilityCode != ''"> and cep_facility_code = #{cepFacilityCode}</if>
            <if test="cepFacilityName != null  and cepFacilityName != ''"> and cep_facility_name like concat('%', #{cepFacilityName}, '%')</if>
            <if test="cepFacilityCraft != null  and cepFacilityCraft != ''"> and cep_facility_craft = #{cepFacilityCraft}</if>
            <if test="pollutionType != null  and pollutionType != ''"> and pollution_type = #{pollutionType}</if>
            <if test="waterType != null  and waterType != ''"> and water_type = #{waterType}</if>
        </where>
    </select>
    <select id="selectPwxkList" resultType="cn.cecep.talroad.vo.BMainProductionWaterVo$PwxkCzp">
        SELECT
            t1.poll_dis_license_code,
            t1.factory_name,
            t1.license_man_type,
            t1.country_name,
            t1.river_basin,
            t1.industrial_park_name,
            t1.industrial_park_code,
            t1.industry_permission_name,

            t2.water_type,
            t2.pollution_type,
            t2.cep_facility_code,
            t2.cep_facility_name,
            t2.cep_facility_craft,

            t2.direction_drainage,
            t2.emissions_form,
            t2.discharge_law,
            t2.outlet_code,
            t2.outlet_name,
            t2.whether_outlet_requirements,
            t2.outlet_types,
            t2.more_details
        FROM pc_factory t1
        LEFT JOIN (
            SELECT
                t2.id AS production_water_id,
                t2.factory_id,
                t2.water_type,
                t2.pollution_type,
                t2.cep_facility_code,
                t2.cep_facility_name,
                t2.cep_facility_craft,

                t3.direction_drainage,
                t3.emissions_form,
                t3.discharge_law,
                t3.outlet_code,
                t3.outlet_name,
                t3.whether_outlet_requirements,
                t3.outlet_types,
                t3.more_details,
                t3.id AS outlet_id
            FROM b_main_production_water t2
            left join pc_outlet t3 on t3.factory_id = t2.factory_id
                and t3.outlet_code = t2.outlet_code
                and t3.pollutant_type = '2'
            <where>
                <if test="query.waterType != null and query.waterType != ''">
                    and t2.water_type like concat('%', #{query.waterType}, '%')
                </if>
                <if test="query.pollutionType != null and query.pollutionType.size() > 0">
                    and t2.pollution_type in
                    <foreach collection="query.pollutionType" open="(" separator="," close=")" item="item">
                        #{item}
                    </foreach>
                </if>
                <if test="query.cepFacilityCraft != null and query.cepFacilityCraft.size() > 0">
                    and t2.cep_facility_craft in
                    <foreach collection="query.cepFacilityCraft" open="(" separator="," close=")" item="item">
                        #{item}
                    </foreach>
                </if>
                <if test="query.outletTypes != null and query.outletTypes != ''">
                    and t3.outlet_types = #{query.outletTypes}
                </if>
                <if test="query.whetherOutletRequirements != null and query.whetherOutletRequirements != ''">
                    and t3.whether_outlet_requirements = #{query.whetherOutletRequirements}
                </if>
                <if test="query.directionDrainage != null and query.directionDrainage.size() > 0">
                    and t3.direction_drainage IN
                    <foreach collection="query.directionDrainage" open="(" separator="," close=")" item="item">
                        #{item}
                    </foreach>
                </if>
                <if test="query.dischargeLaw != null and query.dischargeLaw.size() > 0">
                    and t3.discharge_law IN
                    <foreach collection="query.dischargeLaw" open="(" separator="," close=")" item="item">
                        #{item}
                    </foreach>
                </if>
                <if test="query.emissionsForm != null and query.emissionsForm != ''">
                    and t3.emissions_form = #{query.emissionsForm}
                </if>
            </where>
        ) t2 ON t2.factory_id = t1.id
        where t1.id in
            <foreach collection="factoryIds" open="(" separator="," close=")" item="item">
                #{item}
            </foreach>
        order by t1.order_num, t1.id, t2.outlet_code, t2.production_water_id
    </select>

</mapper>
