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


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

import java.sql.Timestamp;
import java.util.List;

@Mapper
public interface SimulationManualProjectTaskMapper {

    @Results(id = "task", value = {
            @Result(column = "id", property = "id", jdbcType = JdbcType.VARCHAR),
            @Result(column = "p_id", property = "pId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "scene_id", property = "sceneId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "scene_name", property = "sceneName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "scene_type", property = "sceneType", jdbcType = JdbcType.VARCHAR),
            @Result(column = "run_state", property = "runState", jdbcType = JdbcType.VARCHAR),
            @Result(column = "run_result_file_path", property = "runResultFilePath", jdbcType = JdbcType.VARCHAR),
            @Result(column = "max_simulation_time", property = "maxSimulationTime", jdbcType = JdbcType.VARCHAR),
            @Result(column = "last_targer_id", property = "lastTargetId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_user_id", property = "createUserId", jdbcType = JdbcType.VARCHAR)
    })
    @Select("select id,\n" +
            "       p_id,\n" +
            "       scene_id,\n" +
            "       scene_name,\n" +
            "       scene_type,\n" +
            "       run_state,\n" +
            "       run_result_file_path,\n" +
            "       last_targer_id\n" +
            "from simulation_manual_project_task\n" +
            "where p_id = #{projectId}")
    List<TaskEntity> selectTaskListByProjectId(@Param("projectId") String projectId);

    @ResultMap("task")
    @Select("select p_id, create_user_id, scene_id\n" +
            "from simulation_manual_project_task\n" +
            "where id = #{taskId}")
    TaskEntity selectById(@Param("taskId")String taskId);


    @Update("update simulation_manual_project_task\n" +
            "set run_state = #{runState},run_start_time = #{runStartTime}\n" +
            "where id = #{id}")
    void updateStateWithStartTime(@Param("id") String id, @Param("runState") String runState, @Param("runStartTime") Timestamp runStartTime);

    @Update("update simulation_manual_project_task\n" +
            "set run_state = #{runState},run_end_time = #{runStopTime},run_result='Success'\n" +
            "where id = #{id}")
    void updateSuccessStateWithStopTime(@Param("id") String id, @Param("runState") String runState, @Param("runStopTime") Timestamp runStopTime);

    @Update("update simulation_manual_project_task\n" +
            "set run_state = #{runState}\n," +
            "   run_end_time = #{runStopTime},\n" +
            "   run_result = 'Failed',\n" +
            "   score = 0,\n" +
            "   target_evaluate = #{targetEvaluate}\n" +
            "where id = #{id}" +
            "   and score is null")
    void updateFailStateWithStopTime(@Param("id") String id,
                                     @Param("runState") String runState,
                                     @Param("runStopTime") Timestamp runStopTime,
                                     @Param("targetEvaluate") String targetEvaluate
                                     );

    @Insert("insert into simulation_manual_project_task(id, p_id, scene_id, scene_name, scene_type, last_targer_id,\n" +
            "                                           run_state, run_result_file_path, create_time, create_user_id, modify_time,\n" +
            "                                           modify_user_id, is_deleted)\n" +
            "values (#{task.id},#{task.pId},#{task.sceneId},#{task.sceneName},#{task.sceneType},#{task.lastTargetId},#{task.runState},\n" +
            "#{task.runResultFilePath},#{task.createTime},#{task.createUserId},#{task.modifyTime},#{task.modifyUserId},#{task.isDeleted})")
    void insert(@Param("task") TaskEntity task);

    @Select("select run_state\n" +
            "from simulation_manual_project_task\n" +
            "where id = #{id}")
    String selectStateById(@Param("id") String id);


    void updateSuccessState(@Param("task") TaskEntity task, @Param("runState") String runState);

    @Delete("delete from simulation_manual_project_task where p_id = #{projectId}")
    void deleteByProject(@Param("projectId") String projectId);
}
