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

  <!-- 基础结果映射 -->
  <resultMap id="BaseResultMap" type="org.xyzh.common.dto.dept.TbSysDeptRole">
    <id column="id" property="id" jdbcType="VARCHAR"/>
    <result column="dept_id" property="deptID" jdbcType="VARCHAR"/>
    <result column="role_id" property="roleID" jdbcType="VARCHAR"/>
    <result column="creator" property="creator" jdbcType="VARCHAR"/>
    <result column="updater" property="updater" jdbcType="VARCHAR"/>
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    <result column="delete_time" property="deleteTime" jdbcType="TIMESTAMP"/>
    <result column="deleted" property="deleted" jdbcType="BOOLEAN"/>
  </resultMap>

  <!-- 基础字段 -->
  <sql id="Base_Column_List">
      id, dept_id, role_id, creator, updater, 
      create_time, update_time, delete_time, deleted
  </sql>
  <sql id="Where_Clause">
    <where>
      deleted = 0
      <if test="deptID != null and deptID != ''">
        AND dept_id = #{deptID}
      </if>
    </where>
  </sql>

  <!-- selectDeptRole --> 

  <select id="selectDeptRole">
    SELECT 
    <include refid="Base_Column_List"/>
    FROM tb_sys_dept_role
    <include refid="Where_Clause"/>
    ORDER BY create_time DESC
  </select>

  <select id="selectDeptRoleList">
    SELECT 
    <include refid="Base_Column_List"/>
    FROM tb_sys_dept_role
    ORDER BY dept_id, role_id, create_time DESC
  </select>

  <!-- batchBindDeptRole --> 

  <insert id="batchBindDeptRole">
    INSERT INTO tb_sys_dept_role (id, dept_id, role_id, creator, create_time, deleted)
    VALUES
    <foreach collection="deptRoles" item="deptRole" separator=",">
      (#{deptRole.id}, #{deptRole.deptID}, #{deptRole.roleID}, #{deptRole.creator}, #{deptRole.createTime}, 0)
    </foreach>
  </insert>

  <!-- batchUnbindDeptRole --> 

  <delete id="batchUnbindDeptRole">
    DELETE FROM tb_sys_dept_role
    WHERE (dept_id, role_id) IN
    <foreach collection="deptRoles" item="deptRole" open="(" separator="," close=")">
      (#{deptRole.deptID}, #{deptRole.roleID})
    </foreach>
  </delete>
</mapper>