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

    <!-- 基础结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.menu.TbSysMenuPermission">
        <id column="id" property="id" jdbcType="VARCHAR"/>
        <result column="menu_id" property="menuID" 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>

    <!-- 权限VO结果映射（用于菜单权限查询） -->
    <resultMap id="PermissionVOResultMap" type="org.xyzh.common.vo.PermissionVO">
        <id column="id" property="id" jdbcType="VARCHAR"/>
        <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="name" jdbcType="VARCHAR"/>
        <result column="permission_code" property="code" jdbcType="VARCHAR"/>
        <result column="permission_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, menu_id, permission_id, creator, updater, 
        create_time, update_time, delete_time, deleted
    </sql>

    <!-- 批量绑定菜单权限 -->
    <insert id="batchBindMenuPermission">
        INSERT INTO tb_sys_menu_permission (id, menu_id, permission_id, creator, create_time, deleted)
        VALUES
        <foreach collection="menuPermissions" item="menuPermission" separator=",">
            (#{menuPermission.id}, #{menuPermission.menuID}, #{menuPermission.permissionID}, #{menuPermission.creator}, #{menuPermission.createTime}, 0)
        </foreach>
    </insert>

    <!-- 批量解绑菜单权限（真删除） -->
    <delete id="batchUnbindMenuPermission">
        DELETE FROM tb_sys_menu_permission 
        WHERE (menu_id, permission_id) IN
        <foreach collection="menuPermissions" item="menuPermission" open="(" separator="," close=")">
            (#{menuPermission.menuID}, #{menuPermission.permissionID})
        </foreach>
        AND deleted = 0
    </delete>

    <!-- 根据菜单ID和权限ID查询关联关系 -->
    <select id="selectByMenuIdAndPermissionId" resultMap="BaseResultMap">
        SELECT 
        <include refid="Base_Column_List"/>
        FROM tb_sys_menu_permission
        WHERE deleted = 0 
        AND menu_id = #{menuId}
        AND permission_id = #{permissionId}
        LIMIT 1
    </select>

    <!-- 根据菜单ID查询权限列表（返回PermissionVO） -->
    <select id="selectByMenuId" resultMap="PermissionVOResultMap">
        SELECT 
            mp.id,
            p.module_id,
            m.name AS module_name,
            m.description AS module_description,
            mp.permission_id,
            p.name AS permission_name,
            p.code AS permission_code,
            p.description AS permission_description,
            mp.creator,
            mp.updater,
            mp.create_time,
            mp.update_time,
            mp.delete_time,
            mp.deleted
        FROM tb_sys_menu_permission mp
        LEFT JOIN tb_sys_permission p ON mp.permission_id = p.permission_id AND p.deleted = 0
        LEFT JOIN tb_sys_module m ON p.module_id = m.module_id AND m.deleted = 0
        WHERE mp.deleted = 0 
        AND mp.menu_id = #{menuId}
        ORDER BY mp.create_time ASC
    </select>

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

    <!-- 删除菜单权限关联（逻辑删除） -->
    <update id="deleteByMenuIdAndPermissionId">
        UPDATE tb_sys_menu_permission 
        SET deleted = 1,
            delete_time = NOW(),
            updater = #{updater}
        WHERE deleted = 0
        AND menu_id = #{menuId}
        AND permission_id = #{permissionId}
    </update>

</mapper>
