<?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.zxdmy.excite.system.mapper.SysMenuMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.zxdmy.excite.system.entity.SysMenu">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="icon" property="icon"/>
        <result column="path" property="path"/>
        <result column="permission" property="permission"/>
        <result column="component" property="component"/>
        <result column="parent_id" property="parentId"/>
        <result column="type" property="type"/>
        <result column="sort" property="sort"/>
        <result column="editable" property="editable"/>
        <result column="removable" property="removable"/>
        <result column="status" property="status"/>
        <result column="is_delete" property="isDelete"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="delete_time" property="deleteTime"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id
        , name, icon, path, permission, component, parent_id, type, sort, editable, removable, status, is_delete, create_time, update_time, delete_time
    </sql>

    <!-- 通过角色ID查询菜单列表（不含禁用菜单） -->
    <select id="selectMenusByRoleId" parameterType="int" resultType="com.zxdmy.excite.system.entity.SysMenu">
        SELECT m.*
        FROM sys_role_menu rm,
             sys_menu m
        WHERE rm.role_id = #{roleId}
          AND m.status != 0
          AND m.id = rm.menu_id
    </select>

    <!-- 通过用户ID查询菜单列表（不含：被禁用的用户、角色、菜单） -->
    <select id="selectMenusByUserId" resultType="com.zxdmy.excite.system.entity.SysMenu">
        SELECT DISTINCT m.*
        FROM sys_user u
        LEFT JOIN sys_user_role ur ON u.id = ur.user_id
        LEFT JOIN sys_role r ON r.id = ur.role_id
        LEFT JOIN sys_role_menu rm ON ur.role_id = rm.role_id
        LEFT JOIN sys_menu m ON rm.menu_id = m.id
        WHERE ur.user_id = #{userId}
        AND u.status != 0
        AND u.is_delete = 0
        AND r.status != 0
        AND r.is_delete = 0
        AND m.status != 0
        AND rm.role_id IS NOT NULL
        <if test="isOnlyMenu == 1">
            AND m.type != 'B'
            ORDER BY m.sort DESC
        </if>
    </select>


</mapper>
