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

import api.common.pojo.param.project.ProjectTaskParam;
import api.common.pojo.param.project.SceneScoreParam;
import api.common.pojo.param.project.SimulationManualProjectParam;
import api.common.pojo.po.project.ManualProjectTaskPO;
import api.common.pojo.po.project.SimulationMptSceneScorePO;
import api.common.pojo.vo.project.ManualProjectTaskVo;
import api.common.pojo.vo.project.ProjectRunResultRatioNumVo;
import api.common.pojo.vo.project.ProjectRunStateNumVo;
import api.common.pojo.vo.project.SceneScoreVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Mapper
@Repository
public interface SimulationManualProjectTaskMapper {

    List<ManualProjectTaskPO> selectProjectTask(ProjectTaskParam vo);

    List<ManualProjectTaskVo> selectProjectTaskByProjectId(ProjectTaskParam vo);

    Integer selectProjectTaskNumByProjectId(ProjectTaskParam vo);

    List<ProjectRunStateNumVo> selectRunStateCount(String id);

    List<ProjectRunResultRatioNumVo> selectRunResultCount(String id);

    List<ProjectRunResultRatioNumVo> selectScoreCount(String id);

    List<SceneScoreVo> selectSceneScoreByIds(SceneScoreParam param);

    SceneScoreVo selectSceneScoreNumQuery(SimulationMptSceneScorePO po);

    List<Map<String, Object>> selectRunTaskByState(Map map);

    //更新仿真结果到数据库
    int updateTaksResult(ManualProjectTaskPO po);

    SceneScoreVo selectRunStateByAborted(SimulationMptSceneScorePO po);


    List<ManualProjectTaskVo> selectProjectTaskById(String id);

    @Select("select sum(number)\n" +
            "from (select count(*) number\n" +
            "      from simulation_manual_project_task smpt\n" +
            "               left join simulation_manual_project smp on smpt.p_id = smp.id\n" +
            "               left join model_config mc on smp.vehicle = mc.id\n" +
            "               left join model_vehicle mv on mc.vehicle_id = mv.id\n" +
            "      where smpt.is_deleted = '0'\n" +
            "        and smp.is_deleted = '0'\n" +
            "        and mc.is_deleted = '0'\n" +
            "        and mv.is_deleted = '0'\n" +
            "        and smpt.run_state = 'Running'\n" +
            "        and mv.parameter_type = '2'\n" +
            "      union all\n" +
            "      select count(*) number\n" +
            "      from simulation_manual_project_task smpt\n" +
            "               left join simulation_automatic_subproject sas on smpt.p_id = sas.id\n" +
            "               left join simulation_automatic_project sap on sas.parent_id = sap.id\n" +
            "               left join model_config mc on sap.vehicle = mc.id\n" +
            "               left join model_vehicle mv on mc.vehicle_id = mv.id\n" +
            "      where smpt.is_deleted = '0'\n" +
            "        and sas.is_deleted = '0'\n" +
            "        and sap.is_deleted = '0'\n" +
            "        and mc.is_deleted = '0'\n" +
            "        and mv.is_deleted = '0'\n" +
            "        and smpt.run_state = 'Running'\n" +
            "        and mv.parameter_type = '2') temp\n" +
            ";")
    int selectTaskNumberOfCarsim();

    @Select("select run_result_file_path\n" +
            "from simulation_manual_project_task\n" +
            "where is_deleted = '0'\n" +
            "  and p_id = #{id}\n")
    List<String> selectRunResultPathByProjectId(SimulationManualProjectParam simulationManualProjectParam);

    @Select("select run_result_file_path\n" +
            "from simulation_manual_project_task\n" +
            "where p_id = #{id}\n" +
            "  and scene_id = (select CASE\n" +
            "                             WHEN scene_natural_ids IS NOT NULL AND scene_natural_ids <> ''\n" +
            "                                 THEN scene_natural_ids\n" +
            "                             WHEN scene_traffic_ids IS NOT NULL AND scene_traffic_ids <> ''\n" +
            "                                 THEN scene_traffic_ids\n" +
            "                             WHEN scene_statue_ids IS NOT NULL AND scene_statue_ids <> ''\n" +
            "                                 THEN scene_statue_ids\n" +
            "                             WHEN scene_generalization_ids IS NOT NULL AND scene_generalization_ids <> ''\n" +
            "                                 THEN scene_generalization_ids\n" +
            "                             END AS scene_id\n" +
            "                  from scene_package_sublist t2\n" +
            "                  where t2.is_deleted = 0\n" +
            "                    and root_id =\n" +
            "                        (select scene\n" +
            "                         from simulation_manual_project t1\n" +
            "                         where id = #{id}\n" +
            "                           and t1.is_deleted = 0)\n" +
            "                    and (sublist_name like '%B%' or sublist_name like '%A%'))")
    List<String> selectRunResultPathByProjectIdV202403(SimulationManualProjectParam simulationManualProjectParam);


}
