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

    <resultMap type="com.ruoyi.platform.domain.ExperimentIns" id="ExperimentInsMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="experimentId" column="experiment_id" jdbcType="INTEGER"/>
        <result property="argoInsName" column="argo_ins_name" jdbcType="VARCHAR"/>
        <result property="argoInsNs" column="argo_ins_ns" jdbcType="VARCHAR"/>
        <result property="status" column="status" jdbcType="VARCHAR"/>
        <result property="nodesStatus" column="nodes_status" jdbcType="VARCHAR"/>
        <result property="nodesResult" column="nodes_result" jdbcType="VARCHAR"/>
        <result property="nodesLogs" column="nodes_logs" jdbcType="VARCHAR"/>
        <result property="globalParam" column="global_param" jdbcType="VARCHAR"/>
        <result property="metricRecord" column="metric_record" jdbcType="VARCHAR"/>
        <result property="startTime" column="start_time" jdbcType="TIMESTAMP"/>
        <result property="finishTime" column="finish_time" jdbcType="TIMESTAMP"/>
        <result property="createBy" column="create_by" jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateBy" column="update_by" jdbcType="VARCHAR"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
        <result property="state" column="state" jdbcType="INTEGER"/>
    </resultMap>

    <!--查询非终止态的实例-->
    <select id="queryByExperimentIsNotTerminated" resultMap="ExperimentInsMap">
        select id, experiment_id, argo_ins_name, argo_ins_ns, status, nodes_status,nodes_result, nodes_logs,global_param,metric_record, start_time, finish_time, create_by, create_time, update_by, update_time, state
        from experiment_ins
        where (status NOT IN ('Terminated', 'Succeeded', 'Failed')
    OR status IS NULL) and state = 1
    </select>

    <!--查询单个-->
    <select id="queryById" resultMap="ExperimentInsMap">
        select id, experiment_id, argo_ins_name, argo_ins_ns, status, nodes_status,nodes_result, nodes_logs,global_param,metric_record, start_time, finish_time, create_by, create_time, update_by, update_time, state
        from experiment_ins
        where id = #{id} and state = 1
    </select>

    <!--查询列表-->
    <select id="getByExperimentId" resultMap="ExperimentInsMap">
        select id, experiment_id, argo_ins_name, argo_ins_ns, status, nodes_status,nodes_result, nodes_logs, global_param,metric_record, start_time, finish_time, create_by, create_time, update_by, update_time, state
        from experiment_ins
        where experiment_id = #{experiment_id} and state = 1
        order by create_time DESC
        limit 5
    </select>

    <!--查询最近的3个实例列表-->
    <select id="getLatestInsList" resultMap="ExperimentInsMap">
        select id, experiment_id, argo_ins_name, argo_ins_ns, status, nodes_status,nodes_result, nodes_logs, global_param,metric_record, start_time, finish_time, create_by, create_time, update_by, update_time, state
        from experiment_ins
        where state = 1
        order by create_time DESC
        limit 3
    </select>



    <select id="queryByExperiment" resultMap="ExperimentInsMap">
        select
        id, experiment_id, argo_ins_name, argo_ins_ns, status, nodes_status,nodes_result, nodes_logs,global_param,metric_record, start_time, finish_time, create_by, create_time, update_by, update_time, state
        from experiment_ins
        <where>
            state = 1
            <if test="experimentIns.id != null">
                and id = #{experimentIns.id}
            </if>
            <if test="experimentIns.experimentId != null">
                and experiment_id = #{experimentIns.experimentId}
            </if>
            <if test="experimentIns.argoInsName != null and experimentIns.argoInsName != ''">
                and argo_ins_name = #{experimentIns.argoInsName}
            </if>
            <if test="experimentIns.argoInsNs != null and experimentIns.argoInsNs != ''">
                and argo_ins_ns = #{experimentIns.argoInsNs}
            </if>
            <if test="experimentIns.status != null and experimentIns.status != ''">
                and status = #{experimentIns.status}
            </if>
            <if test="experimentIns.nodesStatus != null and experimentIns.nodesStatus != ''">
                and nodes_status = #{experimentIns.nodesStatus}
            </if>
            <if test="experimentIns.nodesLogs != null and experimentIns.nodesLogs != ''">
                and nodes_logs = #{experimentIns.nodesLogs}
            </if>
            <if test="experimentIns.globalParam != null and experimentIns.globalParam != ''">
                and global_param = #{experimentIns.globalParam}
            </if>
            <if test="experimentIns.metricRecord != null and experimentIns.metricRecord != ''">
                and metric_record = #{experimentIns.metricRecord}
            </if>
            <if test="experimentIns.startTime != null">
                and start_time = #{experimentIns.startTime}
            </if>
            <if test="experimentIns.finishTime != null">
                and finish_time = #{experimentIns.finishTime}
            </if>
            <if test="experimentIns.createBy != null and experimentIns.createBy != ''">
                and create_by = #{experimentIns.createBy}
            </if>
            <if test="experimentIns.createTime != null">
                and create_time = #{experimentIns.createTime}
            </if>
            <if test="experimentIns.updateBy != null and experimentIns.updateBy != ''">
                and update_by = #{experimentIns.updateBy}
            </if>
            <if test="experimentIns.updateTime != null">
                and update_time = #{experimentIns.updateTime}
            </if>
        </where>
    </select>

    <!--查询指定行数据-->
    <select id="queryAllByLimit" resultMap="ExperimentInsMap">
        select
        id, experiment_id, argo_ins_name, argo_ins_ns, status, nodes_status,nodes_result, nodes_logs,global_param,metric_record, start_time, finish_time, create_by, create_time, update_by, update_time, state
        from experiment_ins
        <where>
            state = 1
            <if test="experimentIns.id != null">
                and id = #{experimentIns.id}
            </if>
            <if test="experimentIns.experimentId != null">
                and experiment_id = #{experimentIns.experimentId}
            </if>
            <if test="experimentIns.argoInsName != null and experimentIns.argoInsName != ''">
                and argo_ins_name = #{experimentIns.argoInsName}
            </if>
            <if test="experimentIns.argoInsNs != null and experimentIns.argoInsNs != ''">
                and argo_ins_ns = #{experimentIns.argoInsNs}
            </if>
            <if test="experimentIns.status != null and experimentIns.status != ''">
                and status = #{experimentIns.status}
            </if>
            <if test="experimentIns.nodesStatus != null and experimentIns.nodesStatus != ''">
                and nodes_status = #{experimentIns.nodesStatus}
            </if>
            <if test="experimentIns.nodesLogs != null and experimentIns.nodesLogs != ''">
                and nodes_logs = #{experimentIns.nodesLogs}
            </if>
            <if test="experimentIns.globalParam != null and experimentIns.globalParam != ''">
                and global_param = #{experimentIns.globalParam}
            </if>
            <if test="experimentIns.metricRecord != null and experimentIns.metricRecord != ''">
                and metric_record = #{experimentIns.metricRecord}
            </if>
            <if test="experimentIns.startTime != null">
                and start_time = #{experimentIns.startTime}
            </if>
            <if test="experimentIns.finishTime != null">
                and finish_time = #{experimentIns.finishTime}
            </if>
            <if test="experimentIns.createBy != null and experimentIns.createBy != ''">
                and create_by = #{experimentIns.createBy}
            </if>
            <if test="experimentIns.createTime != null">
                and create_time = #{experimentIns.createTime}
            </if>
            <if test="experimentIns.updateBy != null and experimentIns.updateBy != ''">
                and update_by = #{experimentIns.updateBy}
            </if>
            <if test="experimentIns.updateTime != null">
                and update_time = #{experimentIns.updateTime}
            </if>
        </where>
        order by update_time DESC
        limit #{pageable.offset}, #{pageable.pageSize}
    </select>

    <!--统计总行数-->
    <select id="count" resultType="java.lang.Long">
        select count(1)
        from experiment_ins
        <where>
            state = 1
            <if test="experimentIns.id != null">
                and id = #{experimentIns.id}
            </if>
            <if test="experimentIns.experimentId != null">
                and experiment_id = #{experimentIns.experimentId}
            </if>
            <if test="experimentIns.argoInsName != null and experimentIns.argoInsName != ''">
                and argo_ins_name = #{experimentIns.argoInsName}
            </if>
            <if test="experimentIns.argoInsNs != null and experimentIns.argoInsNs != ''">
                and argo_ins_ns = #{experimentIns.argoInsNs}
            </if>
            <if test="experimentIns.status != null and experimentIns.status != ''">
                and status = #{experimentIns.status}
            </if>
            <if test="experimentIns.nodesStatus != null and experimentIns.nodesStatus != ''">
                and nodes_status = #{experimentIns.nodesStatus}
            </if>
            <if test="experimentIns.nodesLogs != null and experimentIns.nodesLogs != ''">
                and nodes_logs = #{experimentIns.nodesLogs}
            </if>
            <if test="experimentIns.globalParam != null and experimentIns.globalParam != ''">
                and global_param = #{experimentIns.globalParam}
            </if>
            <if test="experimentIns.metricRecord != null and experimentIns.metricRecord != ''">
                and metric_record = #{experimentIns.metricRecord}
            </if>
            <if test="experimentIns.startTime != null">
                and start_time = #{experimentIns.startTime}
            </if>
            <if test="experimentIns.finishTime != null">
                and finish_time = #{experimentIns.finishTime}
            </if>
            <if test="experimentIns.createBy != null and experimentIns.createBy != ''">
                and create_by = #{experimentIns.createBy}
            </if>
            <if test="experimentIns.createTime != null">
                and create_time = #{experimentIns.createTime}
            </if>
            <if test="experimentIns.updateBy != null and experimentIns.updateBy != ''">
                and update_by = #{experimentIns.updateBy}
            </if>
            <if test="experimentIns.updateTime != null">
                and update_time = #{experimentIns.updateTime}
            </if>
        </where>
    </select>

    <select id="queryByExperimentId" resultMap="ExperimentInsMap">
        select id, experiment_id, argo_ins_name, argo_ins_ns, status, nodes_status,nodes_result, nodes_logs, global_param,metric_record, start_time, finish_time, create_by, create_time, update_by, update_time, state
        from experiment_ins
        where experiment_id = #{id} and state = 1
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into experiment_ins(experiment_id,argo_ins_name,argo_ins_ns,status,nodes_status,nodes_result,nodes_logs,global_param,metric_record,start_time,finish_time,create_by,create_time,update_by,update_time,state)
        values (#{experimentIns.experimentId},#{experimentIns.argoInsName},#{experimentIns.argoInsNs},#{experimentIns.status},#{experimentIns.nodesStatus},#{experimentIns.nodesResult},#{experimentIns.nodesLogs},#{experimentIns.globalParam},#{experimentIns.metricRecord},#{experimentIns.startTime},#{experimentIns.finishTime},#{experimentIns.createBy},#{experimentIns.createTime},#{experimentIns.updateBy},#{experimentIns.updateTime},#{experimentIns.state})
    </insert>

    <insert id="insertBatch" keyProperty="id" useGeneratedKeys="true">
        insert into
        experiment_ins(experiment_id,argo_ins_name,argo_ins_ns,status,nodes_status,nodes_result,nodes_logs,global_param,metric_record,start_time,finish_time,create_by,create_time,update_by,update_time,state)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.experimentId},#{entity.argoInsName},#{entity.argoInsNs},#{entity.status},#{entity.nodesStatus},#{entity.nodesResult},#{entity.nodesLogs},#{entity.globalParam},#{entity.metricRecord},#{entity.startTime},#{entity.finishTime},#{entity.createBy},#{entity.createTime},#{entity.updateBy},#{entity.updateTime},#{entity.state})
        </foreach>
    </insert>

