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

    <resultMap type="cn.cecep.talroad.domain.BStatTableZxbgPflAirSpecial" id="BStatTableZxbgPflAirSpecialResult">
        <result property="id"    column="id"    />
        <result property="factoryId"    column="factory_id"    />
        <result property="zxbgType"    column="zxbg_type"    />
        <result property="zxbgPeriod"    column="zxbg_period"    />
        <result property="zxbgAirType"    column="zxbg_air_type"    />
        <result property="zxbgAirAllType"    column="zxbg_air_all_type"    />
        <result property="zxbgDischargeXk"    column="zxbg_discharge_xk"    />
        <result property="zxbgDischargeActual"    column="zxbg_discharge_actual"    />
        <result property="createTime"    column="create_time"    />
        <result property="isDelete"    column="is_delete"    />
        <result property="zxbgPollutant"    column="zxbg_pollutant"    />
        <result property="zxbgTime"    column="zxbg_time"    />
        <result property="overCause"    column="over_cause"    />
    </resultMap>


    <sql id="selectBStatTableZxbgPflAirSpecialVo">
        select id, factory_id, zxbg_type, zxbg_period, zxbg_air_type, zxbg_air_all_type, zxbg_discharge_xk, zxbg_discharge_actual, create_time, is_delete, zxbg_pollutant, zxbg_time, over_cause from b_stat_table_zxbg_pfl_air_special
    </sql>

    <select id="selectBStatTableZxbgPflAirSpecialList" parameterType="cn.cecep.talroad.domain.BStatTableZxbgPflAirSpecial" resultMap="BStatTableZxbgPflAirSpecialResult">
        <include refid="selectBStatTableZxbgPflAirSpecialVo"/>
        <where>
            <if test="factoryId != null  and factoryId != ''"> and factory_id = #{factoryId}</if>
            <if test="zxbgType != null  and zxbgType != ''"> and zxbg_type = #{zxbgType}</if>
            <if test="zxbgPeriod != null  and zxbgPeriod != ''"> and zxbg_period = #{zxbgPeriod}</if>
            <if test="zxbgAirType != null  and zxbgAirType != ''"> and zxbg_air_type = #{zxbgAirType}</if>
            <if test="zxbgAirAllType != null  and zxbgAirAllType != ''"> and zxbg_air_all_type = #{zxbgAirAllType}</if>
            <if test="zxbgDischargeXk != null  and zxbgDischargeXk != ''"> and zxbg_discharge_xk = #{zxbgDischargeXk}</if>
            <if test="zxbgDischargeActual != null  and zxbgDischargeActual != ''"> and zxbg_discharge_actual = #{zxbgDischargeActual}</if>
            <if test="isDelete != null  and isDelete != ''"> and is_delete = #{isDelete}</if>
            <if test="zxbgPollutant != null  and zxbgPollutant != ''"> and zxbg_pollutant = #{zxbgPollutant}</if>
            <if test="zxbgTime != null  and zxbgTime != ''"> and zxbg_time = #{zxbgTime}</if>
            <if test="overCause != null  and overCause != ''"> and over_cause = #{overCause}</if>
        </where>
    </select>

    <select id="selectListAndFactory" resultMap="BStatTableZxbgPflAirSpecialResult">
        SELECT
        zxbg.*,
        pf.factory_name,
        pf.country_name,
        pf.license_man_type,
        pf.poll_dis_license_code,
        pf.industrial_park_name,
        pf.industrial_park_code,
        pf.industry_permission_name,
        pf.river_basin
        FROM
        (select * from b_stat_table_zxbg_pfl_air_special
        <where>
            <if test="zxbgPollutant != null  and zxbgPollutant != ''"> and zxbg_pollutant like concat('%', #{zxbgPollutant}, '%')</if>
            <if test="zxbgAirType != null  and zxbgAirType != ''"> and zxbg_air_type = #{zxbgAirType}</if>
            <if test="zxbgAirAllType != null  and zxbgAirAllType != ''"> and zxbg_air_all_type = ANY (string_to_array(#{zxbgAirAllType}, ','))</if>
            <if test="zxbgStartTime != null  and zxbgStartTime != ''"> and zxbg_time >= #{zxbgStartTime}</if>
            <if test="zxbgEndTime != null  and zxbgEndTime != ''"> and zxbg_time &lt;= #{zxbgEndTime}</if>
            <if test="zxbgType != null  and zxbgType != ''"> and zxbg_type = #{zxbgType}</if>
            <if test="zxbgPeriod != null  and zxbgPeriod != ''"> and zxbg_period  like concat('%', #{zxbgPeriod}, '%')</if>
        </where>
        ) zxbg
        right JOIN pc_factory pf ON pf.ID = zxbg.factory_id
        <where>
            <if test="factoryIdList != null and factoryIdList.size() != 0">
                and pf.id
                <foreach collection="factoryIdList" close=")" open=" IN (" separator="," item="item">
                    #{item,jdbcType=VARCHAR}
                </foreach>
            </if>
        </where>
        ORDER BY pf.order_num, pf.id
    </select>
</mapper>
