<?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.aizuda.snailjob.template.datasource.persistence.mapper.RetryMapper">

    <insert id="insertBatch" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO sj_retry_task (namespace_id, unique_id, group_name, scene_name, idempotent_id, biz_no,
                                   executor_name, args_str, ext_attrs, next_trigger_at, task_type, retry_status,
                                   create_dt)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (
                #{item.namespaceId},
                #{item.uniqueId},
                #{item.groupName},
                #{item.sceneName},
                #{item.idempotentId},
                #{item.bizNo},
                #{item.executorName},
                #{item.argsStr},
                #{item.extAttrs},
                #{item.nextTriggerAt},
                #{item.taskType},
                #{item.retryStatus},
                #{item.createDt}
            )
        </foreach>
    </insert>

    <update id="updateBatchNextTriggerAtById" parameterType="java.util.List">
        UPDATE sj_retry_task_${partition}
           SET next_trigger_at = src.next_trigger_at,
               update_dt = CURRENT_TIMESTAMP
          FROM sj_retry_task_${partition} AS target
                JOIN (
                <foreach collection="list" item="item" index="index" separator="UNION ALL">
                    SELECT
                        #{item.id}              AS id,
                        #{item.nextTriggerAt}   AS next_trigger_at
                </foreach>
                ) AS src ON target.id = src.id;
    </update>
</mapper>
