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.SimulationManualProjectEntity;
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 SimulationManualProjectMapper {

    @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 = "complexity_evaluation_rule_id", property = "complexityEvaluationRuleId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "risk_evaluation_rule_id", property = "riskEvaluationRuleId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "max_simulation_time", property = "maxSimulationTime", jdbcType = JdbcType.VARCHAR)
    })
    @Select("select id,\n" +
            "       scene,\n" +
            "       create_user_id,\n" +
            "       parallelism,\n" +
            "       now_run_state,\n" +
            "       is_choice_gpu,\n" +
            "       algorithm,\n" +
            "       vehicle,\n" +
            "       risk_evaluation_rule_id,\n" +
            "       complexity_evaluation_rule_id,\n" +
            "       '1' project_type,\n" +
            "       max_simulation_time\n" +
            "from simulation_manual_project\n" +
            "where id = #{projectId}")
    ProjectEntity selectById(@Param("projectId") String projectId);

    @Update("update simulation_manual_project\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);


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

    @Update("update simulation_manual_project\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_manual_project t3 on t2.id = t3.vehicle\n" +
            "where t3.id = #{projectId}")
    String selectModelTypeByProjectId(String projectId);

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

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


    String selectAlgorithmTypeById(SimulationManualProjectEntity build);

    String selectAlgorithmById(SimulationManualProjectEntity build);
}
