<?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.role.TbSysRolePermissionMapper">

    <!-- 结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.sys.TbSysRolePermissionDTO">
        <!-- 角色权限关系字段 -->
        <id column="role_id" property="roleId" jdbcType="VARCHAR"/>
        <id column="permission_id" property="permissionId" jdbcType="VARCHAR"/>
        <!-- 基础字段 -->
        <result column="optsn" property="optsn" jdbcType="VARCHAR"/>
        <result column="creator" property="creator" jdbcType="VARCHAR"/>
        <result column="updater" property="updater" jdbcType="VARCHAR"/>
        <result column="dept_path" property="deptPath" jdbcType="VARCHAR"/>
        <result column="remark" property="remark" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="DATE"/>
        <result column="update_time" property="updateTime" jdbcType="DATE"/>
        <result column="delete_time" property="deleteTime" jdbcType="DATE"/>
        <result column="deleted" property="deleted" jdbcType="BOOLEAN"/>
    </resultMap>

    <!-- VO结果映射（用于前端展示） -->
    <resultMap id="PermissionVOResultMap" type="org.xyzh.api.system.vo.PermissionVO">
        <!-- 角色字段 -->
        <result column="role_id" property="roleId" jdbcType="VARCHAR"/>
        <result column="role_name" property="roleName" jdbcType="VARCHAR"/>
        <result column="role_description" property="roleDescription" jdbcType="VARCHAR"/>
        <result column="role_scope" property="roleScope" jdbcType="VARCHAR"/>
        <result column="role_owner_dept_id" property="roleOwnerDeptId" jdbcType="VARCHAR"/>
        <result column="role_status" property="roleStatus" jdbcType="BOOLEAN"/>
        <!-- 模块字段 -->
        <result column="module_id" property="moduleId" jdbcType="VARCHAR"/>
        <result column="module_name" property="moduleName" jdbcType="VARCHAR"/>
        <result column="module_description" property="moduleDescription" jdbcType="VARCHAR"/>
        <!-- 权限字段 -->
        <result column="permission_id" property="permissionId" jdbcType="VARCHAR"/>
        <result column="permission_name" property="permissionName" jdbcType="VARCHAR"/>
        <result column="permission_code" property="permissionCode" jdbcType="VARCHAR"/>
        <result column="permission_description" property="permissionDescription" jdbcType="VARCHAR"/>
        <result column="permission_status" property="permissionStatus" jdbcType="BOOLEAN"/>
        <!-- 视图字段 -->
        <result column="view_id" property="viewId" jdbcType="VARCHAR"/>
        <result column="view_name" property="viewName" jdbcType="VARCHAR"/>
        <result column="view_parent_id" property="viewParentId" jdbcType="VARCHAR"/>
        <result column="view_url" property="viewUrl" jdbcType="VARCHAR"/>
        <result column="view_component" property="viewComponent" jdbcType="VARCHAR"/>
        <result column="view_icon" property="viewIcon" jdbcType="VARCHAR"/>
        <result column="view_type" property="viewType" jdbcType="INTEGER"/>
        <result column="view_view_type" property="viewViewType" jdbcType="VARCHAR"/>
        <result column="view_iframe_url" property="viewIframeUrl" jdbcType="VARCHAR"/>
        <result column="view_service" property="viewService" jdbcType="VARCHAR"/>
        <result column="view_layout" property="viewLayout" jdbcType="VARCHAR"/>
        <result column="view_order_num" property="viewOrderNum" jdbcType="INTEGER"/>
        <result column="view_description" property="viewDescription" jdbcType="VARCHAR"/>
        <!-- BaseVO 基础字段 -->
        <result column="optsn" property="optsn" jdbcType="VARCHAR"/>
        <result column="creator" property="creator" jdbcType="VARCHAR"/>
        <result column="updater" property="updater" jdbcType="VARCHAR"/>
        <result column="dept_path" property="deptPath" jdbcType="VARCHAR"/>
        <result column="remark" property="remark" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="DATE"/>
        <result column="update_time" property="updateTime" jdbcType="DATE"/>
        <result column="delete_time" property="deleteTime" jdbcType="DATE"/>
        <result column="deleted" property="deleted" jdbcType="BOOLEAN"/>
    </resultMap>

    <!-- 基础列 -->
    <sql id="Base_Column_List">
        role_id, permission_id,
        optsn, creator, updater, dept_path, remark, create_time, update_time, delete_time, deleted
    </sql>

    <!-- 批量插入系统角色权限关系（必填 + 可选字段动态列，仅修改 insert） -->
    <insert id="insertRolePermissionBatch" parameterType="java.util.List">
        INSERT INTO sys.tb_sys_role_permission
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <!-- 必填字段：role_id, permission_id, optsn -->
            role_id,
            permission_id,
            optsn,
            <!-- 可选字段：统一列定义，值侧按 item 是否有值动态拼接 -->
            <if test="list[0].creator != null">creator,</if>
            <if test="list[0].deptPath != null">dept_path,</if>
            <if test="list[0].remark != null">remark,</if>
            <if test="list[0].createTime != null">create_time,</if>
            <if test="list[0].updateTime != null">update_time,</if>
            <if test="list[0].deleteTime != null">delete_time,</if>
            <if test="list[0].deleted != null">deleted</if>
        </trim>
        VALUES
        <foreach collection="list" item="item" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <!-- 必填字段值 -->
                #{item.roleId},
                #{item.permissionId},
                #{item.optsn},
                <!-- 可选字段值：允许为 null，走数据库默认值或 NULL -->
                <if test="item.creator != null">#{item.creator},</if>
                <if test="item.deptPath != null">#{item.deptPath},</if>
                <if test="item.remark != null">#{item.remark},</if>
                <if test="item.createTime != null">#{item.createTime},</if>
                <if test="item.updateTime != null">#{item.updateTime},</if>
                <if test="item.deleteTime != null">#{item.deleteTime},</if>
                <if test="item.deleted != null">#{item.deleted}</if>
            </trim>
        </foreach>
    </insert>

    <!-- 插入系统角色权限关系 -->
    <insert id="insertRolePermission" parameterType="org.xyzh.common.dto.sys.TbSysRolePermissionDTO">
        INSERT INTO sys.tb_sys_role_permission (
            role_id, permission_id,
            optsn, creator, dept_path, remark, create_time
        ) VALUES (
            #{roleId}, #{permissionId},
            #{optsn}, #{creator}, #{deptPath}, #{remark}, #{createTime}
        )
    </insert>

    <!-- 更新系统角色权限关系 -->
    <update id="updateRolePermission" parameterType="org.xyzh.common.dto.sys.TbSysRolePermissionDTO">
        UPDATE sys.tb_sys_role_permission
        <set>
            <if test="updater != null and updater != ''">
                updater = #{updater},
            </if>
            <if test="deptPath != null and deptPath != ''">
                dept_path = #{deptPath},
            </if>
            <if test="remark != null">
                remark = #{remark},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
        </set>
        WHERE role_id = #{roleId} AND permission_id = #{permissionId}
        <if test="deleted != null">
            AND deleted = #{deleted}
        </if>
    </update>

    <!-- 删除系统角色权限关系 -->
    <update id="deleteRolePermission" parameterType="org.xyzh.common.dto.sys.TbSysRolePermissionDTO">
        UPDATE sys.tb_sys_role_permission
        SET deleted = true,
            delete_time = NOW()
        WHERE role_id = #{roleId} AND permission_id = #{permissionId}
    </update>

    <!-- 根据角色ID查询系统角色权限关系 -->
    <select id="getRolePermissionByRoleId" resultMap="PermissionVOResultMap" parameterType="java.lang.String">
        SELECT
            rp.role_id, rp.permission_id,
            r.role_id, r.name AS role_name, r.description AS role_description, r.scope AS role_scope, r.owner_dept_id AS role_owner_dept_id, r.status AS role_status,
            p.permission_id, p.name AS permission_name, p.code AS permission_code, p.description AS permission_description, p.status AS permission_status,
            p.module_id, m.name AS module_name, m.description AS module_description,
            rp.optsn, rp.creator, rp.updater, rp.dept_path, rp.create_time, rp.update_time, rp.delete_time, rp.deleted
        FROM sys.tb_sys_role_permission rp
        LEFT JOIN sys.tb_sys_role r ON rp.role_id = r.role_id AND (r.deleted IS NULL OR r.deleted = false)
        LEFT JOIN sys.tb_sys_permission p ON rp.permission_id = p.permission_id AND (p.deleted IS NULL OR p.deleted = false)
        LEFT JOIN sys.tb_sys_module m ON p.module_id = m.module_id AND (m.deleted IS NULL OR m.deleted = false)
        WHERE rp.role_id = #{roleId}
        AND (rp.deleted IS NULL OR rp.deleted = false)
        ORDER BY rp.create_time DESC
    </select>

    <!-- 根据条件查询系统角色权限关系列表 -->
    <select id="getRolePermissionByFilter" resultMap="PermissionVOResultMap" parameterType="org.xyzh.common.dto.sys.TbSysRolePermissionDTO">
        SELECT
            rp.role_id, rp.permission_id,
            r.role_id, r.name AS role_name, r.description AS role_description, r.scope AS role_scope, r.owner_dept_id AS role_owner_dept_id, r.status AS role_status,
            p.permission_id, p.name AS permission_name, p.code AS permission_code, p.description AS permission_description, p.status AS permission_status,
            p.module_id, m.name AS module_name, m.description AS module_description,
            rp.optsn, rp.creator, rp.updater, rp.dept_path, rp.create_time, rp.update_time, rp.delete_time, rp.deleted
        FROM sys.tb_sys_role_permission rp
        LEFT JOIN sys.tb_sys_role r ON rp.role_id = r.role_id AND (r.deleted IS NULL OR r.deleted = false)
        LEFT JOIN sys.tb_sys_permission p ON rp.permission_id = p.permission_id AND (p.deleted IS NULL OR p.deleted = false)
        LEFT JOIN sys.tb_sys_module m ON p.module_id = m.module_id AND (m.deleted IS NULL OR m.deleted = false)
        <where>
            <if test="filter.roleId != null and filter.roleId != ''">
                AND rp.role_id = #{filter.roleId}
            </if>
            <if test="filter.permissionId != null and filter.permissionId != ''">
                AND rp.permission_id = #{filter.permissionId}
            </if>
            <if test="filter.deptPath != null and filter.deptPath != ''">
                AND rp.dept_path LIKE CONCAT(#{filter.deptPath}, '%')
            </if>
            AND (rp.deleted IS NULL OR rp.deleted = false)
        </where>
        ORDER BY rp.create_time DESC
    </select>

    <!-- 根据条件查询系统角色权限关系分页列表 -->
    <select id="getRolePermissionPageByFilter" resultMap="PermissionVOResultMap">
        SELECT
            rp.role_id, rp.permission_id,
            r.role_id, r.name AS role_name, r.description AS role_description, r.scope AS role_scope, r.owner_dept_id AS role_owner_dept_id, r.status AS role_status,
            p.permission_id, p.name AS permission_name, p.code AS permission_code, p.description AS permission_description, p.status AS permission_status,
            p.module_id, m.name AS module_name, m.description AS module_description,
            rp.optsn, rp.creator, rp.updater, rp.dept_path, rp.create_time, rp.update_time, rp.delete_time, rp.deleted
        FROM sys.tb_sys_role_permission rp
        LEFT JOIN sys.tb_sys_role r ON rp.role_id = r.role_id AND (r.deleted IS NULL OR r.deleted = false)
        LEFT JOIN sys.tb_sys_permission p ON rp.permission_id = p.permission_id AND (p.deleted IS NULL OR p.deleted = false)
        LEFT JOIN sys.tb_sys_module m ON p.module_id = m.module_id AND (m.deleted IS NULL OR m.deleted = false)
        <where>
            <if test="filter.roleId != null and filter.roleId != ''">
                AND rp.role_id = #{filter.roleId}
            </if>
            <if test="filter.permissionId != null and filter.permissionId != ''">
                AND rp.permission_id = #{filter.permissionId}
            </if>
            <if test="filter.deptPath != null and filter.deptPath != ''">
                AND rp.dept_path LIKE CONCAT(#{filter.deptPath}, '%')
            </if>
            AND (rp.deleted IS NULL OR rp.deleted = false)
        </where>
        ORDER BY rp.create_time DESC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <!-- 根据条件查询系统角色权限关系数量 -->
    <select id="getRolePermissionCount" resultType="java.lang.Integer" parameterType="org.xyzh.common.dto.sys.TbSysRolePermissionDTO">
        SELECT COUNT(1)
        FROM sys.tb_sys_role_permission
        <where>
            <if test="filter.roleId != null and filter.roleId != ''">
                AND role_id = #{filter.roleId}
            </if>
            <if test="filter.permissionId != null and filter.permissionId != ''">
                AND permission_id = #{filter.permissionId}
            </if>
            <if test="filter.deptPath != null and filter.deptPath != ''">
                AND dept_path LIKE CONCAT(#{filter.deptPath}, '%')
            </if>
            AND (deleted IS NULL OR deleted = false)
        </where>
    </select>

    <!-- 根据用户ID一次性查询该用户所有权限（连表查询完整权限信息） -->
    <select id="getPermissionsByUserId" resultMap="PermissionVOResultMap">
        SELECT DISTINCT
            -- 角色字段
            r.role_id,
            r.name AS role_name,
            r.description AS role_description,
            r.scope AS role_scope,
            r.owner_dept_id AS role_owner_dept_id,
            r.status AS role_status,
            -- 模块字段
            m.module_id,
            m.name AS module_name,
            m.description AS module_description,
            -- 权限字段
            p.permission_id,
            p.name AS permission_name,
            p.code AS permission_code,
            p.description AS permission_description,
            p.status AS permission_status,
            -- 视图字段
            v.view_id,
            v.name AS view_name,
            v.parent_id AS view_parent_id,
            v.url AS view_url,
            v.component AS view_component,
            v.icon AS view_icon,
            v.type AS view_type,
            v.view_type AS view_view_type,
            v.iframe_url AS view_iframe_url,
            v.service AS view_service,
            v.layout AS view_layout,
            v.order_num AS view_order_num,
            v.description AS view_description,
            -- 基础字段 (从role_permission表获取)
            rp.optsn,
            rp.creator,
            rp.updater,
            rp.dept_path,
            rp.create_time,
            rp.update_time,
            rp.delete_time,
            rp.deleted
        FROM sys.tb_sys_user_role ur
        -- 关联角色表
        JOIN sys.tb_sys_role r ON ur.role_id = r.role_id
            AND (r.deleted IS NULL OR r.deleted = false)
            AND r.status = true
        -- 关联角色权限关系表
        JOIN sys.tb_sys_role_permission rp ON r.role_id = rp.role_id
            AND (rp.deleted IS NULL OR rp.deleted = false)
        -- 关联权限表
        JOIN sys.tb_sys_permission p ON rp.permission_id = p.permission_id
            AND (p.deleted IS NULL OR p.deleted = false)
            AND p.status = true
        -- 关联模块表
        LEFT JOIN sys.tb_sys_module m ON p.module_id = m.module_id
            AND (m.deleted IS NULL OR m.deleted = false)
        -- 关联视图权限关系表
        LEFT JOIN sys.tb_sys_view_permission vp ON p.permission_id = vp.permission_id
            AND (vp.deleted IS NULL OR vp.deleted = false)
        -- 关联视图表
        LEFT JOIN sys.tb_sys_view v ON vp.view_id = v.view_id
            AND (v.deleted IS NULL OR v.deleted = false)
        WHERE ur.user_id = #{userId}
            AND (ur.deleted IS NULL OR ur.deleted = false)
        ORDER BY 
            m.module_id,
            p.permission_id,
            v.order_num NULLS LAST,
            v.view_id
    </select>
</mapper>

