<?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="org.xyzh.crontab.mapper.CrontabLogMapper">

    <!-- 结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.crontab.TbCrontabLog">
        <id column="id" property="ID" />
        <result column="task_id" property="taskId" />
        <result column="task_name" property="taskName" />
        <result column="task_group" property="taskGroup" />
        <result column="bean_name" property="beanName" />
        <result column="method_name" property="methodName" />
        <result column="method_params" property="methodParams" />
        <result column="execute_status" property="executeStatus" />
        <result column="execute_message" property="executeMessage" />
        <result column="exception_info" property="exceptionInfo" />
        <result column="start_time" property="startTime" />
        <result column="end_time" property="endTime" />
        <result column="execute_duration" property="executeDuration" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
        <result column="delete_time" property="deleteTime" />
        <result column="deleted" property="deleted" />
    </resultMap>

    <!-- 字段列表 -->
    <sql id="Base_Column_List">
        id, task_id, task_name, task_group, bean_name, method_name, method_params,
        execute_status, execute_message, exception_info, start_time, end_time, 
        execute_duration, create_time, update_time, delete_time, deleted
    </sql>

    <!-- 查询条件 -->
    <sql id="Base_Where_Clause">
        <where>
            <if test="filter != null">
                <if test="filter.ID != null and filter.ID != ''">
                    AND id = #{filter.ID}
                </if>
                <if test="filter.taskId != null and filter.taskId != ''">
                    AND task_id = #{filter.taskId}
                </if>
                <if test="filter.taskName != null and filter.taskName != ''">
                    AND task_name LIKE CONCAT('%', #{filter.taskName}, '%')
                </if>
                <if test="filter.taskGroup != null and filter.taskGroup != ''">
                    AND task_group = #{filter.taskGroup}
                </if>
                <if test="filter.beanName != null and filter.beanName != ''">
                    AND bean_name = #{filter.beanName}
                </if>
                <if test="filter.executeStatus != null">
                    AND execute_status = #{filter.executeStatus}
                </if>
                <if test="filter.deleted != null">
                    AND deleted = #{filter.deleted}
                </if>
            </if>
        </where>
    </sql>

    <!-- 插入日志 -->
    <insert id="insertLog">
        INSERT INTO tb_crontab_log
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="log.ID != null">id,</if>
            <if test="log.taskId != null">task_id,</if>
            <if test="log.taskName != null">task_name,</if>
            <if test="log.taskGroup != null">task_group,</if>
            <if test="log.beanName != null">bean_name,</if>
            <if test="log.methodName != null">method_name,</if>
            <if test="log.methodParams != null">method_params,</if>
            <if test="log.executeStatus != null">execute_status,</if>
            <if test="log.executeMessage != null">execute_message,</if>
            <if test="log.exceptionInfo != null">exception_info,</if>
            <if test="log.startTime != null">start_time,</if>
            <if test="log.endTime != null">end_time,</if>
            <if test="log.executeDuration != null">execute_duration,</if>
            <if test="log.createTime != null">create_time,</if>
            deleted
        </trim>
        VALUES
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="log.ID != null">#{log.ID},</if>
            <if test="log.taskId != null">#{log.taskId},</if>
            <if test="log.taskName != null">#{log.taskName},</if>
            <if test="log.taskGroup != null">#{log.taskGroup},</if>
            <if test="log.beanName != null">#{log.beanName},</if>
            <if test="log.methodName != null">#{log.methodName},</if>
            <if test="log.methodParams != null">#{log.methodParams},</if>
            <if test="log.executeStatus != null">#{log.executeStatus},</if>
            <if test="log.executeMessage != null">#{log.executeMessage},</if>
            <if test="log.exceptionInfo != null">#{log.exceptionInfo},</if>
            <if test="log.startTime != null">#{log.startTime},</if>
            <if test="log.endTime != null">#{log.endTime},</if>
            <if test="log.executeDuration != null">#{log.executeDuration},</if>
            <if test="log.createTime != null">#{log.createTime},</if>
            0
        </trim>
    </insert>

    <!-- updateLog --> 

    <update id="updateLog">
        UPDATE tb_crontab_log
        SET 
            <if test="log.executeStatus != null">execute_status = #{log.executeStatus},</if>
            <if test="log.executeMessage != null">execute_message = #{log.executeMessage},</if>
            <if test="log.exceptionInfo != null">exception_info = #{log.exceptionInfo},</if>
            <if test="log.endTime != null">end_time = #{log.endTime},</if>
            <if test="log.executeDuration != null">execute_duration = #{log.executeDuration},</if>
            update_time = NOW()
        WHERE id = #{log.ID} AND deleted = 0
    </update>

    <!-- 根据ID查询日志 -->
    <select id="selectLogById" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_crontab_log
        WHERE id = #{logId} AND deleted = 0
    </select>

    <!-- 根据任务ID查询日志列表 -->
    <select id="selectLogsByTaskId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_crontab_log
        WHERE task_id = #{taskId} AND deleted = 0
        ORDER BY start_time DESC
    </select>

    <!-- 根据过滤条件查询日志列表 -->
    <select id="selectLogList" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_crontab_log
        <include refid="Base_Where_Clause" />
        ORDER BY start_time DESC
    </select>

    <!-- 分页查询日志列表 -->
    <select id="selectLogPage" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_crontab_log
        <include refid="Base_Where_Clause" />
        ORDER BY start_time DESC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <!-- 统计日志总数 -->
    <select id="countLogs" resultType="long">
        SELECT COUNT(*)
        FROM tb_crontab_log
        <include refid="Base_Where_Clause" />
    </select>

    <!-- 删除日志（逻辑删除） -->
    <update id="deleteLog">
        UPDATE tb_crontab_log 
        SET deleted = 1,
            delete_time = NOW()
        WHERE id = #{logId} AND deleted = 0
    </update>

    <!-- 批量删除日志 -->
    <update id="batchDeleteLogs">
        UPDATE tb_crontab_log 
        SET deleted = 1,
            delete_time = NOW()
        WHERE deleted = 0
        AND id IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <!-- 清理指定时间之前的日志 -->
    <update id="cleanLogsByDate">
        UPDATE tb_crontab_log 
        SET deleted = 1,
            delete_time = NOW()
        WHERE deleted = 0
        AND create_time &lt; #{beforeDate}
    </update>

    <!-- 根据任务ID和执行状态查询日志 -->
    <select id="selectLogsByTaskIdAndStatus" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_crontab_log
        WHERE task_id = #{taskId} 
        AND execute_status = #{executeStatus}
        AND deleted = 0
        ORDER BY start_time DESC
    </select>

    <!-- 查询最近的执行日志 -->
    <select id="selectRecentLogs" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_crontab_log
        WHERE task_id = #{taskId} AND deleted = 0
        ORDER BY start_time DESC
        LIMIT #{limit}
    </select>
</mapper>
