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

	<resultMap id="allPageList" type="page">
		<id property="pageId" column="PAGE_ID" />
		<result property="menuId" column="MENU_ID" />
		<result property="authorizationType" column="AUTHORIZATION_ID" />
		<result property="pagePath" column="PAGE_PATH" />
		<result property="pageName" column="PAGE_NAME" />
		<result property="componentName" column="COMPONENT_NAME" />
		<result property="defaulted" column="DEFAULTED" />
		<result property="used" column="USE_YN" />
	</resultMap>
	
	<resultMap id="pageResult" type="page">
		<id property="pageId" column="PAGE_ID" />
		<result property="authorizationType" column="AUTHORIZATION_ID" />
		<result property="pageName" column="PAGE_NAME" />
		<result property="pagePath" column="PAGE_PATH" />
		<result property="componentName" column="COMPONENT_NAME" />
		<result property="defaulted" column="DEFAULTED" />
		<result property="used" column="USE_YN" />
	</resultMap>
	
	<sql id="tablePage">TN_CF_PAGE</sql>

	<select id="selectPageList" parameterType="java.util.HashMap" resultMap="pageResult">
		SELECT P.PAGE_ID, SA.AUTHORIZATION_ID, P.PAGE_NAME,
		       P.PAGE_PATH, P.COMPONENT_NAME, P.DEFAULTED, P.USE_YN
		FROM   <include refid="tablePage" /> P,
		       <include refid="sysResource.tableSysResource" /> SR,
		       <include refid="sysResource.tableSysResourceMapp" /> SRM,
		       <include refid="sysResource.tableSysAuthorization" /> SA
		WHERE  P.PAGE_ID = SR.SYS_RESOURCE_ID
		AND    P.PAGE_ID = SRM.SUB_SYS_RESOURCE_ID
		AND    P.PAGE_ID = SA.SYS_RESOURCE_ID
		AND    P.DELETE_YN = '0'
		AND    SRM.MAIN_SYS_RESOURCE_ID = #{menuId}
	</select>
	
	<insert id="insertPage" parameterType="page">
		INSERT INTO <include refid="tablePage" />
		(PAGE_ID, PAGE_NAME, PAGE_PATH,
		 COMPONENT_NAME, DEFAULTED, USE_YN, DELETE_YN)
		VALUES
		(#{pageId}, #{pageName}, #{pagePath},
		 #{componentName}, #{defaulted}, #{used}, '0')
	</insert>
	
	<update id="updatePage" parameterType="page">
		UPDATE <include refid="tablePage" />
		SET    PAGE_NAME = #{pageName},
		       PAGE_PATH = #{pagePath},
		       COMPONENT_NAME = #{componentName},
		       DEFAULTED = #{defaulted},
		       USE_YN = #{used}
		WHERE  PAGE_ID = #{pageId}
	</update>
	
	<delete id="deletePage" parameterType="java.util.HashMap">
		UPDATE <include refid="tablePage" />
		SET    USE_YN = '0',
		       DELETE_YN = '1'
		WHERE  PAGE_ID = #{pageId}
	</delete>
	
	<delete id="deletePageListByMenu" parameterType="java.util.HashMap">
		UPDATE <include refid="tablePage" />
		SET    USE_YN = '0',
		       DELETE_YN = '1'
		WHERE  EXISTS (SELECT SUB_SYS_RESOURCE_ID
		               FROM   <include refid="sysResource.tableSysResourceMapp" />
		               WHERE  SUB_SYS_RESOURCE_ID = <include refid="tablePage" />.PAGE_ID
		               AND    MAIN_SYS_RESOURCE_ID = #{menuId})
	</delete>
	
	<delete id="deletePageListByMenuId" parameterType="java.util.HashMap">
		UPDATE <include refid="tablePage" />
		SET    DELETE_YN = '1'
		WHERE  EXISTS (SELECT SRM.SUB_SYS_RESOURCE_ID
		               FROM   <include refid="sysResource.tableSysResourceMapp" /> SRM
		               WHERE  SRM.SUB_SYS_RESOURCE_ID = PAGE_ID
		               AND    SRM.MAIN_SYS_RESOURCE_ID = #{menuId})
	</delete>

	<select id="selectAllPageList" resultMap="allPageList">
		SELECT M.MENU_ID,
		       PAGE.AUTHORIZATION_ID,
		       PAGE.PAGE_ID,
		       PAGE.PAGE_NAME,
			   CONCAT(#{contextPath}, PAGE.PAGE_PATH) PAGE_PATH,
		       PAGE.COMPONENT_NAME,
		       PAGE.DEFAULTED,
		       PAGE.USE_YN
		FROM   <include refid="menu.tableMenu" /> M,
		       (SELECT SRM.MAIN_SYS_RESOURCE_ID,
		               P.PAGE_ID,
		               SA.AUTHORIZATION_ID,
		               P.PAGE_NAME,
		               P.PAGE_PATH,
		               P.COMPONENT_NAME,
		               P.DEFAULTED,
		               P.USE_YN
		        FROM   <include refid="tablePage" /> P,
		               <include refid="sysResource.tableSysResource" /> SR,
		               <include refid="sysResource.tableSysResourceMapp" /> SRM,
		               <include refid="sysResource.tableSysAuthorization" /> SA
		        WHERE  P.PAGE_ID = SR.SYS_RESOURCE_ID
		        AND    SR.SYS_RESOURCE_ID = SRM.SUB_SYS_RESOURCE_ID
		        AND    SR.SYS_RESOURCE_ID = SA.SYS_RESOURCE_ID
		        AND    P.USE_YN = '1'
		        AND    P.DELETE_YN = '0') PAGE
		WHERE  M.MENU_ID = PAGE.MAIN_SYS_RESOURCE_ID
		AND    M.USE_YN = '1'
		AND    M.DELETE_YN = '0'
		ORDER BY M.MENU_ID, PAGE.AUTHORIZATION_ID
	</select>
	
</mapper>