<?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="MobilePhoneAuth">

	<!-- 휴대폰 인증 수 조회(당일) -->
	<select id="selectListPhoneAuthCountToday" resultType="hashmap" parameterType="hashmap">
		SELECT COUNT(MPA.MU_PHONE_AUTH_ID)	AS "totalCount"
	 	  FROM MU_PHONE_AUTH AS MPA
	 	 WHERE MPA.USE_YN = 'Y'
	 	   AND MPA.PHONE_NUMBER = #{phoneNumber}
	  	   AND MPA.SEND_DATE = DATE_FORMAT(NOW(), '%Y-%m-%d')
	</select>

	<!-- 휴대폰 인증 확인-->
	<select id="selectPhoneAuthCheck" resultType="hashmap" parameterType="hashmap">
		SELECT COUNT(*) AS "successCount"
		  FROM (
				   SELECT MPA.AUTH_NUMBER AS AUTH_NUMBER
					 FROM MU_PHONE_AUTH AS MPA
					WHERE MPA.USE_YN = 'Y'
					  AND MPA.PHONE_NUMBER = #{phoneNumber}
					  AND MPA.REG_DATE >= DATE_SUB(NOW(), INTERVAL 3 MINUTE)  <!-- 현재 시간보다 3분 이내인 데이터만 조회 -->
					ORDER BY MPA.REG_DATE DESC
					LIMIT 0, 1
			   ) AS AA
		 WHERE AA.AUTH_NUMBER = #{authNumber};
	</select>

	<!-- 인증번호 등록 -->
	<insert id="insertPhoneAuth" parameterType="hashmap">
		<selectKey resultType="string" keyProperty="id" order="BEFORE">
			SELECT CONCAT(#{id},LPAD((SELECT NEXTVAL(MU_PHONE_AUTH_SEQ)), 10, 0))
		</selectKey>
		INSERT INTO MU_PHONE_AUTH (
					MU_PHONE_AUTH_ID
				   ,MU_SMS_ID
				   ,AUTH_TYPE
				   ,MU_USER_ID
				   ,PHONE_NUMBER
				   ,AUTH_NUMBER
				   ,SEND_DATE
				   ,WRITE_DATE
				   ,CUD_FLAG
				   ,USE_YN
				   ,REG_ID
				   ,REG_DATE
				   ,MOD_ID
				   ,MOD_DATE
				   ,T_ID
				   ,T_DATE
		)VALUES(
					#{id}
				   ,#{sbSmsId, jdbcType=VARCHAR}
				   ,#{authType, jdbcType=VARCHAR}
				   ,#{sbUserId, jdbcType=VARCHAR}
				   ,#{phoneNumber, jdbcType=VARCHAR}
				   ,#{authNumber, jdbcType=VARCHAR}
				   ,NOW()
				   ,NOW()
				   ,'C'
				   ,'Y'
				   ,'SYSTEM'
				   ,NOW()
				   ,'SYSTEM'
				   ,NOW()
				   ,#{tId}
				   ,NOW()
		)
	</insert>

</mapper>