<?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.SceneRiskMapper">
    <select id="selectSceneIdsByEvaluation" parameterType="api.common.pojo.po.scene.SceneRiskPO"
            resultType="api.common.pojo.po.scene.SceneRiskPO">
        SELECT
        *
        FROM
        (
        SELECT
        DISTINCT(scene_id),
        risk,
        risk_level,
        create_time
        FROM
        scene_risk
        <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 test="sceneIdList != null and sceneIdList.size()>0">
                AND scene_id IN
                <foreach collection="sceneIdList" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        ORDER BY
        create_time DESC
        ) srlsc GROUP BY scene_id;
    </select>

    <select id="selectSceneRiskEvaluationForExport" resultType="api.common.pojo.po.scene.SceneRiskPO">
        SELECT
        scene_id, scene_type, task_id, risk, risk_level
        FROM scene_risk
        <where>
            is_deleted = '0' AND task_id = #{taskId}
            <if test="list != null and list.size()>0">
                AND scene_id IN
                <foreach collection="list" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        ORDER BY create_time DESC;
    </select>

    <insert id="saveSceneRisk" parameterType="api.common.pojo.po.scene.SceneRiskPO">
        insert into simulation.scene_risk
        (risk_id, scene_id, scene_type, rule_id,algorithm_id,vehicle_id
         task_id, risk, risk_level,
         is_deleted, create_user_id, create_time
        )
        values (#{riskId,jdbcType=VARCHAR}, #{sceneId,jdbcType=VARCHAR}, #{sceneType,jdbcType=VARCHAR},
                #{ruleId,jdbcType=VARCHAR},#{algorithmId,jdbcType=VARCHAR},#{vehicleId,jdbcType=VARCHAR},
                #{taskId,jdbcType=VARCHAR}, #{risk,jdbcType=VARCHAR},
                #{riskLevel,jdbcType=VARCHAR},
                #{isDeleted,jdbcType=VARCHAR},
                #{createUserId,jdbcType=VARCHAR}, #{createTime})
    </insert>
</mapper>
