<?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.com.tenlion.systembase.dao.importantevent.IImportantEventDao">

    <resultMap id="importantEventDTO" type="cn.com.tenlion.systembase.pojo.dtos.importantevent.ImportantEventDTO">
        <result column="important_event_id" property="importantEventId"/>
        <result column="event_name" property="eventName"/>
        <result column="event_date" property="eventDate"/>
        <result column="event_site" property="eventSite"/>
        <result column="event_level_id" property="eventLevelId"/>
        <result column="event_level_text" property="eventLevelText"/>
        <result column="event_type_id" property="eventTypeId"/>
        <result column="event_type_text" property="eventTypeText"/>
        <result column="event_explain" property="eventExplain"/>
        <result column="creator" property="creator"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="modifier" property="modifier"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="is_delete" property="isDelete"/>
        <result column="location" property="location"/>
        <result column="location_code" property="locationCode"/>
        <result column="grid" property="grid"/>
        <result column="longitude" property="longitude"/>
        <result column="latitude" property="latitude"/>
    </resultMap>

    <resultMap id="importantEventBO" type="cn.com.tenlion.systembase.pojo.bos.importantevent.ImportantEventBO">
        <result column="important_event_id" property="importantEventId"/>
        <result column="event_name" property="eventName"/>
        <result column="event_date" property="eventDate"/>
        <result column="event_site" property="eventSite"/>
        <result column="event_level_id" property="eventLevelId"/>
        <result column="event_level_text" property="eventLevelText"/>
        <result column="event_type_id" property="eventTypeId"/>
        <result column="event_type_text" property="eventTypeText"/>
        <result column="event_explain" property="eventExplain"/>
        <result column="creator" property="creator"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="modifier" property="modifier"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="is_delete" property="isDelete"/>
    </resultMap>

    <resultMap id="importantEventPO" type="cn.com.tenlion.systembase.pojo.pos.importantevent.ImportantEventPO">
        <result column="important_event_id" property="importantEventId"/>
        <result column="event_name" property="eventName"/>
        <result column="event_date" property="eventDate"/>
        <result column="event_site" property="eventSite"/>
        <result column="event_level_id" property="eventLevelId"/>
        <result column="event_level_text" property="eventLevelText"/>
        <result column="event_type_id" property="eventTypeId"/>
        <result column="event_type_text" property="eventTypeText"/>
        <result column="event_explain" property="eventExplain"/>
        <result column="creator" property="creator"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="modifier" property="modifier"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="is_delete" property="isDelete"/>
    </resultMap>

    <!-- 新增重大事件 -->
    <insert id="save" parameterType="map">
        INSERT INTO cg_important_event(
            important_event_id,
            event_name,
            event_date,
            event_site,
            event_level_id,
            event_level_text,
            event_type_id,
            event_type_text,
            event_explain,
            location,
            location_code,
            grid,
            longitude,
            latitude,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{importantEventId},
            #{eventName},
            #{eventDate},
            #{eventSite},
            #{eventLevelId},
            #{eventLevelText},
            #{eventTypeId},
            #{eventTypeText},
            #{eventExplain},
            #{location},
            #{locationCode},
            #{grid},
            #{longitude},
            #{latitude},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

    <!-- 删除重大事件 -->
    <update id="remove" parameterType="map">
        UPDATE
            cg_important_event
        SET
            gmt_modified = #{gmtModified},
            modifier = #{modifier},
            is_delete = 1
        WHERE
            important_event_id IN
        <foreach collection="importantEventIds" index="index" open="(" separator="," close=")">
            #{importantEventIds[${index}]}
        </foreach>
    </update>

    <!-- 删除重大事件（物理） -->
    <update id="delete" parameterType="map">
        DELETE FROM
            cg_important_event
        WHERE
            important_event_id IN
        <foreach collection="importantEventIds" index="index" open="(" separator="," close=")">
            #{importantEventIds[${index}]}
        </foreach>
    </update>

    <!-- 修改重大事件 -->
    <update id="update" parameterType="map">
        UPDATE
            cg_important_event
        SET
        <if test="eventName != null and eventName != ''">
            event_name = #{eventName},
        </if>
        <if test="eventDate != null and eventDate != ''">
            event_date = #{eventDate},
        </if>
        <if test="eventSite != null and eventSite != ''">
            event_site = #{eventSite},
        </if>
        <if test="eventLevelId != null and eventLevelId != ''">
            event_level_id = #{eventLevelId},
        </if>
        <if test="eventLevelText != null and eventLevelText != ''">
            event_level_text = #{eventLevelText},
        </if>
        <if test="eventTypeId != null and eventTypeId != ''">
            event_type_id = #{eventTypeId},
        </if>
        <if test="eventTypeText != null and eventTypeText != ''">
            event_type_text = #{eventTypeText},
        </if>
        <if test="eventExplain != null and eventExplain != ''">
            event_explain = #{eventExplain},
        </if>
        <if test="location != null and location != ''">
            location = #{location},
        </if>
        <if test="locationCode != null and locationCode != ''">
            location_code = #{locationCode},
        </if>
        <if test="longitude != null and longitude != ''">
            longitude = #{longitude},
        </if>
        <if test="latitude != null and latitude != ''">
            latitude = #{latitude},
        </if>
        <if test="grid != null and grid != ''">
            grid = #{grid},
        </if>
            gmt_modified = #{gmtModified},
            modifier = #{modifier}
        WHERE
            important_event_id = #{importantEventId}
    </update>

    <!-- 重大事件详情 -->
    <select id="get" parameterType="map" resultMap="importantEventDTO">
        SELECT
            t1.event_name,
            t1.event_date,
            t1.event_site,
            t1.event_level_id,
            t1.event_level_text,
            t1.event_type_id,
            t1.event_type_text,
            t1.event_explain,
            t1.location,
            t1.location_code,
            t1.grid,
            t1.longitude,
            t1.latitude,
            t1.important_event_id
        FROM
            cg_important_event t1
        WHERE
            t1.is_delete = 0
            <if test="importantEventId != null and importantEventId != ''">
        AND
            t1.important_event_id = #{importantEventId}
            </if>
    </select>

    <!-- 重大事件详情 -->
    <select id="getBO" parameterType="map" resultMap="importantEventBO">
        SELECT
            t1.important_event_id,
            t1.event_name,
            t1.event_date,
            t1.event_site,
            t1.event_level_id,
            t1.event_level_text,
            t1.event_type_id,
            t1.event_type_text,
            t1.event_explain,,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            cg_important_event t1
        WHERE
            t1.is_delete = 0
            <if test="importantEventId != null and importantEventId != ''">
        AND
            t1.important_event_id = #{importantEventId}
            </if>
    </select>

    <!-- 重大事件详情 -->
    <select id="getPO" parameterType="map" resultMap="importantEventPO">
        SELECT
            t1.important_event_id,
            t1.event_name,
            t1.event_date,
            t1.event_site,
            t1.event_level_id,
            t1.event_level_text,
            t1.event_type_id,
            t1.event_type_text,
            t1.event_explain,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            cg_important_event t1
        WHERE
            t1.is_delete = 0
            <if test="importantEventId != null and importantEventId != ''">
        AND
            t1.important_event_id = #{importantEventId}
            </if>
    </select>

    <!-- 重大事件列表 -->
    <select id="list" parameterType="map" resultMap="importantEventDTO">
        SELECT
            t1.important_event_id,
            t1.event_name,
            t1.event_date,
            t1.event_site,
            t1.event_level_id,
            t1.event_level_text,
            t1.event_type_id,
            t1.event_type_text,
            t1.event_explain,
            t1.location,
            t1.location_code,
            t1.longitude,
            t1.latitude,
            t1.grid,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified
        FROM
            cg_important_event t1
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND (
            t1.event_name LIKE CONCAT('%', #{keywords}, '%')
            OR
            t1.event_site LIKE CONCAT('%', #{keywords}, '%')
        )
        </if>
        <if test="areaCode != null and areaCode != ''">
            AND t1.location_code = #{areaCode}
        </if>
        <if test="areaCodeLike != null and areaCodeLike != ''">
            AND t1.location_code LIKE CONCAT(#{areaCodeLike}, '%')
        </if>
        <if test="grid != null and grid != ''">
            AND t1.grid = #{grid}
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="eventLevelId != null and eventLevelId != ''">
            AND t1.event_level_id = #{eventLevelId}
        </if>
        <if test="eventTypeId != null and eventTypeId != ''">
            AND t1.event_type_id = #{eventTypeId}
        </if>
        ORDER BY t1.gmt_create DESC
    </select>

    <!-- 重大事件列表 -->
    <select id="listBO" parameterType="map" resultMap="importantEventBO">
        SELECT
            t1.important_event_id,
            t1.event_name,
            t1.event_date,
            t1.event_site,
            t1.event_level_id,
            t1.event_level_text,
            t1.event_type_id,
            t1.event_type_text,
            t1.event_explain,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            cg_important_event t1
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND (
            t1.event_name LIKE CONCAT('%', #{keywords}, '%')
        )
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="importantEventIds != null and importantEventIds.size > 0">
        AND
            t1.important_event_id IN
            <foreach collection="importantEventIds" index="index" open="(" separator="," close=")">
                #{importantEventIds[${index}]}
            </foreach>
        </if>
        ORDER BY t1.gmt_create DESC
    </select>

    <!-- 重大事件列表 -->
    <select id="listPO" parameterType="map" resultMap="importantEventPO">
        SELECT
            t1.important_event_id,
            t1.event_name,
            t1.event_date,
            t1.event_site,
            t1.event_level_id,
            t1.event_level_text,
            t1.event_type_id,
            t1.event_type_text,
            t1.event_explain,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            cg_important_event t1
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND (
            <!-- 这里添加其他条件 -->
            t1.id LIKE CONCAT('%', #{keywords}, '%')
        )
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="importantEventIds != null and importantEventIds.size > 0">
        AND
            t1.important_event_id IN
            <foreach collection="importantEventIds" index="index" open="(" separator="," close=")">
                #{importantEventIds[${index}]}
            </foreach>
        </if>
        ORDER BY t1.gmt_create DESC
    </select>

    <!-- 重大事件统计  -->
    <select id="count" parameterType="map" resultType="Integer">
        SELECT
            COUNT(*)
        FROM
            cg_important_event t1
        WHERE
            t1.is_delete = 0
    </select>

</mapper>