<?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.oqpo.api.mapper.system.MenuMapper">

    <select id="selectUserAuthMenuList" resultType="com.oqpo.api.entity.oper.MenuEntity">
        with recursive tree_menu_list    /* selectUserAuthMenuList */
        as
        (
        select 1 level
        , menu_no
        , top_menu_no
        , menu_nm
        , view_num
        , open_cd
        , conn_url
        , cast(menu_no as char(100) character set utf8) id_path
        , use_yn
        , fn_code_nm('USE_YN', use_yn) as use_yn_nm
        , menu_id
        from np_sys_menu
        where top_menu_no = 10
        union all
        select c.level + 1
        , b.menu_no
        , b.top_menu_no
        , b.menu_nm
        , b.view_num
        , b.open_cd
        , b.conn_url
        , concat(c.id_path, '->', case when b.view_num  <![CDATA[ < ]]>  10 then concat('0',b.view_num) else b.view_num end ,'->', b.menu_no) as id_path
        , b.use_yn
        , fn_code_nm('USE_YN', b.use_yn) as use_yn_nm
        , b.menu_id
        from np_sys_menu b, tree_menu_list c
        where c.menu_no = b.top_menu_no
        and   b.sys_type_cd = 'W'
        )
        select distinct tlist.menu_no, tlist.top_menu_no, fn_get_menu_nm(tlist.top_menu_no) as top_menu_nm, tlist.menu_nm, tlist.view_num, tlist.level , tlist.use_yn, tlist.use_yn_nm
        , tlist.open_cd, case when tlist.open_cd != 'C' then (select count(*) from np_sys_menu where top_menu_no = tlist.menu_no ) else 0 end as b_menu_cnt
        , tlist.conn_url, tlist.menu_id
        from   tree_menu_list tlist
        <if test="systemAdminYn neq 'Y'.toString() ">
        join  (	select 100 as top_menu_no, top_menu_no as  menu_no, 'Y' as wr_yn
        from   np_user_auth uauth
            join np_auth_info ainfo on uauth.auth_no = ainfo.auth_no and ainfo.use_yn = 'Y'
            where  uauth.user_id = #{userId}
            union all
            select ainfo.top_menu_no, adtl.menu_no, adtl.wr_yn
            from   np_user_auth uauth
            join np_auth_info ainfo on uauth.auth_no = ainfo.auth_no and ainfo.use_yn = 'Y'
            join np_auth_dtl adtl on uauth.auth_no = adtl.auth_no  and adtl.use_yn = 'Y'
            where  uauth.user_id = #{userId} ) umenu
            on tlist.top_menu_no = umenu.top_menu_no and tlist.menu_no = umenu.menu_no
        </if>
        where tlist.use_yn = 'Y'
        and   tlist.open_cd in ('T','C')
        order by id_path
    </select>


    <select id="selectAuthMenuList" resultType="com.oqpo.api.entity.oper.MenuEntity">
        select x.* from ( /** selectAuthMenuList */
        select distinct
        <choose>
            <when test="systemAdminYn eq 'Y'.toString()">
                menu.menu_no
                , menu.menu_nm
                , '' as 'auth.authNo'
                , #{userId} as 'userAuth.userId'
            </when>
            <when test="superAdminYn eq 'Y'.toString()">
                menu.menu_no
                , menu.menu_nm
                , '' as 'auth.authNo'
                , #{userId} as 'userAuth.userId'
            </when>
            <otherwise>
                menu.menu_no
                , menu.menu_nm
                , auth.auth_no as 'auth.authNo'
                , userAuth.user_id as 'userAuth.userId'
            </otherwise>
        </choose>
        , menu.menu_id
        , menu.conn_url
        , #{systemAdminYn} as 'member.systemAdminYn'
        , #{superAdminYn} as 'member.superAdminYn'
        , 1 as level
        , view_num
        from np_sys_menu menu
        <if test="systemAdminYn neq 'Y'.toString() and superAdminYn neq 'Y'.toString()">
            inner join np_auth_info auth
            on menu.menu_no = auth.top_menu_no
            and auth.use_yn = 'Y'
            inner join np_user_auth userAuth
            on  auth.auth_no = userAuth.auth_no
            and userAuth.user_id = #{userId}
        </if>
        where menu.open_cd in ('S', 'T', 'C')
        and menu.use_yn = 'Y'
        and   menu.sys_type_cd = 'W'
        <if test="systemAdminYn eq 'Y'.toString() or superAdminYn eq 'Y'.toString()">
            and menu.top_menu_no = 100
            <if test="superAdminYn eq 'Y'.toString()">
                and menu.menu_no != 2
            </if>
        </if>
        order by menu.view_num
        ) x
        order by x.view_num
    </select>

    <select id="selectAuthSubMenuList" resultType="com.oqpo.api.entity.oper.MenuEntity">
        select distinct /** selectAuthSubMenuList */
        menu.menu_no
        , menu.menu_nm
        , menu.menu_id
        , menu.conn_url
        , menu.icon_yn
        <if test="memberEntity.systemAdminYn neq 'Y'.toString() and memberEntity.superAdminYn neq 'Y'.toString()">
            , authDtl.wr_yn as 'authDtl.writeYn'
        </if>
        <if test="memberEntity.systemAdminYn eq 'Y'.toString() or memberEntity.superAdminYn eq 'Y'.toString()">
            , 'Y' as 'authDtl.writeYn'
        </if>
        , 2 as level
        from np_sys_menu menu
        <if test="memberEntity.systemAdminYn neq 'Y'.toString() and memberEntity.superAdminYn neq 'Y'.toString()">
            inner join np_auth_dtl authDtl
            on menu.menu_no = authDtl.menu_no
            and authDtl.auth_no = #{menuEntity.authDtl.authNo}
            and authDtl.use_yn = 'Y'
            inner join np_user_auth auth
            on  authDtl.auth_no = auth.auth_no
            and auth.user_id = #{menuEntity.userAuth.userId}
        </if>
        where menu.open_cd in ('S', 'T', 'C')
        and menu.use_yn = 'Y'
        and menu.top_menu_no = #{menuEntity.menuNo}
        and menu.sys_type_cd = 'W'
        order by menu.view_num
    </select>

    <select id="selectAllMenuList" resultType="com.oqpo.api.entity.oper.MenuEntity">
        with recursive tree_menu_list    /* selectAllMenuList */
        as
        (
        select 1 level
             , menu_no
             , top_menu_no
             , menu_nm
             , view_num
             , open_cd
             , cast(menu_no as char(100) character set utf8) id_path
             , use_yn
             , fn_code_nm('USE_YN', use_yn) as use_yn_nm
          from np_sys_menu
         where top_menu_no = 0
         union all
         select c.level + 1
              , b.menu_no
              , b.top_menu_no
              , b.menu_nm
              , b.view_num
              , b.open_cd
              , concat(c.id_path, '->', case when b.view_num <![CDATA[ < ]]> 10 then concat('0',b.view_num) else b.view_num end ,'->', b.menu_no) as id_path
              , b.use_yn
              , fn_code_nm('USE_YN', b.use_yn) as use_yn_nm
           from np_sys_menu b, tree_menu_list c
          where c.menu_no = b.top_menu_no
        )

        select  tlist.menu_no, tlist.top_menu_no, fn_get_menu_nm(tlist.top_menu_no) as top_menu_nm, tlist.menu_nm, tlist.view_num, tlist.level , tlist.use_yn, tlist.use_yn_nm
                , tlist.open_cd, case when tlist.open_cd != 'C' then (select count(*) from np_sys_menu where top_menu_no = tlist.menu_no ) else 0 end as b_menu_cnt
        from tree_menu_list tlist
        order by id_path
    </select>

    <select id="selectMenuInfo" resultType="com.oqpo.api.entity.oper.MenuEntity">
        select /* selectMenuInfo */
                menu_no, menu_id, top_menu_no, menu_nm, use_yn, view_num, conn_url, icon_yn, role_desc, open_cd
                ,fn_get_menu_nm(top_menu_no) as top_menu_nm
        from   np_sys_menu
        where  menu_no = #{menuNo}
    </select>

    <insert id="insertMenu" >
        insert into np_sys_menu /** insertMenu */
        ( menu_id, top_menu_no, menu_nm, use_yn, view_num, conn_url, icon_yn, role_desc, open_cd , add_dt, add_id, chg_dt, chg_id)
        values
         ( #{entity.menuId}, #{entity.topMenuNo}, #{entity.menuNm}, #{entity.useYn}, #{entity.viewNum}, #{entity.connUrl}, #{entity.iconYn}, #{entity.roleDesc}, #{entity.openCd}, now(), #{userId}, now(), #{userId})
    </insert>

    <update id="updateMenu" >
            update np_sys_menu
            set     chg_dt      = now()
                  , chg_id      = #{userId}
                  , menu_nm     = #{entity.menuNm}
                  , use_yn      = #{entity.useYn}
                  , view_num    = #{entity.viewNum}
                  , conn_url    = #{entity.connUrl}
                  , icon_yn     = #{entity.iconYn}
                  , role_desc   = #{entity.roleDesc}
                  , open_cd     = #{entity.openCd}
            where  menu_no = #{entity.menuNo}
    </update>

    <delete id="deleteMenu" >
            delete from np_sys_menu
            where  menu_no = #{menuNo}
    </delete>

    <select id="selectMenuIdDupYn" resultType="String">
        select case when count(*) <![CDATA[ > ]]> 0 then 'Y' else 'N' end as cnt /* selectMenuIdDupYn */
        from np_sys_menu
        where menu_id = #{menuId}
    </select>

    <select id="selectTopMenuList" resultType="com.oqpo.api.entity.oper.MenuEntity">
        select /* selectTopMenuList */
                menu_no, menu_id, top_menu_no, menu_nm, use_yn, view_num, conn_url, icon_yn, role_desc, open_cd
                ,fn_get_menu_nm(top_menu_no) as top_menu_nm
        from   np_sys_menu
        where  top_menu_no = 100
        and    use_yn = 'Y'
        and    open_cd in ('T','C')
        order by view_num asc
    </select>

    <select id="selectBottomMenuList" resultType="com.oqpo.api.entity.oper.MenuEntity">
        with recursive tree_menu_list    /* selectBottomMenuList */
        as
        (
        select 1 level
        , menu_no
        , top_menu_no
        , menu_nm
        , cast(menu_no as char(100) character set utf8) id_path
        from np_sys_menu
        where top_menu_no = #{topMenuNo}
        and   open_cd <![CDATA[ <> ]]> 'P'
        union all
        select c.level + 1
        , b.menu_no
        , b.top_menu_no
        , b.menu_nm
        , concat(c.id_path, '->', case when b.view_num  <![CDATA[ < ]]> 10 then concat('0',b.view_num) else b.view_num end ,'->', b.menu_no) as id_path
        from np_sys_menu b, tree_menu_list c
        where c.menu_no = b.top_menu_no
        and   b.open_cd <![CDATA[ <> ]]> 'P'
        )

        select  tlist.menu_no, tlist.top_menu_no, fn_get_menu_nm(tlist.top_menu_no) as top_menu_nm, tlist.menu_nm
                <if test="authNo != null and authNo != ''">
                , case when dtl.menu_no is null then 'N' else 'Y' end as 'authDtl.readYn'
                , nvl(dtl.wr_yn,'N') as 'authDtl.writeYn'

                </if>
                <if test="authNo == null or authNo == ''">
                , 'N' as 'authDtl.readYn', 'N' as 'authDtl.writeYn'
                </if>
        from tree_menu_list tlist
        <if test="authNo != null and authNo != ''">
        left outer join np_auth_dtl dtl on tlist.menu_no = dtl.menu_no and dtl.auth_no = nvl(#{authNo},'N')
        </if>
        order by id_path
    </select>

    <select id="selectPopMenuList" resultType="com.oqpo.api.entity.oper.MenuEntity">
        with recursive tree_menu_list
            as
            (
            select 1 level
            , menu_no
            , top_menu_no
            , menu_nm
            , menu_nm as id_path_nm
            , open_cd
            from np_sys_menu
            where top_menu_no = 10
            and   sys_type_cd = 'W'
            and   open_cd  <![CDATA[ <> ]]> 'p'
            union all
            select c.level + 1
            , b.menu_no
            , b.top_menu_no
            , b.menu_nm
            , concat(c.id_path_nm, '>', b.menu_nm ) as id_path_nm
            , b.open_cd
            from np_sys_menu b, tree_menu_list c
            where c.menu_no = b.top_menu_no
            and   b.open_cd  <![CDATA[ <> ]]>  'p'
            )
        select menu_no, menu_nm, id_path_nm
        from tree_menu_list
        where open_cd = 'C'
        <if test="sTopMenuNo != null and sTopMenuNo != ''">
        and   top_menu_no = #{sTopMenuNo}
        </if>
        <if test="sMenuNm != null and sMenuNm != ''">
        and   menu_nm like concat('%',#{sMenuNm},'%')
        </if>
        <choose>
            <when test="gridRequest.sidx != null and gridRequest.sidx != ''">
                <if test="gridRequest.sidx == 'VIEW_NUM'.toString()">
                    <if test="gridRequest.sord == 'asc'.toString()">
                        order by menu_nm asc
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        order by menu_nm desc
                    </if>
                </if>
            </when>
            <otherwise>
                order by menu_nm asc
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectPopMenuCnt" resultType="Integer">
        with recursive tree_menu_list
        as
        (
        select 1 level
        , menu_no
        , top_menu_no
        , menu_nm
        , menu_nm as id_path_nm
        , open_cd
        from np_sys_menu
        where top_menu_no = 10
        and   open_cd  <![CDATA[ <> ]]> 'p'
        union all
        select c.level + 1
        , b.menu_no
        , b.top_menu_no
        , b.menu_nm
        , concat(c.id_path_nm, '>', b.menu_nm ) as id_path_nm
        , b.open_cd
        from np_sys_menu b, tree_menu_list c
        where c.menu_no = b.top_menu_no
        and   b.open_cd  <![CDATA[ <> ]]>  'p'
        )
        select count(*) as cnt
        from tree_menu_list
        where open_cd = 'C'
        <if test="sTopMenuNo != null and sTopMenuNo != ''">
            and   top_menu_no = #{sTopMenuNo}
        </if>
        <if test="sMenuNm != null and sMenuNm != ''">
            and   menu_nm like concat('%',#{sMenuNm},'%')
        </if>
    </select>

    <select id="selectMobleMenuList" resultType="com.oqpo.api.entity.oper.MenuEntity">
        select 2 level
        , menu_no
        , menu_id
        , top_menu_no
        , menu_nm
        , menu_nm as id_path_nm
        , open_cd
        , view_num
        from np_sys_menu
        where top_menu_no = 1002
        and   sys_type_cd = 'M'
        order by view_num
    </select>

    <select id="selectUserMobileAuthMenuList" resultType="com.oqpo.api.entity.oper.MenuEntity">
        with recursive tree_menu_list    /* selectUserMobileAuthMenuList */
        as
        (
        select 1 level
        , menu_no
        , top_menu_no
        , menu_nm
        , view_num
        , open_cd
        , conn_url
        , cast(menu_no as char(100) character set utf8) id_path
        , use_yn
        , fn_code_nm('USE_YN', use_yn) as use_yn_nm
        , menu_id
        from np_sys_menu
        where top_menu_no = 10
        union all
        select c.level + 1
        , b.menu_no
        , b.top_menu_no
        , b.menu_nm
        , b.view_num
        , b.open_cd
        , b.conn_url
        , concat(c.id_path, '->', case when b.view_num  <![CDATA[ < ]]>  10 then concat('0',b.view_num) else b.view_num end ,'->', b.menu_no) as id_path
        , b.use_yn
        , fn_code_nm('USE_YN', b.use_yn) as use_yn_nm
        , b.menu_id
        from np_sys_menu b, tree_menu_list c
        where c.menu_no = b.top_menu_no
        and   b.sys_type_cd = 'M'
        )
        select tlist.menu_no, tlist.top_menu_no, fn_get_menu_nm(tlist.top_menu_no) as top_menu_nm, tlist.menu_nm, tlist.view_num, tlist.level , tlist.use_yn, tlist.use_yn_nm
        , tlist.open_cd, case when tlist.open_cd != 'C' then (select count(*) from np_sys_menu where top_menu_no = tlist.menu_no ) else 0 end as b_menu_cnt
        , tlist.conn_url, tlist.menu_id
        from   tree_menu_list tlist
            join  (	select 200 as top_menu_no, top_menu_no as  menu_no, 'Y' as wr_yn
            from   np_user_auth uauth
            join np_auth_info ainfo on uauth.auth_no = ainfo.auth_no and ainfo.use_yn = 'Y'
            where  uauth.user_id = #{userId}
            union all
            select ainfo.top_menu_no, adtl.menu_no, adtl.wr_yn
            from   np_user_auth uauth
            join np_auth_info ainfo on uauth.auth_no = ainfo.auth_no and ainfo.use_yn = 'Y'
            join np_auth_dtl adtl on uauth.auth_no = adtl.auth_no  and adtl.use_yn = 'Y'
            where  uauth.user_id = #{userId} ) umenu
            on tlist.top_menu_no = umenu.top_menu_no and tlist.menu_no = umenu.menu_no
        where tlist.use_yn = 'Y'
        and   tlist.open_cd in ('T','C')
        order by id_path
    </select>

</mapper>
