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

    <insert id="saveSystemUserScene" parameterType="api.common.pojo.po.scene.SystemUserScenePO">
        insert into simulation.system_user_scene
        (id,user_id,package_id,
        create_user_id,create_time,modify_user_id,modify_time,
        is_deleted) values
        <foreach collection="list" index="index" item="item" separator=",">
            (#{item.id,jdbcType=VARCHAR},#{item.userId,jdbcType=VARCHAR},#{item.packageId,jdbcType=VARCHAR},
            #{item.createUserId,jdbcType=VARCHAR},#{item.createTime},#{item.modifyUserId,jdbcType=VARCHAR},#{item.modifyTime},
            #{item.isDeleted,jdbcType=VARCHAR})
        </foreach>
    </insert>

    <select id="querySystemUserScene" parameterType="api.common.pojo.po.scene.SystemUserScenePO"
            resultType="api.common.pojo.po.scene.SystemUserScenePO">
        select
        id,user_id,package_id,
        create_time,modify_time,
        is_deleted from simulation.system_user_scene
        <where>
            is_deleted = '0'
            <if test="id != null and id != ''">
                and id = #{id,jdbcType=VARCHAR}
            </if>
            <if test="userId != null and userId != ''">
                and user_id = #{userId,jdbcType=VARCHAR}
            </if>
            <if test="packageId != null and packageId != ''">
                and package_id = #{packageId,jdbcType=VARCHAR}
            </if>
            order by modify_time desc
        </where>
    </select>
    <select id="querySystemUserSceneByUserId" parameterType="api.common.pojo.po.scene.SystemUserScenePO"
            resultType="java.lang.String">
        select
        package_id from simulation.system_user_scene
        <where>
            is_deleted = '0'
            <if test="userId != null and userId != ''">
                and user_id = #{userId,jdbcType=VARCHAR}
            </if>
        </where>
    </select>
    <select id="queryUserByPackageId" parameterType="java.lang.String"
            resultType="api.common.pojo.vo.scene.UserSceneVO">
        select a.user_id userId,b.username userName from simulation.system_user_scene a left join system_user b on a.user_id=b.id
        <where>
            a.is_deleted = '0' and b.is_deleted = '0'
            <if test="packageId != null and packageId != ''">
                and a.package_id = #{packageId,jdbcType=VARCHAR}
            </if>
        </where>
    </select>


    <update id="deleteSystemUserScene" parameterType="api.common.pojo.po.scene.SystemUserScenePO">
        update simulation.system_user_scene
        <set>
        <if test="isDeleted != null and isDeleted!=''">
            is_deleted = #{isDeleted},
        </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="packageId != null and packageId != ''">
                and package_id = #{packageId,jdbcType=VARCHAR}
            </if>
            <if test="userId != null and userId != ''">
                and user_id = #{userId,jdbcType=VARCHAR}
            </if>
        </where>
    </update>

    <update id="deleteSystemUserSceneList" parameterType="java.util.List">
        <foreach collection="list" index="index" item="item" separator=";">
            update simulation.system_user_scene
            set modify_time=#{item.modifyTime},
            modify_user_id=#{item.modifyUserId},
            is_deleted = #{item.isDeleted}
            where is_deleted = '0'
            and package_id = #{item.packageId}
            and user_id = #{item.userId}
        </foreach>
    </update>
</mapper>
