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

    <resultMap type="cn.cecep.talroad.domain.EarlyWarning" id="EarlyWarningResult">
        <result property="ewId"    column="ew_id"    />
        <result property="factoryId"    column="factory_id"    />
        <result property="industryCode"    column="industry_code"    />
        <result property="stationId"    column="station_id"    />
        <result property="warningTime"    column="warning_time"    />
        <result property="warningContent"    column="warning_content"    />
        <result property="createTime"    column="create_time"    />
        <result property="factoryName"    column="factory_name"    />
        <result property="stationName"    column="station_name"    />
        <result property="countryCode"    column="country_code"    />
        <result property="countryName"    column="country_name"    />
        <result property="industryName"    column="industry_name"    />
    </resultMap>


    <sql id="selectEarlyWarningVo">
        select ew_id, factory_id, industry_code, station_id, warning_time, warning_content, create_time, factory_name, station_name, country_code, country_name,industry_name from early_warning
    </sql>

    <select id="selectEarlyWarningList" parameterType="cn.cecep.talroad.domain.EarlyWarning" resultMap="EarlyWarningResult">
        <include refid="selectEarlyWarningVo"/>
        <where>
            <if test="warning.factoryId != null  and warning.factoryId != ''"> and factory_id = #{warning.factoryId}</if>
            <if test="warning.industryCode != null  and warning.industryCode != ''"> and industry_code = #{warning.industryCode}</if>
            <if test="warning.stationId != null  and warning.stationId != ''"> and station_id = #{warning.stationId}</if>
            <if test="startTime != null and endTime != null  "> and warning_time between #{startTime} and #{endTime} </if>
            <if test="warning.warningContent != null  and warning.warningContent != ''"> and warning_content = #{warning.warningContent}</if>
            <if test="warning.factoryName != null  and warning.factoryName != ''"> and factory_name like concat('%', #{warning.factoryName}, '%')</if>
            <if test="warning.stationName != null  and warning.stationName != ''"> and station_name like concat('%', #{warning.stationName}, '%')</if>
            <if test="warning.countryCode != null  and warning.countryCode != ''"> and country_code = #{warning.countryCode}</if>
            <if test="warning.regionCodes != null and warning.regionCodes.size() != 0">
                and country_code IN
                <foreach collection="warning.regionCodes" separator="," item="item" close=")" open="(">
                    #{item,jdbcType=VARCHAR}
                </foreach>
            </if>
            <if test="warning.countryName != null  and warning.countryName != ''"> and country_name like concat('%', #{warning.countryName}, '%')</if>
        </where>
        ${ew.customSqlSegment}
    </select>

    <insert id="batchSave">
        INSERT INTO early_warning (ew_id, factory_id, industry_code, station_id, warning_time, warning_content, factory_name, station_name, country_code, country_name,industry_name,poll_code,poll_name,create_time) VALUES
        <foreach collection="addList" separator="," item="item">
            (#{item.ewId}, #{item.factoryId}, #{item.industryCode}, #{item.stationId}, #{item.warningDate}, #{item.warningContent}, #{item.factoryName}, #{item.stationName}, #{item.countryCode}, #{item.countryName}, #{item.industryName}, #{item.pollCode}, #{item.pollName}, #{item.createTime})
        </foreach>
    </insert>
</mapper>
