<?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">
    select (ROW_NUMBER() OVER()) AS 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
                inner join TCODE CODE on LOG.UL_POSITION = CODE.CODE2 and CODE.CODE1 = 'C002'
        <where>
          <if test="sdate != null and sdate != '' and edate != null and edate != ''">
            AND UL_REGDATE >= (#{sdate} || ' 00:00:00')::date
            AND UL_REGDATE &lt;= (#{edate} || ' 23:59:59')::date
          </if>
          <if test="place1 != null and place1 != ''">
            AND	UL_PLACE1 = #{place1}
          </if>
        </where>
        ORDER BY UL_REGDATE <if test="order != 0">DESC</if>
        limit #{recordCountPerPage} offset #{firstIndex}
        ) LOG_LIST
	</select>
	
	<select id="Log.selectLoginoutLog" parameterType="logSearchVO" resultType="logVO">
    SELECT (ROW_NUMBER() OVER()) AS RNUM,
        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,
        CODE.CODENM AS POSITION
    FROM H3_LOGINOUT_LOG log
        inner join TCODE CODE on log.IO_POSITION = CODE.CODE2 AND CODE.CODE1 = 'C002'
    <where>
      <if test="sdate != null and sdate != '' and edate != null and edate != ''">
        AND TO_CHAR(log.IO_REGDATE, 'yyyy-mm-dd') >= #{sdate}
        AND TO_CHAR(log.IO_REGDATE, 'yyyy-mm-dd') &lt;= #{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>
    </where>
    ORDER BY IO_REGDATE <if test="order != 0">DESC</if>
    limit #{recordCountPerPage} offset #{firstIndex}
	</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">
    with recursive menu_hierarchy (
          level,
          id,
          name,
          eng_name,
          parent,
          depth,
          url,
          menu_position,
          visible,
          url_group,
          sub_visibel,
          role_level,
          url_query,
          board_id,
          window_target
      ) as (
          select 1, a.*
          from t_menu a
          where url = #{location2}

          union all

          select b.level +1, a.*
          from t_menu a
                 inner join menu_hierarchy b
                            on a.id  = b.parent
    )
    select * from menu_hierarchy
    order by level 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>
