<?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.starry.admin.modules.system.mapper.SysMenuMapper">
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.starry.admin.modules.system.entity.SysMenuEntity">
        <id column="menu_id" property="menuId"/>
        <result column="menu_name" property="menuName"/>
        <result column="menu_code" property="menuCode"/>
        <result column="icon" property="icon"/>
        <result column="menu_level" property="menuLevel"/>
        <result column="permission" property="permission"/>
        <result column="sort" property="sort"/>
        <result column="parent_id" property="parentId"/>
        <result column="menu_type" property="menuType"/>
        <result column="status" property="status"/>
        <result column="remark" property="remark"/>
        <result column="path" property="path"/>
        <result column="component" property="component"/>
        <result column="router_query" property="routerQuery"/>
        <result column="is_frame" property="isFrame"/>
        <result column="visible" property="visible"/>
        <result column="updated_time" property="updatedTime"/>
        <result column="updated_by" property="updatedBy"/>
        <result column="created_time" property="createdTime"/>
        <result column="created_by" property="createdBy"/>
        <result column="deleted" property="deleted"/>
        <result column="version" property="version"/>
    </resultMap>

    <select id="selectMenuList" resultType="com.starry.admin.modules.system.entity.SysMenuEntity">
        select distinct m.menu_id, m.parent_id, m.menu_name, m.path, m.component, m.router_query,m.permission,
        m.visible, m.status, m.menu_code, m.is_frame, m.menu_type, m.icon, m.sort, m.created_time
        from sys_menu m
        <where>
            <if test="menuName != null and menuName != ''">
                AND m.menu_name like concat('%', #{menuName}, '%')
            </if>
            <if test="visible != null and visible != ''">
                AND m.visible = #{visible}
            </if>
            <if test="status != null and status != ''">
                AND m.status = #{status}
            </if>
        </where>
        order by m.parent_id, m.sort
    </select>

    <select id="selectMenuListByUserId" resultType="com.starry.admin.modules.system.entity.SysMenuEntity">
        select distinct m.menu_id, m.parent_id, m.menu_name, m.path, m.component, m.router_query,m.permission,
        m.visible, m.status, m.menu_code, m.is_frame, m.menu_type, m.icon, m.sort, m.created_time
        from sys_menu m
        left join sys_role_menu rm on m.menu_id = rm.menu_id
        left join sys_user_role ur on rm.role_id = ur.role_id
        left join sys_role ro on ur.role_id = ro.role_id
        where ur.user_id = #{userId}
        <if test="menu.menuName != null and menu.menuName != ''">
            AND m.menu_name like concat('%', #{menu.menuName}, '%')
        </if>
        <if test="menu.visible != null and menu.visible != ''">
            AND m.visible = #{menu.visible}
        </if>
        <if test="menu.status != null and menu.status != ''">
            AND m.status = #{menu.status}
        </if>
        order by m.parent_id, m.sort
    </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 m.deleted = 0
        and rm.deleted = 0
        and 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.sort
    </select>

    <select id="selectMenuPermsByUserId" resultType="String">
        select distinct m.permission
        from sys_menu m
        left join sys_role_menu rm on m.menu_id = rm.menu_id
        left join sys_user_role ur on rm.role_id = ur.role_id
        left join sys_role r on r.role_id = ur.role_id
        where m.deleted = 0 and rm.deleted = 0 and ur.deleted = 0 and r.deleted = 0
        and m.status = 0 and r.status = 0 and ur.user_id = #{userId}
    </select>

    <select id="selectMenuTreeAll" resultMap="BaseResultMap">
        select distinct m.menu_id,
                        m.parent_id,
                        m.menu_name,
                        m.path,
                        m.component,
                        m.router_query,
                        m.visible,
                        m.status,
                        ifnull(m.permission, '') as permission,
                        m.is_frame,
                        m.menu_type,
                        m.icon,
                        m.sort
        from sys_menu m
        where m.deleted = 0
          and m.menu_type in (0, 1)
          and m.status = 0
        order by m.parent_id, m.sort
    </select>

    <select id="selectMenuTreeByUserId" resultMap="BaseResultMap">
        select distinct m.menu_id, m.parent_id, m.menu_name, m.path, m.component, m.router_query, m.visible, m.status,
        ifnull(m.permission,'') as permission, m.is_frame, m.menu_type, m.icon, m.sort
        from sys_menu m
        left join sys_role_menu rm on m.menu_id = rm.menu_id
        left join sys_user_role ur on rm.role_id = ur.role_id
        left join sys_role r on ur.role_id = r.role_id
        left join sys_user u on ur.user_id = u.user_id
        where m.deleted = 0 and rm.deleted = 0 and ur.deleted = 0 and r.deleted = 0 and r.deleted = 0 and u.deleted = 0
        and u.user_id = #{userId} and m.menu_type in (0, 1) and m.status = 0 AND r.status = 0
        order by m.parent_id, m.sort
    </select>

    <select id="selectSimpleMenuList" resultType="com.starry.admin.modules.system.vo.SimpleMenu">
        select menu_id as id, menu_name as name, parent_id as parentId, menu_type as type
        from sys_menu
        where status = 0
    </select>
</mapper>
