<?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.SceneComplexityMapper">
    <select id="selectSceneIdsByEvaluation" parameterType="api.common.pojo.po.scene.SceneComplexityPO"
            resultType="api.common.pojo.po.scene.SceneComplexityPO">
        SELECT
        *
        FROM
        (
        SELECT
        DISTINCT(scene_id),
        complexity,
        complexity_level,
        create_time
        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 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="selectSceneComplexityEvaluationForExport" resultType="api.common.pojo.po.scene.SceneComplexityPO">
        SELECT
        scene_id, scene_type, task_id, complexity, complexity_level
        FROM scene_complexity
        <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="saveSceneComplexity" parameterType="api.common.pojo.po.scene.SceneComplexityPO">
        insert into simulation.scene_complexity
        (complexity_id, scene_id, scene_type, rule_id,
         task_id, complexity, complexity_level,
         is_deleted, create_user_id, create_time
        )
        values (#{complexityId,jdbcType=VARCHAR}, #{sceneId,jdbcType=VARCHAR}, #{sceneType,jdbcType=VARCHAR},
                #{ruleId,jdbcType=VARCHAR},
                #{taskId,jdbcType=VARCHAR}, #{complexity,jdbcType=VARCHAR},
                #{complexityLevel,jdbcType=VARCHAR},
                #{isDeleted,jdbcType=VARCHAR},
                #{createUserId,jdbcType=VARCHAR}, #{createTime})
    </insert>

    <insert id="saveSceneComplexityS">
        insert into simulation.scene_complexity
        (complexity_id, scene_id, scene_type, rule_id,
        task_id, complexity, complexity_level,
        is_deleted, create_user_id, create_time
        )
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.complexityId,jdbcType=VARCHAR}, #{item.sceneId,jdbcType=VARCHAR}, #{item.sceneType,jdbcType=VARCHAR},
            #{item.ruleId,jdbcType=VARCHAR},
            #{item.taskId,jdbcType=VARCHAR}, #{item.complexity,jdbcType=VARCHAR},
            #{item.complexityLevel,jdbcType=VARCHAR},
            #{item.isDeleted,jdbcType=VARCHAR},
            #{item.createUserId,jdbcType=VARCHAR}, #{item.createTime})
        </foreach>
    </insert>

</mapper>
