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

    <insert id="saveSceneAccident" parameterType="api.common.pojo.po.scene.SceneAccidentPO">
        insert into simulation.scene_accident
        (accident_id, scene_name, self_driving, target_driving,
         self_reaction, conflict_behavior, conflict_type,
         scence_resume, scene_time, scene_weather,
         create_user_id, create_time, modify_user_id, modify_time,
         is_deleted, share, video_preview, video_address, osgb_address, xml_address, xodr_address,
         json_address, max_time)
        values (#{accidentId,jdbcType=VARCHAR}, #{sceneName,jdbcType=VARCHAR}, #{selfDriving,jdbcType=VARCHAR},
                #{targetDriving,jdbcType=VARCHAR},
                #{selfReaction,jdbcType=VARCHAR}, #{conflictBehavior,jdbcType=VARCHAR},
                #{conflictType,jdbcType=VARCHAR},
                #{scenceResume,jdbcType=VARCHAR}, #{sceneTime,jdbcType=VARCHAR}, #{sceneWeather,jdbcType=VARCHAR},
                #{createUserId,jdbcType=VARCHAR}, #{createTime}, #{modifyUserId,jdbcType=VARCHAR}, #{modifyTime},
                #{isDeleted,jdbcType=VARCHAR}, #{share,jdbcType=VARCHAR}, #{videoPreview,jdbcType=VARCHAR},
                #{videoAddress,jdbcType=VARCHAR}, #{osgbAddress,jdbcType=VARCHAR},
                #{xmlAddress,jdbcType=VARCHAR}, #{xodrAddress,jdbcType=VARCHAR}, #{jsonAddress,jdbcType=VARCHAR},
                #{maxTime,jdbcType=VARCHAR})
    </insert>
    <insert id="saveSceneAccidentList" parameterType="java.util.List">
        insert into simulation.scene_accident
        (accident_id,scene_name,self_driving,target_driving,
        self_reaction,conflict_behavior,conflict_type,
        scence_resume,scene_time,scene_weather,
        create_user_id,create_time,modify_user_id,modify_time,
        is_deleted,share,video_preview,video_address,osgb_address,xml_address,xodr_address,
        json_address,max_time)
        values
        <foreach collection="list" index="index" item="item" separator=",">
            (#{item.accidentId,jdbcType=VARCHAR},#{item.sceneName,jdbcType=VARCHAR},#{item.selfDriving,jdbcType=VARCHAR},
            #{item.targetDriving,jdbcType=VARCHAR},
            #{item.selfReaction,jdbcType=VARCHAR},
            #{item.conflictBehavior,jdbcType=VARCHAR},#{item.conflictType,jdbcType=VARCHAR},
            #{item.scenceResume,jdbcType=VARCHAR},
            #{item.sceneTime,jdbcType=VARCHAR},#{item.sceneWeather,jdbcType=VARCHAR},
            #{item.createUserId,jdbcType=VARCHAR},#{item.createTime},#{item.modifyUserId,jdbcType=VARCHAR},#{item.modifyTime},
            #{item.isDeleted,jdbcType=VARCHAR},
            #{item.share,jdbcType=VARCHAR},#{item.videoPreview,jdbcType=VARCHAR},#{item.videoAddress,jdbcType=VARCHAR},#{item.osgbAddress,jdbcType=VARCHAR},
            #{item.xmlAddress,jdbcType=VARCHAR},
            #{item.xodrAddress,jdbcType=VARCHAR},#{item.jsonAddress,jdbcType=VARCHAR},#{item.maxTime,jdbcType=VARCHAR})
        </foreach>
    </insert>
    <update id="deleteBySceneNames">
        update scene_accident
        set is_deleted = '1',
        modify_user_id = #{modifyUserId},
        modify_time = #{modifyTime}
        where is_deleted = '0' and scene_name in
        <foreach item="item" collection="sceneNames" separator="," open="(" close=")">
            #{item}
        </foreach>
    </update>


    <select id="querySceneAccidentList" parameterType="api.common.pojo.param.scene.SceneAccidentParam"
            resultType="api.common.pojo.po.scene.SceneAccidentPO">
        select
        accident_id,scene_name,self_driving,target_driving,
        self_reaction,conflict_behavior,conflict_type,
        scence_resume,scene_time,scene_weather,
        create_user_id,create_time,modify_user_id,modify_time,
        is_deleted,share,video_address,video_preview,osgb_address,xml_address,xodr_address,
        json_address,max_time
        from simulation.scene_accident
        <where>
            is_deleted = '0'
            <if test="userId != null and userId != ''">
                and create_user_id =#{userId}
            </if>
            <if test="sceneName != null and sceneName != ''">
                and scene_name like CONCAT('%',#{sceneName,jdbcType=VARCHAR},'%')
            </if>
            <if test="share != null and share==0">
                and share =#{share}
            </if>
            <if test="share != null and share==1">
                and share !='0'
            </if>
            <if test="selfDriving != null and selfDriving.size()>0 ">
                and self_driving in
                <foreach collection="selfDriving" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="targetDriving != null and targetDriving.size()>0">
                and target_driving in
                <foreach collection="targetDriving" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="selfReaction != null and selfReaction.size()>0">
                and self_reaction in
                <foreach collection="selfReaction" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="conflictBehavior != null and conflictBehavior.size()>0">
                and conflict_behavior in
                <foreach collection="conflictBehavior" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="conflictType != null and conflictType.size()>0">
                and conflict_type in
                <foreach collection="conflictType" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="ids != null and ids.length>0">
                and accident_id in
                <foreach collection="ids" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="allSceneNames != null and allSceneNames.length>0">
                and scene_name in
                <foreach collection="allSceneNames" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        order by create_time, scene_name desc
    </select>
    <select id="querySceneAccidentListByBq" parameterType="api.common.pojo.param.scene.SceneAccidentParam"
            resultType="api.common.pojo.po.scene.SceneAccidentPO">
        select
        accident_id,scene_name,self_driving,target_driving,
        self_reaction,conflict_behavior,conflict_type,
        scence_resume,scene_time,scene_weather,
        create_user_id,create_time,modify_user_id,modify_time,
        is_deleted,share,video_address,video_preview,osgb_address,xml_address,xodr_address,
        json_address,max_time
        from( select
        accident_id,scene_name,self_driving,target_driving,
        self_reaction,conflict_behavior,conflict_type,
        scence_resume,scene_time,scene_weather,
        create_user_id,create_time,modify_user_id,modify_time,
        is_deleted,share,video_address,video_preview,osgb_address,xml_address,xodr_address,
        json_address,max_time
        from simulation.scene_accident
        <where>
            <if test="rearEndCollision != null and rearEndCollision!=''">
                conflict_behavior =#{rearEndCollision}
            </if>
            <if test="cutIn != null and cutIn!=''">
                or target_driving =#{cutIn}
            </if>
            <if test="driveCurve != null and driveCurve!=''">
                or target_driving =#{driveCurve}
            </if>
            <if test="carConflict != null and carConflict!=''">
                or conflict_type =#{carConflict}
            </if>
        </where>
        ) a where a.is_deleted = '0'
        <if test="AllIds != null and AllIds.size()>0">
            and a.accident_id in
            <foreach collection="AllIds" item="item" index="index"
                     separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        order by a.modify_time desc
    </select>

    <select id="querySceneAccidentListByXlk" parameterType="api.common.pojo.param.scene.SceneAccidentParam"
            resultType="api.common.pojo.po.scene.SceneAccidentPO">
        select
        accident_id,scene_name,self_driving,target_driving,
        self_reaction,conflict_behavior,conflict_type,
        scence_resume,scene_time,scene_weather,
        create_user_id,create_time,modify_user_id,modify_time,
        is_deleted,share,video_address,video_preview,osgb_address,xml_address,xodr_address,
        json_address,max_time
        from simulation.scene_accident
        <where>
            is_deleted = '0'
            <if test="sceneName != null and sceneName != ''">
                and scene_name like CONCAT('%',#{sceneName,jdbcType=VARCHAR},'%')
            </if>
            <if test="selfDriving != null and selfDriving.size()>0 ">
                and self_driving in
                <foreach collection="selfDriving" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="targetDriving != null and targetDriving.size()>0">
                and target_driving in
                <foreach collection="targetDriving" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="selfReaction != null and selfReaction.size()>0">
                and self_reaction in
                <foreach collection="selfReaction" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="conflictBehavior != null and conflictBehavior.size()>0">
                and conflict_behavior in
                <foreach collection="conflictBehavior" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="conflictType != null and conflictType.size()>0">
                and conflict_type in
                <foreach collection="conflictType" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="ids != null and ids.length>0">
                and accident_id in
                <foreach collection="ids" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="allSceneNames != null and allSceneNames.length>0">
                and scene_name in
                <foreach collection="allSceneNames" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="AllIds != null and AllIds.size()>0">
                and accident_id in
                <foreach collection="AllIds" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="share != null and share != ''">
                and share =#{share}
            </if>
        </where>

        order by modify_time desc
    </select>

    <update id="deleteSceneAccidentList" parameterType="api.common.pojo.po.scene.SceneAccidentPO">
        update simulation.scene_accident
        <set>
            <if test="isDeleted != null and isDeleted!=''">
                is_deleted = #{isDeleted,jdbcType=VARCHAR},
            </if>
            <if test="modifyUserId != null and modifyUserId!=''">
                modify_user_id = #{modifyUserId,jdbcType=VARCHAR},
            </if>
            <if test="modifyTime != null">
                modify_time = #{modifyTime},
            </if>
        </set>
        where accident_id = #{accidentId,jdbcType=VARCHAR}
    </update>


    <select id="querySceneAccidentByNmae" parameterType="api.common.pojo.po.scene.SceneAccidentPO"
            resultType="api.common.pojo.po.scene.SceneAccidentPO">
        select
        accident_id,scene_name,self_driving,target_driving,
        self_reaction,conflict_behavior,conflict_type,
        scence_resume,scene_time,scene_weather,
        create_user_id,create_time,modify_user_id,modify_time,
        is_deleted,share,video_preview,video_address,osgb_address,xml_address,xodr_address,
        json_address,max_time
        from simulation.scene_accident
        where
        is_deleted = '0'
        <if test="sceneName != null and sceneName != ''">
            and scene_name =#{sceneName,jdbcType=VARCHAR}
        </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>
    </select>
    <select id="selectAllByName" parameterType="api.common.pojo.po.scene.SceneAccidentPO"
            resultType="api.common.pojo.po.scene.SceneAccidentPO">
        select accident_id,
               scene_name,
               self_driving,
               target_driving,
               self_reaction,
               conflict_behavior,
               conflict_type,
               scence_resume,
               scene_time,
               scene_weather,
               create_user_id,
               create_time,
               modify_user_id,
               modify_time,
               is_deleted,
               share,
               video_preview,
               video_address,
               osgb_address,
               xml_address,
               xodr_address,
               json_address,
               max_time
        from simulation.scene_accident
        where is_deleted = '0'
          and scene_name = #{sceneName,jdbcType=VARCHAR}
    </select>


    <update id="updateSceneAccident" parameterType="api.common.pojo.po.scene.SceneAccidentPO">
        update simulation.scene_accident
        set scene_name=#{sceneName},
            self_driving=#{selfDriving},
            target_driving=#{targetDriving},
            self_reaction=#{selfReaction},
            conflict_behavior=#{conflictBehavior},
            conflict_type=#{conflictType},
            scence_resume=#{scenceResume},
            scene_time=#{sceneTime},
            scene_weather=#{sceneWeather},
            modify_time=#{modifyTime},
            modify_user_id=#{modifyUserId},
            video_address=#{videoAddress},
            osgb_address=#{osgbAddress},
            xml_address=#{xmlAddress},
            xodr_address=#{xodrAddress},
            json_address=#{jsonAddress},
            max_time=#{maxTime},
            share=#{share}
        where accident_id = #{accidentId,jdbcType=VARCHAR}
    </update>

    <update id="updateSceneAccidentList" parameterType="java.util.List">
        <foreach collection="list" index="index" item="item" separator=";">
            update simulation.scene_accident
            set scene_name=#{item.sceneName},self_driving=#{item.selfDriving},
            target_driving=#{item.targetDriving},self_reaction=#{item.selfReaction},
            conflict_behavior=#{item.conflictBehavior},conflict_type=#{item.conflictType},
            scence_resume=#{item.scenceResume},scene_time=#{item.sceneTime},
            scene_weather=#{item.sceneWeather},modify_time=#{item.modifyTime},
            modify_user_id=#{item.modifyUserId},video_address=#{item.videoAddress},
            osgb_address=#{item.osgbAddress},xml_address=#{item.xmlAddress},
            xodr_address=#{item.xodrAddress},json_address=#{item.jsonAddress},max_time=#{item.maxTime},share=#{share}
            where accident_id = #{item.accidentId,jdbcType=VARCHAR}
        </foreach>
    </update>


    <select id="querySceneAccidentListByQx" parameterType="api.common.pojo.param.scene.SceneAccidentParam"
            resultType="api.common.pojo.po.scene.SceneAccidentPO">

        select
        accident_id,scene_name,self_driving,target_driving,
        self_reaction,conflict_behavior,conflict_type,
        scence_resume,scene_time,scene_weather,
        create_user_id,create_time,modify_user_id,modify_time,
        is_deleted,share,video_address,video_preview,osgb_address,xml_address,xodr_address,
        json_address,max_time
        from ( select jt.* 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='3' and s.is_deleted = '0' and user_id=#{userId} group by s.scene_name)qx join scene_accident
        jt on qx.scene_name=jt.scene_name and jt.share='2' union select jtNew.* from scene_accident jtNew where
        jtNew.share='1' and is_deleted='0') cc
        <where>
            is_deleted = '0'
            <if test="sceneName != null and sceneName != ''">
                and scene_name like CONCAT('%',#{sceneName,jdbcType=VARCHAR},'%')
            </if>
            <if test="selfDriving != null and selfDriving.size()>0 ">
                and self_driving in
                <foreach collection="selfDriving" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="targetDriving != null and targetDriving.size()>0">
                and target_driving in
                <foreach collection="targetDriving" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="selfReaction != null and selfReaction.size()>0">
                and self_reaction in
                <foreach collection="selfReaction" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="conflictBehavior != null and conflictBehavior.size()>0">
                and conflict_behavior in
                <foreach collection="conflictBehavior" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="conflictType != null and conflictType.size()>0">
                and conflict_type in
                <foreach collection="conflictType" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="ids != null and ids.length>0">
                and accident_id in
                <foreach collection="ids" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="allSceneNames != null and allSceneNames.length>0">
                and scene_name in
                <foreach collection="allSceneNames" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="AllIds != null and AllIds.size()>0">
                and a.accident_id in
                <foreach collection="AllIds" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        order by cc.create_time,cc.scene_name desc
    </select>

    <select id="querySceneAccidentListByBqAndQx" parameterType="api.common.pojo.param.scene.SceneAccidentParam"
            resultType="api.common.pojo.po.scene.SceneAccidentPO">
        select
        accident_id,scene_name,self_driving,target_driving,
        self_reaction,conflict_behavior,conflict_type,
        scence_resume,scene_time,scene_weather,
        create_user_id,create_time,modify_user_id,modify_time,
        is_deleted,share,video_address,video_preview,osgb_address,xml_address,xodr_address,
        json_address,max_time
        from( select
        accident_id,scene_name,self_driving,target_driving,
        self_reaction,conflict_behavior,conflict_type,
        scence_resume,scene_time,scene_weather,
        create_user_id,create_time,modify_user_id,modify_time,
        is_deleted,share,video_address,video_preview,osgb_address,xml_address,xodr_address,
        json_address,max_time
        from ( select jt.* 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='3' and s.is_deleted = '0' and user_id=#{userId} group by s.scene_name)qx join scene_accident
        jt on qx.scene_name=jt.scene_name) dd
        <where>
            <if test="rearEndCollision != null and rearEndCollision!=''">
                conflict_behavior =#{rearEndCollision}
            </if>
            <if test="cutIn != null and cutIn!=''">
                or target_driving =#{cutIn}
            </if>
            <if test="driveCurve != null and driveCurve!=''">
                or target_driving =#{driveCurve}
            </if>
            <if test="carConflict != null and carConflict!=''">
                or conflict_type =#{carConflict}
            </if>
        </where>
        ) a where a.is_deleted = '0'
        <if test="AllIds != null and AllIds.size()>0">
            and a.accident_id in
            <foreach collection="AllIds" item="item" index="index"
                     separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        order by a.modify_time desc
    </select>


    <select id="querySceneAccidentById" parameterType="java.lang.String"
            resultType="api.common.pojo.po.scene.SceneAccidentPO">
        select accident_id,
               scene_name,
               self_driving,
               target_driving,
               self_reaction,
               conflict_behavior,
               conflict_type,
               scence_resume,
               scene_time,
               scene_weather,
               create_user_id,
               create_time,
               modify_user_id,
               modify_time,
               is_deleted,
               share,
               video_preview,
               video_address,
               osgb_address,
               xml_address,
               xodr_address,
               json_address,
               max_time
        from simulation.scene_accident
        where accident_id = #{id}
    </select>
    <select id="selectIdsByNames" resultType="java.lang.String">
        select accident_id
        from scene_accident
        where is_deleted = '0'
        and scene_name in
        <foreach item="item" collection="list" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>


</mapper>
