<?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="com.haha.mapper.MenuMapper">

    <resultMap id="MenuResultMap" type="com.haha.entity.Menu">
        <id column="id" property="id"/>
        <result column="parent_id" property="parentId"/>
        <result column="menu_type" property="menuType"/>
        <result column="title" property="title"/>
        <result column="name" property="name"/>
        <result column="path" property="path"/>
        <result column="component" property="component"/>
        <result column="rank" property="rank"/>
        <result column="redirect" property="redirect"/>
        <result column="icon" property="icon"/>
        <result column="extra_icon" property="extraIcon"/>
        <result column="enter_transition" property="enterTransition"/>
        <result column="leave_transition" property="leaveTransition"/>
        <result column="active_path" property="activePath"/>
        <result column="auths" property="auths"/>
        <result column="frame_src" property="frameSrc"/>
        <result column="frame_loading" property="frameLoading"/>
        <result column="keep_alive" property="keepAlive"/>
        <result column="hidden_tag" property="hiddenTag"/>
        <result column="fixed_tag" property="fixedTag"/>
        <result column="show_link" property="showLink"/>
        <result column="show_parent" property="showParent"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="deleted" property="deleted"/>
    </resultMap>

    <!-- 更新菜单排序 (rank 是 MySQL 保留字，必须用反引号) -->
    <update id="updateRank">
        UPDATE t_menu SET `rank` = #{rank}, update_time = NOW() WHERE id = #{id}
    </update>

    <!-- 根据角色ID列表查询菜单 -->
    <select id="selectMenusByRoleIds" resultMap="MenuResultMap">
        SELECT DISTINCT m.* FROM t_menu m
        INNER JOIN t_role_menu rm ON m.id = rm.menu_id
        WHERE rm.role_id IN
        <foreach collection="roleIds" item="roleId" open="(" separator="," close=")">
            #{roleId}
        </foreach>
        AND m.deleted = 0
        ORDER BY m.`rank` ASC, m.id ASC
    </select>

</mapper>
