<?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.yida.data.system.mapper.MenuMapper">
  <select id="findUserPermissions" resultType="com.yida.data.common.core.entity.system.Menu">
    select distinct m.*
    from t_role r
           left join t_user_role ur on (r.role_id = ur.role_id)
           left join t_user u on (u.user_id = ur.user_id)
           left join t_role_menu rm on (rm.role_id = r.role_id)
           left join t_menu m on (m.menu_id = rm.menu_id)
    where u.username = #{userName}
      and m.menu_id is not null
    order by m.order_num
  </select>

  <!-- 查询用户真实权限数据 -->
  <select id="findUserRealMenus" resultType="com.yida.data.common.core.entity.system.Menu">
    SELECT
    m.*
    FROM
    t_menu m
    LEFT JOIN (
    SELECT DISTINCT
    m.menu_id
    FROM
    t_user_menu m
    LEFT JOIN t_user t ON m.user_id = t.USER_ID
    WHERE
    t.USERNAME = #{username}
    ) a ON m.MENU_ID = a.menu_id
    where
    a.menu_id IS NOT NULL
    <if test='selectType != null'>
      and m.type = #{selectType}
    </if>
    order by m.order_num
  </select>

  <!-- 查询用户真实权限数据 -->
  <select id="findUserMenus" resultType="com.yida.data.common.core.entity.system.Menu">
    SELECT
    m.*,
    a.type as user_menu_type
    FROM
    t_menu m
    inner JOIN (
    SELECT m.menu_id,m.type
    FROM
    t_user_menu m
    LEFT JOIN t_user t ON m.user_id = t.USER_ID
    WHERE
    t.USERNAME = #{username}
    ) a ON m.MENU_ID = a.menu_id
    where
    a.menu_id IS NOT NULL
    <if test='selectType != null'>
      and m.type = #{selectType}
    </if>
    order by m.order_num
  </select>
</mapper>