<?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="com.common.mapper.SyslogNonNormalMessageMapper">

    <!-- 基础结果映射 -->
    <resultMap id="BaseResultMap" type="com.common.entity.SyslogNonNormalMessage">
        <id column="id" property="id" />
        <result column="created_at" property="createdAt" />
        <result column="log_time" property="logTime" />
        <result column="device_id" property="deviceId" />
        <result column="syslog_message" property="syslogMessage" />
        <result column="syslog_uuid" property="syslogUuid" />
        <result column="syslog_topic" property="syslogTopic" />
        <result column="tenant_id" property="tenantId" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
        <result column="create_by" property="createBy" />
        <result column="update_by" property="updateBy" />
        <result column="create_dept" property="createDept" />
        <result column="header_message" property="headerMessage" />
        <result column="receive_time" property="receiveTime" />
        <result column="rule_time" property="ruleTime" />
        <result column="device_name" property="deviceName" />
        <result column="etl_node" property="etlNode" />
        <result column="collect_task_id" property="collectTaskId" />
        <result column="collect_task_name" property="collectTaskName" />
        <result column="reason" property="reason" />
        <result column="reason_detail" property="reasonDetail" />
        <result column="rule_result" property="ruleResult" />
        <result column="del_flag" property="delFlag" />
    </resultMap>

    <!-- 批量插入 -->
    <insert id="batchInsert" parameterType="java.util.List">
        INSERT INTO syslog_non_normal_message (
        id, created_at, log_time, device_id, syslog_message,
        syslog_uuid, syslog_topic, tenant_id, create_time,
        update_time, create_by, update_by, create_dept,
        header_message, receive_time, rule_time, device_name,
        etl_node, collect_task_id, collect_task_name, reason,
        reason_detail, rule_result, del_flag
        ) VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.id},
            #{item.createdAt},
            #{item.logTime},
            #{item.deviceId},
            #{item.syslogMessage},
            #{item.syslogUuid},
            #{item.syslogTopic},
            #{item.tenantId},
            #{item.createTime},
            #{item.updateTime},
            #{item.createBy},
            #{item.updateBy},
            #{item.createDept},
            #{item.headerMessage},
            #{item.receiveTime},
            #{item.ruleTime},
            #{item.deviceName},
            #{item.etlNode},
            #{item.collectTaskId},
            #{item.collectTaskName},
            #{item.reason},
            #{item.reasonDetail},
            #{item.ruleResult},
            #{item.delFlag}
            )
        </foreach>
    </insert>

    <!-- 动态更新 -->
    <update id="updateByCondition" parameterType="com.common.entity.SyslogNonNormalMessage">
        UPDATE syslog_non_normal_message
        <set>
            <if test="logTime != null">log_time = #{logTime},</if>
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="syslogMessage != null and syslogMessage != ''">syslog_message = #{syslogMessage},</if>
            <if test="syslogUuid != null and syslogUuid != ''">syslog_uuid = #{syslogUuid},</if>
            <if test="syslogTopic != null and syslogTopic != ''">syslog_topic = #{syslogTopic},</if>
            <if test="tenantId != null and tenantId != ''">tenant_id = #{tenantId},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="headerMessage != null and headerMessage != ''">header_message = #{headerMessage},</if>
            <if test="receiveTime != null">receive_time = #{receiveTime},</if>
            <if test="ruleTime != null">rule_time = #{ruleTime},</if>
            <if test="deviceName != null and deviceName != ''">device_name = #{deviceName},</if>
            <if test="etlNode != null and etlNode != ''">etl_node = #{etlNode},</if>
            <if test="collectTaskId != null">collect_task_id = #{collectTaskId},</if>
            <if test="collectTaskName != null and collectTaskName != ''">collect_task_name = #{collectTaskName},</if>
            <if test="reason != null and reason != ''">reason = #{reason},</if>
            <if test="reasonDetail != null and reasonDetail != ''">reason_detail = #{reasonDetail},</if>
            <if test="ruleResult != null and ruleResult != ''">rule_result = #{ruleResult},</if>
            <if test="delFlag != null and delFlag != ''">del_flag = #{delFlag},</if>
        </set>
        WHERE id = #{id} AND del_flag = '0'
    </update>

    <!-- 根据条件查询 -->
    <select id="selectByCondition" parameterType="map" resultMap="BaseResultMap">
        SELECT * FROM syslog_non_normal_message
        WHERE del_flag = '0'
        <if test="deviceId != null">AND device_id = #{deviceId}</if>
        <if test="tenantId != null and tenantId != ''">AND tenant_id = #{tenantId}</if>
        <if test="ruleResult != null and ruleResult != ''">AND rule_result = #{ruleResult}</if>
        <if test="collectTaskId != null">AND collect_task_id = #{collectTaskId}</if>
        <if test="startTime != null">AND log_time >= #{startTime}</if>
        <if test="endTime != null">AND log_time &lt;= #{endTime}</if>
        <if test="deviceName != null and deviceName != ''">AND device_name LIKE CONCAT('%', #{deviceName}, '%')</if>
        ORDER BY log_time DESC
    </select>


    <!-- 根据ID和创建时间批量查询 -->
    <select id="getMessagesByIdsAndCreatedAts" resultType="com.common.entity.SyslogNonNormalMessage">
        SELECT * FROM syslog_non_normal_message
        WHERE del_flag = '0'
        AND (
        <foreach collection="ids" item="id" index="index" separator=" OR ">
            (id = #{id} AND created_at = #{createdAts[${index}]})
        </foreach>
        )
    </select>

    <!-- 根据ID列表批量查询 -->
    <select id="getMessagesByIds" resultType="com.common.entity.SyslogNonNormalMessage">
        SELECT * FROM syslog_non_normal_message
        WHERE del_flag = '0'
        AND id IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <!-- 批量更新del_flag -->
    <update id="updateBatchDelFlag">
        UPDATE syslog_non_normal_message
        SET del_flag = '1',
        update_time = SYSDATE
        WHERE id IN
        <foreach collection="messages" item="item" open="(" separator="," close=")">
            #{item.id}
        </foreach>
    </update>
</mapper>