<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.css.simulation.resource.server.infra.db.mysql.mapper.SimulationManualProjectTaskMapper">


    <!--查询任务列表-->
    <select id="selectProjectTask" parameterType="api.common.pojo.param.project.ProjectTaskParam"
            resultType="api.common.pojo.po.project.ManualProjectTaskPO">
        select id,
        p_id,
        last_targer_id,
        scene_id,
        scene_name,
        scene_type,
        run_start_time,
        run_end_time,
        run_state,
        run_result,
        run_result_file_path,
        score,
        target_evaluate,
        return_scene_id,
        score_explain,
        mileage,
        average_velocity,
        maximun_speed,
        minimun_velocity,
        maximum_acceleration,
        maximum_deceleration,
        maximum_swingSpeed,
        speed_variance,
        speed_comfort_level,
        swing_speed_mean_square_root,
        swing_comfort_level,
        acceleration,
        lane_offset,
        brake,
        steeting_wheel,
        throttle,
        yaw_rate,
        velocity,
        other_curve,
        timer_shaft,
        create_time,
        create_user_id,
        modify_time,
        modify_user_id,
        is_deleted,
        retry_count
        from simulation_manual_project_task
        <where>
            is_deleted = '0'
            <if test="id != null and id != ''">
                and id = #{id,jdbcType=VARCHAR}
            </if>
            <if test="pId != null and pId != ''">
                and p_id = #{pId,jdbcType=VARCHAR}
            </if>
            <if test="sceneId != null and sceneId != ''">
                and scene_id = #{sceneId,jdbcType=VARCHAR}
            </if>
        </where>
    </select>
    <!--根据项目id查询任务列表-->
    <select id="selectProjectTaskByProjectId" parameterType="api.common.pojo.param.project.ProjectTaskParam"
            resultType="api.common.pojo.vo.project.ManualProjectTaskVo">
        select id,
        p_id,
        last_targer_id,
        scene_id,
        scene_name,
        scene_type,
        run_start_time,
        run_end_time,
        run_state,
        run_result,
        run_result_file_path,
        score,
        target_evaluate,
        return_scene_id,
        score_explain,
        mileage,
        average_velocity,
        maximun_speed,
        minimun_velocity,
        maximum_acceleration,
        maximum_deceleration,
        maximum_swingSpeed,
        speed_variance,
        speed_comfort_level,
        swing_speed_mean_square_root,
        swing_comfort_level,
        acceleration,
        lane_offset,
        brake,
        steeting_wheel,
        throttle,
        yaw_rate,
        velocity,
        other_curve,
        timer_shaft,
        create_time,
        create_user_id,
        modify_time,
        modify_user_id,
        is_deleted,
        retry_count,
        time_actual,
        time_correction
        from simulation_manual_project_task
        where is_deleted = '0' and p_id = #{pId,jdbcType=VARCHAR}
        <if test="taskId != null and taskId != ''">
            and id = #{taskId, jdbcType=VARCHAR}
        </if>
        order by run_start_time desc
    </select>

    <!--根据项目id查询任务列表总数-->
    <select id="selectProjectTaskNumByProjectId" parameterType="api.common.pojo.param.project.ProjectTaskParam"
            resultType="int">
        select count(*) as num
        from simulation_manual_project_task
        where is_deleted = '0'
          and p_id = #{pId,jdbcType=VARCHAR}
    </select>

    <!--根据任务运行状态分组查询统计数量-->
    <select id="selectRunStateCount" parameterType="string"
            resultType="api.common.pojo.vo.project.ProjectRunStateNumVo">
        select run_state as stateName, count(*) as num
        from simulation_manual_project_task
        where p_id = #{id,jdbcType=VARCHAR}
          and is_deleted = '0'
        group by run_state
    </select>

    <!--根据任务运行状态分组查询统计数量-->
    <select id="selectRunResultCount" parameterType="string"
            resultType="api.common.pojo.vo.project.ProjectRunResultRatioNumVo">
        select run_result as resultName, count(*) as num
        from simulation_manual_project_task
        where p_id = #{id,jdbcType=VARCHAR}
          and is_deleted = '0'
          and run_result is not null
          and run_result != ""
        group by run_result
    </select>

    <!--任务得分统计分组-->
    <select id="selectScoreCount" parameterType="string"
            resultType="api.common.pojo.vo.project.ProjectRunResultRatioNumVo">
        select score as resultName, count(*) as num
        from simulation_manual_project_task
        where p_id = #{id,jdbcType=VARCHAR}
          and is_deleted = '0'
        group by score
    </select>

    <!--运行任务状态-->
    <select id="selectRunTaskByState" parameterType="java.util.Map" resultType="java.util.Map">
        select count(1) num, smpt.run_state runState
        from simulation_manual_project_task smpt
        left join simulation_manual_project smp on smpt.p_id = smp.id
        left join simulation_automatic_subproject sas on smpt.p_id = sas.id
        where smpt.is_deleted = '0'
        and (smp.is_deleted = '0' or sas.is_deleted = '0')
        <if test="createUserId != null and createUserId != ''">
            and smpt.create_user_id = #{createUserId}
        </if>
        group by smpt.run_state
    </select>

    <!--查询场景不等于0的得分的数量-->
    <select id="selectSceneScoreNumQuery" parameterType="api.common.pojo.po.project.SimulationMptSceneScorePO"
            resultType="api.common.pojo.vo.project.SceneScoreVo">
        select count(*) as num
        from simulation_manual_project_task
        where p_id = #{pId,jdbcType=VARCHAR}
          and last_targer_id = #{lastTargerId}
          and score != 0 and is_deleted = '0'
    </select>

    <!--批量查询场景得分-->
    <select id="selectSceneScoreByIds" parameterType="api.common.pojo.param.project.SceneScoreParam"
            resultType="api.common.pojo.vo.project.SceneScoreVo">
        select id, scene_id, scene_type,score, target_evaluate, return_scene_id, score_explain
        from simulation_manual_project_task
        where p_id = #{pId,jdbcType=VARCHAR} and scene_type = #{sceneType,jdbcType=VARCHAR}
        and last_targer_id = #{lastTargerId,jdbcType=VARCHAR} and is_deleted = '0'
        and scene_id in
        <foreach collection="sceneIds" open="(" separator="," close=")" item="id">
            #{id,jdbcType=VARCHAR}
        </foreach>
    </select>

    <!--更新仿真结果到数据库-->
    <update id="updateTaksResult" parameterType="api.common.pojo.po.project.ManualProjectTaskPO">
        update simulation_manual_project_task
        set mileage                      = #{mileage, jdbcType=VARCHAR},
            average_velocity             = #{averageVelocity, jdbcType=VARCHAR},
            maximun_speed                = #{maximunSpeed, jdbcType=VARCHAR},
            minimun_velocity             = #{minimunVelocity, jdbcType=VARCHAR},
            maximum_acceleration         = #{maximumAcceleration, jdbcType=VARCHAR},
            maximum_deceleration         = #{maximumDeceleration, jdbcType=VARCHAR},
            maximum_swingSpeed           = #{maximumSwingSpeed, jdbcType=VARCHAR},
            speed_variance               = #{speedVariance, jdbcType=VARCHAR},
            swing_speed_mean_square_root = #{swingSpeedMeanSquareRoot, jdbcType=VARCHAR},
            acceleration                 = #{acceleration, jdbcType=LONGVARCHAR},
            lane_offset                  = #{laneOffset, jdbcType=LONGVARCHAR},
            brake                        = #{brake, jdbcType=LONGVARCHAR},
            steeting_wheel               = #{steetingWheel, jdbcType=LONGVARCHAR},
            throttle                     = #{throttle, jdbcType=LONGVARCHAR},
            yaw_rate                     = #{yawRate, jdbcType=LONGVARCHAR},
            velocity                     = #{velocity, jdbcType=LONGVARCHAR},
            other_curve                  = #{otherCurve, jdbcType=LONGVARCHAR},
            timer_shaft                  = #{timerShaft, jdbcType=LONGVARCHAR}
        where id = #{id, jdbcType=VARCHAR}
          and p_id = #{pId, jdbcType=VARCHAR}
          and is_deleted = '0'

    </update>


    <!--查询Aborted的数量-->
    <select id="selectRunStateByAborted" parameterType="api.common.pojo.po.project.SimulationMptSceneScorePO"
            resultType="api.common.pojo.vo.project.SceneScoreVo">
        select count(*) as num
        from simulation_manual_project_task
        where p_id = #{pId,jdbcType=VARCHAR}
          and run_state = 'Aborted'
          and is_deleted = '0'
    </select>

    <select id="selectProjectTaskById" parameterType="string"
            resultType="api.common.pojo.vo.project.ManualProjectTaskVo">
        select *
        from simulation_manual_project_task
        where id = #{id}
    </select>


</mapper>
