<?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.fs.provider.mapper.ServiceGenConfigRunLogMapper">

    <!-- 抢占执行权：插入成功返回1；如果(config_id,run_key)已存在则返回0 -->
    <insert id="insertIgnoreForClaim" parameterType="com.fs.provider.domain.ServiceGenConfigRunLog">
        INSERT IGNORE INTO service_gen_config_run_log
        (config_id, run_key, frequency_unit, status, started_at, server_id, created_at, updated_at)
        VALUES
        (#{configId}, #{runKey}, #{frequencyUnit}, #{status}, #{startedAt}, #{serverId}, NOW(), NOW())
    </insert>

    <update id="markSuccess" parameterType="com.fs.provider.domain.ServiceGenConfigRunLog">
        UPDATE service_gen_config_run_log
        SET status = 1,
        finished_at = NOW(),
        task_total = #{taskTotal},
        task_created = #{taskCreated},
        order_created = #{orderCreated},
        remark = #{remark},
        updated_at = NOW()
        WHERE config_id = #{configId}
        AND run_key = #{runKey}
    </update>

    <update id="markFailed" parameterType="com.fs.provider.domain.ServiceGenConfigRunLog">
        UPDATE service_gen_config_run_log
        SET status = 2,
        finished_at = NOW(),
        remark = #{remark},
        updated_at = NOW()
        WHERE config_id = #{configId}
        AND run_key = #{runKey}
    </update>

    <!-- 可选：一般不用 -->
    <select id="exists" resultType="java.lang.Integer">
        SELECT 1
        FROM service_gen_config_run_log
        WHERE config_id = #{configId}
        AND run_key = #{runKey}
        LIMIT 1
    </select>







</mapper>