<?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="org.xyzh.system.mapper.UserDeptRoleMapper">
  <resultMap id="UserDeptRoleResultMap" type="TbSysUserDeptRole">
    <id column="id" property="id" />
    <result column="user_id" property="userID" />
    <result column="dept_id" property="deptID" />
    <result column="role_id" property="roleID" />
    <result column="creator" property="creator" />
    <result column="updater" property="updater" />
    <result column="create_time" property="createTime" />
    <result column="update_time" property="updateTime" />
    <result column="delete_time" property="deleteTime" />
    <result column="deleted" property="deleted" />
  </resultMap>

  <sql id="UserDeptRole_Column_List">
    id, user_id, dept_id, role_id, creator, updater, create_time, update_time, delete_time, deleted
  </sql>

  <sql id="Where_Clause" >
    <where>
      <if test="id != null">
        id = #{id}
      </if>
      <if test="userID != null">
        user_id = #{userID}
      </if>
      <if test="deptID != null">
        dept_id = #{deptID}
      </if>
      <if test="roleID != null">
        role_id = #{roleID}
      </if>
      <if test="creator != null">
        creator = #{creator}
      </if>
      <if test="updater != null">
        updater = #{updater}
      </if>
      <if test="createTime != null">
        create_time = #{createTime}
      </if>
    </where>
  </sql>

  <!-- selectByFilter --> 

  <select id="selectByFilter">
    SELECT 
    <include refid="UserDeptRole_Column_List"/>
    FROM tb_sys_user_dept_role
    <include refid="Where_Clause"/>
    ORDER BY user_id, dept_id, role_id, create_time DESC
  </select>

  <insert id="bindUser" parameterType="TbSysUserDeptRole">
    INSERT INTO tb_sys_user_dept_role (id, user_id, dept_id, role_id, creator, create_time)
    VALUES 
    <foreach collection="userDeptRoles" item="userDeptRole" separator=",">
      (#{userDeptRole.id}, #{userDeptRole.userID}, #{userDeptRole.deptID}, #{userDeptRole.roleID}, #{userDeptRole.creator}, #{userDeptRole.createTime})
    </foreach>
  </insert>

  <delete id="unbindUser" parameterType="TbSysUserDeptRole">
    DELETE FROM tb_sys_user_dept_role
    WHERE (user_id, dept_id, role_id) IN
    <foreach collection="userDeptRoles" item="userDeptRole" open="(" separator="," close=")">
      (#{userDeptRole.userID}, #{userDeptRole.deptID}, #{userDeptRole.roleID})
    </foreach>
  </delete>

</mapper>