<?xml version="1.0" encoding="UTF-8"?><!--Converted at: Wed May 11 15:49:38 KST 2016-->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="nlib.user.service.impl.MemberDAO">
    <!-- <resultMap type="nlib.user.service.NlibLoginVO" id="NlibLoginVO">
		<result column="MB_INFO_ID" property="mbInfoId"/>
		<result column="LOGIN_USER_ID" property="loginUserId"/>
		<result column="NAME" property="name"/>
		<result column="PASSWORD" property="password"/>
		<result column="BIRTHDAY" property="birthday"/>
		<result column="GENDER" property="gender"/>
		<result column="REG_ID" property="regId"/>
		<result column="MOD_ID" property="modId"/>
		<result column="MOBILE_NO" property="mobileNo"/>
		<result column="EMAIL" property="email"/>
		<result column="ZIP_CODE" property="zipCode"/>
		<result column="ADDRESS" property="address"/>
		<result column="ADDRESS_DETAIL" property="addressDetail"/>
		<result column="JOIN_DATE" property="joinDate"/>
		<result column="IP" property="IP"/>
		<result column="LAST_ACCESS_DD" property="lastAccessDd"/>
	</resultMap> -->
	
	<!-- 회원가입 -->
    <insert id="insertMemberInfo" parameterType="NlibLoginVO">
        <selectKey keyProperty="mbInfoId" resultType="String"  order="BEFORE">
            SELECT CONCAT(LEFT(MAX(MB_INFO_ID),2),
                LPAD(RIGHT(MAX(MB_INFO_ID),9)+1,9,0)) AS mbInfoId  FROM MB_INFO;      
        </selectKey> 
        INSERT INTO 
                MB_INFO
                    (
                     MB_INFO_ID
                    ,LOGIN_USER_ID
                    ,NAME
                    ,PASSWORD
                    ,EMAIL
                    ,BIRTHDAY
                    ,GENDER
                    ,MOBILE_NO
                    ,ZIP_CODE
                    ,ADDRESS
                    ,ADDRESS_DETAIL
                    ,REG_ID
                    ,REG_DD
                    ,MOD_ID
                    ,MOD_DD
                    ,STATUS
                    ,JOIN_DATE
                    ,MOBILE_VRFCT_NO
                    ,MOBILE_VRFCT_DD
                    ,IP
                    ,LAST_ACCESS_DD
                    ,INFO_RECV_EMAIL_YN
                    ,INFO_RECV_SMS_YN
                    ,MKT_RECV_EMAIL_YN
                    ,MKT_RECV_SMS_YN
                    )   
            VALUES
                    (
                    #{mbInfoId}
                    ,#{loginUserId}
                    ,#{name}
                    ,#{password}
                    ,#{email}
                    ,#{birthday}
                    ,#{gender}
                    ,#{mobileNo}
                    ,#{zipCode}
                    ,#{address}
                    ,#{addressDetail}
                    ,#{mbInfoId}
                    ,NOW()
                    ,#{mbInfoId}
                    ,NOW()
                    ,'S'
                    ,DATE_FORMAT(CURDATE(), '%Y%m%d')
                    ,#{mobileVrfctNo}
                    ,STR_TO_DATE(#{mobileVrfctDd},'%Y-%m-%d %H:%i:%S')
                    ,#{ip}
                    ,NOW()
                    ,#{infoRecvEmailYn}
                    ,#{infoRecvSmsYn} 
                    ,#{mktRecvEmailYn}
                    ,#{mktRecvSmsYn} 
                    )
    </insert>
    
    <!-- 비밀번호 변경 -->
    <update id="changePassword" parameterType="NlibLoginVO">
            UPDATE
                    MB_INFO
                SET
                    PASSWORD=#{password}
                   ,MOD_ID = #{mbInfoId}
                   ,MOD_DD = NOW()
                WHERE 1=1 
                    AND MB_INFO_ID = #{mbInfoId}
                    
    </update>
    <!-- 회원가입시 SNS인증한 계정 ID와 일치하는 계정이 있는지 체크 -->
    <select id="selectAlreadySignUpId" parameterType="egovframework.com.ext.oauth.service.OAuthUniversalUser" resultType="NlibLoginVO">
            SELECT 
			    MB_INFO_ID
			    ,LOGIN_USER_ID
			    ,NAME
			    ,EMAIL 
			FROM 
		     (
		         SELECT
                    s1.MB_INFO_ID
                    ,s1.LOGIN_USER_ID LOGIN_USER_ID
                    ,s1.EMAIL EMAIL
                    ,s1.NAME
                    ,s1.STATUS
                    FROM 
                    MB_INFO s1, MB_SNS s2
                    WHERE 1=1
                    AND s1.MB_INFO_ID = s2.MB_INFO_ID 
                    AND s1.LOGIN_USER_ID = #{snsUserId} OR s1.EMAIL = #{snsEmail} OR s2.SNS_USER_ID = #{snsUserId}
                    AND s2.UNLINK_DD IS NULL
                    AND s1.STATUS ="S"
		    	) MB
				LIMIT 1;
    </select>
    
    <!-- 회원가입시 입력한 휴대폰번호와 일치하는 계정이 있는지 체크 -->
    <select id="selectAlreadySignUpMobile" parameterType="NlibLoginVO" resultType="NlibLoginVO">
            SELECT
                    MB_INFO_ID
                   ,LOGIN_USER_ID 
                   ,EMAIL
                   ,PASSWORD
                   ,NAME
                FROM 
                    MB_INFO  
                    WHERE 1=1
                    AND MOBILE_NO = #{mobileNo}
                    AND STATUS ="S"
    </select>
    
    <!-- 회원가입시 sns 계정 추가 , 기존 계정 ID와 일치시 통합처리시 추가 -->
    <insert id="insertMemberSns" parameterType="egovframework.com.ext.oauth.service.OAuthUniversalUser">
        INSERT INTO 
            MB_SNS
            (
             MB_INFO_ID
            ,SNS_TYPE
            ,SNS_ID
            ,SNS_NAME
            ,SNS_NICK_NAME
            ,SNS_USER_ID
            ,SNS_GENDER
            ,SNS_BIRTHDAY
            ,SNS_EMAIL
            ,SNS_MOBILE_NO
            ,REG_DD
            ,UNLINK_DD
            )
            VALUES
            (
             #{mbInfoId}
            ,#{snsType}
            ,#{snsId}
            ,#{snsName}
            ,#{snsNickName}
            ,#{snsUserId}
            ,#{snsGender}
            ,#{snsBirthday}
            ,#{snsEmail}
            ,#{snsMobileNo}
            ,NOW()
            ,NULL
            );
        
    </insert>
    
    <!-- SnsId로 기가입자인지 판별  -->
    <select id="selectSnsIdCount" parameterType="egovframework.com.ext.oauth.service.OAuthUniversalUser" resultType="int">
            SELECT
                 COUNT(*)
                FROM 
                    MB_SNS
                    WHERE 1=1
                    AND SNS_ID = #{snsId}
                    AND SNS_TYPE = #{snsType}
                    AND UNLINK_DD IS NULL
                    
    </select>
    
    <!-- 회원 탈퇴  -->
    <update id="deleteMembership" parameterType="NlibLoginVO">
    /* deleteMembership */
            UPDATE
                  MB_INFO
              SET
                  LEAVE_SITE_DATE = DATE_FORMAT(CURDATE(), '%Y%m%d')
                 ,LOGIN_USER_ID= CONCAT(#{loginUserId},'-',#{mbInfoId})
                 ,STATUS = "D"
                 ,BIRTHDAY = NULL
                 ,ZIP_CODE = NULL
                 ,ADDRESS = NULL
                 ,ADDRESS_DETAIL = NULL
                 ,GENDER = NULL
                 ,MOBILE_NO = "-"
                 ,EMAIL = "-"
                 ,PASSWORD = "-"                      
                 WHERE 1=1
                 AND MB_INFO_ID = #{mbInfoId}
    </update>
    
    <!-- sms 발송 -->
    <insert id="sendSms" parameterType="NlibSmsVO">
        /* MemberDAO.sendSms */
        INSERT INTO biz_msg ( CMID, REQUEST_TIME, SEND_TIME, DEST_PHONE, SEND_PHONE, MSG_BODY, NATION_CODE)
			VALUES (#{cmid}, NOW(), NOW(), #{destPhone}, #{sendPhone},#{msgBody},'82') 
    </insert>
</mapper>