<?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.ruoyi.project.system.mapper.SysUserDeptMapper">

    <resultMap type="SysUserDept" id="SysUserDeptResult">
        <result property="userId" column="user_id"/>
        <result property="deptId" column="dept_id"/>
    </resultMap>

    <delete id="deleteUserDeptByUserId" parameterType="Long">
		delete from sys_user_dept where user_id=#{userId}
	</delete>

    <sql id="selectSysUserDeptVo">
        select user_id, dept_id from sys_user_dept
    </sql>

    <select id="selectSysUserDeptList" parameterType="SysUserDept" resultMap="SysUserDeptResult">
        <include refid="selectSysUserDeptVo"/>
        <where>
            <if test="userId != null ">and user_id = #{userId}</if>
            <if test="deptId != null ">and dept_id = #{deptId}</if>
        </where>
    </select>

    <select id="selectSysUserDeptById" parameterType="Long" resultMap="SysUserDeptResult">
        <include refid="selectSysUserDeptVo"/>
        where user_id = #{userId}
    </select>

    <insert id="insertSysUserDept" parameterType="SysUserDept">
        insert into sys_user_dept
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null ">user_id,</if>
            <if test="deptId != null ">dept_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null ">#{userId},</if>
            <if test="deptId != null ">#{deptId},</if>
        </trim>
    </insert>

    <update id="updateSysUserDept" parameterType="SysUserDept">
        update sys_user_dept
        <trim prefix="SET" suffixOverrides=",">
            <if test="deptId != null ">dept_id = #{deptId},</if>
        </trim>
        where user_id = #{userId}
    </update>

    <delete id="deleteSysUserDeptById" parameterType="Long">
        delete from sys_user_dept where user_id = #{userId}
    </delete>

    <delete id="deleteSysUserDeptByIds" parameterType="String">
        delete from sys_user_dept where user_id in
        <foreach item="userId" collection="array" open="(" separator="," close=")">
            #{userId}
        </foreach>
    </delete>

    <insert id="batchUserDept">
        insert into sys_user_dept(user_id, dept_id) values
        <foreach item="item" index="index" collection="list" separator=",">
            (#{item.userId},#{item.deptId})
        </foreach>
    </insert>

</mapper>