<!--    <insert id="insertOrUpdateBatch" keyProperty="id" useGeneratedKeys="true">-->
<!--        insert into-->
<!--        experiment_ins(experiment_id,argo_ins_name,argo_ins_ns,status,nodes_status,nodes_result,start_time,finish_time,create_by,create_time,update_by,update_time,state)-->
<!--        values-->
<!--        <foreach collection="entities" item="entity" separator=",">-->
<!--            (#{entity.experimentId},#{entity.argoInsName},#{entity.argoInsNs},#{entity.status},#{entity.nodesStatus},#{entity.nodesResult},#{entity.startTime},#{entity.finishTime},#{entity.createBy},#{entity.createTime},#{entity.updateBy},#{entity.updateTime},#{entity.state})-->
<!--        </foreach>-->
<!--        on duplicate key update-->
<!--        experiment_id = values(experiment_id)argo_ins_name = values(argo_ins_name)argo_ins_ns =  values(argo_ins_ns)status = values(status) nodes_status = values(nodes_status) create_by = values(create_by)create_time = values(create_time)update_by = values(update_by)update_time = values(update_time)state = values(state)-->
<!--    </insert>-->
    <update id="insertOrUpdateBatch">
        insert into experiment_ins (id, experiment_id, argo_ins_name, argo_ins_ns, status, nodes_status, nodes_result, nodes_logs, global_param,metric_record, start_time, finish_time, create_by, create_time, update_by, update_time, state)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.id}, #{item.experimentId}, #{item.argoInsName}, #{item.argoInsNs}, #{item.status}, #{item.nodesStatus}, #{item.nodesResult}, #{item.nodesLogs}, #{item.globalParam},#{item.metricRecord}, #{item.startTime}, #{item.finishTime}, #{item.createBy}, #{item.createTime}, #{item.updateBy}, #{item.updateTime}, #{item.state})
        </foreach>
        ON DUPLICATE KEY UPDATE
        experiment_id = VALUES(experiment_id),
        argo_ins_name = VALUES(argo_ins_name),
        argo_ins_ns = VALUES(argo_ins_ns),
        status = VALUES(status),
        nodes_status = VALUES(nodes_status),
        nodes_result = VALUES(nodes_result),
        nodes_logs = VALUES(nodes_logs),
        global_param = VALUES(global_param),
        metric_record = VALUES(metric_record),
        start_time = VALUES(start_time),
        finish_time = VALUES(finish_time),
        create_by = VALUES(create_by),
        create_time = VALUES(create_time),
        update_by = VALUES(update_by),
        update_time = VALUES(update_time),
        state = VALUES(state);
    </update>

    <update id="update">
        update experiment_ins
        <set>
            <if test="experimentIns.experimentId != null">
                experiment_id = #{experimentIns.experimentId},
            </if>
            <if test="experimentIns.argoInsName != null and experimentIns.argoInsName != ''">
                argo_ins_name = #{experimentIns.argoInsName},
            </if>
            <if test="experimentIns.argoInsNs != null and experimentIns.argoInsNs != ''">
                argo_ins_ns = #{experimentIns.argoInsNs},
            </if>
            <if test="experimentIns.status != null and experimentIns.status != ''">
                status = #{experimentIns.status},
            </if>
            <if test="experimentIns.nodesStatus != null and experimentIns.nodesStatus != ''">
                nodes_status = #{experimentIns.nodesStatus},
            </if>
            <if test="experimentIns.nodesResult != null and experimentIns.nodesResult != ''">
                nodes_result = #{experimentIns.nodesResult},
            </if>
            <if test="experimentIns.nodesLogs != null and experimentIns.nodesLogs != ''">
                nodes_logs = #{experimentIns.nodesLogs},
            </if>
            <if test="experimentIns.globalParam != null and experimentIns.globalParam != ''">
                global_param = #{experimentIns.globalParam},
            </if>
            <if test="experimentIns.metricRecord != null and experimentIns.metricRecord != ''">
                metric_record = #{experimentIns.metricRecord},
            </if>
            <if test="experimentIns.startTime != null">
                start_time = #{experimentIns.startTime},
            </if>
            <if test="experimentIns.finishTime != null">
                finish_time = #{experimentIns.finishTime},
            </if>
            <if test="experimentIns.createBy != null and experimentIns.createBy != ''">
                create_by = #{experimentIns.createBy},
            </if>
            <if test="experimentIns.createTime != null">
                create_time = #{experimentIns.createTime},
            </if>
            <if test="experimentIns.updateBy != null and experimentIns.updateBy != ''">
                update_by = #{experimentIns.updateBy},
            </if>
            <if test="experimentIns.updateTime != null">
                update_time = #{experimentIns.updateTime},
            </if>
            <if test="experimentIns.state != null">
                state = #{experimentIns.state},
            </if>
        </set>
        where id = #{experimentIns.id}
    </update>


    <!--通过主键删除-->
    <delete id="deleteById">
        delete
        from experiment_ins
        where id = #{id}
    </delete>

</mapper>

