package com.css.simulation.resource.scheduler.infra.db.mysql.mapper;


import com.css.simulation.resource.scheduler.infra.entity.AlgorithmEntity;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

/**
 * 场景查询
 * scene_natural
 * scene_standard
 * scene_accident
 */
@Mapper
public interface AlgorithmMapper {

    @Results(id = "algorithm", value = {
            @Result(column = "id", property = "id", jdbcType = JdbcType.VARCHAR),
            @Result(column = "algorithm_code", property = "algorithmCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "minio_path", property = "minioPath", jdbcType = JdbcType.VARCHAR),
            @Result(column = "docker_import", property = "dockerImport", jdbcType = JdbcType.VARCHAR),
            @Result(column = "docker_image", property = "dockerImage", jdbcType = JdbcType.VARCHAR),
            @Result(column = "upload_mode", property = "uploadMode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "git_url", property = "gitUrl", jdbcType = JdbcType.VARCHAR),
            @Result(column = "git_user_name", property = "gitUserName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "git_password", property = "gitPassword", jdbcType = JdbcType.VARCHAR)
    })
    @Select("select id,\n" +
            "       algorithm_code,\n" +
            "       minio_path,\n" +
            "       docker_import,\n" +
            "       docker_image,\n" +
            "       upload_mode,\n" +
            "       git_url,\n" +
            "       git_user_name,\n" +
            "       git_password\n" +
            "from algorithm\n" +
            "where id = #{id}")
    AlgorithmEntity selectById(@Param("id") String id);

    @Select("select docker_image\n" +
            "from algorithm\n" +
            "where id = #{id}")
    String selectDockerImageById(@Param("id") String id);

    @Select("select id,\n" +
            "       algorithm_code,\n" +
            "       minio_path,\n" +
            "       docker_import,\n" +
            "       docker_image,\n" +
            "       upload_mode,\n" +
            "       git_url,\n" +
            "       git_user_name,\n" +
            "       git_password\n" +
            "from algorithm\n" +
            "where docker_import = #{dockerImport}\n" +
            "order by create_time desc")
    List<AlgorithmEntity> selectByDockerImport(@Param("dockerImport") String dockerImport);

    @Update("update algorithm\n" +
            "set docker_import = #{dockerImport},\n" +
            "    docker_image  = #{dockerImage}\n" +
            "where id = #{algorithmId}")
    void updateDockerImportAndDockerImageById(@Param("dockerImport") String dockerImport, @Param("dockerImage") String dockerImage, @Param("algorithmId") String algorithmId);

    @Update("<script>" +
            "update algorithm\n" +
            "set docker_import = #{dockerImport},\n" +
            "    docker_image  = #{dockerImage}\n" +
            "where 1 = 1\n" +
            "<if test='idList != null and idList.size() > 0'>\n" +
            "   and id in\n" +
            "       <foreach collection='idList' index='index' item='item' open='(' close=')' separator=','>\n" +
            "           #{item}" +
            "       </foreach>\n" +
            "</if>\n" +
            "</script>")
    void updateDockerImportAndDockerImageByIdList(@Param("dockerImport") String dockerImport, @Param("dockerImage") String dockerImage, @Param("idList") List<String> idList);
}
