<?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.security.mapper.LoginLogMapper">

	<resultMap id="loginLogUser" type="loginLog">
		<constructor />
		<id property="loginLogId" column="LOGIN_LOG_ID" />
		<result property="userId" column="USER_ID" />
		<result property="loginIp" column="LOGIN_IP" />
		<result property="loginDt" column="LOGIN_DT" />
		<result property="logoutDt" column="LOGOUT_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>
	</resultMap>
	
	<select id="selectList" parameterType="loginLogSearch" resultMap="loginLogUser">
		SELECT *
		FROM (
			SELECT ROWNUM RNUM , LIST.*
			FROM (
				SELECT A.*, B.USER_LOGIN_ID, B.USER_NM
				FROM JCMS_LOGIN_LOG A
					LEFT JOIN JCMS_USER B
					ON A.USER_ID = B.USER_ID
				WHERE B.USER_TY_CD != 'S'
					<if test="!empty(user.userId)" > AND A.USER_ID = #{user.userId} </if>
					<if test="!empty(userLoginIdLike)" > AND B.USER_LOGIN_ID LIKE '%'|| #{userLoginIdLike}|| '%' </if>
					<if test="!empty(userNmLike)" > AND B.USER_NM LIKE '%'|| #{userNmLike}|| '%' </if>
					<if test="!empty(loginIpLike)" > AND A.LOGIN_IP LIKE '%'|| #{loginIpLike}|| '%' </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="count" parameterType="loginLogSearch" resultType="java.lang.Integer">
		SELECT COUNT(*)
		FROM JCMS_LOGIN_LOG A
			LEFT JOIN JCMS_USER B ON A.USER_ID = B.USER_ID
		WHERE B.USER_TY_CD != 'S'
		<if test="!empty(user.userId)" > AND A.USER_ID = #{user.userId}</if>
		<if test="!empty(userLoginIdLike)" > AND B.USER_LOGIN_ID LIKE '%'|| #{userLoginIdLike}|| '%' </if>
		<if test="!empty(userNmLike)" > AND B.USER_NM LIKE '%'|| #{userNmLike}|| '%' </if>
		<if test="!empty(loginIpLike)" > AND A.LOGIN_IP LIKE '%'|| #{loginIpLike}|| '%' </if>
	</select>
	
	<insert id="insert" parameterType="loginLog" >
		<selectKey keyProperty="loginLogId" resultType="java.lang.Long" order="BEFORE">
	    	select LOGIN_LOG_ID_SEQ.NEXTVAL AS loginLogId from DUAL
	  	</selectKey>
			insert into JCMS_LOGIN_LOG (
				LOGIN_LOG_ID,
				USER_ID,
				LOGIN_IP,
				LOGIN_DT
			) values (
				#{loginLogId},
				#{user.userId},
				#{loginIp},
				SYSDATE
			)		
	</insert>
	
	<update id="update" parameterType="loginLog">
		update JCMS_LOGIN_LOG set
			LOGOUT_DT = SYSDATE
		where
			LOGIN_LOG_ID = #{loginLogId}
	</update>
</mapper>
