<?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.platform.mapper.RayInsDao">
    <insert id="insert">
        insert into ray_ins(ray_id, result_path, argo_ins_name, argo_ins_ns, node_status, node_result, param, source,
                            status)
        values (#{rayIns.rayId}, #{rayIns.resultPath}, #{rayIns.argoInsName}, #{rayIns.argoInsNs},
                #{rayIns.nodeStatus}, #{rayIns.nodeResult}, #{rayIns.param}, #{rayIns.source}, #{rayIns.status})
    </insert>

    <update id="update">
        update ray_ins
        <set>
            <if test="rayIns.resultPath != null and rayIns.resultPath != ''">
                result_path = #{rayIns.resultPath},
            </if>
            <if test="rayIns.status != null and rayIns.status != ''">
                status = #{rayIns.status},
            </if>
            <if test="rayIns.nodeStatus != null and rayIns.nodeStatus != ''">
                node_status = #{rayIns.nodeStatus},
            </if>
            <if test="rayIns.nodeResult != null and rayIns.nodeResult != ''">
                node_result = #{rayIns.nodeResult},
            </if>
            <if test="rayIns.state != null">
                state = #{rayIns.state},
            </if>
            <if test="rayIns.finishTime != null">
                finish_time = #{rayIns.finishTime},
            </if>
        </set>
        where id = #{rayIns.id}
    </update>

    <select id="count" resultType="java.lang.Long">
        select count(1)
        from ray_ins
        <where>
            state = 1
            and ray_id = #{rayId}
        </where>
    </select>

    <select id="queryAllByLimit" resultType="com.ruoyi.platform.domain.RayIns">
        select * from ray_ins
        <where>
            state = 1
            and ray_id = #{rayId}
        </where>
        order by update_time DESC
        limit #{pageable.offset}, #{pageable.pageSize}
    </select>

    <select id="queryById" resultType="com.ruoyi.platform.domain.RayIns">
        select * from ray_ins
        <where>
            state = 1 and id = #{id}
        </where>
    </select>

    <select id="getByRayId" resultType="com.ruoyi.platform.domain.RayIns">
        select *
        from ray_ins
        where ray_id = #{rayId}
          and state = 1
        order by update_time DESC limit 5
    </select>

    <select id="queryByRayInsIsNotTerminated" resultType="com.ruoyi.platform.domain.RayIns">
        select *
        from ray_ins
        where (status NOT IN ('Terminated', 'Succeeded', 'Failed')
            OR status IS NULL)
          and state = 1
    </select>
</mapper>