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

    <!-- 基础结果映射 -->
    <resultMap id="tbSysRoleResultMap" type="org.xyzh.common.dto.role.TbSysRole">
        <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="name" property="name" jdbcType="VARCHAR"/>
        <result column="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>

    <resultMap id="deptRoleVOResultMap" type="org.xyzh.common.vo.DeptRoleVO">
        <result column="dept_id" property="dept.deptID" jdbcType="VARCHAR"/>
        <result column="dept_name" property="dept.name" jdbcType="VARCHAR"/>
        <result column="dept_description" property="dept.description" jdbcType="VARCHAR"/>
        <result column="dept_creator" property="dept.creator" jdbcType="VARCHAR"/>
        <result column="dept_updater" property="dept.updater" jdbcType="VARCHAR"/>
        <result column="dept_create_time" property="dept.createTime" jdbcType="TIMESTAMP"/>
        <result column="dept_update_time" property="dept.updateTime" jdbcType="TIMESTAMP"/>
        <result column="dept_delete_time" property="dept.deleteTime" jdbcType="TIMESTAMP"/>
        <result column="dept_deleted" property="dept.deleted" jdbcType="BOOLEAN"/>
        <result column="role_id" property="role.roleID" jdbcType="VARCHAR"/>
        <result column="role_name" property="role.name" jdbcType="VARCHAR"/>
        <result column="role_description" property="role.description" jdbcType="VARCHAR"/>
        <result column="role_creator" property="role.creator" jdbcType="VARCHAR"/>
        <result column="role_updater" property="role.updater" jdbcType="VARCHAR"/>
        <result column="role_create_time" property="role.createTime" jdbcType="TIMESTAMP"/>
        <result column="role_update_time" property="role.updateTime" jdbcType="TIMESTAMP"/>
        <result column="role_delete_time" property="role.deleteTime" jdbcType="TIMESTAMP"/>
        <result column="role_deleted" property="role.deleted" jdbcType="BOOLEAN"/>
    </resultMap>
    <!-- 基础字段 -->
    <sql id="TbSysRole_Column_List">
        id, role_id, name, description, creator, updater, 
        create_time, update_time, delete_time, deleted
    </sql>
    <sql id="TbSysDeptRole_Column_List">
        dr.id, dr.dept_id, dr.role_id, 
        r.name as role_name, d.name as dept_name,
        r.description as role_description, d.description as dept_description, 
        r.creator as role_creator, d.creator as dept_creator, 
        r.updater as role_updater, d.updater as dept_updater, 
        r.create_time as role_create_time, d.create_time as dept_create_time, 
        r.update_time as role_update_time, d.update_time as dept_update_time, 
        r.delete_time as role_delete_time, d.delete_time as dept_delete_time, 
        r.deleted as role_deleted, d.deleted as dept_deleted
    </sql>

    <!-- 通用条件 -->
    <sql id="Where_Clause">
        <where>
            deleted = 0
            <if test="roleID != null and roleID != ''">
                AND role_id = #{roleID}
            </if>
            <if test="name != null and name != ''">
                AND name LIKE CONCAT('%', #{name}, '%')
            </if>
        </where>
    </sql>

        <!-- selectAllRoles --> 

    <select id="selectAllRoles" resultMap="tbSysRoleResultMap">
        SELECT 
        <include refid="TbSysRole_Column_List"/>
        FROM tb_sys_role
        WHERE deleted = 0
        ORDER BY role_id, create_time ASC
    </select>

    <!-- 根据过滤条件查询角色列表 -->
    <select id="selectRole" resultMap="tbSysRoleResultMap">
        SELECT 
        <include refid="TbSysRole_Column_List"/>
        FROM tb_sys_role
        <include refid="Where_Clause"/>
        ORDER BY role_id, create_time ASC
    </select>

    <!-- 插入角色 -->
    <insert id="insertRole" parameterType="org.xyzh.common.dto.role.TbSysRole">
        INSERT INTO tb_sys_role
        (id, role_id, name, description, creator, create_time)
        VALUES (#{id}, #{roleID}, #{name}, #{description}, #{creator}, #{createTime})
    </insert>
    
    <!-- 更新角色 -->
    <update id="updateRole" parameterType="org.xyzh.common.dto.role.TbSysRole">
        UPDATE tb_sys_role
        (name, description, updater, update_time)
        VALUES (#{name}, #{description}, #{updater}, #{updateTime})
        WHERE role_id = #{roleID}
    </update>

    <!-- 删除角色 -->
    <delete id="deleteRole" parameterType="org.xyzh.common.dto.role.TbSysRole">
        DELETE FROM tb_sys_role
        WHERE role_id = #{roleID}
    </delete>

    <!-- 根据用户ID查询角色列表 -->
    <select id="selectDeptRolesByUserId" resultMap="deptRoleVOResultMap">
        SELECT 
        <include refid="TbSysDeptRole_Column_List"/>
        FROM tb_sys_user_dept_role dr
        INNER JOIN tb_sys_role r ON r.role_id = dr.role_id
        INNER JOIN tb_sys_dept d ON d.dept_id = dr.dept_id
        WHERE dr.deleted = 0 
        AND dr.user_id = #{userId}
        ORDER BY dr.create_time ASC
    </select>

    <!-- 根据角色编码查询角色 -->
    <select id="selectByRoleCode" resultMap="tbSysRoleResultMap">
        SELECT 
        <include refid="TbSysRole_Column_List"/>
        FROM tb_sys_role
        WHERE deleted = 0 
        AND role_code = #{roleCode}
        LIMIT 1
    </select>

    <!-- 检查角色名称是否存在 -->
    <select id="countByRoleName" resultType="int">
        SELECT COUNT(1)
        FROM tb_sys_role
        WHERE deleted = 0 
        AND name = #{roleName}
        <if test="excludeId != null and excludeId != ''">
            AND id != #{excludeId}
        </if>
    </select>

    <!-- 检查角色编码是否存在 -->
    <select id="countByRoleCode" resultType="int">
        SELECT COUNT(1)
        FROM tb_sys_role
        WHERE deleted = 0 
        AND role_code = #{roleCode}
        <if test="excludeId != null and excludeId != ''">
            AND id != #{excludeId}
        </if>
    </select>

    <!-- 批量删除角色（逻辑删除） -->
    <update id="batchDeleteByIds">
        UPDATE tb_sys_role 
        SET deleted = 1,
            delete_time = NOW(),
            updater = #{updater}
        WHERE deleted = 0
        AND id IN
        <foreach collection="roleIds" item="roleId" open="(" separator="," close=")">
            #{roleId}
        </foreach>
    </update>



    <!-- checkRoleExists --> 

    <select id="checkRoleExists">
        SELECT 
        <include refid="TbSysRole_Column_List"/>
        FROM tb_sys_role
        WHERE deleted = 0 
        AND role_id IN
        <foreach collection="roleIds" item="roleId" open="(" separator="," close=")">
            #{roleId}
        </foreach>
    </select>
</mapper>
