<?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.ruoyi.jarvis.mapper.TencentDocBatchPushRecordMapper">
    
    <resultMap type="com.ruoyi.jarvis.domain.TencentDocBatchPushRecord" id="BatchPushRecordResult">
        <id     property="id"              column="id"              />
        <result property="batchId"         column="batch_id"        />
        <result property="fileId"          column="file_id"         />
        <result property="sheetId"         column="sheet_id"        />
        <result property="pushType"        column="push_type"       />
        <result property="triggerSource"   column="trigger_source"  />
        <result property="startTime"       column="start_time"      />
        <result property="endTime"         column="end_time"        />
        <result property="durationMs"      column="duration_ms"     />
        <result property="startRow"        column="start_row"       />
        <result property="endRow"          column="end_row"         />
        <result property="totalRows"       column="total_rows"      />
        <result property="successCount"    column="success_count"   />
        <result property="skipCount"       column="skip_count"      />
        <result property="errorCount"      column="error_count"     />
        <result property="status"          column="status"          />
        <result property="resultMessage"   column="result_message"  />
        <result property="errorMessage"    column="error_message"   />
        <result property="createTime"      column="create_time"     />
        <result property="updateTime"      column="update_time"     />
    </resultMap>

    <sql id="selectBatchPushRecordVo">
        SELECT id, batch_id, file_id, sheet_id, push_type, trigger_source,
               start_time, end_time, duration_ms, start_row, end_row, total_rows,
               success_count, skip_count, error_count, status, result_message,
               error_message, create_time, update_time
        FROM tencent_doc_batch_push_record
    </sql>

    <insert id="insertBatchPushRecord" parameterType="com.ruoyi.jarvis.domain.TencentDocBatchPushRecord">
        INSERT INTO tencent_doc_batch_push_record (
            batch_id, file_id, sheet_id, push_type, trigger_source,
            start_time, end_time, duration_ms, start_row, end_row, total_rows,
            success_count, skip_count, error_count, status, result_message, error_message
        ) VALUES (
            #{batchId}, #{fileId}, #{sheetId}, #{pushType}, #{triggerSource},
            #{startTime}, #{endTime}, #{durationMs}, #{startRow}, #{endRow}, #{totalRows},
            #{successCount}, #{skipCount}, #{errorCount}, #{status}, #{resultMessage}, #{errorMessage}
        )
    </insert>

    <update id="updateBatchPushRecord" parameterType="com.ruoyi.jarvis.domain.TencentDocBatchPushRecord">
        UPDATE tencent_doc_batch_push_record
        <set>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="durationMs != null">duration_ms = #{durationMs},</if>
            <if test="successCount != null">success_count = #{successCount},</if>
            <if test="skipCount != null">skip_count = #{skipCount},</if>
            <if test="errorCount != null">error_count = #{errorCount},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="resultMessage != null">result_message = #{resultMessage},</if>
            <if test="errorMessage != null">error_message = #{errorMessage},</if>
        </set>
        WHERE batch_id = #{batchId}
    </update>

    <select id="selectByBatchId" parameterType="String" resultMap="BatchPushRecordResult">
        <include refid="selectBatchPushRecordVo"/>
        WHERE batch_id = #{batchId}
    </select>

    <select id="selectBatchPushRecordList" parameterType="com.ruoyi.jarvis.domain.TencentDocBatchPushRecord" resultMap="BatchPushRecordResult">
        <include refid="selectBatchPushRecordVo"/>
        <where>
            <if test="fileId != null and fileId != ''">AND file_id = #{fileId}</if>
            <if test="sheetId != null and sheetId != ''">AND sheet_id = #{sheetId}</if>
            <if test="status != null and status != ''">AND status = #{status}</if>
            <if test="pushType != null and pushType != ''">AND push_type = #{pushType}</if>
        </where>
        ORDER BY create_time DESC
    </select>

    <select id="selectRecentRecords" resultMap="BatchPushRecordResult">
        <include refid="selectBatchPushRecordVo"/>
        <where>
            <if test="fileId != null and fileId != ''">AND file_id = #{fileId}</if>
        </where>
        ORDER BY create_time DESC
        LIMIT #{limit}
    </select>

    <select id="selectLastSuccessRecord" resultMap="BatchPushRecordResult">
        <include refid="selectBatchPushRecordVo"/>
        WHERE file_id = #{fileId} 
          AND sheet_id = #{sheetId}
          AND status IN ('SUCCESS', 'PARTIAL', 'PARTIAL_SUCCESS')
        ORDER BY end_time DESC
        LIMIT 1
    </select>

    <select id="selectRunningRecordsBefore" resultMap="BatchPushRecordResult">
        <include refid="selectBatchPushRecordVo"/>
        WHERE status = 'RUNNING'
          <if test="fileId != null and fileId != ''">AND file_id = #{fileId}</if>
          AND start_time &lt; #{beforeTime}
        ORDER BY start_time ASC
    </select>

</mapper>

