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

    <select id="checkUser" parameterType="hashMap" resultType="hashMap">
        SELECT  MU.USER_NAME        AS "userName"
               ,MU.PHONE_NUMBER     AS "phoneNumber"
        FROM MU_USER AS MU
        WHERE MU.MU_USER_ID  = #{muUserId}
    </select>

    <select id="checkUser2" parameterType="hashMap" resultType="hashMap">
        SELECT  MU.MU_USER_ID   AS muUserId
               ,MU.USER_NAME    AS userName
        FROM MU_USER AS MU
        WHERE MU.MEMBERSHIP_ACCOUNT  = #{membershipAccount}
    </select>

    <select id="selectUserAccount" parameterType="hashmap" resultType="hashmap">
        SELECT MU.MEMBERSHIP_ACCOUNT                        AS 'membershipAccount'
             , MU.PHONE_NUMBER                              AS 'phoneNumber'
             , MU.EMAIL                                     AS 'email'
             , CASE
               WHEN MU.LOGIN_TYPE = 'MU' THEN '-'
               WHEN MU.LOGIN_TYPE = 'K' THEN '카카오'
               WHEN MU.LOGIN_TYPE = 'N' THEN '네이버'
               WHEN MU.LOGIN_TYPE = 'A' THEN '애플'
               ELSE '-'
               END                                          AS 'loginType'
             , MU.LOGIN_TYPE                                AS 'email'
             ,(
                  SELECT USER_NAME
                    FROM MU_USER
                   WHERE MU_USER_ID = MU.INTRO_USER_ID
              )                                             AS 'introUserName'
          FROM MU_USER MU
         WHERE MU.MU_USER_ID = #{muUserId}
           AND MU.APP_USE_YN = 'Y'
           AND MU.USE_YN = 'Y'
    </select>

    <!--이메일 계정 중복 확인-->
    <select id="selectEmailCheck" parameterType="hashmap" resultType="hashmap">
        SELECT MU.MU_USER_ID                                AS "muUserId"
              ,MU.MEMBERSHIP_ACCOUNT                        AS "membershipAccount"
              ,MU.PHONE_NUMBER                              AS "phoneNumber"
              ,MU.EMAIL                                     AS "email"
          FROM MU_USER MU
         WHERE MU.MEMBERSHIP_ACCOUNT = #{email}
           AND MU.LOGIN_TYPE = #{loginType}
           AND MU.APP_USE_YN = 'Y'
           AND MU.USE_YN = 'Y'
    </select>

    <select id="selectUserPhoneNumber" parameterType="hashmap" resultType="String">
        SELECT MU.PHONE_NUMBER                              AS 'phoneNumber'
          FROM MU_USER MU
         WHERE MU.MEMBERSHIP_ACCOUNT = #{membershipAccount}
           AND MU.APP_USE_YN = 'Y'
           AND MU.USE_YN = 'Y'
    </select>

    <select id="selectUserId" parameterType="hashmap" resultType="hashmap">
        SELECT MU.MEMBERSHIP_ACCOUNT                        AS 'membershipAccount'
             , DATE_FORMAT(MU.JOIN_DATE,'%Y-%m-%d')         AS 'joinDate'
             , DATE_FORMAT(MU.MOD_DATE,'%Y-%m-%d')          AS 'modDate'
          FROM MU_USER MU
         WHERE MU.USER_NAME = #{userName}
           AND MU.PHONE_NUMBER = #{phoneNumber}
           AND MU.APP_USE_YN = 'Y'
           AND MU.USE_YN = 'Y'
    </select>

    <select id="selectUserPassword" parameterType="hashMap" resultType="hashMap">
        SELECT count(*)    AS "totalCount"
          FROM MU_USER AS MM
         WHERE MM.USE_YN                   = 'Y'
           AND MM.MU_USER_ID               = #{muUserId}
           AND MM.MEMBERSHIP_PASSWORD      = #{password}
    </select>

    <update id="updatePassword" parameterType="hashmap">
        UPDATE MU_USER
           SET MOD_ID              = #{modId}
             ,MOD_DATE             = NOW()
             ,CUD_FLAG             = 'U'
             ,MEMBERSHIP_PASSWORD  = #{password}
         WHERE USE_YN        = 'Y'
           AND MU_USER_ID  = #{muUserId}
    </update>

    <update id="updateUserPassword" parameterType="hashmap">
        UPDATE MU_USER
           SET MOD_ID              = #{modId}
             ,MOD_DATE             = NOW()
             ,CUD_FLAG             = 'U'
             ,MEMBERSHIP_PASSWORD  = #{password}
         WHERE USE_YN        = 'Y'
           AND MEMBERSHIP_ACCOUNT  = #{membershipAccount}
           AND PHONE_NUMBER        = #{phoneNumber}
    </update>

    <update id="updateEmail" parameterType="hashmap">
        UPDATE MU_USER
           SET MOD_ID              = #{modId}
             ,MOD_DATE             = NOW()
             ,CUD_FLAG             = 'U'
             ,EMAIL                = #{email}
             ,PHONE_NUMBER         = #{phoneNumber}
         WHERE USE_YN        = 'Y'
           AND MU_USER_ID  = #{muUserId}
    </update>
</mapper>