<?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.AuthMapper">
    <select id="selectSearchAuthList" resultType="com.oqpo.api.entity.oper.AuthEntity">
        select /* selectSearchAuthList */
                auth_no, auth_nm, fn_code_nm('AUTH_TP_CD',auth_tp_cd) as auth_tp_nm, top_menu_no, use_yn
        from np_auth_info
        where 1 = 1
        <if test="sTopMenuNo != null and sTopMenuNo != ''">
        and   top_menu_no = #{sTopMenuNo}
        </if>
        <if test="sAuthTpCd != null and sAuthTpCd != ''">
        and   auth_tp_cd = #{sAuthTpCd}
        </if>
        <if test="sUseYn != null and sUseYn != ''">
        and   use_yn = #{sUseYn}
        </if>
        <if test="sAuthNm != null and sAuthNm != ''">
        and   auth_nm like concat('%',#{sAuthNm},'%')
        </if>
        order by auth_nm asc
    </select>

    <select id="selectAuthInfo" resultType="com.oqpo.api.entity.oper.AuthEntity">
        select /* selectAuthInfo */
           auth_no, auth_nm, auth_tp_cd,  fn_code_nm('AUTH_TP_CD',auth_tp_cd) as auth_tp_nm, top_menu_no, use_yn
           , fn_get_menu_nm(top_menu_no) as top_menu_nm
        from np_auth_info
        where auth_no = #{authNo}
    </select>

    <select id="selectAuthDtlGridList" resultType="com.oqpo.api.entity.oper.AuthEntity">
        select /* selectAuthDtlGridList   'auth.authNo' */
               ainfo.auth_no
             , fn_get_menu_nm(ainfo.top_menu_no) as top_menu_nm
             , adtl.menu_no as 'authDtl.menu_no'
             ,  fn_get_menu_nm(adtl.menu_no) as 'authDtl.menu_nm'
             ,  adtl.use_yn as 'authDtl.read_yn'
             ,  adtl.wr_yn as 'authDtl.write_yn'
        from np_auth_info ainfo, np_auth_dtl adtl
        where ainfo.auth_no = adtl.auth_no
        and   ainfo.auth_no = #{authNo}
        <choose>
            <when test="gridRequest.sidx != null and gridRequest.sidx != ''">
                <if test="gridRequest.sidx == 'VIEW_NUM'.toString()">
                    <if test="gridRequest.sord == 'asc'.toString()">
                        order by top_menu_nm asc
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        order by top_menu_nm desc
                    </if>
                </if>
            </when>
            <otherwise>
                order by top_menu_nm asc
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectAuthDtlGridListCnt" resultType="int">
        select count(1) as cnt /* selectAuthDtlGridListCnt */
        from np_auth_dtl
        where auth_no = #{authNo}
    </select>

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

    <insert id="insertAuthInfo" >
        insert into np_auth_info  /* insertAuthInfo */
        (auth_no, auth_nm, auth_tp_cd, top_menu_no, use_yn, add_dt, add_id, chg_dt, chg_id)
        values
        (#{entity.authNo}, #{entity.authNm}, #{entity.authTpCd}, #{entity.topMenuNo}, #{entity.useYn}, now(), #{userId}, now(), #{userId})
    </insert>

    <insert id="insertAuthDtl" >
        insert into np_auth_dtl /** insertAuthDtl */
        (auth_no, menu_no, use_yn, wr_yn, add_dt, add_id, chg_dt, chg_id)
        values
        <foreach collection="gridInsertData" item="item" separator=",">
            ( #{item.authNo},#{item.menuNo},#{item.readYn},#{item.writeYn}, now(), #{userId}, now(), #{userId})
        </foreach>
    </insert>

    <update id="updateAuthInfo">
        update np_auth_info  /** updateAuthInfo */
        set    auth_nm      = #{entity.authNm}
            ,  auth_tp_cd   = #{entity.authTpCd}
            ,  top_menu_no  = #{entity.topMenuNo}
            ,  use_yn       = #{entity.useYn}
            ,  chg_dt       = now()
            ,  chg_id       = #{userId}
        where auth_no       = #{entity.authNo}
    </update>

    <insert id="updateAuthDtl">
        insert into np_auth_dtl /** Merge updateAuthDtl */
        (auth_no, menu_no, use_yn, wr_yn, add_dt, add_id, chg_dt, chg_id)
        values
        <foreach collection="gridUpdateData" item="item" separator=",">
            ( #{item.authNo},#{item.menuNo},#{item.readYn},#{item.writeYn}, now(), #{userId}, now(), #{userId})
        </foreach>
        on duplicate key
        update
                  chg_dt       = now()
                , chg_id       = #{userId}
                , use_yn       = VALUES(use_yn)
                , wr_yn        = VALUES(wr_yn) ;
    </insert>

    <delete id="deleteAuthDtl" >
        delete from np_auth_dtl /** deleteAuthDtl */
        where auth_no = #{authNo}
        and menu_no in
        <foreach collection="gridDeleteData" item="item" separator="," open="(" close=")">
            #{item.menuNo}
        </foreach>
    </delete>

    <select id="selectAuthSearchGridList" resultType="com.oqpo.api.entity.oper.AuthEntity">
        select /* selectAuthSearchGridList  */
        auth_no
        ,auth_nm
        ,top_menu_no
        , fn_get_menu_nm(cast(top_menu_no as int)) as top_menu_nm
        from np_auth_info
        where use_yn = 'Y'
        <if test="authTpCd == '30' ">
            and   auth_tp_cd in ('30','40','50','60')
        </if>
        <if test="authTpCd == '40' ">
            and   auth_tp_cd in ('40','50','60')
        </if>
        <if test="sAuthTpCd != null and sAuthTpCd != ''">
        and   auth_tp_cd = #{sAuthTpCd}
        </if>
        <if test="sTopMenuNo != null and sTopMenuNo != ''">
            and top_menu_no like concat('%',#{sTopMenuNo},'%')
        </if>
        <if test="sAuthNm != null and sAuthNm != ''">
        and   auth_nm like concat('%',#{sAuthNm},'%')
        </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 auth_nm asc
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        order by auth_nm desc
                    </if>
                </if>
            </when>
            <otherwise>
                order by auth_nm asc
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectAuthSearchGridListCnt" resultType="int">
        select count(1) as cnt /* selectAuthSearchGridListCnt */
        from np_auth_info
        where use_yn = 'Y'
        <if test="authTpCd == '30' ">
            and   auth_tp_cd in ('30','40','50','60')
        </if>
        <if test="authTpCd == '40' ">
            and   auth_tp_cd in ('40','50','60')
        </if>
        <if test="sAuthTpCd != null and sAuthTpCd != ''">
            and   auth_tp_cd = #{sAuthTpCd}
        </if>
        <if test="sTopMenuNo != null and sTopMenuNo != ''">
            and top_menu_no like concat('%',#{sTopMenuNo},'%')
        </if>
        <if test="sAuthNm != null and sAuthNm != ''">
            and   auth_nm like concat('%',#{sAuthNm},'%')
        </if>
    </select>


</mapper>
