<?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>
  <resultMap id="DeptRoleResultMap" type="org.xyzh.common.dto.role.TbSysRole">
    <id column="role_id" property="roleID" jdbcType="VARCHAR"/>
    <result column="role_name" property="name" jdbcType="VARCHAR"/>
    <result column="role_description" property="description" 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>
  <!-- 部门角色VO结果映射 -->
  <resultMap id="DeptRoleVOResultMap" type="org.xyzh.common.vo.UserDeptRoleVO">
    <result column="dept_id" property="deptID" jdbcType="VARCHAR"/>
    <result column="dept_name" property="deptName" jdbcType="VARCHAR"/>
    <result column="dept_description" property="deptDescription" jdbcType="VARCHAR"/>
    <result column="role_id" property="roleID" jdbcType="VARCHAR"/>
    <result column="role_name" property="roleName" jdbcType="VARCHAR"/>
    <result column="role_description" property="roleDescription" jdbcType="VARCHAR"/>
  </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" resultMap="DeptRoleVOResultMap">
    SELECT 
      dr.dept_id,
      d.name AS dept_name,
      d.description AS dept_description,
      dr.role_id,
      r.name AS role_name,
      r.description AS role_description
    FROM tb_sys_dept_role dr
    LEFT JOIN tb_sys_dept d ON dr.dept_id = d.dept_id AND d.deleted = 0
    LEFT JOIN tb_sys_role r ON dr.role_id = r.role_id AND r.deleted = 0
    WHERE dr.deleted = 0 
      AND dr.dept_id = #{deptId}
    ORDER BY dr.create_time DESC
  </select>


  <select id="selectDeptRoleList" resultMap="DeptRoleVOResultMap">
    SELECT 
      dr.dept_id,
      d.name AS dept_name,
      d.description AS dept_description,
      dr.role_id,
      r.name AS role_name,
      r.description AS role_description,
      d.parent_id
    FROM tb_sys_dept_role dr
    LEFT JOIN tb_sys_dept d ON dr.dept_id = d.dept_id AND d.deleted = 0
    LEFT JOIN tb_sys_role r ON dr.role_id = r.role_id AND r.deleted = 0
    WHERE dr.deleted = 0
    ORDER BY dr.dept_id, dr.role_id, dr.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>

  <!-- selectParentDeptAdmin --> 

  <select id="selectParentDeptAdmin">
    WITH RECURSIVE dept_hierarchy AS (
      -- 基础查询：查询起始部门
      SELECT 
        dept_id, 
        parent_id, 
        name, 
        description,
        1 AS level
      FROM tb_sys_dept
      WHERE dept_id = #{deptID}
        AND deleted = 0
      
      UNION ALL
      
      -- 递归查询：查询父级部门
      SELECT 
        d.dept_id, 
        d.parent_id, 
        d.name, 
        d.description,
        dh.level + 1 AS level
      FROM tb_sys_dept d
      INNER JOIN dept_hierarchy dh ON d.dept_id = dh.parent_id
      WHERE d.deleted = 0
        AND d.parent_id IS NOT NULL
    )
    SELECT 
      dh.dept_id AS deptID,
			tsdr.role_id
    FROM dept_hierarchy dh
    INNER JOIN tb_sys_dept_role tsdr ON dh.dept_id = tsdr.dept_id
    WHERE tsdr.role_id = 'admin'
    AND tsdr.deleted = 0
    ORDER BY level DESC
  </select>

  <!-- selectChildDeptRole --> 

  <select id="selectChildDeptRole">
    WITH RECURSIVE dept_hierarchy AS (
      -- 基础查询：查询起始部门
      SELECT 
        dept_id, 
        parent_id, 
        name, 
        description,
        1 AS level
      FROM tb_sys_dept
      WHERE dept_id = #{deptID}
        AND deleted = 0
      
      UNION ALL
      
      -- 递归查询：查询子级部门
      SELECT 
        d.dept_id, 
        d.parent_id, 
        d.name, 
        d.description,
        dh.level + 1 AS level
      FROM tb_sys_dept d
      INNER JOIN dept_hierarchy dh ON d.parent_id = dh.dept_id
      WHERE d.deleted = 0
        AND d.parent_id IS NOT NULL
    )
    SELECT 
      dh.dept_id AS deptID,
      tsdr.role_id
    FROM dept_hierarchy dh
    INNER JOIN tb_sys_dept_role tsdr ON dh.dept_id = tsdr.dept_id
    AND tsdr.deleted = 0
    ORDER BY level DESC
  </select>
</mapper>