<?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="MobileLogin">
	
	<!-- 사용자 로그인 -->
	<select id="userLogin" parameterType="hashmap" resultType="hashmap">
		SELECT MU.MU_USER_ID                          AS "muUserId"
			  ,MU.MEMBERSHIP_ACCOUNT                  AS "loginId"
			  ,MU.MEMBERSHIP_PASSWORD                 AS "password"
			  ,MU.USER_NAME                           AS "name"
			  ,MU.PHONE_NUMBER                        AS "phoneNumber"
			  ,MU.APP_USE_YN                          AS "appUseYn"
		  FROM MU_USER AS MU
		 WHERE MU.USE_YN  = 'Y'
		   AND APP_USE_YN = 'Y'
		   AND APP_STATUS = 'C'
		   AND MU.MEMBERSHIP_ACCOUNT = #{loginId}
		   AND MU.MEMBERSHIP_PASSWORD = #{password}
	</select>

	<!-- 사용자 로그인 -->
	<select id="snsLogin" parameterType="hashmap" resultType="hashmap">
		SELECT MU.MU_USER_ID                          AS "muUserId"
			  ,MU.MEMBERSHIP_ACCOUNT                  AS "loginId"
			  ,MU.MEMBERSHIP_PASSWORD                 AS "password"
			  ,MU.USER_NAME                           AS "name"
			  ,MU.PHONE_NUMBER                        AS "phoneNumber"
			  ,MU.APP_USE_YN                          AS "appUseYn"
		  FROM MU_USER AS MU
		 WHERE MU.USE_YN  = 'Y'
		   AND APP_USE_YN = 'Y'
		   AND APP_STATUS = 'C'
		   AND MU.MEMBERSHIP_ACCOUNT = #{loginId}
		   AND MU.LOGIN_TYPE = #{loginType}
	</select>

	<!-- 로그인 아이디 중복체크 -->
	<select id="checkloginId" 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	= #{loginId}
		   AND APP_USE_YN = 'Y'
		   AND APP_STATUS = 'C'
	</select>
	
	<update id="updateLastLoginDate" parameterType="hashmap">
		UPDATE MU_USER
		   SET MOD_DATE              = NOW()
			  ,MOD_ID                = #{modId}
			  ,LAST_LOGIN_DATE       = NOW()
		 WHERE USE_YN          = 'Y'
		   AND MU_USER_ID    = #{muUserId}
	</update>

	<!-- SNS 연동 수정 -->
	<update id="updateSnsLoginYN" parameterType="hashmap">
		UPDATE MU_USER
		SET MOD_ID               = #{modId}
		,MOD_DATE             = NOW()
		,CUD_FLAG             = 'U'
		<if test="type eq 'K'.toString()">
			,KAKAO_YN 			= 'Y'
			,KAKAO_ID 			= #{email}
		</if>
		<if test="type eq 'A'.toString()">
			,APPLE_YN 			= 'Y'
			,APPLE_ID 			= #{email}
		</if>
		<if test="type eq 'G'.toString()">
			,GOOGLE_YN 			= 'Y'
			,GOOGLE_ID 			= #{email}
		</if>
		<if test="type eq 'N'.toString()">
			,NAVER_YN 			= 'Y'
			,NAVER_ID 			= #{email}
		</if>
		WHERE USE_YN               = 'Y'
		AND MU_USER_ID           = #{muUserId}
		AND EMAIL				= #{email}
	</update>

	<update id="deleteUser" parameterType="hashmap">
		UPDATE MU_USER
		   SET MOD_ID            = #{modId}
			 , MOD_DATE	         = NOW()
			 , APP_STATUS 		 = 'D'
		     , LEAVE_DATE        = CURDATE()
		 WHERE USE_YN        = 'Y'
		   AND MU_USER_ID    = #{muUserId}
	</update>

	<update id="deleteDevice" parameterType="hashmap">
		UPDATE MU_DEVICE
		   SET MOD_ID            = #{modId}
			 , MOD_DATE	         = NOW()
			 , USE_YN 		 = 'N'
		 WHERE USE_YN        = 'Y'
		   AND MU_USER_ID    = #{muUserId}
	</update>
</mapper>