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

    <!-- 基础结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.role.TbSysRolePermission">
        <id column="id" property="id" jdbcType="VARCHAR"/>
        <result column="role_id" property="roleID" jdbcType="VARCHAR"/>
        <result column="permission_id" property="permissionID" 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="PermissionResultMap" type="org.xyzh.common.dto.permission.TbSysPermission">
        <id column="id" property="id" jdbcType="VARCHAR"/>
        <result column="permission_id" property="permissionID" jdbcType="VARCHAR"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="code" property="code" 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>

    <!-- 基础字段 -->
    <sql id="Base_Column_List">
        id, role_id, permission_id, creator, updater, 
        create_time, update_time, delete_time, deleted
    </sql>

    <!-- 权限基础字段 -->
    <sql id="Permission_Column_List">
        id, permission_id, name, code, description, creator, updater, create_time, update_time, delete_time, deleted
    </sql>

    <!-- 批量绑定角色权限 -->
    <insert id="batchBindRolePermission">
        INSERT INTO tb_sys_role_permission (id, role_id, permission_id, creator, create_time, deleted)
        VALUES
        <foreach collection="rolePermissions" item="rolePermission" separator=",">
            (#{rolePermission.id}, #{rolePermission.roleID}, #{rolePermission.permissionID}, #{rolePermission.creator}, #{rolePermission.createTime}, 0)
        </foreach>
    </insert>

    <!-- 批量解绑角色权限（真删除,避免批量绑定时报错存在的问题） -->
    <delete id="batchUnbindRolePermission">
        DELETE FROM tb_sys_role_permission
        WHERE (role_id, permission_id) IN
        <foreach collection="rolePermissions" item="rolePermission" open="(" separator="," close=")">
            (#{rolePermission.roleID}, #{rolePermission.permissionID})
        </foreach>
        AND deleted = 0
    </delete>

    <!-- 根据角色ID和权限ID查询关联关系 -->
    <select id="selectByRoleIdAndPermissionId" resultMap="BaseResultMap">
        SELECT 
        <include refid="Base_Column_List"/>
        FROM tb_sys_role_permission
        WHERE deleted = 0 
        AND role_id = #{roleId}
        AND permission_id = #{permissionId}
        LIMIT 1
    </select>

    <!-- 根据角色ID查询权限关联列表 -->
    <select id="selectByRoleId" resultMap="PermissionResultMap">
        SELECT 
            tsp.permission_id,
            tsp.name,
            tsp.code,
            tsp.description,
            tsp.creator,
            tsp.updater,
            tsp.create_time,
            tsp.update_time
        FROM tb_sys_role_permission tsrp
        INNER JOIN tb_sys_permission tsp ON tsrp.permission_id = tsp.permission_id
        WHERE tsrp.deleted = 0 AND tsp.deleted = 0
        AND tsrp.role_id = #{roleId}
        ORDER BY tsrp.permission_id , tsrp.create_time ASC
    </select>

    <!-- 根据权限ID查询角色关联列表 -->
    <select id="selectByPermissionId" resultMap="BaseResultMap">
        SELECT 
        <include refid="Base_Column_List"/>
        FROM tb_sys_role_permission
        WHERE deleted = 0 
        AND permission_id = #{permissionId}
        ORDER BY create_time ASC
    </select>

    <!-- 删除角色权限关联（逻辑删除） -->
    <update id="deleteByRoleIdAndPermissionId">
        UPDATE tb_sys_role_permission 
        SET deleted = 1,
            delete_time = NOW(),
            updater = #{updater}
        WHERE deleted = 0
        AND role_id = #{roleId}
        AND permission_id = #{permissionId}
    </update>

</mapper>
