<?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="com.style24.front.biz.dao.TsfLoginDao">

	<!-- 로그인체크 정보 조회 -->
	<select id="getLoginCheckInfo" parameterType="Login" resultType="Login">
		/* TsfLogin.getLoginCheckInfo */
		SELECT CUST_NO                                      --고객번호
		     , CUST_ID                                      --고객ID
		     , CUST_NM                                      --고객명
		     , PASSWD                                       --비밀번호
		     , CUST_GB                                      --고객구분
		     , FN_GET_CODE_NM('G100',CUST_GB) AS CUST_GB_NM --고객구분명
		     , CUST_STAT                                    --회원상태
		     , EMAIL                                        --이메일
		     , SNS_TYPE                                     --SNS유형
		     , SNS_JOIN_ID                                  --SNS가입ID
		     , NVL((SELECT LOGIN_FAIL_CNT
		            FROM   TB_LOGIN_FAIL
		            WHERE  LOGIN_ID = TO_CHAR(A.CUST_NO)
		            AND    IP_ADDR = #{ipAddr}
		            AND    SITE_CD = #{siteCd}
		           ),0)    AS LOGIN_FAIL_CNT                --로그인실패건수
		FROM   TB_CUSTOMER A
		WHERE  CUST_STAT <![CDATA[<>]]> '40'                --전환대상고객제외
		<choose>
		    <when test="snsType != null and snsType != ''"> <!-- SNS로그인 -->
		AND    (
		        SNS_TYPE||'-'||SNS_JOIN_ID = #{custId}
		        OR
		        EMAIL = #{email}
		       )
		    </when>
		    <otherwise> <!-- 일반로그인 -->
		AND    CUST_ID = #{custId}
		    </otherwise>
		</choose>
	</select>

	<!-- 로그인실패 남기기 -->
	<insert id="createLoginFail" parameterType="Login">
		/* TsfLogin.createLoginFail */
		MERGE INTO TB_LOGIN_FAIL
		USING (
		       SELECT TO_CHAR(CUST_NO) AS CUST_NO
		       FROM   TB_CUSTOMER
		       WHERE  CUST_ID = #{custId}
		      ) B
		ON    (
		       LOGIN_ID = B.CUST_NO
		AND    IP_ADDR = #{ipAddr}
		AND    SITE_CD = #{siteCd}
		      )
		WHEN MATCHED THEN
		    UPDATE
		    SET    LOGIN_FAIL_CNT = DECODE(#{loginFailYn},'Y',LOGIN_FAIL_CNT + 1,0)
		         , UPD_NO = B.CUST_NO
		         , UPD_DT = NOW()
		WHEN NOT MATCHED THEN
		    INSERT (LOGIN_ID, IP_ADDR, SITE_CD, LOGIN_FAIL_CNT, REG_ID, REG_DT, UPD_NO, UPD_DT)
		    VALUES (B.CUST_NO, #{ipAddr}, #{siteCd}, 1, B.CUST_NO, NOW(), B.CUST_NO, NOW())
	</insert>

	<!-- 로그인 실패건수 조회 -->
	<select id="getLoginFailCount" parameterType="Login" resultType="int">
		/* TsfLogin.getLoginFailCount */
		SELECT NVL((SELECT LOGIN_FAIL_CNT
		            FROM   TB_LOGIN_FAIL
		            WHERE  LOGIN_ID = #{custNo}
		            AND    IP_ADDR = #{ipAddr}
		            AND    SITE_CD = #{siteCd}
		           ),0)
		FROM   DUAL
	</select>

	<!-- 최종로그인일시 Update -->
	<update id="updateLastLoginDate" parameterType="Integer">
		/* TsfLogin.updateLastLoginDate */
		UPDATE TB_CUSTOMER
		SET    LOGIN_LDT = NOW()
		     , UPD_NO = #{custNo}
		     , UPD_DT = NOW()
		WHERE  CUST_NO = #{custNo}
	</update>

	<!-- 로그인이력 남기기 -->
	<insert id="createLoginHistory" parameterType="Login">
		/* TsfLogin.createLoginHistory */
		INSERT INTO TB_LOGIN_HST (
		       LOGIN_HST_SQ
		     , LOGIN_ID
		     , IP_ADDR
		     , SITE_CD
		     , FRONT_GB
		     , LOGIN_DT
		     , REG_ID
		     , REG_DT
		)
		VALUES (
		       SEQ_LOGIN_HST.NEXTVAL
		     , #{custNo}
		     , #{ipAddr}
		     , #{siteCd}
		     , #{frontGb}
		     , NOW()
		     , #{custNo}
		     , NOW()
		)
	</insert>

	<!-- 로그인 세션정보 조회 -->
	<select id="getLoginSessionInfo" parameterType="Login" resultType="Login">
		/* TsfLogin.getLoginSessionInfo */
		SELECT CUST_NO                                                --고객번호
		     , CUST_ID                                                --고객ID
		     , CUST_NM                                                --고객명
		     , PASSWD                                                 --비밀번호
		     , CELL_PHNNO                                             --휴대전화번호
		     , EMAIL                                                  --이메일
		     , CUST_GB                                                --회원구분코드
		     , FN_GET_CODE_NM('G100',CUST_GB)        AS CUST_GB_NM    --회원구분명
		     , CUST_GRADE                                             --회원등급코드
		     , FN_GET_CODE_NM('G110',CUST_GRADE)     AS CUST_GRADE_NM --회원등급명
		     , SITE_CD                                                --사이트코드
		     , TO_CHAR(LOGIN_LDT,'YYYYMMDDHH24MISS') AS LOGIN_LDT     --최종로그인일시
		     , MANAGED_RSN                                            --관리대상지정사유(공통코드G120)
		     , CASE WHEN ADD_MONTHS(PASSWD_CHG_DT,6) <![CDATA[<]]> NOW() THEN 'Y'
		            ELSE 'N'
		       END                                   AS PASSWD_CHG_YN --비밀번호변경도래여부
		FROM   TB_CUSTOMER A
		WHERE  CUST_ID = #{custId}
		AND    CUST_STAT = '10' --활동회원
	</select>

	<!-- 로그인유지토큰 생성 -->
	<insert id="createPersistentToken" parameterType="PersistentToken">
		/* TsfLogin.createPersistentToken */
		MERGE INTO TB_PERSISTENT_TOKEN
		USING DUAL
		ON    (
		       CUST_NO = #{custNo}
		       AND
		       REMEMBERME_TOKEN = #{remembermeToken}
		)
		WHEN MATCHED THEN
		    UPDATE
		    SET    LIMIT_DT = #{limitDt}
		WHEN NOT MATCHED THEN
		    INSERT (
		            CUST_NO
		          , REMEMBERME_TOKEN
		          , LIMIT_DT
		    )
		    VALUES (
		           #{custNo}
		         , #{remembermeToken}
		         , NOW()
		    )
	</insert>

	<!-- 로그인유지토큰 조회 -->
	<select id="getPersistentToken" parameterType="String" resultType="PersistentToken">
		/* TsfLogin.getPersistentToken */
		SELECT PT.CUST_NO
		     , PT.REMEMBERME_TOKEN
		     , PT.LIMIT_DT
		     , C.CUST_ID
		FROM   TB_PERSISTENT_TOKEN PT
		     , TB_CUSTOMER C
		WHERE  PT.CUST_NO = C.CUST_NO
		AND    PT.REMEMBERME_TOKEN = #{remembermeToken}
		AND    NOW() <![CDATA[<=]]> PT.LIMIT_DT
	</select>

	<!-- 로그인유지토큰 갱신 -->
	<update id="updatePersistentToken" parameterType="PersistentToken">
		/* TsfLogin.updatePersistentToken */
		UPDATE TB_PERSISTENT_TOKEN
		SET    REMEMBERME_TOKEN = #{remembermeToken}
		     , LIMIT_DT = #{limitDt}
		WHERE  CUST_NO = #{custNo}
		AND    REMEMBERME_TOKEN = #{prevRemembermeToken}
	</update>

	<!-- 로그인유지토근 삭제 -->
	<delete id="deletePersistentToken" parameterType="String">
		/* TsfLogin.deletePersistentToken */
		DELETE FROM TB_PERSISTENT_TOKEN
		WHERE  REMEMBERME_TOKEN = #{remembermeToken}
	</delete>

</mapper>