<?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.UserInfoDAO">
    <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="MOBILE_NO" property="mobileNo"/>
        <result column="ZIP_CODE" property="zipCode"/>
        <result column="ADDRESS" property="address"/>
        <result column="ADDRESS_DETAIL" property="addressDetail"/>
        <result column="GENDER" property="gender"/>
        <result column="REG_ID" property="regId"/>
        <result column="MOD_ID" property="modId"/>
    </resultMap>
    <resultMap type="egovframework.com.ext.oauth.service.OAuthUniversalUser" id="OAuthUniversalUser">
        <result column="MB_SNS_ID" property="mbSnsId"/>
        <result column="MB_INFO_ID" property="mbInfoId"/>
        <result column="SNS_TYPE" property="snsType"/>
        <result column="SNS_ID" property="snsId"/>
        <result column="SNS_USER_ID" property="snsUserId"/>
        <result column="REG_DD" property="regDd"/>
    </resultMap>
    <!-- sns계정 리스트 -->
    <select id="selectSnsSignUpList" parameterType="nlib.user.service.NlibLoginVO" resultMap="OAuthUniversalUser">
            SELECT
                    MB_SNS_ID
                   ,MB_INFO_ID
                   ,SNS_TYPE 
                   ,SNS_ID
                   ,SNS_USER_ID
                   ,REG_DD
                FROM 
                    MB_SNS  
                    WHERE 1=1
                    AND MB_INFO_ID = #{mbInfoId}
                    AND UNLINK_DD IS NULL
    </select>
    
    <!-- 사용자의 정보를 조회한다. -->
    <select id="selectMyInfo" parameterType="nlib.user.service.NlibLoginVO" resultMap="NlibLoginVO">
            SELECT
                    MB_INFO_ID
                   ,LOGIN_USER_ID 
                   ,MOBILE_NO
                   ,PASSWORD
                   ,NAME
                   ,EMAIL
                FROM 
                    MB_INFO  
                    WHERE 1=1
                   <if test='mbInfoId != null and mbInfoId != ""'>
                    AND MB_INFO_ID = #{mbInfoId}
                   </if>
                   <if test='mobileNo != null and mobileNo != ""'>
                    AND MOBILE_NO = #{mobileNo}
                   </if>
                   <if test='loginUserId != null and loginUserId != ""'>
                    AND LOGIN_USER_ID = #{loginUserId}
                   </if>
                   <if test='email != null and email != ""'>
                    AND EMAIL = #{email}
                   </if>
                   LIMIT 1;
    </select>
    
    <!-- 내 정보 수정 -->
    <update id="updateMyInfo" parameterType="nlib.user.service.NlibLoginVO">
    /*  updateMyInfo    */
        UPDATE 
            MB_INFO
           SET
	             NAME=#{name}
	            ,BIRTHDAY=#{birthday}
	            ,MOBILE_NO=#{mobileNo}
	            ,ZIP_CODE=#{zipCode}
	            ,ADDRESS=#{address}
	            ,EMAIL=#{email}
	            ,ADDRESS_DETAIL=#{addressDetail}
	            ,GENDER=#{gender}
	            ,MOD_ID=#{mbInfoId}
	            ,MOD_DD=NOW()
	            ,INFO_RECV_SMS_YN=#{infoRecvSmsYn}
                ,INFO_RECV_EMAIL_YN=#{infoRecvEmailYn}
                ,MKT_RECV_SMS_YN=#{mktRecvSmsYn}
                ,MKT_RECV_EMAIL_YN=#{mktRecvEmailYn}
            WHERE 1=1
               AND MB_INFO_ID=#{mbInfoId}
    </update>
    
    <!-- SNS연동을 해지한다. -->
   <update id="deleteSnsLink" parameterType="egovframework.com.ext.oauth.service.OAuthUniversalUser">
       UPDATE
             MB_SNS
           SET
              UNLINK_DD = NOW()
             ,SNS_ID = '-'
             ,SNS_NAME = NULL
             ,SNS_NICK_NAME = NULL
             ,SNS_USER_ID = NULL
             ,SNS_GENDER = NULL
             ,SNS_BIRTHDAY = NULL
             ,SNS_EMAIL = NULL
             ,SNS_MOBILE_NO = NULL
            WHERE 1=1
            <if test="mbSnsId != null">
                AND MB_SNS_ID = #{mbSnsId};
            </if>
            <if test="mbInfoId != null">
                AND MB_INFO_ID = #{mbInfoId};
            </if>
    </update>

    <!-- 직전 로그인 시간을 조회한다.  -->
    <select id="getLastLogin" parameterType="String" resultType="EgovMap">
        <![CDATA[
        SELECT
            DATE_FORMAT(T.ACC_DATE, '%Y-%m-%d') as dtStr
          , DATE_FORMAT(T.ACC_DATE, '%H:%i:%s') as tmStr
        FROM (SELECT RANK() OVER(ORDER BY ACC_DATE DESC) AS RNK
                   , A.ACC_DATE
              FROM SM_ACC_LOG A
              WHERE USER_ID = #{mbInfoId}
              AND LOGIN_SUCS_YN = 'Y'
              AND SYS_DIV = '02') T
        WHERE RNK < 3 /* 현재 및 직전 로그인 정보 */
        ]]> 
    </select>
    
    <!-- 최근1년이내 접속한 문화원 5 개 -->
    <select id="getVisitedOrgs" parameterType="String" resultType="EgovMap">
        <![CDATA[
		SELECT T2.DEPT_NM AS DEPT_NM
		     , T2.DOMAIN_HOST_NM AS DOMAIN_HOST_NM
		FROM (
			SELECT 
			     DEPT_SEQ, 
			     MAX(ACC_DATE) AS ACC_DATE, 
			     RANK() OVER(ORDER BY MAX(ACC_DATE) DESC) AS RNK
			FROM SM_ACC_LOG
			     WHERE USER_ID = #{mbInfoId}
			     AND ACC_DATE > DATE_SUB(NOW(), INTERVAL 1 YEAR)
			GROUP BY DEPT_SEQ    
		) T1 
		INNER JOIN SM_DEPT T2 ON (T1.DEPT_SEQ = T2.DEPT_SEQ)
		WHERE RNK < 6
		ORDER BY RNK 
		]]>   
    </select>


</mapper>