<?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.AutoMlInsDao">

    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into auto_ml_ins(auto_ml_id, result_path, model_path, img_path, run_history_path, node_status,
                                node_result, param, source, argo_ins_name, argo_ins_ns, status)
        values (#{autoMlIns.autoMlId}, #{autoMlIns.resultPath}, #{autoMlIns.modelPath}, #{autoMlIns.imgPath},
                #{autoMlIns.runHistoryPath}, #{autoMlIns.nodeStatus},
                #{autoMlIns.nodeResult}, #{autoMlIns.param}, #{autoMlIns.source}, #{autoMlIns.argoInsName},
                #{autoMlIns.argoInsNs}, #{autoMlIns.status})
    </insert>

    <update id="update">
        update auto_ml_ins
        <set>
            <if test="autoMlIns.modelPath != null and autoMlIns.modelPath != ''">
                model_path = #{autoMlIns.modelPath},
            </if>
            <if test="autoMlIns.imgPath != null and autoMlIns.imgPath != ''">
                img_path = #{autoMlIns.imgPath},
            </if>
            <if test="autoMlIns.status != null and autoMlIns.status != ''">
                status = #{autoMlIns.status},
            </if>
            <if test="autoMlIns.nodeStatus != null and autoMlIns.nodeStatus != ''">
                node_status = #{autoMlIns.nodeStatus},
            </if>
            <if test="autoMlIns.nodeResult != null and autoMlIns.nodeResult != ''">
                node_result = #{autoMlIns.nodeResult},
            </if>
            <if test="autoMlIns.state != null">
                state = #{autoMlIns.state},
            </if>
            <if test="autoMlIns.updateTime != null">
                update_time = #{autoMlIns.updateTime},
            </if>
            <if test="autoMlIns.finishTime != null">
                finish_time = #{autoMlIns.finishTime},
            </if>
        </set>
        where id = #{autoMlIns.id}
    </update>

    <select id="count" resultType="java.lang.Long">
        select count(1)
        from auto_ml_ins
        <where>
            state = 1
            and auto_ml_id = #{autoMlIns.autoMlId}
        </where>
    </select>

    <select id="queryAllByLimit" resultType="com.ruoyi.platform.domain.AutoMlIns">
        select * from auto_ml_ins
        <where>
            state = 1
            and auto_ml_id = #{autoMlIns.autoMlId}
        </where>
        order by update_time DESC
        limit #{pageable.offset}, #{pageable.pageSize}
    </select>

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

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

    <select id="getByAutoMlId" resultType="com.ruoyi.platform.domain.AutoMlIns">
        select *
        from auto_ml_ins
        where auto_ml_id = #{autoMlId}
          and state = 1
        order by update_time DESC limit 5
    </select>
</mapper>