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


    <!--根据id查询工作信息-->
    <select id="selectById" parameterType="string" resultType="api.common.pojo.po.project.SimulationAutomaticProjectPO">
        select *
        from simulation_automatic_project
        where id = #{id,jdbcType=VARCHAR}
    </select>

    <!--根据条件查询工作信息-->
    <select id="selectAutomaticProjectByQuery"
            parameterType="api.common.pojo.param.project.SimulationManualProjectParam"
            resultType="api.common.pojo.po.project.SimulationAutomaticProjectPO">
        select * from simulation_automatic_project
        where is_deleted = '0'
        <if test="projectName != null and projectName != ''">
            and project_name = #{projectName,jdbcType=VARCHAR}
        </if>
    </select>

    <!--查询最新项目id-->
    <select id="selectLastProjectId" resultType="api.common.pojo.po.project.SimulationAutomaticProjectPO">
        select project_date, project_num, project_id
        from simulation_automatic_project
        where project_date = #{nowRq,jdbcType=INTEGER}
          and is_deleted = '0'
        order by project_date desc, project_num desc limit 1
    </select>

    <!--查询自动项目列表-->
    <select id="selectAutomaticProjectList" parameterType="api.common.pojo.param.project.SimulationManualProjectParam"
            resultType="api.common.pojo.vo.project.SimulationManualProjectVO">
        select id,
        project_date,
        project_num,
        project_id,
        project_name,
        project_describe,
        sap.algorithm,
        algorithm_type,
        vehicle,
        scene,
        operation_cycle,
        parallelism,
        rule_view,
        max_simulation_time,
        is_choice_gpu,
        (select count(1)
        from simulation_automatic_subproject sas
        where sas.is_deleted = '0'
        and sas.parent_id = sap.id) automatic_run_times,
        (select max(sas.create_time) from simulation_automatic_subproject sas where sas.parent_id = sap.id) last_run_time,
        (select max(sas.create_time) from simulation_automatic_subproject sas where sas.parent_id = sap.id) last_run_time_fmt,
        automatic_run_state,
        create_time,
        create_user_id,
        modify_time,
        modify_user_id,
        is_deleted
        from simulation_automatic_project sap
        where is_deleted = '0'
        <if test="projectId != null and projectId != ''">
            and project_id like CONCAT('%',#{projectId,jdbcType=VARCHAR},'%')
        </if>
        <if test="projectName != null and projectName != ''">
            and project_name like CONCAT('%',#{projectName,jdbcType=VARCHAR},'%')
        </if>
        <if test="createTimeStart != null">
            and create_time &gt;= #{createTimeStart,jdbcType=TIMESTAMP}
        </if>
        <if test="createTimeEnd != null">
            and create_time &lt;= #{createTimeEnd,jdbcType=TIMESTAMP}
        </if>
        <if test="createUserId != null and createUserId != ''">
            and create_user_id = #{createUserId,jdbcType=VARCHAR}
        </if>
        order by project_date desc, project_num desc, modify_time desc
    </select>

    <!--创建项目信息-->
    <insert id="add" parameterType="api.common.pojo.po.project.SimulationAutomaticProjectPO">
        insert into simulation_automatic_project
        (id,
         project_date,
         project_num,
         project_id,
         project_name,
         project_describe,
         algorithm,
         algorithm_type,
         vehicle,
         scene,
         operation_cycle,
         parallelism,
         rule_view,
         max_simulation_time,
         is_choice_gpu,
         automatic_run_times,
         last_run_time,
         automatic_run_state,
         create_time,
         create_user_id,
         modify_time,
         modify_user_id,
         is_deleted,
         algorithm_array,
         vehicle_array,
         scene_array)
        values (#{id,jdbcType=VARCHAR},
                #{projectDate,jdbcType=INTEGER},
                #{projectNum,jdbcType=INTEGER},
                #{projectId,jdbcType=VARCHAR},
                #{projectName,jdbcType=VARCHAR},
                #{projectDescribe,jdbcType=VARCHAR},
                #{algorithm,jdbcType=VARCHAR},
                #{algorithmType,jdbcType=VARCHAR},
                #{vehicle,jdbcType=VARCHAR},
                #{scene,jdbcType=VARCHAR},
                #{operationCycle,jdbcType=VARCHAR},
                #{parallelism,jdbcType=VARCHAR},
                #{ruleView,jdbcType=VARCHAR},
                #{maxSimulationTime},
                #{isChoiceGpu,jdbcType=VARCHAR},
                #{automaticRunTimes,jdbcType=BIGINT},
                #{lastRunTime,jdbcType=TIMESTAMP},
                #{automaticRunState,jdbcType=VARCHAR},
                #{createTime,jdbcType=TIMESTAMP},
                #{createUserId,jdbcType=VARCHAR},
                #{modifyTime,jdbcType=TIMESTAMP},
                #{modifyUserId,jdbcType=VARCHAR},
                #{isDeleted,jdbcType=VARCHAR},
                #{algorithmArray,jdbcType=VARCHAR},
                #{vehicleArray,jdbcType=VARCHAR},
                #{sceneArray,jdbcType=VARCHAR})
    </insert>

    <!--修改项目信息-->
    <update id="updateById" parameterType="api.common.pojo.po.project.SimulationAutomaticProjectPO">
        update simulation_automatic_project
        set project_name        = #{projectName,jdbcType=VARCHAR},
            project_describe    = #{projectDescribe,jdbcType=VARCHAR},
            algorithm           = #{algorithm,jdbcType=VARCHAR},
            algorithm_type      = #{algorithmType,jdbcType=VARCHAR},
            vehicle             = #{vehicle,jdbcType=VARCHAR},
            scene               = #{scene,jdbcType=VARCHAR},
            operation_cycle     = #{operationCycle,jdbcType=VARCHAR},
            parallelism         = #{parallelism,jdbcType=VARCHAR},
            rule_view           = #{ruleView,jdbcType=VARCHAR},
            max_simulation_time = #{maxSimulationTime},
            is_choice_gpu       = #{isChoiceGpu,jdbcType=VARCHAR},
            modify_time         = #{modifyTime,jdbcType=TIMESTAMP},
            modify_user_id      = #{modifyUserId,jdbcType=VARCHAR},
            algorithm_array       = #{algorithmArray,jdbcType=VARCHAR},
            vehicle_array       = #{vehicleArray,jdbcType=VARCHAR},
            scene_array       = #{sceneArray,jdbcType=VARCHAR}
        where id = #{id,jdbcType=VARCHAR}
          and is_deleted = '0'
    </update>


    <!--查询项目自动运行状态-->
    <select id="selectProjectNowRunState" resultType="api.common.pojo.po.project.SimulationAutomaticProjectPO">
        select automatic_run_state
        from simulation_automatic_project
        where
        is_deleted='0' and
        id in
        <foreach collection="array" open="(" separator="," close=")" item="id">
            #{id,jdbcType=VARCHAR}
        </foreach>
    </select>

    <!--删除-->
    <update id="deleteProject">
        update simulation_automatic_project
        set is_deleted='1'
        where id in
        <foreach collection="array" open="(" separator="," close=")" item="id">
            #{id,jdbcType=VARCHAR}
        </foreach>

    </update>

    <!--修改自动运行状态-->
    <update id="updateAutomaticRunState" parameterType="api.common.pojo.param.project.SimulationManualProjectParam">
        update simulation_automatic_project
        set automatic_run_state=#{automaticRunState,jdbcType=VARCHAR}
        where id = #{id,jdbcType=VARCHAR}
    </update>

    <!--修改自动运行次数-->
    <update id="updateAutomaticRunTimes" parameterType="api.common.pojo.po.project.SimulationAutomaticProjectPO">
        update simulation_automatic_project
        set automatic_Run_Times=#{automaticRunTimes,jdbcType=BIGINT}
        where id = #{id,jdbcType=VARCHAR}
    </update>
    <!--更新算法版本号-->
    <update id="updateAlgorithmGitVersion" parameterType="api.common.pojo.param.algorithm.AlgorithmParameter">
        update algorithm
        set git_version = #{gitVersion}
        where id = #{id}
    </update>

    <!--自动运行项目详细页面信息-->
    <select id="selectAutomaticProjectInfoById" parameterType="string"
            resultType="api.common.pojo.vo.project.AutomaticProjectVO">
        select p.id,
               p.project_name,
               p.project_describe,
               p.operation_cycle,
               p.parallelism,
               p.is_choice_gpu,
               p.vehicle,
               a.algorithm_name,
               s.package_name as sceneName,
               p.max_simulation_time,
                p.algorithm_array,
               p.vehicle_array,
               p.scene_array
        from simulation_automatic_project p
                 left join algorithm a on p.algorithm = a.id
                 left join scene_package s on p.scene = s.package_id
        where p.id = #{id,jdbcType=VARCHAR}
          and p.is_deleted = '0'
    </select>
    <select id="selectAlgorithmTypeById" resultType="java.lang.String">
        select algorithm_type from simulation_automatic_project where id = #{id}
    </select>
    <select id="selectAlgorithmById" resultType="java.lang.String">
        select algorithm from simulation_automatic_project where id = #{id}
    </select>




</mapper>
