<?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.css.simulation.resource.server.infra.db.mysql.mapper.AlgorithmMapper">


    <!--添加-->
    <insert id="add" parameterType="api.common.pojo.po.algorithm.AlgorithmPO">
        insert into algorithm
        (id,
         algorithm_name,
         algorithm_code,
         description,
         validation_status,
         upload_mode,
         git_url,
         git_user_name,
         git_password,
         file_name,
         minio_path,
         share,
         create_time,
         create_user_id,
         modify_time,
         modify_user_id,
         is_deleted)
            value
            ( #{id,jdbcType=VARCHAR},
            #{algorithmName,jdbcType=VARCHAR},
            #{algorithmCode,jdbcType=VARCHAR},
            #{description,jdbcType=LONGNVARCHAR},
            #{validationStatus,jdbcType=VARCHAR},
            #{uploadMode,jdbcType=VARCHAR},
            #{gitUrl,jdbcType=VARCHAR},
            #{gitUserName,jdbcType=VARCHAR},
            #{gitPassword,jdbcType=VARCHAR},
            #{fileName,jdbcType=VARCHAR},
            #{minioPath,jdbcType=VARCHAR},
            #{share,jdbcType=VARCHAR},
            #{createTime,jdbcType=TIMESTAMP},
            #{createUserId,jdbcType=VARCHAR},
            #{modifyTime,jdbcType=TIMESTAMP},
            #{modifyUserId,jdbcType=VARCHAR},
            #{isDeleted,jdbcType=VARCHAR}
            )
    </insert>

    <!--查询算法详情-->
    <select id="selectDetailsById" parameterType="java.lang.String"
            resultType="api.common.pojo.po.algorithm.AlgorithmPO">
        select *
        from algorithm
        where id = #{algorithmId,jdbcType=VARCHAR}
    </select>

    <!--查询算法列表—私有-->
    <select id="selectAlgorithmList" parameterType="api.common.pojo.param.algorithm.AlgorithmParameter"
            resultType="api.common.pojo.vo.algorithm.AlgorithmVO">
        select id, algorithm_code, algorithm_name, description, validation_status, upload_mode from algorithm
        <where>
            is_deleted = '0' and share = '0'
        </where>
        <if test="algorithmCode != null and algorithmCode != ''">
            and algorithm_code like CONCAT('%',#{algorithmCode,jdbcType=VARCHAR},'%')
        </if>
        <if test="algorithmName != null and algorithmName != ''">
            and algorithm_name like CONCAT('%',#{algorithmName,jdbcType=VARCHAR},'%')
        </if>
        <if test="description != null and description != ''">
            and description like CONCAT('%',#{description,jdbcType=LONGNVARCHAR},'%')
        </if>
        <if test="validationStatus != null and validationStatus != ''">
            and validation_status = #{validationStatus,jdbcType=VARCHAR}
        </if>
        <if test="uploadMode != null and uploadMode != ''">
            and upload_mode = #{uploadMode,jdbcType=VARCHAR}
        </if>
        <if test="createUserId != null and createUserId != ''">
            and create_user_id = #{createUserId,jdbcType=VARCHAR}
        </if>
        order by modify_time desc, create_time desc
    </select>

    <!--查询绑定算法的正在运行的项目-->
    <select id="selectRunningProject" parameterType="java.lang.String"
            resultType="api.common.pojo.vo.algorithm.RunningProjectVO">
        select a.id,a.project_id from simulation_automatic_subproject a left join simulation_automatic_project b
        on a.parent_id=b.id
        where a.now_run_state = '20' and a.is_deleted = '0' and b.is_deleted = '0'
        <if test="id != null and id != ''">
            and b.algorithm = #{id,jdbcType=VARCHAR}
        </if>
    </select>

    <!--查询绑定算法的正在运行的项目的父项目-->
    <select id="selectRunningProjectParent" parameterType="java.lang.String"
            resultType="api.common.pojo.vo.algorithm.RunningProjectVO">
        select distinct a.parent_id as id,a.parent_project_id as projectId from simulation_automatic_subproject a left
        join simulation_automatic_project b
        on a.parent_id=b.id
        where a.now_run_state = '20' and a.is_deleted = '0' and b.is_deleted = '0'
        <if test="id != null and id != ''">
            and b.algorithm = #{id,jdbcType=VARCHAR}
        </if>
    </select>

    <!--查询算法绑定的自动运行父项目-->
    <select id="selectAutoProjectIdByAlgorithmId" parameterType="java.lang.String"
            resultType="api.common.pojo.vo.algorithm.RunningProjectVO">
        select id, project_id
        from simulation_automatic_project
        where is_deleted = '0'
          and algorithm = #{algorithmId}
    </select>

    <!--查询算法名称是否已存在-当前用户-->
    <select id="selectAlgorithmName" parameterType="api.common.pojo.param.algorithm.AlgorithmParameter"
            resultType="api.common.pojo.po.algorithm.AlgorithmPO">
        select id from algorithm
        where is_deleted = '0'
        <if test="algorithmName != null and algorithmName != ''">
            and algorithm_name = #{algorithmName,jdbcType=VARCHAR}
        </if>
        <if test="createUserId != null and createUserId != ''">
            and create_user_id = #{createUserId,jdbcType=VARCHAR}
        </if>
        <if test="id != null and id != ''">
            and id != #{id,jdbcType=VARCHAR}
        </if>
        <if test="share != null and share != ''">
            and share = #{share,jdbcType=VARCHAR}
        </if>
    </select>


    <!--查询算法列表—公有-->
    <select id="selectSharedAlgorithmList" parameterType="api.common.pojo.param.algorithm.AlgorithmParameter"
            resultType="api.common.pojo.vo.algorithm.AlgorithmVO">
        select id, algorithm_code, algorithm_name, description, validation_status, upload_mode from algorithm
        <where>
            is_deleted = '0'
        </where>
        <if test="algorithmCode != null and algorithmCode != ''">
            and algorithm_code like CONCAT('%',#{algorithmCode,jdbcType=VARCHAR},'%')
        </if>
        <if test="algorithmName != null and algorithmName != ''">
            and algorithm_name like CONCAT('%',#{algorithmName,jdbcType=VARCHAR},'%')
        </if>
        <if test="description != null and description != ''">
            and description like CONCAT('%',#{description,jdbcType=LONGNVARCHAR},'%')
        </if>
        <if test="validationStatus != null and validationStatus != ''">
            and validation_status = #{validationStatus,jdbcType=VARCHAR}
        </if>
        <if test="share != null and share != ''">
            and share = #{share,jdbcType=VARCHAR}
        </if>
        order by modify_time desc, create_time desc
    </select>

    <!--查询算法名称是否已存在-公有-->
    <select id="selectSharedAlgorithmName" resultType="api.common.pojo.po.algorithm.AlgorithmPO">
        select id from algorithm
        where is_deleted = '0' and share = '1'
        <if test="algorithmName != null and algorithmName != ''">
            and algorithm_name = #{algorithmName,jdbcType=VARCHAR}
        </if>
    </select>

    <!--删除-->
    <update id="deleteById">
        update algorithm
        set is_deleted = '1'
        where id = #{algorithmId}
    </update>

    <update id="update" parameterType="api.common.pojo.po.algorithm.AlgorithmPO">
        update algorithm
        set algorithm_name=#{algorithmName,jdbcType=VARCHAR},
            description=#{description,jdbcType=VARCHAR},
            validation_status=#{validationStatus,jdbcType=VARCHAR},
            upload_mode=#{uploadMode,jdbcType=VARCHAR},
            git_url=#{gitUrl,jdbcType=VARCHAR},
            git_user_name=#{gitUserName,jdbcType=VARCHAR},
            git_password=#{gitPassword,jdbcType=VARCHAR},
            file_name=#{fileName,jdbcType=VARCHAR},
            minio_path=#{minioPath,jdbcType=VARCHAR},
            modify_time=#{modifyTime,jdbcType=VARCHAR},
            modify_user_id=#{modifyUserId,jdbcType=VARCHAR}
        where id = #{id,jdbcType=VARCHAR}
    </update>

    <!--查询算法数量首页-->
    <select id="selectDetailsBySy" parameterType="api.common.pojo.param.algorithm.AlgorithmParameter"
            resultType="java.lang.Integer">
        select count(id)
        from algorithm
        where
        is_deleted = '0'
        <if test="createUserId != null and createUserId != ''">
            and create_user_id=#{createUserId,jdbcType=VARCHAR}
        </if>
        <if test="share != null and share != ''">
            and share=#{share}
        </if>
        <if test="uploadMode != null and uploadMode != ''">
            and upload_mode=#{uploadMode}
        </if>

    </select>
    <select id="selectAlgorithmNameByAlgorithmId" resultType="java.lang.String">
        select algorithm_name from algorithm where id = #{algorithmId}
    </select>
</mapper>
