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


import api.common.pojo.po.scheduler.SchedulerProjectPO;
import com.css.simulation.resource.scheduler.infra.db.entity.SimulationAutomaticSubprojectEntity;
import com.css.simulation.resource.scheduler.infra.entity.ProjectEntity;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.sql.Timestamp;

@Mapper
public interface SimulationAutomaticSubprojectMapper {

    @Results(id = "project", value = {
            @Result(column = "id", property = "id", jdbcType = JdbcType.VARCHAR),
            @Result(column = "scene", property = "scenePackageId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_user_id", property = "createUserId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "parallelism", property = "parallelism", jdbcType = JdbcType.VARCHAR),
            @Result(column = "now_run_state", property = "nowRunState", jdbcType = JdbcType.VARCHAR),
            @Result(column = "project_type", property = "projectType", jdbcType = JdbcType.VARCHAR),
            @Result(column = "is_choice_gpu", property = "isChoiceGpu", jdbcType = JdbcType.VARCHAR),
            @Result(column = "max_simulation_time", property = "maxSimulationTime", jdbcType = JdbcType.VARCHAR)
    })
    @Select("select sas.id,\n" +
            "       sap.scene,\n" +
            "       sas.create_user_id,\n" +
            "       sap.parallelism,\n" +
            "       sas.now_run_state,\n" +
            "       '2' project_type,\n" +
            "       sap.is_choice_gpu,\n" +
            "       sap.max_simulation_time\n" +
            "from simulation_automatic_subproject sas\n" +
            "         left join simulation_automatic_project sap on sas.parent_id = sap.id\n" +
            "where sas.id = #{projectId}")
    ProjectEntity selectById(@Param("projectId") String projectId);

    @Select("select create_user_id\n" +
            "from simulation_automatic_subproject\n" +
            "where id = #{subProjectId}")
    String selectCreateUserById(@Param("subProjectId") String subProjectId);


    @Update("update simulation_automatic_subproject\n" +
            "set now_run_state  = #{state},\n" +
            "    finish_time = #{finishTime}\n" +
            "where id = #{id}")
    void updateProjectState(@Param("id") String id, @Param("state") String state, @Param("finishTime") Timestamp finishTime);


    @Update("update simulation_automatic_subproject\n" +
            "set error_message = #{errorMessage}\n" +
            "where project_id = #{id}\n")
    void saveErrorMessage(SchedulerProjectPO schedulerProjectPO);

    @Select("select parameter_type\n" +
            "from model_vehicle t1\n" +
            "         left join model_config t2 on t1.id = t2.vehicle_id\n" +
            "         left join simulation_automatic_project t3 on t2.id = t3.vehicle\n" +
            "         left join simulation_automatic_subproject t4 on t3.id = t4.parent_id\n" +
            "where t4.id = #{projectId}\n")
    String selectModelTypeByProjectId(String projectId);

    @Update("update simulation_automatic_subproject\n" +
            "set details  = #{details}\n" +
            "where id = #{id}")
    void updateProjectDetails(@Param("id") String id, @Param("details") String details);


    @Update("update simulation_automatic_subproject\n" +
            "set report  = #{report}\n" +
            "where id = #{id}")
    void updateProjectReport(@Param("id") String id, @Param("report") String report);

    String selectParentIdById(SimulationAutomaticSubprojectEntity build);


}
