<?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="kr.co.jinwoosi.clfd.framework.weblog.mapper.WebLogMapper">

	<resultMap id="webLogUser" type="webLog">
		<constructor />
		<id property="webLogId" column="WEB_LOG_ID" />
		<result property="reqTp" column="REQ_TP" />
		<result property="reqUrl" column="REQ_URL" />
		<result property="pgmId" column="PGM_ID" />
		<result property="reqMthd" column="REQ_MTHD" />
		<result property="reqParam" column="REQ_PARAM" />
		<result property="reqIp" column="REQ_IP" />
		<result property="regDt" column="REG_DT" />
		<association property="user" javaType="userSearch">
			<id property="userId" column="USER_ID"  />
			<result property="userLoginId" column="USER_LOGIN_ID"  />
			<result property="userNm" column="USER_NM"  />
		</association>
		<association property="loginLog"  javaType="loginLog">
			<id property="loginLogId" column="LOGIN_LOG_ID" />
			<result property="loginDt" column="LOGIN_DT" />
			<result property="logoutDt" column="LOGOUT_DT" />
		</association>
		<!-- 
		<association property="user" javaType="user" columnPrefix="user_" />
		<association property="loginLog" javaType="loginLog" columnPrefix="log_" />
		 -->
	</resultMap>

	
	<select id="selectList" parameterType="webLogSearch" resultMap="webLogUser">
		SELECT *
		FROM (
			SELECT ROWNUM RNUM , LIST.*
			FROM (
				SELECT A.WEB_LOG_ID, A.LOGIN_LOG_ID, A.REQ_TP, A.REQ_URL, A.PGM_ID, A.REQ_MTHD, A.REQ_PARAM, A.REQ_IP, A.REG_DT,
					E.USER_ID, E.LOGIN_DT, E.LOGOUT_DT, E.USER_LOGIN_ID, E.USER_NM, D.PGM_NM
				FROM JCMS_WEB_LOG A
					LEFT JOIN
				    (SELECT B.LOGIN_LOG_ID AS LOGIN_LOG_ID, B.USER_ID AS USER_ID, B.LOGIN_DT AS LOGIN_DT, B.LOGOUT_DT AS LOGOUT_DT,
						C.USER_LOGIN_ID AS USER_LOGIN_ID, C.USER_NM AS USER_NM
					FROM JCMS_LOGIN_LOG B
						LEFT JOIN
					    JCMS_USER C
						ON B.USER_ID = C.USER_ID) E
					ON A.LOGIN_LOG_ID = E.LOGIN_LOG_ID
					LEFT JOIN
					(SELECT * FROM JCMS_PGM D WHERE DEL_ID IS NULL) D
					ON A.REQ_URL = D.PGM_URL
				WHERE A.REQ_TP = 'S'
					<if test="!empty(loginLog.loginLogId)" > AND A.LOGIN_LOG_ID = #{loginLog.loginLogId} </if>
					<if test="user.userId != null" > AND E.USER_ID = #{user.userId} </if>
					<if test="!empty(userLoginIdLike)" > AND E.USER_LOGIN_ID LIKE '%'|| #{userLoginIdLike}|| '%' </if>
					<if test="!empty(userNmLike)" > AND E.USER_NM LIKE '%'|| #{userNmLike}|| '%' </if>
				<if test="!empty(sort) and !empty(sortOrd)">
				ORDER BY ${sort} ${sortOrd}
				</if>
			) LIST
		)
		<if test="pagingYn == true" >
			<![CDATA[
				WHERE RNUM > #{start} AND RNUM <= #{start} + #{length}
			]]>
		</if>
	</select>
	
	<select id="select" parameterType="webLogSearch" resultMap="webLogUser">
		select	
			A.WEB_LOG_ID, A.LOGIN_LOG_ID, A.REQ_TP, A.REQ_URL, A.PGM_ID, A.REQ_MTHD, A.REQ_PARAM, A.REQ_IP, A.REG_DT, 
			E.USER_ID, E.LOGIN_DT, E.LOGOUT_DT, E.USER_LOGIN_ID, E.USER_NM, D.PGM_NM
		from		
			JCMS_WEB_LOG A
		left join
			(select
				B.LOGIN_LOG_ID AS LOGIN_LOG_ID,
				B.USER_ID AS USER_ID,
				B.LOGIN_DT AS LOGIN_DT,
				B.LOGOUT_DT AS LOGOUT_DT,
				C.USER_LOGIN_ID AS USER_LOGIN_ID,
				C.USER_NM AS USER_NM
			from
				JCMS_LOGIN_LOG B
			left join
				JCMS_USER C
			on
				B.USER_ID = C.USER_ID) E
		on
			A.LOGIN_LOG_ID = E.LOGIN_LOG_ID
		left join
			(select * from JCMS_PGM D
				where DEL_ID IS NULL) D
		on
			A.REQ_URL = D.PGM_URL
		where
			A.REQ_TP = 'S'
			<if test="!empty(loginLog.loginLogId)" > and A.LOGIN_LOG_ID = #{loginLog.loginLogId} </if>
			<if test="webLogId != null" > and A.WEB_LOG_ID = #{webLogId} </if>
			<if test="user.userId != null" > and E.USER_ID = #{user.userId} </if>
			<if test="!empty(userLoginIdLike)" > and E.USER_LOGIN_ID LIKE '%'|| #{userLoginIdLike}|| '%' </if>
			<if test="!empty(userNmLike)" > and E.USER_NM LIKE '%'|| #{userNmLike}|| '%' </if>
	</select>
	
	<select id="count" parameterType="webLog" resultType="java.lang.Integer">
		select	
			COUNT(*)
		from		
			JCMS_WEB_LOG A
		left join
			(select
				B.LOGIN_LOG_ID AS LOGIN_LOG_ID,
				B.USER_ID AS USER_ID,
				B.LOGIN_DT AS LOGIN_DT,
				B.LOGOUT_DT AS LOGOUT_DT,
				C.USER_LOGIN_ID AS USER_LOGIN_ID,
				C.USER_NM AS USER_NM
			from
				JCMS_LOGIN_LOG B
			left join
				JCMS_USER C
			on
				B.USER_ID = C.USER_ID) E
		on
			A.LOGIN_LOG_ID = E.LOGIN_LOG_ID
		left join
			(select * from JCMS_PGM D
				where DEL_ID IS NULL) D
		on
			A.REQ_URL = D.PGM_URL
		where
			A.LOGIN_LOG_ID IS NOT NULL
			<if test="!empty(loginLog.loginLogId)" > and A.LOGIN_LOG_ID = #{loginLog.loginLogId} </if>
			<if test="!empty(user.userId)" > and E.USER_ID = #{user.userId} </if>
			<if test="!empty(userLoginIdLike)" > and E.USER_LOGIN_ID LIKE '%'|| #{userLoginIdLike}|| '%' </if>
			<if test="!empty(userNmLike)" > and E.USER_NM LIKE '%'|| #{userNmLike}|| '%' </if>
	</select>
	
	<insert id="insert" parameterType="webLog" >
		<selectKey keyProperty="webLogId" resultType="java.lang.Long" order="BEFORE" >
	    	select WEB_LOG_ID_SEQ.NEXTVAL from DUAL
	  	</selectKey>	
		insert into JCMS_WEB_LOG (
			WEB_LOG_ID,
			LOGIN_LOG_ID,
			REQ_TP,
			REQ_URL,
			REQ_MTHD,
			REQ_PARAM,
			REQ_IP,
			REG_DT
		) values (
			#{webLogId},
			#{loginLog.loginLogId,jdbcType=NUMERIC},
			#{reqTp},
			#{reqUrl},
			#{reqMthd},
			#{reqParam},
			#{reqIp},
			SYSDATE
		)		
	</insert>
</mapper>
