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


import com.css.simulation.resource.scheduler.infra.entity.LeafIndexEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 任务指标
 * simulation_mpt_first_target_score
 * simulation_mpt_last_target_score
 */
@Mapper
public interface IndexMapper {

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

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



    @Insert("insert into simulation_mpt_last_target_score(id,\n" +
            "                                             p_id,\n" +
            "                                             target,\n" +
            "                                             error_scene_num,\n" +
            "                                             not_scored_scene_num,\n" +
            "                                             not_standard_scene_num,\n" +
            "                                             standard_scene_num,\n" +
            "                                             score,\n" +
            "                                             score_explain,\n" +
            "                                             create_time,\n" +
            "                                             create_user_id,\n" +
            "                                             modify_time,\n" +
            "                                             modify_user_id,\n" +
            "                                             is_deleted)\n" +
            "values (#{leaf.id},\n" +
            "        #{leaf.pId},\n" +
            "        #{leaf.target},\n" +
            "        #{leaf.errorSceneNum},\n" +
            "        #{leaf.notScoredSceneNum},\n" +
            "        #{leaf.notStandardSceneNum},\n" +
            "        #{leaf.standardSceneNum},\n" +
            "        #{leaf.score},\n" +
            "        #{leaf.scoreExplain},\n" +
            "        #{leaf.createTime},\n" +
            "        #{leaf.createUserId},\n" +
            "        #{leaf.modifyTime},\n" +
            "        #{leaf.modifyUserId},\n" +
            "        #{leaf.isDeleted})")
    void insertLeafIndex(@Param("leaf") LeafIndexEntity leafIndexEntity);

    @Insert("insert into simulation_mpt_first_target_score(id,\n" +
            "                                             p_id,\n" +
            "                                             target,\n" +
            "                                             score,\n" +
            "                                             weight,\n" +
            "                                             create_time,\n" +
            "                                             create_user_id,\n" +
            "                                             modify_time,\n" +
            "                                             modify_user_id,\n" +
            "                                             is_deleted)\n" +
            "values (#{first.id},\n" +
            "        #{first.pId},\n" +
            "        #{first.target},\n" +
            "        #{first.score},\n" +
            "        #{first.weight},\n" +
            "        #{first.createTime},\n" +
            "        #{first.createUserId},\n" +
            "        #{first.modifyTime},\n" +
            "        #{first.modifyUserId},\n" +
            "        #{first.isDeleted})")
    void insertFirstIndex(@Param("first") LeafIndexEntity firstTaskIndex);

    @Select("select sublist_id\n" +
            "from scene_package_sublist\n" +
            "where is_deleted = '0'\n" +
            "  and root_id = (select scene from simulation_manual_project smp where id = #{projectId})\n" +
            "  and (scene_natural_ids like #{idExtend}\n" +
            "       or scene_traffic_ids like #{idExtend}\n" +
            "       or scene_statue_ids like #{idExtend}\n" +
            "       or scene_generalization_ids like #{idExtend}\n" +
            "       or scene_reference_lib_ids like #{idExtend}\n" +
            "       )")
    List<String> selectLeafIndexIdByManualProjectIdAndSceneId(@Param("projectId") String projectId, @Param("idExtend") String idExtend);

    @Select("select sublist_id\n" +
            "from scene_package_sublist sps\n" +
            "where is_deleted = '0'\n" +
            "  and root_id = (select sap.scene\n" +
            "                 from simulation_automatic_project sap\n" +
            "                 where id = (select sas.parent_id\n" +
            "                             from simulation_automatic_subproject sas\n" +
            "                             where id = #{projectId}))\n" +
            "  and (scene_natural_ids like #{idExtend}\n" +
            "    or scene_traffic_ids like #{idExtend}\n" +
            "    or scene_statue_ids like #{idExtend}\n" +
            "    or scene_generalization_ids like #{idExtend}\n" +
            "    or scene_reference_lib_ids like #{idExtend}\n" +
            "    )")
    List<String> selectLeafIndexIdByAutoSubProjectIdAndSceneId(@Param("projectId") String projectId, @Param("idExtend") String idExtend);


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

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