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

	<insert id="Log.insertUserLog" parameterType="hashmap">
	<![CDATA[
		INSERT INTO H3_USER_LOG (
			UL_USERID, UL_NAME, UL_LOCATION1, UL_LOCATION2, UL_LOCATION3, UL_REGDATE, UL_USER_IP, UL_PLACE1, UL_POSITION
		)
		VALUES (
			#{userid}, #{name}, #{location1}, #{location2}, #{location3}, NOW(), #{userip, jdbcType=VARCHAR}, #{place1}, #{position}
		)
	]]>
	</insert>
	
	<insert id="Log.insertLoginoutLog" parameterType="hashmap">
	<![CDATA[
		INSERT INTO H3_LOGINOUT_LOG (
			IO_USERID, IO_TYPE, IO_REGDATE, IO_PLACE1, IO_POSITION, IO_NAME
		)
		VALUES (
			#{user.userid}, #{type}, NOW(), #{user.place1}, #{user.position}, #{user.name}
		)
	]]>
	</insert>
	
	<select id="Log.selectUserLog" parameterType="logSearchVO" resultType="logVO">
	<![CDATA[
		SELECT
		  LOG_LIST.*
		FROM
		  (
		    SELECT
		      ROWNUM RNUM, LOG_LIST.*
		    FROM
		      (	SELECT
			        LOG.UL_USERID AS USERID, LOG.UL_NAME AS NAME, LOG.UL_LOCATION1 AS LOCATION1, LOG.UL_LOCATION2 AS LOCATION2, LOG.UL_LOCATION3 AS LOCATION3, 
					TO_CHAR(LOG.UL_REGDATE, 'YYYY-MM-DD HH24:MI:SS') AS REGDATE, LOG.UL_USER_IP AS IP, LOG.UL_POSITION AS POSITION_CODE, CODE.CODENM AS POSITION
			      FROM
			        H3_USER_LOG LOG, TCODE CODE
			      WHERE
			        LOG.UL_POSITION = CODE.CODE2 
					AND CODE.CODE1 = 'C002' ]]> 
			      	<if test="sdate != null and sdate != '' and edate != null and edate != ''"><![CDATA[
			      		AND UL_REGDATE >= #{sdate} || ' 00:00:00'	
			      		AND UL_REGDATE <= #{edate} || ' 23:59:59'  ]]> 
			      	</if>
			     	<if test="place1 != null and place1 != ''">
						AND	UL_PLACE1 = #{place1}
					</if>
					<if test="name != null and name != ''">
						AND	UL_NAME LIKE '%' || #{name} || '%'
					</if>
	 	<![CDATA[ ORDER BY
			        UL_REGDATE ]]> <if test="order != 0">DESC</if> <![CDATA[
		        ) LOG_LIST 
		    WHERE ROWNUM <= #{firstIndex} + #{recordCountPerPage}
		    ) LOG_LIST
		WHERE
			RNUM > #{firstIndex}
	]]>
	</select>
	
	<select id="Log.selectLoginoutLog" parameterType="logSearchVO" resultType="logVO">
	<![CDATA[
		SELECT
		  LOG_LIST.*, CODE.CODENM AS POSITION
		FROM
		  (
		    SELECT
		      ROWNUM RNUM, LOG_LIST.*
		    FROM
		      (	SELECT
			        IO_USERID AS USERID, IO_NAME AS NAME, TO_CHAR(IO_REGDATE, 'YYYY-MM-DD HH24:MI:SS') AS REGDATE, IO_POSITION AS POSITION_CODE, IO_TYPE AS TYPE
			      FROM
			        H3_LOGINOUT_LOG
			      WHERE 
			      	1 = 1 
			      	]]> 
			      	<if test="sdate != null and sdate != '' and edate != null and edate != ''"><![CDATA[
			      		AND
				      	TO_CHAR(IO_REGDATE, 'yyyy-mm-dd') >= #{sdate}	AND
				      	TO_CHAR(IO_REGDATE, 'yyyy-mm-dd') <= #{edate}   ]]> 
			      	</if>
			     	<if test="place1 != null and place1 != ''">
						AND	IO_PLACE1 = #{place1}
					</if>
					<if test="name != null and name != ''">
						AND	IO_NAME LIKE '%' || #{name} || '%'
					</if>
	 	<![CDATA[ ORDER BY
			        IO_REGDATE ]]> <if test="order != 0">DESC</if> <![CDATA[
		        ) LOG_LIST
		    ) LOG_LIST,
		    TCODE CODE
		      
		WHERE
			RNUM  > #{firstIndex} AND  
	  		RNUM <= #{firstIndex} + #{recordCountPerPage} AND
	  		LOG_LIST.POSITION_CODE = CODE.CODE2 AND
	      	CODE.CODE1 = 'C002'
	  		
		      	
	]]>
	</select>
	
	<select id="Log.selectLogListTotCnt" parameterType="logSearchVO" resultType="int">
	<![CDATA[
		SELECT
	    	COUNT(*)
	    FROM ]]> 
	    <choose>
	    	<when test="type == 1">
	    		H3_LOGINOUT_LOG
	    	</when>
	    	<when test="type == 0">
	    		H3_USER_LOG
	    	</when>
	    </choose>
	   		WHERE
	   			1 = 1
	   		<choose>
		    	<when test="type == 1"> 
		    		<if test="sdate != null and sdate != '' and edate != null and edate != ''"><![CDATA[
		    			AND
			    		TO_CHAR(IO_REGDATE, 'yyyy-mm-dd') >= #{sdate}	AND
			      		TO_CHAR(IO_REGDATE, 'yyyy-mm-dd') <= #{edate} ]]> 
		      		</if>
		      		<if test="place1 != null and place1 != ''">
						AND	IO_PLACE1 = #{place1}
					</if>
					<if test="name != null and name != ''">
						AND	IO_NAME LIKE '%' || #{name} || '%'
					</if>
		    	</when>
		    	<when test="type == 0"> 
					<if test="sdate != null and sdate != '' and edate != null and edate != ''"><![CDATA[
						AND
			    		TO_CHAR(UL_REGDATE, 'yyyy-mm-dd') >= #{sdate}	AND
			      		TO_CHAR(UL_REGDATE, 'yyyy-mm-dd') <= #{edate}  ]]> 
			      	</if>
		      		<if test="place1 != null and place1 != ''">
						AND	UL_PLACE1 = #{place1}
					</if>
					<if test="name != null and name != ''">
						AND	UL_NAME LIKE '%' || #{name} || '%'
					</if>
		    	</when>
		    </choose>
	</select>	
	
	<select id="Log.selectUrlInfo" parameterType="logVO" resultType="hashmap">
	<![CDATA[
		SELECT 
		  LEVEL AS LV, MENU.*
		FROM 
		  T_MENU MENU 
		START WITH 
		  MENU.URL = #{location2}
		CONNECT BY PRIOR  
		  MENU.PARENT = MENU.ID
		ORDER BY
		  LV DESC
    ]]> 
	</select>
	
	<select id="Log.selectLogTotalCnt" parameterType="logSearchVO" resultType="int">
	<![CDATA[
		SELECT
	    	COUNT(*)
	    FROM ]]> 
	    <choose>
	    	<when test="type == 1">
	    		H3_LOGINOUT_LOG
	    	</when>
	    	<when test="type == 0">
	    		H3_USER_LOG
	    	</when>
	    </choose>
	</select>	
</mapper>
