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

    <resultMap type="cn.cecep.talroad.domain.BStatTableZxbgWrzlAbnormalities" id="BStatTableZxbgWrzlAbnormalitiesResult">
        <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="zxbgPollutantType"    column="zxbg_pollutant_type"    />
        <result property="overStartTime"    column="over_start_time"    />
        <result property="overEndTime"    column="over_end_time"    />
        <result property="facilityName"    column="facility_name"    />
        <result property="facilityCode"    column="facility_code"    />
        <result property="zxbgPollutant"    column="zxbg_pollutant"    />
        <result property="dischargeRange"    column="discharge_range"    />
        <result property="solutions"    column="solutions"    />
        <result property="createTime"    column="create_time"    />
        <result property="remark"    column="remark"    />
    </resultMap>


    <sql id="selectBStatTableZxbgWrzlAbnormalitiesVo">
        select id, factory_id, zxbg_type, zxbg_period, zxbg_pollutant_type, over_start_time, over_end_time, facility_name, facility_code, zxbg_pollutant, discharge_range, solutions, create_time, remark from b_stat_table_zxbg_wrzl_abnormalities
    </sql>

    <select id="selectBStatTableZxbgWrzlAbnormalitiesList" parameterType="cn.cecep.talroad.domain.BStatTableZxbgWrzlAbnormalities" resultMap="BStatTableZxbgWrzlAbnormalitiesResult">
        <include refid="selectBStatTableZxbgWrzlAbnormalitiesVo"/>
        <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="zxbgPollutantType != null  and zxbgPollutantType != ''"> and zxbg_pollutant_type = #{zxbgPollutantType}</if>
            <if test="overStartTime != null  and overStartTime != ''"> and over_start_time = #{overStartTime}</if>
            <if test="overEndTime != null  and overEndTime != ''"> and over_end_time = #{overEndTime}</if>
            <if test="facilityName != null  and facilityName != ''"> and facility_name like concat('%', #{facilityName}, '%')</if>
            <if test="facilityCode != null  and facilityCode != ''"> and facility_code = #{facilityCode}</if>
            <if test="zxbgPollutant != null  and zxbgPollutant != ''"> and zxbg_pollutant = #{zxbgPollutant}</if>
            <if test="dischargeRange != null  and dischargeRange != ''"> and discharge_range = #{dischargeRange}</if>
            <if test="solutions != null  and solutions != ''"> and solutions = #{solutions}</if>
        </where>
    </select>

    <select id="selectListAndFactory" resultMap="BStatTableZxbgWrzlAbnormalitiesResult">
        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_wrzl_abnormalities
        <where>
            <if test="overStartTime != null  and overStartTime != ''"> and over_start_time >= CAST(concat(#{overStartTime},':00:00') AS timestamp)</if>
            <if test="overEndTime != null  and overEndTime != ''"> and over_end_time &lt;=  CAST(concat(#{overEndTime},':59:59') AS timestamp)</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>
    <select id="queryAbnormalitiesData" resultMap="BStatTableZxbgWrzlAbnormalitiesResult">
        select id,
        factory_id,
        zxbg_type,
        zxbg_period,
        zxbg_pollutant_type,
        over_start_time,
        over_end_time,
        facility_name,
        facility_code,
        zxbg_pollutant,
        discharge_range,
        solutions,
        create_time,
        remark
        from b_stat_table_zxbg_wrzl_abnormalities
        WHERE zxbg_type = #{type,jdbcType=VARCHAR}
        AND zxbg_period = #{period,jdbcType=VARCHAR}
        AND factory_id IN
        <foreach collection="factoryIds" separator="," item="item" close=")" open="(">
            #{item,jdbcType=VARCHAR}
        </foreach>
    </select>
</mapper>
