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

    <select id="checkMenuNameUnique" parameterType="com.qiaoba.api.system.entity.SysMenu" resultType="string">
        select menu_id from sys_menu
        where menu_name= #{menuName} and parent_id = #{parentId}
        <if test="menuId != null">
            and menu_id != #{menuId}
        </if>
    </select>

    <select id="existChild" resultType="string">
        select menu_id from sys_menu where parent_id = #{menuId} limit 1
    </select>

    <select id="existUsed" resultType="string">
        SELECT t2.role_name
        FROM sys_role_menu t1
        left join sys_role t2
        on t1.role_id = t2.role_id
        where t1.menu_id = #{menuId} limit 1
    </select>

    <select id="selectMenuIdsByRoleId" resultType="string">
        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}
        order by m.parent_id, m.order_num
    </select>

    <select id="selectByUserId" resultType="com.qiaoba.api.system.entity.vo.SysMenuVo">
        select distinct t1.menu_id, t1.parent_id, t1.menu_name, t1.path, t1.component, t1.`query`, t1.is_visible, t1.perms, t1.is_frame, t1.is_cache, t1.menu_type, t1.icon, t1.order_num, t1.create_time
        from sys_menu t1
        LEFT JOIN sys_role_menu t2
        on t2.menu_id = t1.menu_id
        LEFT JOIN sys_role t3
        on t3.role_id = t2.role_id
        LEFT JOIN sys_user_role t4
        on t4.role_id = t3.role_id
        where t4.user_id = #{userId} and t1.menu_type in ('M', 'C')  AND t3.status = '1'
        order by t1.parent_id, t1.order_num
    </select>

    <select id="selectPermsByUserId" resultType="string">
        SELECT t1.perms
        from sys_menu t1
        LEFT JOIN sys_role_menu t2
        on t2.menu_id = t1.menu_id
        LEFT JOIN sys_role t3
        on t3.role_id = t2.role_id
        LEFT JOIN sys_user_role t4
        on t4.role_id = t3.role_id
        where t4.user_id = #{userId}  AND t3.status = '1'
    </select>

</mapper>
