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

    <resultMap id="BaseResultMap" type="com.common.entity.AnalysisTaskHistory">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="rule_id" property="ruleId" jdbcType="VARCHAR"/>
        <result column="start_time" property="startTime" jdbcType="TIMESTAMP"/>
        <result column="end_time" property="endTime" jdbcType="TIMESTAMP"/>
        <result column="duration_time" property="durationTime" jdbcType="BIGINT"/>
        <result column="progress_percent" property="progressPercent" jdbcType="INTEGER"/>
        <result column="input_count" property="inputCount" jdbcType="BIGINT"/>
        <result column="output_count" property="outputCount" jdbcType="BIGINT"/>
        <result column="status" property="status" jdbcType="VARCHAR"/>
        <result column="create_dept" property="createDept" jdbcType="BIGINT"/>
        <result column="del_flag" property="delFlag" jdbcType="CHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="create_by" property="createBy" jdbcType="BIGINT"/>
        <result column="update_by" property="updateBy" jdbcType="BIGINT"/>
        <result column="remark" property="remark" jdbcType="VARCHAR"/>
        <result column="tenant_id" property="tenantId" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, rule_id, start_time, end_time, duration_time, progress_percent,
        input_count, output_count, status, create_dept, del_flag,
        create_time, update_time, create_by, update_by, remark, tenant_id
    </sql>

    <!-- 插入任务历史记录 -->
    <insert id="insert" parameterType="com.common.entity.AnalysisTaskHistory">
        INSERT INTO analysis_task_history (
        <include refid="Base_Column_List"/>
        ) VALUES (
        #{id}, #{ruleId}, #{startTime}, #{endTime}, #{durationTime}, #{progressPercent},
        #{inputCount}, #{outputCount}, #{status}, #{createDept}, #{delFlag},
        #{createTime}, #{updateTime}, #{createBy}, #{updateBy}, #{remark}, #{tenantId}
        )
    </insert>

    <!-- 更新任务历史记录 -->
    <update id="update" parameterType="com.common.entity.AnalysisTaskHistory">
        UPDATE analysis_task_history
        SET end_time = #{endTime},
        duration_time = #{durationTime},
        progress_percent = #{progressPercent},
        input_count = #{inputCount},
        output_count = #{outputCount},
        status = #{status},
        update_time = SYSDATE
        <if test="remark != null">
            ,remark = #{remark}
        </if>
        WHERE id = #{id}
    </update>

    <!-- 根据规则ID查询最近的任务历史 -->
    <select id="selectRecentByRuleId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM analysis_task_history
        WHERE rule_id =#{ruleId, jdbcType=OTHER}
        AND del_flag = '0'
        ORDER BY create_time DESC
        <if test="limit != null and limit > 0">
            LIMIT #{limit}
        </if>
    </select>

    <!-- 根据规则ID和状态查询任务历史 -->
    <select id="selectByRuleIdAndStatus" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM analysis_task_history
        WHERE rule_id =#{ruleId, jdbcType=OTHER}
        AND status = #{status}
        AND del_flag = '0'
        ORDER BY create_time DESC
        LIMIT 1
    </select>

</mapper>
