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

    <resultMap type="org.dromara.system.domain.SysMenu" id="SysMenuResult">
    </resultMap>

    <select id="selectMenuListByUserId" resultMap="SysMenuResult">
        select distinct m.menu_id, m.parent_id, m.menu_name, m.path, m.component, m.query_param, m.visible, m.status,
        m.perms, m.is_frame, m.is_cache, m.menu_type, m.icon, m.order_num, m.create_time
        from sys_menu m
        left join sys_role_menu rm on m.menu_id = rm.menu_id
        left join sys_role r on rm.role_id = r.role_id
        ${ew.getCustomSqlSegment}
    </select>

    <select id="selectMenuTreeByUserId" parameterType="Long" resultMap="SysMenuResult">
        select distinct m.menu_id,
                        m.parent_id,
                        m.menu_name,
                        m.path,
                        m.component,
                        m.query_param,
                        m.visible,
                        m.status,
                        m.perms,
                        m.is_frame,
                        m.is_cache,
                        m.menu_type,
                        m.icon,
                        m.order_num,
                        m.create_time
        from sys_menu m
        left join sys_role_menu rm on m.menu_id = rm.menu_id and m.status = '0'
        left join sys_role r on rm.role_id = r.role_id and r.status = '0'
        where m.menu_type in ('M', 'C')
        <if test="userId != null and userId != ''">
            and r.role_id in (select role_id from sys_user_role where user_id = #{userId})
        </if>
        <if test="userId == null">
            and r.role_id = 1
        </if>
        order by m.parent_id, m.order_num
    </select>

    <select id="selectMenuListByRoleId" resultType="Long">
        select m.menu_id
        from sys_menu m
        left join sys_role_menu rm on m.menu_id = rm.menu_id
        where rm.role_id = #{roleId}
        <if test="menuCheckStrictly">
            and m.menu_id not in (select m.parent_id from sys_menu m inner join sys_role_menu rm on m.menu_id =
            rm.menu_id and rm.role_id = #{roleId})
        </if>
        order by m.parent_id, m.order_num
    </select>

    <select id="selectMenuPermsByUserId" parameterType="Long" resultType="String">
        select distinct m.perms
        from sys_menu m
        left join sys_role_menu rm on m.menu_id = rm.menu_id and m.status = '0'
        left join sys_role r on r.role_id = rm.role_id and r.status = '0'
        where r.role_id in (select role_id from sys_user_role where user_id = #{userId})
    </select>

    <select id="selectMenuPermsByRoleId" parameterType="Long" resultType="String">
        select distinct m.perms
        from sys_menu m
                 left join sys_role_menu rm on m.menu_id = rm.menu_id
        where m.status = '0' and rm.role_id = #{roleId}
    </select>

</mapper>
