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


    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into model_dependency1(parent_model, model_name, version, repo_id, identifier, owner, meta, state)
        values (#{parentModel}, #{modelName}, #{version}, #{repoId}, #{identifier}, #{owner}, #{meta}, #{state})
    </insert>

    <select id="queryModelDependency" resultType="com.ruoyi.platform.domain.ModelDependency1">
        select *
        from model_dependency1
        where
        state = 1
        <if test="modelName != null and modelName != ''">
            and model_name = #{modelName}
        </if>
        <if test="repoId != null and repoId != ''">
            and repo_id = #{repoId}
        </if>
        <if test="owner != null and owner != ''">
            and owner = #{owner}
        </if>
        order by create_time
    </select>

    <select id="queryByRepoAndVersion" resultType="com.ruoyi.platform.domain.ModelDependency1">
        select *
        from model_dependency1
        where repo_id = #{repoId}
          and identifier = #{identifier}
          and version = #{version}
          and state = 1
        order by create_time desc limit 1
    </select>

    <select id="queryByParentModel" resultType="com.ruoyi.platform.domain.ModelDependency1">
        select *
        from model_dependency1
        where JSON_CONTAINS(parent_model,#{parentModel})
          and state = 1
    </select>

    <select id="getBuildingModel" resultType="com.ruoyi.platform.domain.ModelDependency1">
        select *
        from model_dependency1
        where repo_id = #{repoId}
          and identifier = #{identifier}
          and version = #{version}
          and state = 2
        order by create_time desc limit 1
    </select>

    <select id="queryByTrainTask" resultType="com.ruoyi.platform.domain.ModelDependency1">
        select *
        from model_dependency1
        where JSON_CONTAINS(meta, #{trainTask})
          and state = 2
        order by create_time desc limit 1
    </select>

    <select id="queryByInsId" resultType="com.ruoyi.platform.domain.ModelDependency1">
        select *
        from model_dependency1
        where JSON_CONTAINS(meta, #{insId})
        order by id desc limit 1
    </select>

    <select id="getMeta" resultType="java.lang.String">
        select meta from model_dependency1
        where identifier = #{identifier}
        and owner = #{owner}
        and version = #{version}
        and state = 1
        order by create_time desc limit 1
    </select>

    <update id="deleteModel">
        update model_dependency1
        set state = 0
        where repo_id = #{repoId} and identifier = #{identifier} and owner = #{owner}
        <if test="version != null and version != ''">
            and version = #{version}
        </if>
    </update>

    <update id="deleteModelDependency">
        update model_dependency1
        set parent_model = null
        where JSON_CONTAINS(parent_model, #{parentModel})
    </update>

    <update id="updateState">
        update model_dependency1
        set state = 1,
        meta = #{meta}
        where repo_id = #{repoId}
          and identifier = #{identifier}
          and version = #{version}
          and state = 2
    </update>
</mapper>