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

	<!-- 로그인 아이디 중복체크 -->
	<select id="checkMembershipId" parameterType="hashmap" resultType="hashmap">
		SELECT MU.MU_USER_ID                        AS "muUserId"
		  FROM MU_USER AS MU
		 WHERE MU.USE_YN = 'Y'
		   AND MU.MEMBERSHIP_ACCOUNT	= #{membershipAccount}
		   AND APP_USE_YN = 'Y'
		   AND APP_STATUS = 'C'
	</select>

	<!-- 기존회원 확인 -->
	<select id="selectUserInfo" parameterType="hashmap" resultType="hashmap">
		SELECT MU_USER_ID						 AS "muUserId"
			  ,APP_STATUS                        AS "appStatus"
		      ,LAST_VISIT_DATE					 AS "lastVisitDate"
			  ,MEMBERSHIP_YN                     AS "membershipYn"
		  FROM MU_USER AS MU
		 WHERE MU.USE_YN = 'Y'
		   AND MU.USER_NAME	= #{userName}
		   AND MU.PHONE_NUMBER	= #{phoneNumber}
	</select>

	<update id="updateUserInfo" parameterType="hashmap">
		UPDATE MU_USER
		   SET MOD_ID              = #{modId}
			  ,MOD_DATE            = NOW()
			  ,CUD_FLAG            = 'U'
			  ,APP_STATUS          = 'C'
			  ,MEMBERSHIP_ACCOUNT  = #{membershipAccount}
			  ,MEMBERSHIP_PASSWORD = #{membershipPassword}
			  ,APP_INTRO_DATE      = NOW()
			  ,APP_USE_YN          = 'Y'
			  ,APP_INTRO_TYPE 	   = #{appIntroType}
              ,LEAVE_DATE          = NULL
			<if test="introUserId != null and introUserId != ''">
			  ,INTRO_USER_ID = #{introUserId}
			</if>
		 WHERE USE_YN        = 'Y'
		   AND USER_NAME     = #{userName}
		   AND PHONE_NUMBER  = #{phoneNumber}
	</update>

	<insert id="insertUser" parameterType="hashmap">
		<selectKey resultType="HashMap" keyProperty="userNumber" order="BEFORE">
			SELECT USER.id AS id
			      ,USER.number AS number
			FROM (
					SELECT CONCAT('U'
								, DATE_FORMAT(NOW(), '%Y%m%d')
			   				    , '-'
							    , LPAD(IFNULL(MAX(SUBSTRING(USER_NUMBER, 11)) + 1, 1), 3, '0')
					              ) 															AS number
						 , CONCAT(#{muUserId}
								, LPAD((SELECT NEXTVAL(MU_USER_SEQ)), 11, 0)
						   		 ) 															    AS id
					  FROM MU_USER AS MU
			         WHERE USER_NUMBER LIKE CONCAT('U', DATE_FORMAT(NOW(), '%Y%m%d'), '%')
				 ) AS USER
		</selectKey>
		INSERT INTO MU_USER (
		MU_USER_ID
		,USER_NUMBER
		,USER_NAME
		,LOGIN_TYPE
		,PHONE_NUMBER
		,MEMBERSHIP_YN
		,MEMBERSHIP_ACCOUNT
		,MEMBERSHIP_PASSWORD
		,APP_INTRO_DATE
		,INTRO_USER_ID
		,APP_USE_YN
		,APP_INTRO_TYPE
		,APP_STATUS
		,JOIN_DATE
		,SMS_YN
		,WRITE_DATE
		,WRITE_TIME
		,REMARK
		,CUD_FLAG
		,USE_YN
		,REG_ID
		,REG_DATE
		,MOD_ID
		,MOD_DATE
		) VALUES (
		#{userNumber.id}
		,#{userNumber.number}
		,#{userName}
		,#{loginType}
		,#{phoneNumber}
		,'N'
		,#{membershipAccount}
		,#{membershipPassword}
		,now()
		,#{introUserId}
		,'Y'
		,#{appIntroType}
		,'C'
		,NOW()
		,'Y'
		,NOW()
		,NOW()
		,#{remark}
		,'C'
		,'Y'
		,#{regId}
		,NOW()
		,#{modId}
		,NOW()
		)
	</insert>
</mapper>