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

    <insert id="saveScenePackageSublist" parameterType="api.common.pojo.po.scene.ScenePackageSublistPO">
        insert into simulation.scene_package_sublist
        (sublist_id, sublist_name, weight, rule_name, package_and_rules,
         scene_natural_ids, scene_traffic_ids, scene_statue_ids, scene_generalization_ids,
         scene_num, parent_id, root_id, seq, package_level, remarks,
         create_user_id, create_time, modify_user_id, modify_time,
         is_deleted, share, tree_node, template_id, example_id, rule_name_array)
        values (#{id,jdbcType=VARCHAR}, #{sublistName,jdbcType=VARCHAR}, #{weight,jdbcType=VARCHAR},
                #{ruleName,jdbcType=VARCHAR},
                #{packageAndRules,jdbcType=VARCHAR},
                #{sceneNaturalIds}, #{sceneTrafficIds}, #{sceneStatueIds}, #{sceneGeneralizationIds},
                #{sceneNum}, #{parentId,jdbcType=VARCHAR}, #{rootId,jdbcType=VARCHAR}, #{seq},
                #{packageLevel}, #{remarks},
                #{createUserId,jdbcType=VARCHAR}, #{createTime}, #{modifyUserId,jdbcType=VARCHAR}, #{modifyTime},
                #{isDeleted,jdbcType=VARCHAR}, #{share,jdbcType=VARCHAR}, #{treeNode,jdbcType=VARCHAR},
                #{templateId,jdbcType=VARCHAR}, #{exampleId,jdbcType=VARCHAR}, #{ruleNameArray,jdbcType=VARCHAR})
    </insert>


    <insert id="saveScenePackageSublistAll" parameterType="java.util.List">

        insert into simulation.scene_package_sublist
        (sublist_id,sublist_name,weight,rule_name,package_and_rules,
        scene_natural_ids,scene_traffic_ids,scene_statue_ids,scene_generalization_ids,scene_reference_lib_ids,
        scene_num,parent_id,root_id,seq,package_level,remarks,
        create_user_id,create_time,modify_user_id,modify_time,
        is_deleted,share,tree_node,template_id,example_id,rule_name_array) values
        <foreach collection="list" index="index" item="item" separator=",">
            (#{item.id,jdbcType=VARCHAR},#{item.sublistName,jdbcType=VARCHAR},#{item.weight,jdbcType=VARCHAR},#{item.ruleName,jdbcType=VARCHAR},
            #{item.packageAndRules,jdbcType=VARCHAR},
            #{item.sceneNaturalIds}, #{item.sceneTrafficIds}, #{item.sceneStatueIds}, #{item.sceneGeneralizationIds},#{item.sceneReferenceLibIds},
            #{item.sceneNum},#{item.parentId,jdbcType=VARCHAR},#{item.rootId,jdbcType=VARCHAR},#{index}+1,
            #{item.packageLevel},#{item.remarks},
            #{item.createUserId,jdbcType=VARCHAR},#{item.createTime},#{item.modifyUserId,jdbcType=VARCHAR},#{item.modifyTime},
            #{item.isDeleted,jdbcType=VARCHAR}, #{item.share,jdbcType=VARCHAR}, #{item.treeNode,jdbcType=VARCHAR},
            #{item.templateId,jdbcType=VARCHAR},#{item.exampleId,jdbcType=VARCHAR},#{item.ruleNameArray,jdbcType=VARCHAR})
        </foreach>
    </insert>

    <select id="queryScenePackageSublistList" parameterType="api.common.pojo.po.scene.ScenePackageSublistPO"
            resultType="api.common.pojo.vo.scene.ScenePackageSublistVO">
        select
        a.sublist_id id,a.sublist_name,a.weight,b.rule_name,
        a.scene_num,a.parent_id,a.root_id,a.package_level,a.remarks,a.seq,a.package_and_rules,a.rule_name_array,
        a.scene_natural_ids,a.scene_traffic_ids,a.scene_statue_ids,a.scene_generalization_ids,a.tree_node,a.template_id,a.example_id
        from simulation.scene_package_sublist as a left join simulation.scoring_rules as b on
        a.package_and_rules=b.rules_id
        <where>
            a.is_deleted = '0'
            <if test="id != null and id != ''">
                and a.sublist_id = #{id,jdbcType=VARCHAR}
            </if>
            <if test="sublistName != null and sublistName != ''">
                and a.sublist_name = #{sublistName,jdbcType=VARCHAR}
            </if>
            <if test="createUserId != null and createUserId != ''">
                and create_user_id=#{createUserId,jdbcType=VARCHAR}
            </if>
            <if test="parentId != null and parentId != ''">
                and a.parent_id = #{parentId}
            </if>
            <if test="rootId != null and rootId != ''">
                and a.root_id = #{rootId}
            </if>
            <if test="templateId != null and templateId != ''">
                and a.template_id = #{templateId}
            </if>
            <if test="exampleId != null and exampleId != ''">
                and a.example_id = #{exampleId}
            </if>
            order by seq
        </where>
    </select>


    <update id="deleteScenePackageSublist" parameterType="java.lang.String">
        update simulation.scene_package_sublist
        set is_deleted='1'
        where root_id = #{rootId,jdbcType=VARCHAR}
    </update>
    <delete id="deleteScenePackageSublistById" parameterType="java.lang.String">
        delete
        from simulation.scene_package_sublist
        where sublist_id = #{sublistId,jdbcType=VARCHAR}
    </delete>
    <select id="queryBySublistName" parameterType="api.common.pojo.po.scene.ScenePackageSublistPO"
            resultType="api.common.pojo.vo.scene.ScenePackageSublistVO">
        select sublist_id id,sublist_name
        from simulation.scene_package_sublist
        <where>
            is_deleted = '0'
            <if test="id != null and id != ''">
                and a.sublist_id != #{id,jdbcType=VARCHAR}
            </if>
            <if test="sublistName != null and sublistName != ''">
                and a.sublist_name = #{sublistName,jdbcType=VARCHAR}
            </if>
            <if test="createUserId != null and createUserId != ''">
                and create_user_id=#{createUserId,jdbcType=VARCHAR}
            </if>
            <if test="share != null and share != ''">
                and share=#{share,jdbcType=VARCHAR}
            </if>
            <if test="templateId != null and templateId != ''">
                and a.template_id = #{templateId}
            </if>
            <if test="exampleId != null and exampleId != ''">
                and a.example_id = #{exampleId}
            </if>
        </where>
    </select>

    <update id="updateScenePackageSublistById" parameterType="api.common.pojo.param.scene.SceneDeleteParam">
        update simulation.scene_package_sublist
        <set>
            <if test="sceneFhId != null and sceneFhId!=''">
                scene_generalization_ids='',
                scene_num='0',
                template_id ='',
            </if>
            <if test="modifyUserId != null and modifyUserId!=''">
                modify_user_id = #{modifyUserId,jdbcType=VARCHAR},
            </if>
            <if test="modifyTime != null">
                modify_time = #{modifyTime},
            </if>

        </set>
        where
        is_deleted='0'
        <if test="sceneZrId != null and sceneZrId != ''">
            and scene_natural_ids like CONCAT('%',#{sceneZrId,jdbcType=VARCHAR},'%')
        </if>
        <if test="sceneJtId != null and sceneJtId != ''">
            and scene_traffic_ids like CONCAT('%',#{sceneJtId,jdbcType=VARCHAR},'%')
        </if>
        <if test="sceneBzId != null and sceneBzId != ''">
            and scene_statue_ids like CONCAT('%',#{sceneBzId,jdbcType=VARCHAR},'%')
        </if>

        <if test="sceneFhId != null and sceneFhId != ''">
            and template_id =#{sceneFhId}
        </if>
    </update>
    <update id="update" parameterType="java.util.List">
        <foreach collection="indexes" item="item" index="index" open="" close="" separator=";">
            update scene_package_sublist
            set scene_natural_ids = #{item.sceneNaturalIds},
            scene_traffic_ids = #{item.sceneTrafficIds},
            scene_statue_ids = #{item.sceneStatueIds},
            scene_generalization_ids = #{item.sceneGeneralizationIds},
            scene_num = #{item.sceneNum}
            where sublist_id = #{item.sublistId}
        </foreach>
    </update>


    <select id="selectPackageIdById" parameterType="api.common.pojo.param.scene.SceneDeleteParam"
            resultType="java.lang.String">
        select root_id
        from simulation.scene_package_sublist
        <where>
            is_deleted = '0'
            <if test="sceneZrId != null and sceneZrId != ''">
                and scene_natural_ids like CONCAT('%',#{sceneZrId,jdbcType=VARCHAR},'%')
            </if>
            <if test="sceneJtId != null and sceneJtId != ''">
                and scene_traffic_ids like CONCAT('%',#{sceneJtId,jdbcType=VARCHAR},'%')
            </if>
            <if test="sceneBzId != null and sceneBzId != ''">
                and scene_statue_ids like CONCAT('%',#{sceneBzId,jdbcType=VARCHAR},'%')
            </if>
            <if test="sceneFhId != null and sceneFhId != ''">
                and scene_generalization_ids like CONCAT('%',#{sceneFhId,jdbcType=VARCHAR},'%')
            </if>
        </where>
    </select>
    <select id="selectPackageFhById" parameterType="api.common.pojo.param.scene.SceneDeleteParam"
            resultType="api.common.pojo.po.scene.ScenePackageSublistPO">
        select root_id, scene_num, sublist_id
        from simulation.scene_package_sublist
        where is_deleted = '0'
          and template_id = #{sceneFhId}

    </select>
    <select id="selectBySceneIds" resultType="api.common.pojo.po.scene.ScenePackageSublistPO">
        select sublist_id, scene_natural_ids, scene_traffic_ids, scene_statue_ids, scene_generalization_ids, scene_reference_lib_ids, scene_num
        from scene_package_sublist
        where is_deleted = '0' and
        <if test='sceneType == "1"'>
            <foreach item="item" collection="sceneIds" separator=" or " open="(" close=")">
                scene_natural_ids like concat('%',#{item},'%')
            </foreach>
        </if>
        <if test='sceneType == "2"'>
            <foreach item="item" collection="sceneIds" separator=" or " open="(" close=")">
                scene_statue_ids like concat('%',#{item},'%')
            </foreach>
        </if>
        <if test='sceneType == "3"'>
            <foreach item="item" collection="sceneIds" separator=" or " open="(" close=")">
                scene_traffic_ids like concat('%',#{item},'%')
            </foreach>
        </if>
        <if test='sceneType == "5"'>
            <foreach item="item" collection="sceneIds" separator=" or " open="(" close=")">
                scene_reference_lib_ids like concat('%',#{item},'%')
            </foreach>
        </if>
    </select>
    <select id="selectSceneNumByPackageId" resultType="java.lang.Integer">
        select sum(scene_num)
        from scene_package_sublist
        where is_deleted = '0'
          and root_id = #{scenePackageId}
    </select>
</mapper>
