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


    <insert id="saveScenePackage" parameterType="api.common.pojo.po.scene.ScenePackagePO">
        insert into simulation.scene_package
        (package_id, package_code, package_name, weight,
         package_and_rules, scene_num, remarks,
         create_user_id, create_time, modify_user_id, modify_time,
         is_deleted, share, tree_node, is_unavailable)
        values (#{packageId,jdbcType=VARCHAR}, #{packageCode,jdbcType=VARCHAR}, #{packageName,jdbcType=VARCHAR},
                #{weight,jdbcType=VARCHAR},
                #{packageAndRules,jdbcType=VARCHAR}, #{sceneNum}, #{remarks,jdbcType=VARCHAR},
                #{createUserId,jdbcType=VARCHAR}, #{createTime}, #{modifyUserId,jdbcType=VARCHAR}, #{modifyTime},
                #{isDeleted,jdbcType=VARCHAR}, #{share,jdbcType=VARCHAR}, #{treeNode,jdbcType=VARCHAR}, '0')
    </insert>


    <select id="queryScenePackageList" parameterType="api.common.pojo.param.scene.ScenePackageParam"
            resultType="api.common.pojo.po.scene.ScenePackagePO">
        select
        a.package_id,a.package_name,a.weight,a.package_code,
        a.package_and_rules,a.scene_num,a.remarks,
        a.create_user_id,a.create_time,a.modify_user_id,a.modify_time,
        a.is_deleted,a.share,a.tree_node,a.is_unavailable
        from simulation.scene_package as a
        <where>
            a.is_deleted = '0'
            <if test="packageId != null and packageId != ''">
                and a.package_id = #{packageId,jdbcType=VARCHAR}
            </if>
            <if test="packageCode != null and packageCode != ''">
                and a.package_code like CONCAT('%',#{packageCode,jdbcType=VARCHAR},'%')
            </if>
            <if test="share != null and share != ''">
                and a.share = #{share,jdbcType=VARCHAR}
            </if>
            <if test="packageName != null and packageName != ''">
                and a.package_name like CONCAT('%',#{packageName,jdbcType=VARCHAR},'%')
            </if>
            <if test="yearMax != null and yearMax != ''">
                and #{yearMax,jdbcType=VARCHAR} &gt; a.CREATE_TIME
            </if>
            <if test="yearMin != null and yearMin != ''">
                and #{yearMin,jdbcType=VARCHAR} &lt; a.CREATE_TIME
            </if>
            <if test="createUserId != null and createUserId != ''">
                and create_user_id=#{createUserId,jdbcType=VARCHAR}
            </if>

            order by a.modify_time desc
        </where>
    </select>


    <select id="queryScenePackage" parameterType="api.common.pojo.param.scene.ScenePackageParam"
            resultType="api.common.pojo.po.scene.ScenePackagePO">
        select
        a.package_id,a.package_name,a.package_code,a.weight,
        a.package_and_rules,a.scene_num,a.remarks,
        a.create_user_id,a.create_time,a.modify_user_id,a.modify_time,
        a.is_deleted,a.share,a.tree_node
        from simulation.scene_package as a
        <where>
            a.is_deleted = '0'
            <if test="packageId != null and packageId != ''">
                and a.package_id = #{packageId,jdbcType=VARCHAR}
            </if>
            <if test="packageCode != null and packageCode != ''">
                and a.package_code = #{packageCode,jdbcType=VARCHAR}
            </if>
            <if test="packageName != null and packageName != ''">
                and a.package_name like CONCAT('%',#{packageName,jdbcType=VARCHAR},'%')
            </if>
            <if test="yearMax != null and yearMax != ''">
                and #{yearMax,jdbcType=VARCHAR} &gt; a.CREATE_TIME
            </if>
            <if test="yearMin != null and yearMin != ''">
                and #{yearMin,jdbcType=VARCHAR} &lt; a.CREATE_TIME
            </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>
            order by a.modify_time desc
        </where>
    </select>

    <select id="queryScenePackageBySublist" parameterType="java.lang.String"
            resultType="api.common.pojo.vo.scene.ScenePackageNewVO">
        select
        package_id,
        package_name,
        weight,
        package_code,
        scene_num,
        remarks,
        tree_node
        from scene_package
        <where>
            is_deleted = '0'
            <if test="packageId != null and packageId != ''">
                and package_id = #{packageId}
            </if>
        </where>
    </select>

    <update id="updateScenePackage" parameterType="api.common.pojo.po.scene.ScenePackagePO">
        update simulation.scene_package
        <set>
            <if test="isDeleted != null and isDeleted!=''">
                is_deleted = #{isDeleted,jdbcType=VARCHAR},
            </if>
            <if test="packageName != null and packageName!=''">
                package_name = #{packageName,jdbcType=VARCHAR},
            </if>
            <if test="share != null and share!=''">
                share = #{share,jdbcType=VARCHAR},
            </if>
            <if test="weight != null and weight!=''">
                weight = #{weight,jdbcType=VARCHAR},
            </if>
            <if test="sceneNum != null and sceneNum!=''">
                scene_num = #{sceneNum},
            </if>
            <if test="remarks != null and remarks!=''">
                remarks = #{remarks},
            </if>

            <if test="modifyUserId != null and modifyUserId!=''">
                modify_user_id = #{modifyUserId},
            </if>
            <if test="modifyTime != null ">
                modify_time = #{modifyTime},
            </if>
            <if test="treeNode != null and treeNode!=''">
                tree_node = #{treeNode},
            </if>
            <if test="isUnavailable != null and isUnavailable!=''">
                is_unavailable = #{isUnavailable},
            </if>
        </set>
        where package_id = #{packageId,jdbcType=VARCHAR}
    </update>

    <update id="deleteScenePackage" parameterType="api.common.pojo.po.scene.ScenePackagePO">
        update simulation.scene_package
        <set>
            <if test="isDeleted != null and isDeleted!=''">
                is_deleted = #{isDeleted,jdbcType=VARCHAR},
            </if>
            <if test="modifyUserId != null and modifyUserId!=''">
                modify_user_id = #{modifyUserId},
            </if>
            <if test="modifyTime != null ">
                modify_time = #{modifyTime},
            </if>
        </set>
        where package_id = #{packageId,jdbcType=VARCHAR}
    </update>


    <select id="queryByPackageName" parameterType="api.common.pojo.po.scene.ScenePackagePO"
            resultType="api.common.pojo.vo.scene.ScenePackageNewVO">
        select
        package_id,package_name,package_code
        from simulation.scene_package
        <where>
            is_deleted = '0'
            <if test="packageId != null and packageId != ''">
                and package_id != #{packageId,jdbcType=VARCHAR}
            </if>
            <if test="packageName != null and packageName != ''">
                and package_name =#{packageName,jdbcType=VARCHAR}
            </if>
            <if test="share != null and share != ''">
                and share=#{share,jdbcType=VARCHAR}
            </if>
            <if test="createUserId != null and createUserId != ''">
                and create_user_id=#{createUserId}
            </if>
        </where>
    </select>

    <select id="querySelfOnly" parameterType="api.common.pojo.po.scene.ScenePackagePO"
            resultType="api.common.pojo.vo.scene.ScenePackageNewVO">
        select package_id,
               package_name,
               package_code
        from simulation.scene_package
        where is_deleted = '0'
          and share = '0'
          and create_user_id = #{createUserId}
          and package_name = #{packageName}
    </select>

    <select id="queryPackageCount" parameterType="api.common.pojo.po.scene.ScenePackagePO"
            resultType="java.lang.Integer">
        select
        count(package_id)
        from simulation.scene_package
        <where>
            is_deleted = '0'
            <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>
        </where>
    </select>
    <select id="selectByIndexIds" resultType="api.common.pojo.po.scene.ScenePackagePO">
        select package_id, scene_num
        from scene_package
        where package_id in (
        select root_id
        from scene_package_sublist
        where sublist_id in
        <foreach item="item" collection="indexIds" separator="," open="(" close=")">
            #{item}
        </foreach>
        )
    </select>
    <update id="updateIsUnavailable" parameterType="api.common.pojo.po.scene.ScenePackagePO">
        update simulation.scene_package
        set is_unavailable = #{isUnavailable},
            modify_user_id = #{modifyUserId},
            modify_time    = #{modifyTime},
            scene_num      =scene_num - '${sceneNum}'
        where is_deleted = '0'
          and package_id = #{packageId}
    </update>

    <update id="updateIsUnavailable2" parameterType="api.common.pojo.po.scene.ScenePackagePO">
        update simulation.scene_package
        <set>
            <if test="isUnavailable != null and isUnavailable!=''">
                is_unavailable = #{isUnavailable,jdbcType=VARCHAR},
            </if>
            <if test="modifyUserId != null and modifyUserId!=''">
                modify_user_id = #{modifyUserId},
            </if>
            <if test="modifyTime != null ">
                modify_time = #{modifyTime},
            </if>
        </set>
        where package_id = #{packageId}
    </update>
    <update id="updateIsUnavailable3" parameterType="api.common.pojo.po.scene.ScoringRulesPO">
        update simulation.scene_package
        set is_unavailable = '1',
            modify_user_id = #{modifyUserId},
            modify_time    = #{modifyTime}
        where package_id in (select distinct root_id from scene_package_sublist where package_and_rules = #{rulesId})
    </update>
    <update id="updateIsUnavailableBySceneEvaluationRuleId" parameterType="api.common.pojo.po.scene.SceneEvaluationRulePO">
        update simulation.scene_package
        set is_unavailable = '1',
            modify_user_id = #{modifyUserId},
            modify_time    = #{modifyTime}
        where package_id in (select distinct root_id from scene_package_sublist where package_and_rules = #{ruleId})
    </update>
    <update id="update" parameterType="java.util.List">
        <foreach collection="testPackages" item="item" index="index" open="" close="" separator=";">
            update scene_package
            set scene_num = #{item.sceneNum},
            is_unavailable = #{item.isUnavailable}
            where package_id = #{item.packageId}
        </foreach>
    </update>
</mapper>
