<?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="com.style24.core.biz.dao.TscCustomerDao">

	<!-- 회원기본정보 -->
	<select id="getCustomerInfo" parameterType="Customer" resultType="Customer">
		/* TscCustomer.getCustomerInfo */
		SELECT C.CUST_NO
		     , C.CUST_ID
		     , C.CUST_NM
		     , C.PASSWD
		     , C.BIRTH_YMD
		     , C.SEX_GB
		     , C.CELL_PHNNO
		     , C.APP_AGREE_YN
		     , DATE_FORMAT(C.APP_AGREE_DT, '%Y%m%d%H%i%S') AS APP_AGREE_DT
		     , C.SMS_AGREE_YN
		     , DATE_FORMAT(C.SMS_AGREE_DT, '%Y%m%d%H%i%S') AS SMS_AGREE_DT
		     , C.EMAIL
		     , C.EMAIL_AGREE_YN
		     , DATE_FORMAT(C.EMAIL_AGREE_DT, '%Y%m%d%H%i%S') AS EMAIL_AGREE_DT
		     , C.MK_AGREE_YN
		     , DATE_FORMAT(C.MK_AGREE_DT, '%Y%m%d%H%i%S') AS MK_AGREE_DT
		     , C.HOME_ZIPCODE
		     , C.HOME_BASE_ADDR
		     , C.HOME_DTL_ADDR
		     , C.SITE_CD
		     , C.FRONT_GB
		     , C.AF_LINK_CD
		     , C.CUST_GB
		     , C.CUST_GRADE
		     , FN_GET_CODE_NM('G110', C.CUST_GRADE) AS CUST_GRADE_NM
		     , DATE_FORMAT(C.JOIN_DT, '%Y%m%d%H%i%S') AS JOIN_DT
		     , C.FOREIGNER_YN
		     , C.CUST_STAT
		     , DATE_FORMAT(C.PASSWD_CHG_DT, '%Y%m%d%H%i%S') AS PASSWD_CHG_DT
		     , C.TEMP_PASSWD_YN
		     , DATE_FORMAT(C.LOGIN_LDT, '%Y%m%d%H%i%S') AS LOGIN_LDT
		     , C.CI
		     , C.SNS_TYPE
		     , DATE_FORMAT(C.AUTH_DT, '%Y%m%d%H%i%S') AS AUTH_DT
		     , C.MANAGED_RSN
		     , C.MANAGED_DTL_RSN
		     , DATE_FORMAT(C.MANAGED_DT, '%Y%m%d%H%i%S') AS MANAGED_DT
		     , C.SECEDE_RSN
		     , C.SECEDE_DTL_RSN
		     , DATE_FORMAT(C.SECEDE_DT, '%Y%m%d%H%i%S')   AS SECEDE_DT
		     , FN_GET_USER_NM(C.REG_NO)                   AS REG_NM
		     , DATE_FORMAT(C.REG_DT, '%Y%m%d%H%i%S')      AS REG_DT
		     , FN_GET_USER_NM(C.UPD_NO)                   AS UPD_NM
		     , DATE_FORMAT(C.UPD_DT, '%Y%m%d%H%i%S')      AS UPD_DT
		     , (
		        SELECT DATE_FORMAT(MAX(REG_DT), '%Y%m%d%H%i%S')
		        FROM   TB_CUSTOMER_SNS
		        WHERE  CUST_NO = C.CUST_NO
		        AND    SNS_TYPE = 'NV'
		        )                                    AS NV_JOIN_DT
		     , (
		        SELECT DATE_FORMAT(REG_DT, '%Y%m%d%H%i%S')
		        FROM   TB_CUSTOMER_SNS
		        WHERE  CUST_NO = C.CUST_NO
		        AND    SNS_TYPE = 'KK'
		       )                                    AS KK_JOIN_DT
		     , (
		        SELECT DATE_FORMAT(REG_DT, '%Y%m%d%H%i%S')
		        FROM   TB_CUSTOMER_SNS
		        WHERE  CUST_NO = C.CUST_NO
		        AND    SNS_TYPE = 'YS'
		       )                                    AS YS_JOIN_DT
		FROM   TB_CUSTOMER C
		WHERE  C.SITE_CD = #{siteCd}
		<if test="custNo != null and custNo != ''">
		AND    C.CUST_NO = #{custNo}
		</if>
		<if test="custStat != null and custStat != ''">
		AND    C.CUST_STAT = #{custStat}
		</if>
		<if test="encodedEmail != null and encodedEmail != ''">
		AND    C.EMAIL = #{encodedEmail}
		</if>
		<if test="custId != null and custId !=''">
		AND    C.CUST_ID = #{custId}
		</if>
		<if test="encodedCustNm != null and encodedCustNm != ''">
		AND    C.CUST_NM = #{encodedCustNm}
		</if>
		<if test="encodedBirthYmd != null and encodedBirthYmd != ''">
		AND    C.BIRTH_YMD = #{encodedBirthYmd}
		</if>
		<if test="ci != null and ci != ''">
		AND    C.CI = #{ci}
		</if>
	</select>

	<!-- 회원접촉이력 생성 -->
	<insert id="createCustomerContactHistory" parameterType="Customer">
		/* TscCustomer.createCustomerContactHistory */
		INSERT INTO TB_CUST_CONTACT_HST (
		      CONTACT_TYPE
		    , CONTACT_METHOD
		    , CONTACT_CONTENTS
		    , CONTACT_MEMO
		    , SENDER_NO
		    , SEND_DT
		    , RECEIVER_NO
		    , REG_NO
		    , REG_DT
		)
		VALUES (
		      #{contactType}
		    , #{contactMethod}
		    , #{contactContents}
		    , #{contactMemo}
		    , IFNULL(#{senderNo}, 0)
		    , NOW()
		    , #{receiverNo}
		    , IFNULL(#{regNo}, 0)
		    , NOW()
		)
	</insert>

	<!-- 고객이력 생성 -->
	<insert id="createCustomerHistory" parameterType="Customer">
		/* TscCustomer.createCustomerHistory */
		INSERT INTO TB_CUSTOMER_HST (
		       CUST_NO
		     , CUST_ID
		     , CUST_NM
		     , PASSWD
		     , BIRTH_YMD
		     , SEX_GB
		     , CELL_PHNNO
		     , APP_AGREE_YN
		     , APP_AGREE_DT
		     , SMS_AGREE_YN
		     , SMS_AGREE_DT
		     , EMAIL
		     , EMAIL_AGREE_YN
		     , EMAIL_AGREE_DT
		     , MK_AGREE_YN
		     , MK_AGREE_DT
		     , HOME_ZIPCODE
		     , HOME_BASE_ADDR
		     , HOME_DTL_ADDR
		     , SITE_CD
		     , FRONT_GB
		     , AF_LINK_CD
		     , CUST_GB
		     , CUST_GRADE
		     , JOIN_DT
		     , FOREIGNER_YN
		     , CUST_STAT
		     , PASSWD_CHG_DT
		     , TEMP_PASSWD_YN
		     , LOGIN_LDT
		     , CI
		     , AUTH_DT
		     , SNS_TYPE
		     , MANAGED_RSN
		     , MANAGED_DTL_RSN
		     , MANAGED_DT
		     , SECEDE_RSN
		     , SECEDE_DTL_RSN
		     , SECEDE_DT
		     , RM_DORMANT_DT
		     , BIRTH_MM
		     -- , BANK_CD
		     -- , ACCOUNT_NO
		     -- , ACCOUNT_NM
		     , REG_NO
		     , REG_DT
		     , UPD_NO
		     , UPD_DT
		)
		SELECT C.CUST_NO
		     , C.CUST_ID
		     , C.CUST_NM
		     , C.PASSWD
		     , C.BIRTH_YMD
		     , C.SEX_GB
		     , C.CELL_PHNNO
		     , C.APP_AGREE_YN
		     , C.APP_AGREE_DT
		     , C.SMS_AGREE_YN
		     , C.SMS_AGREE_DT
		     , C.EMAIL
		     , C.EMAIL_AGREE_YN
		     , C.EMAIL_AGREE_DT
		     , C.MK_AGREE_YN
		     , C.MK_AGREE_DT
		     , C.HOME_ZIPCODE
		     , C.HOME_BASE_ADDR
		     , C.HOME_DTL_ADDR
		     , C.SITE_CD
		     , C.FRONT_GB
		     , C.AF_LINK_CD
		     , C.CUST_GB
		     , C.CUST_GRADE
		     , C.JOIN_DT
		     , C.FOREIGNER_YN
		     , C.CUST_STAT
		     , C.PASSWD_CHG_DT
		     , C.TEMP_PASSWD_YN
		     , C.LOGIN_LDT
		     , C.CI
		     , C.AUTH_DT
		     , C.SNS_TYPE
		     , C.MANAGED_RSN
		     , C.MANAGED_DTL_RSN
		     , C.MANAGED_DT
		     , C.SECEDE_RSN
		     , C.SECEDE_DTL_RSN
		     , C.SECEDE_DT
		     , C.RM_DORMANT_DT
		     , C.BIRTH_MM
		     -- , CA.BANK_CD
		     -- , CA.ACCOUNT_NO
		     -- , CA.ACCOUNT_NM
		     , #{regNo}   AS REG_NO
		     , NOW()      AS REG_DT
		     , C.UPD_NO
		     , C.UPD_DT
		FROM   TB_CUSTOMER C
		-- LEFT OUTER JOIN TB_CUST_ACCOUNT CA ON C.CUST_NO = CA.CUST_NO
		WHERE  C.CUST_NO = #{custNo}
	</insert>

	<!-- 회원 비밀번호 수정(임시비밀번호)-->
	<update id="updateCustomerPassword" parameterType="Customer">
		/* TscCustomer.updateCustomerPassword */
		UPDATE TB_CUSTOMER
		SET    PASSWD = #{encodedPasswd}
		     , PASSWD_CHG_DT = NOW()
		     , TEMP_PASSWD_YN = #{tempPasswdYn}
		     , UPD_NO = #{updNo}
		     , UPD_DT = NOW()
		WHERE  CUST_NO = #{custNo}
	</update>

	<!--탈퇴회원 정보 생성-->
	<insert id="createCustomerSecede" parameterType="Customer">
		/* TscCustomer.createCustomerSecede */
		INSERT INTO TB_SECEDE_CUST (
		       CUST_NO
		     , CUST_ID
		     , CUST_NM
		     , PASSWD
		     , BIRTH_YMD
		     , SEX_GB
		     , CELL_PHNNO
		     , APP_AGREE_YN
		     , APP_AGREE_DT
		     , SMS_AGREE_YN
		     , SMS_AGREE_DT
		     , EMAIL
		     , EMAIL_AGREE_YN
		     , EMAIL_AGREE_DT
		     , MK_AGREE_YN
		     , MK_AGREE_DT
		     , HOME_ZIPCODE
		     , HOME_BASE_ADDR
		     , HOME_DTL_ADDR
		     , SITE_CD
		     , FRONT_GB
		     , AF_LINK_CD
		     , CUST_GB
		     , CUST_GRADE
		     , JOIN_DT
		     , FOREIGNER_YN
		     , CUST_STAT
		     , PASSWD_CHG_DT
		     , TEMP_PASSWD_YN
		     , LOGIN_LDT
		     , CI
		     , AUTH_DT
		     , SNS_TYPE
		     , MANAGED_RSN
		     , MANAGED_DTL_RSN
		     , MANAGED_DT
		     , SECEDE_RSN
		     , SECEDE_DTL_RSN
		     , SECEDE_DT
		     , BIRTH_MM
		     , REG_NO
		     , REG_DT
		     , UPD_NO
		     , UPD_DT
		)
		SELECT CUST_NO
		     , CUST_ID
		     , CUST_NM
		     , PASSWD
		     , BIRTH_YMD
		     , SEX_GB
		     , CELL_PHNNO
		     , APP_AGREE_YN
		     , APP_AGREE_DT
		     , SMS_AGREE_YN
		     , SMS_AGREE_DT
		     , EMAIL
		     , EMAIL_AGREE_YN
		     , EMAIL_AGREE_DT
		     , MK_AGREE_YN
		     , MK_AGREE_DT
		     , HOME_ZIPCODE
		     , HOME_BASE_ADDR
		     , HOME_DTL_ADDR
		     , SITE_CD
		     , FRONT_GB
		     , AF_LINK_CD
		     , CUST_GB
		     , CUST_GRADE
		     , JOIN_DT
		     , FOREIGNER_YN
		     , #{custStat}         AS CUST_STAT -- G104_30
		     , PASSWD_CHG_DT
		     , TEMP_PASSWD_YN
		     , LOGIN_LDT
		     , CI
		     , AUTH_DT
		     , SNS_TYPE
		     , MANAGED_RSN
		     , MANAGED_DTL_RSN
		     , MANAGED_DT
		     , #{secedeRsn}        AS SECEDE_RSN
		     , #{secedeDtlRsn}     AS SECEDE_DTL_RSN
		     , NOW()               AS SECEDE_DT
		     , BIRTH_MM
		     , #{regNo}            AS REG_NO
		     , NOW()               AS REG_DT
		     , #{updNo}            AS UPD_NO
		     , NOW()               AS UPD_DT
		FROM   TB_CUSTOMER
		WHERE  CUST_NO = #{custNo}
	</insert>

	<!-- 회원 탈퇴 개인정보 처리 -->
	<update id="updateCustomerSecedeInfo" parameterType="Customer">
		/* TscCustomer.updateCustomerSecedeInfo */
		UPDATE TB_CUSTOMER
		SET    CUST_ID = CONCAT('secede_cust_',#{custNo})
		     , CUST_NM = #{encodedCustNm}
		     , CUST_STAT = #{custStat}
		     , SECEDE_RSN = #{secedeRsn}
		     , SECEDE_DTL_RSN = #{secedeDtlRsn}
		     , SECEDE_DT = NOW()
		     , BIRTH_YMD = NULL
		     , SEX_GB = NULL
		     , CELL_PHNNO = NULL
		     , EMAIL = NULL
		     , SMS_AGREE_YN = 'N'
		     , SMS_AGREE_DT = NULL
		     , EMAIL_AGREE_YN = 'N'
		     , EMAIL_AGREE_DT = NULL
		     , APP_AGREE_YN = 'N'
		     , APP_AGREE_DT = NULL
		     , MK_AGREE_YN = 'N'
		     , MK_AGREE_DT = NULL
		     , HOME_ZIPCODE = NULL
		     , HOME_BASE_ADDR = NULL
		     , HOME_DTL_ADDR = NULL
		     , CI = NULL
		     , CUST_STAT = #{custStat} -- G104_30
		WHERE  CUST_NO = #{custNo}
	</update>

	<!--회원 마케팅수신동의 이력-->
	<insert id="createCustomerMarketHst" parameterType="Customer">
		/* TscCustomer.createCustomerMarketHst */
		INSERT INTO TB_CUST_MARKET_HST (
		       CUST_NO
		     , APP_AGREE_YN
		     , SMS_AGREE_YN
		     , EMAIL_AGREE_YN
		     , MK_AGREE_YN
		     , REG_NO
		     , REG_DT
		)
		SELECT CUST_NO
		     , IFNULL(#{appAgreeYn}, APP_AGREE_YN)     AS APP_AGREE_YN
		     , IFNULL(#{smsAgreeYn}, SMS_AGREE_YN)     AS SMS_AGREE_YN
		     , IFNULL(#{emailAgreeYn}, EMAIL_AGREE_YN) AS EMAIL_AGREE_YN
		     , IFNULL(#{mkAgreeYn}, MK_AGREE_YN)       AS MK_AGREE_YN
		     , #{regNo}                                AS REG_NO
		     , NOW()                                   AS REG_DT
		FROM   TB_CUSTOMER
		WHERE  CUST_NO = #{custNo}
	</insert>

	<!-- 위시리스트 삭제 -->
	<delete id="deleteWishList" parameterType="Integer">
		/* TscCustomer.deleteWishInfo */
		DELETE
		FROM   TB_WISHLIST
		WHERE  CUST_NO = #{custNo}
	</delete>

	<!-- 장바구니 상세 삭제 -->
	<delete id="deleteCustomerCartDetail" parameterType="Integer">
		DELETE
		FROM   TB_CART_DETAIL
		WHERE  CART_SQ IN (
		                   SELECT CART_SQ
		                   FROM   TB_CART
		                   WHERE  CUST_NO = #{custNo}
		)
	</delete>

	<!-- 장바구니 삭제 -->
	<delete id="deleteCustomerCart" parameterType="Integer">
		DELETE
		FROM   TB_CART
		WHERE  CUST_NO = #{custNo}
	</delete>

	<!-- 임시비밀번호 조회 -->
	<select id="getTemporaryPassword" parameterType="int" resultType="String">
		/* TscCustomer.getTemporaryPassword */
		SELECT CONVERT(TRUNCATE(RAND() * CAST(CONCAT(1,LPAD(0,(#{length} - 1),'0')) AS UNSIGNED),0),CHAR) AS PASSWD FROM DUAL
	</select>

	<!--휴면 해제처리-->
	<update id="updateDormantCustomerRelease" parameterType="Customer">
		/* TscCustomer.updateDormantCustomerRelease */
		UPDATE TB_CUSTOMER C
		INNER JOIN
		      TB_DORMANT_CUST DC
		ON     C.CUST_NO = DC.CUST_NO
		SET    C.CUST_NM = DC.CUST_NM
		     , C.BIRTH_YMD = DC.BIRTH_YMD
		     , C.SEX_GB = DC.SEX_GB
		     , C.CELL_PHNNO = DC.CELL_PHNNO
		     , C.SMS_AGREE_YN = DC.SMS_AGREE_YN
		     , C.SMS_AGREE_DT= DC.SMS_AGREE_DT
		     , C.EMAIL = DC.EMAIL
		     , C.EMAIL_AGREE_YN = DC.EMAIL_AGREE_YN
		     , C.EMAIL_AGREE_DT = DC.EMAIL_AGREE_DT
		     , C.APP_AGREE_YN = DC.APP_AGREE_YN
		     , C.APP_AGREE_DT = DC.APP_AGREE_DT
		     , C.MK_AGREE_YN = DC.MK_AGREE_YN
		     , C.MK_AGREE_DT = DC.MK_AGREE_DT
		     , C.HOME_ZIPCODE = DC.HOME_ZIPCODE
		     , C.HOME_BASE_ADDR = DC.HOME_BASE_ADDR
		     , C.HOME_DTL_ADDR = DC.HOME_DTL_ADDR
		     , C.CUST_STAT = 'G104_10'
		     , C.RM_DORMANT_DT = NOW()
		     , C.BIRTH_MM = DC.BIRTH_MM
		     , C.CUST_GRADE = #{custGrade}
		     , C.UPD_DT = NOW()
		     , C.UPD_NO = #{updNo}
		WHERE  C.CUST_NO = #{custNo}
	</update>

	<!--휴면 테이블 삭제-->
	<delete id="deleteDormantCustomer" parameterType="Integer">
		/* TscCustomer.deleteDormantCustomer */
		DELETE
		FROM  TB_DORMANT_CUST
		WHERE CUST_NO = #{custNo}
	</delete>

	<!--주문정보 휴면 해제-->
	<update id="updateDormantOrderRelease" parameterType="Customer">
		/* TscCustomer.updateDormantOrderRelease */
		UPDATE  TB_ORDER O
		INNER JOIN
		        TB_ORDER_DORMANT OD
		ON      O.CUST_NO = OD.CUST_NO
		SET     O.ORD_NM = OD.ORD_NM
		      , O.ORD_PHNNO = OD.ORD_PHNNO
		      , O.ORD_TELNO = OD.ORD_TELNO
		      , O.ORD_EMAIL = OD.ORD_EMAIL
		WHERE   O.CUST_NO = #{custNo}
	</update>

	<!--휴면 테이블 삭제-->
	<delete id="deleteDormantOrder" parameterType="Integer">
		/* TscCustomer.deleteDormantOrder */
		DELETE
		FROM  TB_ORDER_DORMANT
		WHERE CUST_NO = #{custNo}
	</delete>

	<!--고객계좌정보-->
	<select id="getCustomerAccuontInfo" parameterType="CustAccount" resultType="CustAccount">
		/* TscCustomer.getCustomerAccuontInfo */
		SELECT CA.CUST_ACCOUNT_SQ
		     , CA.CUST_NO
		     , CA.BANK_CD
		     , FN_GET_CODE_NM('G940',CA.BANK_CD)          AS BANK_NM
		     , CA.ACCOUNT_NO
		     , CA.ACCOUNT_NM
		     , CA.DEFAULT_YN
		     , CA.DEL_YN
		     , CASE WHEN CA.CUST_NO = CA.REG_NO THEN CONCAT(C.CUST_ID,'/',FN_DEC_AES(C.CUST_NM))
		            ELSE (SELECT CONCAT(USER_ID,'/',USER_NM)
		                  FROM   TB_USER
		                  WHERE  USER_NO = CA.REG_NO
		                 )
		       END                                        AS REG_NM
		     , DATE_FORMAT(CA.REG_DT,'%Y-%m-%d %H:%i:%S') AS REG_DT
		     , CASE WHEN CA.CUST_NO = CA.UPD_NO THEN CONCAT(C.CUST_ID,'/',FN_DEC_AES(C.CUST_NM))
		            ELSE (SELECT CONCAT(USER_ID,'/',USER_NM)
		                  FROM   TB_USER
		                  WHERE  USER_NO = CA.UPD_NO
		                 )
		       END                                        AS UPD_NM
		     , DATE_FORMAT(CA.UPD_DT,'%Y-%m-%d %H:%i:%S') AS UPD_DT
		FROM   TB_CUST_ACCOUNT CA
		     , TB_CUSTOMER C
		WHERE  CA.CUST_NO = C.CUST_NO
		AND    CA.CUST_NO = #{custNo}
	</select>

	<!--고객계좌 번호 업데이트 전 초기화-->
	<update id="updateAccountDefaultInit" parameterType="CustAccount">
		/* TscCustomer.updateAccountDefaultInit */
		UPDATE TB_CUST_ACCOUNT
		SET    DEFAULT_YN = 'N'
		WHERE  CUST_NO = #{custNo}
	</update>

	<!--고객계좌 정보 업데이트 -->
	<update id="updateAccountInfo" parameterType="CustAccount">
		/* TscCustomer.updateAccountInfo */
		UPDATE TB_CUST_ACCOUNT
		SET    UPD_DT = NOW()
		     , UPD_NO = #{updNo}
		     , BANK_CD = #{bankCd}
		     , ACCOUNT_NO = #{encodedAccountNo}
		     , ACCOUNT_NM = #{accountNm}
		     , DEFAULT_YN = #{defaultYn}
		WHERE  CUST_NO = #{custNo}
	</update>

	<!-- 고객계좌 정보 등록-->
	<insert id="createAccountInfo" parameterType="CustAccount">
		/* TscCustomer.createAccountInfo */
		INSERT INTO TB_CUST_ACCOUNT (
		       CUST_NO
		     , BANK_CD
		     , ACCOUNT_NO
		     , ACCOUNT_NM
		     , DEFAULT_YN
		     , DEL_YN
		     , REG_NO
		     , REG_DT
		     , UPD_NO
		     , UPD_DT
		) VALUES (
		      #{custNo}
		    , #{bankCd}
		    , #{encodedAccountNo}
		    , #{accountNm}
		    , #{defaultYn}
		    , #{delYn}
		    , #{regNo}
		    , NOW()
		    , #{updNo}
		    , NOW()
		)
	</insert>

	<!--이메일 유효성 체크-->
	<select id="getCustomerByEmail" parameterType="Customer" resultType="int">
		/* TscCustomer.getCustomerByEmail */
		SELECT B.CNT
		FROM (
		      SELECT A.CNT
		           , ROW_NUMBER() OVER (ORDER BY A.CNT DESC) AS NUMB
		      FROM (
		            SELECT COUNT(*) AS CNT
		            FROM   TB_CUSTOMER
		            WHERE  CUST_STAT = 'G104_10'   /* 활동회원*/
		            AND    SITE_CD = #{siteCd}
		            AND    CUST_NO != #{custNo}
		            AND    EMAIL = #{encodedEmail}

		            UNION ALL

		            SELECT COUNT(*) AS CNT
		            FROM   TB_SECEDE_CUST
		            WHERE  SITE_CD = #{siteCd}
		            AND    CUST_NO != #{custNo}
		            AND    EMAIL = #{encodedEmail}

		            UNION ALL

		            SELECT COUNT(*) AS CNT
		            FROM   TB_DORMANT_CUST
		            WHERE  SITE_CD = #{siteCd}
		            AND    CUST_NO != #{custNo}
		            AND    EMAIL = #{encodedEmail}
		           ) A
		    ) B
		WHERE NUMB = 1
	</select>

	<!--고객배송지 목록-->
	<select id="getCustomerDeliveryAddrList" parameterType="CustDeliveryAddr" resultType="CustDeliveryAddr">
		/* TscCustomer.getCustomerDeliveryAddrList */
		SELECT C.CUST_ID
		     , C.CUST_NO
		     , CDA.CUST_DELV_ADDR_SQ
		     , CDA.CUST_NO
		     , CDA.DELV_ADDR_NM
		     , CDA.DEFAULT_YN
		     , CDA.RECIP_NM
		     , CDA.RECIP_PHNNO
		     , CDA.RECIP_TELNO
		     , CDA.RECIP_ZIPCODE
		     , CDA.RECIP_BASE_ADDR
		     , CDA.RECIP_DTL_ADDR
		     , CDA.DEL_YN
		     , CDA.DELV_MEMO
		     , CASE WHEN DDZ.ZIP_NO IS NULL
		            THEN 'N'
		            ELSE 'Y'
		       END                                     AS SHOT_DELV_USE_YN
		     , CDA.REG_NO
		     , DATE_FORMAT(CDA.REG_DT, '%Y%m%d%H%i%S') AS REG_DT
		     , FN_GET_USER_NM(CDA.UPD_NO) AS REG_NM
		     , DATE_FORMAT(CDA.UPD_DT, '%Y%m%d%H%i%S') AS UPD_DT
		FROM   TB_CUSTOMER C
		INNER JOIN
		       TB_CUST_DELIVERY_ADDR CDA
		ON     C.CUST_NO = CDA.CUST_NO
		AND    CDA.DEL_YN = 'N'
		LEFT OUTER JOIN
		       TB_DAILY_DELIVERY_ZONE DDZ
		ON     DDZ.ZIP_NO = CDA.RECIP_ZIPCODE
		AND    DDZ.ISUSE = 'Y'
		WHERE  C.CUST_NO = #{custNo}
		ORDER BY CDA.DEFAULT_YN DESC, CDA.REG_DT DESC
	</select>

	<!-- 주소정보 - 기본배송지 초기화 -->
	<update id="updateCustomerDeliveryAddrDefaultInit" parameterType="CustDeliveryAddr">
		/* TscCustomer.updateCustomerDeliveryAddrDefaultInit */
		UPDATE TB_CUST_DELIVERY_ADDR
		SET    DEFAULT_YN = 'N'
		WHERE  CUST_NO = #{custNo}
		AND    DEFAULT_YN = 'Y'
		AND    DEL_YN = 'N'
	</update>

	<!--주소정보 등록/수정 -->
	<update id="saveCustomerDeliveryAddr" parameterType="CustDeliveryAddr">
		/* TscCustomer.saveCustomerDeliveryAddr */
		INSERT INTO TB_CUST_DELIVERY_ADDR (
		       CUST_DELV_ADDR_SQ
		     , CUST_NO
		     , DELV_ADDR_NM
		     , DEFAULT_YN
		     , RECIP_NM
		     , RECIP_PHNNO
		     , RECIP_TELNO
		     , RECIP_ZIPCODE
		     , RECIP_BASE_ADDR
		     , RECIP_DTL_ADDR
		     , DELV_MEMO
		     , DEL_YN
		     , REG_NO
		     , REG_DT
		     , UPD_NO
		     , UPD_DT
		)
		VALUES (
		       #{custDelvAddrSq}
		     , #{custNo}
		     , #{delvAddrNm}
		     , IFNULL(#{defaultYn}, 'N')
		     , #{recipNm}
		     , #{recipPhnno}
		     , #{recipTelno}
		     , #{recipZipcode}
		     , #{recipBaseAddr}
		     , #{recipDtlAddr}
		     , #{delvMemo}
		     , IFNULL(#{delYn}, 'N')
		     , #{regNo}
		     , NOW()
		     , #{updNo}
		     , NOW()
		)
		ON DUPLICATE KEY UPDATE
		       DELV_ADDR_NM = #{delvAddrNm}
		     , DEFAULT_YN = IFNULL(#{defaultYn}, 'N')
		     , RECIP_NM = #{recipNm}
		     , RECIP_PHNNO = #{recipPhnno}
		     , RECIP_TELNO = #{recipTelno}
		     , RECIP_ZIPCODE = #{recipZipcode}
		     , RECIP_BASE_ADDR = #{recipBaseAddr}
		     , RECIP_DTL_ADDR  = #{recipDtlAddr}
		     , DEL_YN = IFNULL(#{delYn}, 'N')
		     , UPD_NO = #{updNo}
		     , UPD_DT = NOW()
	</update>

	<!--주소정보 삭제 -->
	<update id="deleteCustDeliveryAddr" parameterType="CustDeliveryAddr">
		/* TscCustomer.deleteCustDeliveryAddr */
		UPDATE TB_CUST_DELIVERY_ADDR
		SET    DEL_YN = 'Y'
		    ,  DEFAULT_YN = 'N'
		    ,  UPD_DT = NOW()
		    ,  UPD_NO = #{updNo}
		WHERE  CUST_DELV_ADDR_SQ = #{custDelvAddrSq}
		AND    CUST_NO = #{custNo}
	</update>

	<!--탈퇴일시-->
	<select id="getSecedeDt" parameterType="Integer" resultType="Customer">
		SELECT DATE_FORMAT(SECEDE_DT, '%Y.%m.%d %H:%i') AS SECEDE_DT
		     , SNS_TYPE
		FROM   TB_SECEDE_CUST
		WHERE  CUST_NO = #{custNo}
	</select>

		<!--고객 이력정보 삭제처리-->
	<delete id="deleteCustomerHst" parameterType="Integer">
		/*TsbCustomerDao.deleteCustomerHst*/
		DELETE
		FROM  TB_CUSTOMER_HST
		WHERE CUST_NO = #{custNo}
	</delete>

	<!--SNS 정보 삭제처리-->
	<delete id="deleteCustomerSns" parameterType="Integer">
		/*TsbCustomerDao.deleteCustomerSns*/
		DELETE
		FROM  TB_CUSTOMER_SNS
		WHERE CUST_NO = #{custNo}
	</delete>

	<!--탈퇴정보 삭제 처리-->
	<delete id="deleteSecedeCustomer" parameterType="Integer">
		/*TsbCustomerDao.deleteSecedeCustomer*/
		DELETE
		FROM  TB_SECEDE_CUST
		WHERE CUST_NO = #{custNo}
	</delete>

	<!--Exception 테스트-->
	<insert id="createException">
		/* TscCustomer.createException */
		INSERT INTO TB_CUSTOMER (CUST_NO)
		VALUES (NULL)
	</insert>

</mapper>
