<?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="Menu">

	<resultMap id="menuResult" type="menuVO">
		<result property="id" column="ID" />
		<result property="name" column="NAME" />
		<result property="engName" column="ENG_NAME" />
		<result property="parent" column="PARENT" />
		<result property="depth" column="DEPTH" />
		<result property="url" column="URL" />
		<result property="visible" column="VISIBLE" />
		<result property="urlGroup" column="URL_GROUP" />
		<result property="subVisible" column="SUB_VISIBLE" />
		<result property="roleLevel" column="ROLE_LEVEL" />
		<result property="urlQuery" column="URL_QUERY" />
		<result property="boardId" column="BOARD_ID" />
		<result property="windowTarget" column="WINDOW_TARGET" />
	</resultMap>

	<select id="selectListWithHigher" parameterType="menuVO" resultMap="menuResult">
        with recursive subject (
                                id,
                                name,
                                eng_name,
                                parent,
                                depth,
                                url,
                                menu_position,
                                visible,
                                url_group,
                                sub_visible,
                                role_level,
                                url_query,
                                board_id,
                                window_target
            ) as(
            select
                id,
                name,
                eng_name,
                parent,
                depth,
                url,
                menu_position,
                visible,
                url_group,
                sub_visible,
                role_level,
                url_query,
                board_id,
                window_target
            from t_menu
            where id = #{id}
            union all
            select
                m.id,
                m.name,
                m.eng_name,
                m.parent,
                m.depth,
                m.url,
                m.menu_position,
                m.visible,
                m.url_group,
                m.sub_visible,
                m.role_level,
                m.url_query,
                m.board_id,
                m.window_target
            from t_menu m inner join subject s on m.id = s.parent
        )
        select * from subject
        ORDER BY depth, ID;
	</select>

	<select id="selectListWithConnectBy" parameterType="menuVO" resultMap="menuResult">
    with recursive menu(
                        level,
                        ID,
                        NAME,
                        ENG_NAME,
                        PARENT,
                        DEPTH,
                        MENU_POSITION,
                        VISIBLE,
                        SUB_VISIBLE,
                        ROLE_LEVEL,
                        URL_GROUP,
                        URL,
                        URL_QUERY,
                        BOARD_ID,
                        WINDOW_TARGET
      ) as (
        select
          1,
          M.ID, M.NAME, M.ENG_NAME, M.PARENT, M.DEPTH,
          M.MENU_POSITION, M.VISIBLE, M.SUB_VISIBLE, M.ROLE_LEVEL,
          COALESCE(M.URL_GROUP, c.URL_GROUP) AS URL_GROUP,
          COALESCE(M.URL, c.URL) AS URL,
          COALESCE(M.URL_QUERY, c.URL_QUERY) AS URL_QUERY,
          COALESCE(M.BOARD_ID, c.BOARD_ID) AS BOARD_ID,
          COALESCE(M.WINDOW_TARGET, c.WINDOW_TARGET) AS WINDOW_TARGET
        FROM T_MENU M left outer join (select id, parent, url_group, url, url_query, board_id, window_target
                                      from t_menu a
                                      where a.id in (select min(id) id
                                                     from t_menu
                                                     group by parent )) c
                          on m.id = c.parent
        WHERE M.VISIBLE = 'Y'
          AND M.ROLE_LEVEL >= COALESCE((SELECT ROLE_LEVEL FROM T_SSO_AUTH_CODE AC, TAGENT A WHERE AC.CODE_ID = A.USEGRADE AND A.USERID = #{userId}), 17)
          AND M.MENU_POSITION = #{menuPosition}
          AND M.DEPTH = 0
      union all
        select
          b.level+1,
          M.ID, M.NAME, M.ENG_NAME, M.PARENT, M.DEPTH,
          M.MENU_POSITION, M.VISIBLE, M.SUB_VISIBLE, M.ROLE_LEVEL,
          COALESCE(M.URL_GROUP, c.URL_GROUP) AS URL_GROUP,
          COALESCE(M.URL, c.URL) AS URL,
          COALESCE(M.URL_QUERY, c.URL_QUERY) AS URL_QUERY,
          COALESCE(M.BOARD_ID, c.BOARD_ID) AS BOARD_ID,
          COALESCE(M.WINDOW_TARGET, c.WINDOW_TARGET) AS WINDOW_TARGET
        FROM T_MENU M inner join menu b on M.parent = b.id
                      left outer join (select id, parent, url_group, url, url_query, board_id, window_target
                                       from t_menu a
                                       where a.id in (select min(id) id
                                                      from t_menu
                                                      group by parent )) c
                                      on m.id = c.parent
        where M.VISIBLE = 'Y'
          AND M.ROLE_LEVEL >= COALESCE((SELECT ROLE_LEVEL FROM T_SSO_AUTH_CODE AC, TAGENT A WHERE AC.CODE_ID = A.USEGRADE AND A.USERID = #{userId}), 17)
          AND M.MENU_POSITION = #{menuPosition}
    )
    select *
    from menu
    where level &lt;= ${maxLevel}
    order by level, id
	</select>
<!---->
	<select id="Menu.getMenuInfo" parameterType="map" resultType="map">
    SELECT *
    FROM T_MENU
    WHERE URL_GROUP = #{group}
      AND URL = #{url}
      <if test="boardId != null and boardId != ''">
          AND BOARD_ID = #{boardId}
      </if>
      <if test="query != null and query != ''">
          AND URL_QUERY = #{query}
      </if>
    limit 1
	</select>
	
	<select id="Menu.getMenuInfoById" parameterType="map" resultType="map">
		SELECT
		  *
		FROM
		  T_MENU
		WHERE
			ID = #{id}
	</select>
	
	<select id="Menu.getSubmenu" parameterType="hashmap" resultType="hashmap">
		SELECT
			A.*
		FROM
		  T_MENU A
		  JOIN (
		    SELECT
		      MENU_POSITION, PARENT
		    FROM
		      T_MENU
		    WHERE
		      URL_GROUP = #{url_group} AND
		      URL = #{url}
			<if test="BOARD_ID != null and BOARD_ID != ''">
			  AND BOARD_ID = #{board_id}
			</if>
		  ) B
		  ON 
		    B.MENU_POSITION = A.MENU_POSITION AND
		    A.PARENT = B.PARENT
		WHERE
			A.VISIBLE = 'Y'
			AND A.SUB_VISIBLE = 'Y'
     		AND A.ROLE_LEVEL >= COALESCE((SELECT ROLE_LEVEL FROM T_SSO_AUTH_CODE AC, TAGENT A WHERE AC.CODE_ID = A.USEGRADE AND A.USERID = #{userId}), 17)
		ORDER BY 
  			A.ID
	</select>
</mapper>
