<?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.uplus.ez.api.login.LoginMapper">

	<select id="getByUsername" parameterType="string" resultType="kr.co.uplus.ez.api.login.dto.AuthUser">
		/* login-mapper.xml(getByUsername) */
		SELECT
			T1.OPRTR_ID
			, T1.OPRTR_NM
			, T1.STTUS_CD
			, CONCAT('{noop}',T1.PWD) AS PWD
			, T1.EMAIL
			, T1.HP_NO
			, T1.AUT_CD
			, T1.AUTHCHR_FAIL_CNT
			, T1.LAST_LOGIN_DT
			, T1.LOGIN_FAIL_CNT
			, T1.PWD_CHG_DT
			, T2.USER_CODE
			, T2.NAME
			, T2.STATUS
		FROM
			hubez_admin.EZ_ADM_USER T1
			LEFT JOIN  hubez_imdb.EZ_IM_USER T2  ON T1.OPRTR_ID = T2.LOGIN_ID
		WHERE
			T1.OPRTR_ID = #{oprtrId}
	</select>

	<update id="increaseFailCount" parameterType="string">
		/* login-mapper.xml(increaseFailCount) */
		UPDATE
			hubez_admin.EZ_ADM_USER
		SET
			LOGIN_FAIL_CNT = IFNULL(LOGIN_FAIL_CNT, 0) + 1
		WHERE
			OPRTR_ID = #{oprtrId}
	</update>

	<update id="increaseAuthFailCnt" parameterType="string">
		/* login-mapper.xml(increaseAuthFailCnt) */
		UPDATE
			hubez_admin.EZ_ADM_USER
		SET
			AUTHCHR_FAIL_CNT = IFNULL(AUTHCHR_FAIL_CNT, 0) + 1
		WHERE
			OPRTR_ID = #{oprtrId}
	</update>

	<update id="setUserStatus" parameterType="kr.co.uplus.ez.api.login.dto.AuthUser">
		/* login-mapper.xml(setUserStatus) */
		UPDATE
			hubez_admin.EZ_ADM_USER
		SET
			STTUS_CD = #{sttusCd}
		WHERE
			OPRTR_ID = #{oprtrId}
	</update>

	<update id="setLoginInfo" parameterType="string">
		/* login-mapper.xml(setLoginInfo) */
		UPDATE
			hubez_admin.EZ_ADM_USER
		SET
			LAST_LOGIN_DT = DATE_FORMAT(NOW(), '%Y%m%d%H%i%s')
			, LOGIN_FAIL_CNT = 0
			, AUTHCHR_FAIL_CNT = 0
		WHERE
			OPRTR_ID = #{oprtrId}
	</update>

	<select id="getUser" parameterType="kr.co.uplus.ez.api.login.dto.AuthUser" resultType="kr.co.uplus.ez.api.login.dto.AuthUser">
		/* login-mapper.xml(getUser) */
		SELECT
			T1.OPRTR_ID
			, T1.OPRTR_NM
			, T1.PWD
			, T1.STTUS_CD
			, T1.HP_NO
			, T1.AUTHCHR_FAIL_CNT
		    , T1.LOGIN_FAIL_CNT
			, CASE
				WHEN DATE_ADD(T1.PWD_CHG_DT, INTERVAL 90 DAY) <![CDATA[ < ]]>  NOW() THEN 'Y'
			  	ELSE 'N'
			  END AS PW_CHANGE_CHECK
			, T1.AUT_CD
			, T1.TMP_PWD_YN
			, T1.PWD_OLD
		FROM
			hubez_admin.EZ_ADM_USER	T1
			LEFT JOIN  hubez_imdb.EZ_IM_USER T2  ON T1.OPRTR_ID = T2.LOGIN_ID
		WHERE 1 = 1
		<if test="oprtrId != null and oprtrId != ''">
			AND T1.OPRTR_ID  = #{oprtrId}
		</if>
		<if test="pwd != null and pwd != ''">
			AND T1.PWD = #{pwd}
		</if>
	</select>

	<insert id="addAuthNum" parameterType="kr.co.uplus.ez.api.login.dto.AuthNum">
		/* login-mapper.xml(addAuthNum) */
		INSERT INTO	hubez_common.EZ_AUTH_CHR
		(
			AUTH_TP_CD
			,STTUS_CD
			,HP_NO
			,CHR_VAL
			,EXP_DT
			,REG_ID
			,REG_DT
			,CHG_ID
			,CHG_DT
		)VALUES(
			#{authTpCd}
			,#{sttusCd}
			,#{hpNo}
			,#{chrVal}
			,DATE_FORMAT(DATE_ADD(NOW(), INTERVAL 3 MINUTE), '%Y%m%d%H%i%s')
			,#{regId}
			,DATE_FORMAT(NOW(), '%Y%m%d%H%i%s')
			,#{regId}
			,DATE_FORMAT(NOW(), '%Y%m%d%H%i%s')
		)
	</insert>

	<insert id="insertSendMsg" parameterType="kr.co.uplus.ez.api.login.dto.SendMsgDto">
		INSERT
		INTO hubez_send.${tableName} (
			 CLIENT_KEY
		   , REQ_PRODUCT
		   , MSG_STATUS
		   , REQ_DATE
		   , CALLBACK_NUMBER
		   , MSG
		   , PHONE
		   , WEB_REQ_ID
		   , ORDER_ID
		)value(
			   #{clientKey}
			, 'SMS'
			, 'ready'
			, NOW()
			, '15445992'
			, #{msg}
			, #{phone}
			, '2'
			, '2'
			)
	</insert>

	<select id="getAuthNum" parameterType="kr.co.uplus.ez.api.login.dto.AuthNum" resultType="kr.co.uplus.ez.api.login.dto.AuthNum">
		/* login-mapper.xml(getAuthNum) */
		SELECT
			T2.SEQ_NO
			,T1.OPRTR_ID
			,T1.AUTHCHR_FAIL_CNT
			,T2.AUTH_TP_CD
			,T2.STTUS_CD
			,T2.HP_NO
			,T2.CHR_VAL
			,T2.EXP_DT
		FROM hubez_admin.EZ_ADM_USER T1
			LEFT JOIN hubez_common.EZ_AUTH_CHR T2
			ON T1.OPRTR_ID = T2.REG_ID
			AND T1.HP_NO = T2.HP_NO
		WHERE T2.AUTH_TP_CD = #{authTpCd}
			AND T2.HP_NO = #{hpNo}
			AND T2.STTUS_CD = #{sttusCd}
			AND T2.EXP_DT >= NOW()
		ORDER BY T2.SEQ_NO DESC
		LIMIT 1
	</select>

	<update id="setAuthUserInfo" parameterType="kr.co.uplus.ez.api.login.dto.AuthNum">
		/* login-mapper.xml(setAuthUserInfo) */
		UPDATE
			hubez_admin.EZ_ADM_USER
		SET
			AUTHCHR_FAIL_CNT = #{authchrFailCnt}
		WHERE
			OPRTR_ID  = #{oprtrId}
	</update>

	<update id="setAuthNum" parameterType="kr.co.uplus.ez.api.login.dto.AuthNum">
		/* login-mapper.xml(setAuthNum) */
		UPDATE
			hubez_common.EZ_AUTH_CHR
		SET
			STTUS_CD = #{sttusCd}
		WHERE
			SEQ_NO  = #{seqNo}
	</update>

	<update id="updateAdmUser" parameterType="kr.co.uplus.ez.api.login.dto.AuthUser">
		/* login-mapper.xml(updateAdmUser) */
		UPDATE
			hubez_admin.EZ_ADM_USER
		SET
				CHG_ID = #{oprtrId}
				, CHG_DT = DATE_FORMAT(NOW(), '%Y%m%d%H%i%s')
			<if test="oprtrNm != null and oprtrNm != ''">
				, OPRTR_NM = #{oprtrNm}
			</if>
			<if test="sttusCd != null and sttusCd != ''">
				, STTUS_CD = #{sttusCd}
			</if>
			<if test="pwd != null and pwd != ''">
				, PWD = #{pwd}
			</if>
			<if test="tmpPwdYn != null and tmpPwdYn != ''">
				, TMP_PWD_YN = #{tmpPwdYn}
			</if>
			<if test="email != null and email != ''">
				, EMAIL = #{email}
			</if>
			<if test="hpNo != null and hpNo != ''">
				, HP_NO = #{hpNo}
			</if>
			<if test="autCd != null and autCd != ''">
				, AUT_CD = #{autCd}
			</if>
			<if test="authchrFailCnt != null">
				, AUTHCHR_FAIL_CNT = #{authchrFailCnt}
			</if>
			<if test="lastLoginDt != null and lastLoginDt != ''">
				, LAST_LOGIN_DT = NOW()
			</if>
			<if test="loginFailCnt != null">
				, LOGIN_FAIL_CNT = #{loginFailCnt}
			</if>
			<if test="pwdChgDt != null and pwdChgDt != ''">
				, PWD_CHG_DT = NOW()
			</if>
			<if test="pwdOld != null and pwdOld != ''">
				, PWD_OLD = #{pwdOld}
			</if>
		WHERE
			OPRTR_ID = #{oprtrId}
	</update>
	
	
	<select id="getAuthNumList" parameterType="kr.co.uplus.ez.api.login.dto.AuthNum" resultType="kr.co.uplus.ez.api.login.dto.AuthNum">
		/* login-mapper.xml(getAuthNumList) */
		SELECT
			T2.SEQ_NO
			,T1.OPRTR_ID
			,T1.AUTHCHR_FAIL_CNT
			,T2.AUTH_TP_CD
			,T2.STTUS_CD
			,T2.HP_NO
			,T2.CHR_VAL
			,T2.EXP_DT
			,T2.REG_DT 
	      	,T2.REG_ID 
	      	,T2.CHG_ID 
	      	,T2.CHG_DT
		FROM hubez_admin.EZ_ADM_USER T1
			LEFT JOIN hubez_common.EZ_AUTH_CHR T2
			ON T1.OPRTR_ID = T2.REG_ID
			AND T1.HP_NO = T2.HP_NO
		WHERE 1=1
		<if test="regDt != null and regDt != ''">
			AND T2.REG_DT >= DATE_SUB(#{regDt}, INTERVAL #{searchTime} MINUTE)
		</if>  
		<if test="hpNo != null and hpNo != ''">
			AND T2.HP_NO = #{hpNo}
		</if>
		<if test="sttusCd != null and sttusCd != ''">
			AND T2.STTUS_CD = #{sttusCd}
		</if>
		<if test="authTpCd != null and authTpCd != ''">
			AND T2.AUTH_TP_CD = #{authTpCd}		
		</if>
	</select>
</mapper>