<?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.ActionMapper">

    <select id="selectUserActionList" resultType="com.oqpo.api.entity.ActionEntity">
        select ainfo.menu_no, ainfo.act_id, ainfo.act_tp_cd, ainfo.act_nm, ainfo.conn_url
        <if test="systemAdminYn neq 'Y'.toString() and superAdminYn neq 'Y'.toString()">
               , case when uauth.wr_yn = 'Y' then 'Y'
                        when uauth.wr_yn = 'N' and ainfo.wr_yn = 'N' then 'Y'
                          else 'N' end as view_yn
        </if>
        <if test="systemAdminYn eq 'Y'.toString() or superAdminYn eq 'Y'.toString()">
            , 'Y' as view_yn
        </if>
          , minfo.menu_id
        from np_act_info ainfo
        join  np_sys_menu minfo on ainfo.menu_no = minfo.menu_no
        <if test="systemAdminYn neq 'Y'.toString() and superAdminYn neq 'Y'.toString()">
         left outer join (
                                 select adtl.wr_yn , adtl.menu_no
                                 from   np_user_auth uauth
                                   join np_auth_dtl  adtl on uauth.auth_no = adtl.auth_no 
                                 where uauth.user_id = #{userId}  ) uauth
                                on ainfo.menu_no = uauth.menu_no
        </if>
        order by ainfo.menu_no
    </select>

    <select id="selectActionList" resultType="com.oqpo.api.entity.ActionEntity">
        select /* selectActionList */
           act.menu_no
           , act.act_id
           , act.act_nm
           , act.conn_url
           , act.wr_yn as write_yn
        from np_act_info act
        inner join np_sys_menu menu
            on menu.menu_no = act.menu_no
            and menu.use_yn = 'Y'
            and menu.open_cd = 'C'
        where act.use_yn = 'Y'
        and act.act_tp_cd = 'MU'
        order by act.act_id
    </select>

    <select id="selectActionBtnList" resultType="com.oqpo.api.entity.ActionEntity">
        select /* selectActionBtnList */
           menu_no
           , act_id
           , act_nm
           , conn_url
           , wr_yn as write_yn
        from np_act_info
        where menu_no = #{menuNo}
        and use_yn = 'Y'
        and act_tp_cd = 'BN'
        order by act_id
    </select>

    <insert id="insertActInfo" >
        insert into np_act_info
        (menu_no, act_id, act_tp_cd, act_nm, use_yn, conn_url, wr_yn, add_dt, add_id, chg_dt, chg_id)
        values
        <foreach collection="gridInsertData" item="item" separator=",">
            ( #{item.menuNo},#{item.actId},#{item.actTpCd},#{item.actNm},#{item.useYn},#{item.connUrl}, #{item.writeYn}, now(), #{userId}, now(), #{userId})
        </foreach>
    </insert>

    <update id="updateActInfo">
        <foreach collection="gridUpdateData" item="item" separator=";">
            update np_act_info
            set    chg_dt       = now()
                 , chg_id       = #{userId}
                 , act_tp_cd    = #{item.actTpCd}
                 , act_nm       = #{item.actNm}
                 , use_yn       = #{item.useYn}
                 , conn_url     = #{item.connUrl}
                 , wr_yn        = #{item.writeYn}
            where  menu_no  =  #{item.menuNo}
            and    act_id   =  #{item.actId}
        </foreach>
    </update>

    <delete id="deleteActInfo">
        delete from np_act_info /** deleteActInfo */
        where  menu_no  =  #{menuNo}
        and    act_id   in
        <foreach collection="gridDeleteData" item="item" separator="," open="(" close=")">
            #{item.actId}
        </foreach>
    </delete>

    <select id="selectActionGridList" resultType="com.oqpo.api.entity.ActionEntity">
        select /* selectActionGridList */
              menu_no
            , act_id
            , act_tp_cd
            , fn_code_nm('ACT_TP_CD', act_tp_cd) as act_tp_nm
            , act_nm
            , use_yn
            , conn_url
            , wr_yn as write_yn
        from np_act_info
        where menu_no = #{menuNo}
        <choose>
            <when test="gridRequest.sidx != null and gridRequest.sidx != ''">
                <if test="gridRequest.sidx == 'VIEW_NUM'.toString()">
                    <if test="gridRequest.sord == 'asc'.toString()">
                        order by act_id asc
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        order by act_id desc
                    </if>
                </if>
            </when>
            <otherwise>
                order by act_id asc
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectActionDtlGridListCnt" resultType="int">
        select count(1) as cnt /* selectActionDtlGridListCnt */
        from np_act_info
        where menu_no = #{menuNo}
    </select>

</mapper>
