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


    <insert id="saveSceneReference" parameterType="api.common.pojo.po.scene.SceneReferenceLibPO">
        insert into simulation.scene_reference_lib
        (scene_id, scene_name, main_behavior, other_behavior, weather,road_type,road_geometry_plane,road_geometry_vertical,
         auto_drive_function, operation_area,scene_description,label,
         create_user_id, create_time, modify_user_id, modify_time,
         is_deleted, video_preview, video_address, osgb_address, xml_address, xodr_address,
         json_address, max_time)
        values (#{sceneId,jdbcType=VARCHAR},#{sceneName,jdbcType=VARCHAR}, #{mainBehavior,jdbcType=VARCHAR}, #{otherBehavior,jdbcType=VARCHAR},
                #{weather,jdbcType=VARCHAR}, #{roadType,jdbcType=VARCHAR}, #{roadGeometryPlane,jdbcType=VARCHAR}, #{roadGeometryVertical,jdbcType=VARCHAR},
                #{autoDriveFunction,jdbcType=VARCHAR}, #{operationArea,jdbcType=VARCHAR}, #{sceneDescription,jdbcType=VARCHAR}, #{label,jdbcType=VARCHAR},
                #{createUserId,jdbcType=VARCHAR}, #{createTime}, #{modifyUserId,jdbcType=VARCHAR}, #{modifyTime},
                #{isDeleted,jdbcType=VARCHAR}, #{videoPreview,jdbcType=VARCHAR},
                #{videoAddress,jdbcType=VARCHAR}, #{osgbAddress,jdbcType=VARCHAR},
                #{xmlAddress,jdbcType=VARCHAR}, #{xodrAddress,jdbcType=VARCHAR}, #{jsonAddress,jdbcType=VARCHAR},
                #{maxTime,jdbcType=VARCHAR})
    </insert>

    <select id="selectAllByName" parameterType="api.common.pojo.po.scene.SceneReferenceLibPO"
            resultType="api.common.pojo.po.scene.SceneReferenceLibPO">
        select scene_id,
               scene_name,
               main_behavior,
               other_behavior,
               create_user_id,
               create_time,
               modify_user_id,
               modify_time,
               is_deleted,
               video_preview,
               video_address,
               osgb_address,
               xml_address,
               xodr_address,
               json_address,
               max_time
        from simulation.scene_reference_lib
        where is_deleted = '0'
          and scene_name = #{sceneName,jdbcType=VARCHAR}
    </select>

    <update id="updateSceneReference" parameterType="api.common.pojo.po.scene.SceneReferenceLibPO">
        update simulation.scene_reference_lib
        set scene_name=#{sceneName},
            main_behavior=#{mainBehavior},
            other_behavior=#{otherBehavior},
            weather=#{weather},
            road_type=#{roadType},
            road_geometry_plane=#{roadGeometryPlane},
            road_geometry_vertical=#{roadGeometryVertical},
            auto_drive_function=#{autoDriveFunction},
            operation_area=#{operationArea},
            scene_description=#{sceneDescription},
            label=#{label},
            modify_user_id=#{modifyUserId},
            modify_time=#{modifyTime},
            video_address=#{videoAddress},
            osgb_address=#{osgbAddress},
            xml_address=#{xmlAddress},
            xodr_address=#{xodrAddress},
            json_address=#{jsonAddress},
            video_preview=#{videoPreview},
            max_time=#{maxTime}
        where scene_id = #{sceneId,jdbcType=VARCHAR}
    </update>

    <select id="querySceneReferenceLibList" parameterType="api.common.pojo.param.scene.SceneReferenceLibParam"
            resultType="api.common.pojo.po.scene.SceneReferenceLibPO">
        SELECT
           scene_id,
           scene_name,
           main_behavior,
           other_behavior,
           weather,
           road_type,
           road_geometry_plane,
           road_geometry_vertical,
           auto_drive_function,
           operation_area,
           scene_description,
           label,
           create_time,
           modify_user_id,
           modify_time,
           video_preview,
           video_address,
           osgb_address,
           xml_address,
           xodr_address,
           json_address,
           max_time
        FROM simulation.scene_reference_lib
        <where>
            is_deleted = '0'
            <if test="sceneName != null and sceneName != ''">
                AND scene_name LIKE CONCAT('%',#{sceneName,jdbcType=VARCHAR},'%')
            </if>
            <if test="sceneDescription != null and sceneDescription != ''">
                AND scene_description LIKE CONCAT('%',#{sceneDescription,jdbcType=VARCHAR},'%')
            </if>
            <if test="label != null and label != ''">
                AND label LIKE CONCAT('%',#{label,jdbcType=VARCHAR},'%')
            </if>
            <if test="mainBehavior != null and mainBehavior.size()>0">
                AND
                <foreach collection="mainBehavior" item="item" index="index"
                         separator=" or " open="(" close=")">
                    main_behavior LIKE CONCAT('%,',#{item},',%')
                    OR main_behavior LIKE CONCAT('%,',#{item}) OR
                    main_behavior LIKE CONCAT(#{item},',%') OR
                    main_behavior=#{item}
                </foreach>

            </if>
            <if test="otherBehavior != null and otherBehavior.size()>0">
                and
                <foreach collection="otherBehavior" item="item" index="index"
                         separator=" or " open="(" close=")">
                    other_behavior LIKE CONCAT('%,',#{item},',%')
                    OR other_behavior LIKE CONCAT('%,',#{item}) OR
                    other_behavior LIKE CONCAT(#{item},',%') OR
                    other_behavior=#{item}
                </foreach>
            </if>
            <if test="weather != null and weather.size()>0 ">
                AND weather IN
                <foreach collection="weather" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="roadType != null and roadType.size()>0">
                AND road_type IN
                <foreach collection="roadType" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="roadGeometryPlane != null and roadGeometryPlane.size()>0">
                AND road_geometry_plane IN
                <foreach collection="roadGeometryPlane" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="roadGeometryVertical != null and roadGeometryVertical.size()>0">
                AND road_geometry_vertical IN
                <foreach collection="roadGeometryVertical" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="autoDriveFunction != null and autoDriveFunction.size()>0">
                AND auto_drive_function IN
                <foreach collection="autoDriveFunction" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="operationArea != null and operationArea.size()>0">
                AND operation_area IN
                <foreach collection="operationArea" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="needComplexity != null and needComplexity != ''">
                and scene_id in (select scene_id from scene_complexity where is_deleted = '0'
                <if test="minComplexity != null and minComplexity != ''">
                    AND  complexity+0 >= #{minComplexity}
                </if>
                <if test="maxComplexity != null and maxComplexity != ''">
                    AND  complexity+0 &lt;=  #{maxComplexity}
                </if>
                <if test="complexityLevel != null and complexityLevel != ''">
                    AND complexity_level+0 >=  #{complexityLevel}
                </if>
                )
            </if>
            <if test="needRisk != null and needRisk != ''">
                and scene_id in (select scene_id from scene_complexity where is_deleted = '0'
                <if test="minRisk != null and minRisk != ''">
                    AND risk+0 >= #{minRisk}
                </if>
                <if test="maxRisk != null and maxRisk != ''">
                    AND risk+0 &lt;=  #{maxRisk}
                </if>
                <if test="riskLevel != null and riskLevel != ''">
                    AND risk_level+0 >=  #{riskLevel}
                </if>
                )
            </if>
        </where>
        ORDER BY create_time,scene_name DESC

    </select>

    <select id="selectIdsByNames" resultType="java.lang.String">
        select scene_id
        from scene_reference_lib
        where is_deleted = '0'
        and scene_name in
        <foreach item="item" collection="list" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>
    <update id="deleteBySceneNames">
        update scene_reference_lib
        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>


</mapper>
