<?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="cokr.xit.base.menu.dao.MenuMapper">

<resultMap id="menuRow" type="cokr.xit.base.menu.Menu">
	<result property="id" column="MENU_NO"/>
	<result property="name" column="MENU_NM"/>
	<result property="programFilename" column="PGRM_FILE_NM"/>
	<result property="action" column="ACTION"/>
	<result property="description" column="DSCRP"/>
	<result property="parentID" column="PRNT_NO"/>
	<result property="imageName" column="IMG_NM"/>
	<result property="imageConf" column="IMG_CNF"/>
	<result property="sortOrder" column="SRT_ORD"/>
	<result property="createdAt" column="REG_DT"/>
	<result property="createdBy" column="RGTR"/>
</resultMap>

<sql id="selectMenus">
SELECT A.*
  FROM TB_MENU A
<if test='menuID != null'>WHERE MENU_NO = #{menuID}</if>
 ORDER BY PRNT_NO, SRT_ORD, MENU_NO</sql>

<select id="getMenus" parameterType="map" resultMap="menuRow">/* 메뉴 정보 조회(menuMapper.getMenus) */
<include refid="selectMenus" /></select>

<select id="getMenu" parameterType="int" resultMap="menuRow">/* 메뉴 가져오기(menuMapper.getMenu) */
<include refid="selectMenus" /></select>

<insert id="insertMenu" parameterType="map">/* 메뉴 등록(menuMapper.insertMenu) */
<selectKey order="BEFORE" resultType="map" keyColumn="NEW_NO,NEW_ORD" keyProperty="menu.id,menu.sortOrder">
SELECT NEW_NO, NEW_ORD
  FROM (SELECT IFNULL(MAX(MENU_NO) + 1, 0) NEW_NO FROM TB_MENU) A,
       (<include refid="newSortOrder" />) B</selectKey>
INSERT INTO TB_MENU (
       MENU_NO
     , MENU_NM
     , PRNT_NO
     , PGRM_FILE_NM
     , ACTION
     , DSCRP
     , IMG_NM
     , IMG_CNF
     , SRT_ORD
     , REG_DT
     , RGTR
) VALUES (
       #{menu.id}
     , #{menu.name}
     , #{menu.parentID}
     , #{menu.programFilename}
     , #{menu.action}
     , #{menu.description}
     , #{menu.imageName}
     , #{menu.imageConf}
     , #{menu.sortOrder}
     ,<include refid="utility.now" />
     , #{currentUser.id}
)</insert>

<update id="updateMenu" parameterType="map">/* 메뉴 수정(menuMapper.updateMenu) */
UPDATE TB_MENU SET
       MENU_NM = #{menu.name}
     , PGRM_FILE_NM = #{menu.programFilename}
     , ACTION = #{menu.action}
     , DSCRP = #{menu.description}
     , IMG_NM = #{menu.imageName}
     , IMG_CNF = #{menu.imageConf}
WHERE MENU_NO = #{menu.id}</update>

<sql id="newSortOrder">SELECT IFNULL(MAX(SRT_ORD) + 1, 0) NEW_ORD FROM TB_MENU WHERE PRNT_NO = IFNULL(#{parentID}, IFNULL(#{menu.parentID}, 0))</sql>

<update id="moveMenus" parameterType="map">/* 메뉴 이동(menuMapper.moveMenus) */
UPDATE TB_MENU SET
       PRNT_NO = #{parentID}
     , SRT_ORD = SRT_ORD + (<include refid="newSortOrder" />)
 WHERE MENU_NO IN (<foreach collection="menuIDs" item="menuID" separator=",">#{menuID}</foreach>)
</update>

<update id="reorderMenus" parameterType="map">/* 메뉴 순서 변경(menuMapper.reorderMenus) */
UPDATE TB_MENU SET
       SRT_ORD = CASE MENU_NO
               <foreach collection="menuIDs" item="menuID" index="index">WHEN #{menuID} THEN #{index}
               </foreach>
                 ELSE MENU_NO END
 WHERE MENU_NO IN (<foreach collection="menuIDs" item="menuID" separator=",">#{menuID}</foreach>)</update>

<delete id="removeMenus" parameterType="map">/* 메뉴 제거(menuMapper.removeMenus) */
DELETE FROM TB_MENU
 WHERE MENU_NO IN (<foreach collection="menuIDs" item="menuID" separator=",">#{menuID}</foreach>)
</delete>

</mapper>