<?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.datamate.collection.infrastructure.persistence.mapper.TaskExecutionMapper">

    <!-- Select Latest Execution by Task -->
    <select id="selectLatestByTaskId" resultType="com.datamate.collection.domain.model.entity.TaskExecution">
        SELECT * FROM t_dc_task_executions
        WHERE task_id = #{taskId}
        ORDER BY started_at DESC
        LIMIT 1
    </select>

    <!-- Complete Execution -->
    <update id="completeExecution">
        UPDATE t_dc_task_executions
        SET status = #{status},
            completed_at = #{completedAt},
            records_processed = #{recordsProcessed},
            records_total = #{recordsTotal},
            records_success = #{recordsSuccess},
            records_failed = #{recordsFailed},
            error_message = #{errorMessage},
            updated_at = NOW()
        WHERE id = #{executionId}
    </update>

</mapper>
