<?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.SceneGeneralTemplateMapper">

    <insert id="saveSceneGeneralTemplateAll" parameterType="java.util.List">
        insert into simulation.scene_general_template
        (id,scene_id,scenario_name,scenario_weather,scenario_time,
        scenario_vehicle_model,scenario_road_type,scenario_radius_curvature,ego_start_x,ego_start_y,
        ego_start_velocity,ego_heading_angle,ego_velocity_status,ego_trajectory,ego_duration_time,
        ego_velocity_time,ego_trajectory_time,obs_start_x,obs_start_y,obs_start_velocity,
        obs_lateral_acceleration,obs_longitudinal_acceleration,
        obs_heading_angle_rel,obs_velocity_status,obs_trajectory,obs_duration_time,obs_velocity_time,
        obs_trail_time,generalization_type,scenario_resume,scene_detailed_description,evaluating_indicator,
        video_link,file_name,version,share,
        create_user_id,create_time,modify_time,
        is_deleted,obs_type) values
        <foreach collection="list" index="index" item="item" separator=",">
            (#{item.id,jdbcType=VARCHAR},#{item.sceneId,jdbcType=VARCHAR},#{item.scenarioName,jdbcType=VARCHAR},#{item.scenarioWeather,jdbcType=VARCHAR},#{item.scenarioTime,jdbcType=VARCHAR},
            #{item.scenarioVehicleModel,jdbcType=VARCHAR}, #{item.scenarioRoadType,jdbcType=VARCHAR}, #{item.scenarioRadiusCurvature,jdbcType=VARCHAR},#{item.egoStartX,jdbcType=VARCHAR}, #{item.egoStartY,jdbcType=VARCHAR},
            #{item.egoStartVelocity,jdbcType=VARCHAR}, #{item.egoHeadingAngle,jdbcType=VARCHAR},#{item.egoVelocityStatus,jdbcType=VARCHAR},#{item.egoTrajectory,jdbcType=VARCHAR},#{item.egoDurationTime,jdbcType=VARCHAR},
            #{item.egoVelocityTime,jdbcType=VARCHAR}, #{item.egoTrajectoryTime,jdbcType=VARCHAR},#{item.obsStartX,jdbcType=VARCHAR},#{item.obsStartY,jdbcType=VARCHAR},#{item.obsStartVelocity,jdbcType=VARCHAR},
            #{item.obsLateralAcceleration,jdbcType=VARCHAR}, #{item.obsLongitudinalAcceleration,jdbcType=VARCHAR},
            #{item.obsHeadingAngleRel,jdbcType=VARCHAR}, #{item.obsVelocityStatus,jdbcType=VARCHAR},#{item.obsTrajectory,jdbcType=VARCHAR},#{item.obsDurationTime,jdbcType=VARCHAR},#{item.obsVelocityTime,jdbcType=VARCHAR},
            #{item.obsTrailTime,jdbcType=VARCHAR}, #{item.generalizationType,jdbcType=VARCHAR},#{item.scenarioResume,jdbcType=VARCHAR},#{item.sceneDetailedDescription},#{item.evaluatingIndicator,jdbcType=VARCHAR},
            #{item.videoLink},#{item.fileName},#{item.version},#{item.share},

            #{item.createUserId,jdbcType=VARCHAR},#{item.createTime},#{item.modifyTime},
            #{item.isDeleted,jdbcType=VARCHAR},#{item.obsType})
        </foreach>
    </insert>

    <insert id="saveSceneGeneralTemplate" parameterType="api.common.pojo.po.scene.SceneGeneralTemplatePO">
        insert into simulation.scene_general_template
        (id,scene_id,scenario_name,scenario_weather,scenario_time,
        scenario_vehicle_model,scenario_road_type,scenario_radius_curvature,ego_start_x,ego_start_y,
        ego_start_velocity,ego_heading_angle,ego_velocity_status,ego_trajectory,ego_duration_time,
        ego_velocity_time,ego_trajectory_time,obs_start_x,obs_start_y,obs_start_velocity,
        obs_lateral_acceleration,obs_longitudinal_acceleration,
        obs_heading_angle_rel,obs_velocity_status,obs_trajectory,obs_duration_time,obs_velocity_time,
        obs_trail_time,generalization_type,scenario_resume,scene_detailed_description,evaluating_indicator,
        video_link,file_name,version,share,
        create_user_id,create_time,modify_time,
        is_deleted,obs_type) values
         (#{id,jdbcType=VARCHAR},#{sceneId,jdbcType=VARCHAR},#{scenarioName,jdbcType=VARCHAR},#{scenarioWeather,jdbcType=VARCHAR},#{scenarioTime,jdbcType=VARCHAR},
            #{scenarioVehicleModel,jdbcType=VARCHAR}, #{scenarioRoadType,jdbcType=VARCHAR}, #{scenarioRadiusCurvature,jdbcType=VARCHAR},#{egoStartX,jdbcType=VARCHAR}, #{egoStartY,jdbcType=VARCHAR},
            #{egoStartVelocity,jdbcType=VARCHAR}, #{egoHeadingAngle,jdbcType=VARCHAR},#{egoVelocityStatus,jdbcType=VARCHAR},#{egoTrajectory,jdbcType=VARCHAR},#{egoDurationTime,jdbcType=VARCHAR},
            #{egoVelocityTime,jdbcType=VARCHAR}, #{egoTrajectoryTime,jdbcType=VARCHAR},#{obsStartX,jdbcType=VARCHAR},#{obsStartY,jdbcType=VARCHAR},#{obsStartVelocity,jdbcType=VARCHAR},
            #{obsLateralAcceleration,jdbcType=VARCHAR}, #{obsLongitudinalAcceleration,jdbcType=VARCHAR},
            #{obsHeadingAngleRel,jdbcType=VARCHAR}, #{obsVelocityStatus,jdbcType=VARCHAR},#{obsTrajectory,jdbcType=VARCHAR},#{obsDurationTime,jdbcType=VARCHAR},#{obsVelocityTime,jdbcType=VARCHAR},
            #{obsTrailTime,jdbcType=VARCHAR}, #{generalizationType,jdbcType=VARCHAR},#{scenarioResume,jdbcType=VARCHAR},#{sceneDetailedDescription},#{evaluatingIndicator,jdbcType=VARCHAR},
            #{videoLink},#{fileName},#{version},#{share},
            #{createUserId,jdbcType=VARCHAR},#{createTime},#{modifyTime},
            #{isDeleted,jdbcType=VARCHAR},#{obsType})
    </insert>
    <select id="querySceneGeneralTemplateList" parameterType="api.common.pojo.param.scene.SceneGeneralTemplateParam"
            resultType="api.common.pojo.po.scene.SceneGeneralTemplatePO">
        select
        id,scene_id,scenario_name,scenario_weather,scenario_time,
        scenario_vehicle_model,scenario_road_type,scenario_radius_curvature,ego_start_x,ego_start_y,
        ego_start_velocity,ego_heading_angle,ego_velocity_status,ego_trajectory,ego_duration_time,
        ego_velocity_time,ego_trajectory_time,obs_start_x,obs_start_y,obs_start_velocity,
        obs_lateral_acceleration,obs_longitudinal_acceleration,
        obs_heading_angle_rel,obs_velocity_status,obs_trajectory,obs_duration_time,obs_velocity_time,
        obs_trail_time,generalization_type,scenario_resume,scene_detailed_description,evaluating_indicator,
        video_link,file_name,version,share,
        create_user_id,create_time,modify_time,obs_type
        from simulation.scene_general_template
        <where>
        is_deleted = '0'
            <if test="id != null and id != ''">
                and id #{id,jdbcType=VARCHAR}
            </if>
            <if test="sceneId != null and sceneId != ''">
                and scene_id like CONCAT('%',#{sceneId,jdbcType=VARCHAR},'%')
            </if>
            <if test="scenarioWeather != null and scenarioWeather.size()>0 ">
                and scenario_weather in
                <foreach collection="scenarioWeather" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="fileName != null and fileName.size()>0 ">
                and file_name in
                <foreach collection="fileName" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="scenarioTime != null and scenarioTime != ''">
                and scenario_time= #{scenarioTime,jdbcType=VARCHAR}
            </if>
            <if test="version != null and version != ''">
                and version= #{version}
            </if>
            <if test="obsType != null and obsType != ''">
                and obs_type= #{obsType}
            </if>
            <if test="share != null and share==0">
                and share =#{share}
            </if>
            <if test="share != null and share!= '' and share!=0">
                and share !='0'
            </if>
            <if test="allSceneNames != null and allSceneNames.length>0">
                and scene_id in
                <foreach collection="allSceneNames" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="scenarioRoadType != null and scenarioRoadType.size()>0 ">
                and scenario_road_type in
                <foreach collection="scenarioRoadType" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="userId != null and userId != ''">
                and create_user_id =#{userId}
            </if>
            <if test="scenarioResume != null and scenarioResume != ''">
                and scenario_resume like CONCAT('%',#{scenarioResume,jdbcType=VARCHAR},'%')
            </if>
        order by modify_time desc
        </where>
    </select>
    <select id="querySceneGeneralTemplateById" parameterType="java.lang.String"
            resultType="api.common.pojo.po.scene.SceneGeneralTemplatePO">
        select
        id,scene_id,scenario_name,scenario_weather,scenario_time,
        scenario_vehicle_model,scenario_road_type,scenario_radius_curvature,ego_start_x,ego_start_y,
        ego_start_velocity,ego_heading_angle,ego_velocity_status,ego_trajectory,ego_duration_time,
        ego_velocity_time,ego_trajectory_time,obs_start_x,obs_start_y,obs_start_velocity,
        obs_lateral_acceleration,obs_longitudinal_acceleration,
        obs_heading_angle_rel,obs_velocity_status,obs_trajectory,obs_duration_time,obs_velocity_time,
        obs_trail_time,generalization_type,scenario_resume,scene_detailed_description,evaluating_indicator,
        video_link,file_name,version,is_deleted,
        create_user_id,create_time,modify_time,obs_type
        from simulation.scene_general_template
        where id = #{id,jdbcType=VARCHAR}

    </select>

    <select id="queryType"  resultType="java.lang.String">

        select
       file_name
        from simulation.scene_general_template
        where is_deleted = '0' group by file_name

    </select>


    <update id="deleteSceneGeneralTemplateAll" parameterType="api.common.pojo.po.scene.SceneGeneralTemplatePO">
        update simulation.scene_general_template
        <set>
            <if test="modifyUserId != null and modifyUserId!=''">
                modify_user_id = #{modifyUserId,jdbcType=VARCHAR},
            </if>
            <if test="modifyTime != null">
                modify_time = #{modifyTime},
            </if>
            is_deleted = '1'
        </set>
        <where>
            is_deleted = '0'
            <if test="fileNameAll != null and fileNameAll.size()>0 ">
                and file_name in
                <foreach collection="fileNameAll" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>

        </where>

    </update>

    <update id="deleteSceneGeneralTemplateByScendId" parameterType="api.common.pojo.po.scene.SceneGeneralTemplatePO">
        update simulation.scene_general_template
        <set>
            <if test="modifyUserId != null and modifyUserId!=''">
                modify_user_id = #{modifyUserId,jdbcType=VARCHAR},
            </if>
            <if test="modifyTime != null">
                modify_time = #{modifyTime},
            </if>
            is_deleted = '1'
        </set>
        where is_deleted = '0'  and scene_id=#{sceneId}
    </update>

    <select id="querySceneGeneralTemplateListByQx" parameterType="api.common.pojo.param.scene.SceneGeneralTemplateParam"
            resultType="api.common.pojo.po.scene.SceneGeneralTemplatePO">
        select
        id,scene_id,scenario_name,scenario_weather,scenario_time,
        scenario_vehicle_model,scenario_road_type,scenario_radius_curvature,ego_start_x,ego_start_y,
        ego_start_velocity,ego_heading_angle,ego_velocity_status,ego_trajectory,ego_duration_time,
        ego_velocity_time,ego_trajectory_time,obs_start_x,obs_start_y,obs_start_velocity,
        obs_lateral_acceleration,obs_longitudinal_acceleration,
        obs_heading_angle_rel,obs_velocity_status,obs_trajectory,obs_duration_time,obs_velocity_time,
        obs_trail_time,generalization_type,scenario_resume,scene_detailed_description,evaluating_indicator,
        video_link,file_name,version,share,
        create_user_id,create_time,modify_time
        from (select fh.* from( select s.scene_name
        FROM system_scene_package p
        JOIN system_user_scene u on p.id = u.package_id and u.is_deleted = '0' and p.is_deleted = '0'
        JOIN system_scene_package_sublist s on p.id = s.scene_and_package
        where scene_type='4' and  s.is_deleted = '0' and user_id=#{userId} group by s.scene_name)qx join scene_general_template  fh on qx.scene_name=fh.scene_id and fh.is_deleted = '0' and fh.share='2' union select jtNew.* from scene_general_template jtNew where jtNew.share='1' and is_deleted='0') dd
        <where>
            is_deleted = '0'
            <if test="id != null and id != ''">
                and id #{id,jdbcType=VARCHAR}
            </if>
            <if test="sceneId != null and sceneId != ''">
                and scene_id like CONCAT('%',#{sceneId,jdbcType=VARCHAR},'%')
            </if>
            <if test="scenarioWeather != null and scenarioWeather.size()>0 ">
                and scenario_weather in
                <foreach collection="scenarioWeather" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="fileName != null and fileName.size()>0 ">
                and file_name in
                <foreach collection="fileName" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="scenarioTime != null and scenarioTime != ''">
                and scenario_time= #{scenarioTime,jdbcType=VARCHAR}
            </if>
            <if test="version != null and version != ''">
                and version= #{version}
            </if>
            <if test="share != null and share==0">
                and share =#{share}
            </if>
            <if test="share != null and share!= '' and share!=0">
                and share !='0'
            </if>
            <if test="allSceneNames != null and allSceneNames.length>0">
                and scene_id in
                <foreach collection="allSceneNames" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="scenarioRoadType != null and scenarioRoadType.size()>0 ">
                and scenario_road_type in
                <foreach collection="scenarioRoadType" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="scenarioResume != null and scenarioResume != ''">
                and scenario_resume like CONCAT('%',#{scenarioResume,jdbcType=VARCHAR},'%')
            </if>
            order by modify_time desc
        </where>
    </select>

    <select id="querySceneGeneralTemplateByName" parameterType="api.common.pojo.po.scene.SceneGeneralTemplatePO" resultType="api.common.pojo.po.scene.SceneGeneralTemplatePO">
        select id,scene_id,scenario_name,scenario_weather,scenario_time,
        scenario_vehicle_model,scenario_road_type,scenario_radius_curvature,ego_start_x,ego_start_y,
        ego_start_velocity,ego_heading_angle,ego_velocity_status,ego_trajectory,ego_duration_time,
        ego_velocity_time,ego_trajectory_time,obs_start_x,obs_start_y,obs_start_velocity,
        obs_lateral_acceleration,obs_longitudinal_acceleration,
        obs_heading_angle_rel,obs_velocity_status,obs_trajectory,obs_duration_time,obs_velocity_time,
        obs_trail_time,generalization_type,scenario_resume,scene_detailed_description,evaluating_indicator,
        video_link,file_name,version,share,
        create_user_id,create_time,modify_time,
        is_deleted,obs_type
        from simulation.scene_general_template
        <where >
            is_deleted = '0'
            <if test="sceneId != null and sceneId!=''">
                and scene_id =#{sceneId}
            </if>
            <if test="share != null and share==0">
                and share =#{share}
            </if>
            <if test="share != null and share!= '' and share!=0">
                and share !='0'
            </if>
        </where>
    </select>
    <update id="updateSceneGeneralTemplate" parameterType="api.common.pojo.po.scene.SceneGeneralTemplatePO">
        update simulation.scene_general_template
        set scene_id=#{sceneId,jdbcType=VARCHAR},
        scenario_name=#{scenarioName,jdbcType=VARCHAR},
        scenario_weather=#{scenarioWeather,jdbcType=VARCHAR},
        scenario_time=#{scenarioTime,jdbcType=VARCHAR},
        scenario_vehicle_model= #{scenarioVehicleModel,jdbcType=VARCHAR},
        scenario_road_type=#{scenarioRoadType,jdbcType=VARCHAR},
        scenario_radius_curvature= #{scenarioRadiusCurvature,jdbcType=VARCHAR},
        ego_start_x= #{egoStartX,jdbcType=VARCHAR},
        ego_start_y= #{egoStartY,jdbcType=VARCHAR},
        ego_start_velocity= #{egoStartVelocity,jdbcType=VARCHAR},
        ego_heading_angle=#{egoHeadingAngle,jdbcType=VARCHAR},
        ego_velocity_status=#{egoVelocityStatus,jdbcType=VARCHAR},
        ego_trajectory= #{egoTrajectory,jdbcType=VARCHAR},
        ego_duration_time= #{egoDurationTime,jdbcType=VARCHAR},
        ego_velocity_time=#{egoVelocityTime,jdbcType=VARCHAR},
        ego_trajectory_time=#{egoTrajectoryTime,jdbcType=VARCHAR},
        obs_start_x=#{obsStartX,jdbcType=VARCHAR},
        obs_start_y=#{obsStartY,jdbcType=VARCHAR},
        obs_start_velocity=#{obsStartVelocity,jdbcType=VARCHAR},
        obs_lateral_acceleration=#{obsLateralAcceleration,jdbcType=VARCHAR},
        obs_longitudinal_acceleration=#{obsLongitudinalAcceleration,jdbcType=VARCHAR},
        obs_heading_angle_rel=#{obsHeadingAngleRel,jdbcType=VARCHAR},
        obs_velocity_status=#{obsVelocityStatus,jdbcType=VARCHAR},
        obs_trajectory=#{obsTrajectory,jdbcType=VARCHAR},
        obs_duration_time=#{obsDurationTime,jdbcType=VARCHAR},
        obs_velocity_time=#{obsVelocityTime,jdbcType=VARCHAR},
        obs_trail_time=#{obsTrailTime,jdbcType=VARCHAR},
        generalization_type= #{generalizationType,jdbcType=VARCHAR},
        scenario_resume=#{scenarioResume,jdbcType=VARCHAR},
        scene_detailed_description=#{sceneDetailedDescription},
        evaluating_indicator=#{evaluatingIndicator,jdbcType=VARCHAR},
        video_link=#{videoLink},
        file_name=#{fileName},
        version= #{version},
        share=#{share},
        create_user_id=#{createUserId,jdbcType=VARCHAR},
        create_time=#{createTime},
        modify_time=#{modifyTime},
        is_deleted=#{isDeleted,jdbcType=VARCHAR},
        obs_type=#{obsType}
        where id = #{id,jdbcType=VARCHAR}
    </update>

    <select id="queryIdBySceneId" resultType="java.lang.String">
        select id from simulation.scene_general_template where scene_id=#{sceneId} and is_deleted='0'
    </select>
</mapper>
