<?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.batch.biz.dao.TsbCartDao">

	<!-- 장바구니 만료일이 지난 데이터 삭제 -->
	<delete id="deleteCartExpriration" parameterType="Order">
		/* TsbCart.deleteCartExpriration : 장바구니 만료일이 지난 데이터 삭제 */
		DELETE FROM TB_CART CA
		WHERE  1=1
		AND    CA.REG_DT <![CDATA[ < ]]> CAST(ADDDATE(NOW(), INTERVAL -${saveLimitDay} DAY) AS DATE)
	</delete>

	<!-- 장바구니 마스터 정보 없는 상세데이터 삭제 -->
	<delete id="deleteCartDetail" parameterType="Order">
		/* TsbCart.deleteCartDetail : 장바구니 마스터 정보 없는 상세데이터 삭제 */
		DELETE FROM TB_CART_DETAIL CD
		WHERE  CD.CART_SQ NOT IN (SELECT CA.CART_SQ
		                          FROM   TB_CART CA
		                          WHERE  CA.REG_DT > CAST(ADDDATE(NOW(), INTERVAL -${saveLimitDay} DAY) AS DATE))			/* DeadLock 발생으로 날짜 조건 추가 */
	</delete>

	<!-- 장바구니 미구매 상품 존재 알림(앱푸시) 대상 리스트트(장바구니 삭제 10일전) -->
	<select id="getUnpurchaseCartCustList" parameterType="Order" resultType="Order">
		/* TsbCart.getUnpurchaseCartCustList : 장바구니 미구매 상품 존재 알림(앱푸시) 대상 리스트 */
		SELECT CT.CUST_NO
		     , CT.CELL_PHNNO
		     , CT.CUST_NM
		     , CT.CUST_ID
		FROM   TB_CART C
		INNER  JOIN TB_CUSTOMER CT
		ON     C.CUST_NO = CT.CUST_NO
		WHERE  1=1
		AND    CT.CUST_NO > 0
		AND    CT.SECEDE_DT IS NULL
		AND    CT.APP_AGREE_YN = 'Y'		-- 장바구니 미구매 PUSH는 정보성 데이터이므로 마케팅 여부는 바라보지 않음. > 20210712 한수인리더 확인. xodud1202
		AND    C.CART_GB = 'G026_BC'
		AND    DATE_FORMAT(C.REG_DT, '%Y%m%d') = DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -${saveLimitDay} DAY), '%Y%m%d')
		GROUP  BY CT.CUST_NO
		        , CT.CELL_PHNNO
		        , CT.CUST_NM
		        , CT.CUST_ID
	</select>

	<!-- 장바구니 추가 쿠폰 혜택 알림 푸시 대상 쿠폰 조회 -->
	<select id="getAddCouponList" resultType="Order" parameterType="Order">
		/* TsbCart.getAddCouponList : 장바구니 추가 쿠폰 혜택 알림 푸시 대상 쿠폰 조회 */
		SELECT CP.CPN_ID
		     , CP.APPLY_SCOPE
		FROM   TB_COUPON CP
		LEFT   OUTER JOIN TB_COUPON_NOTICE_SENDED CNS
		ON     CP.CPN_ID = CNS.CPN_ID
		AND    CNS.BENEFIT_GB = #{benefitGb}
		WHERE  1=1
		AND    CNS.CPN_ID IS NULL
		AND    CP.CPN_TYPE <![CDATA[ <> ]]> 'G230_10'
		AND    CP.CPN_STAT = 'G232_11'
		AND    CP.DC_CD_GB = 'G233_00'
		AND    CP.DOWN_ABL_YN = 'Y'
		AND    CP.FIRST_ORD_YN = 'N'
		AND    CP.NEW_CUST_YN = 'N'
		AND    NOW() BETWEEN CP.DOWN_STDT AND CP.DOWN_EDDT
	</select>

	<!-- 장바구니 혜택 추가 푸시 알림 대상 조회 -->
	<select id="getCartAddBenefitPushCustomerList" parameterType="Order" resultType="Customer">
		/* TsbCart.getCartAddBenefitPushCustomerList : 장바구니 혜택 추가 푸시 알림 대상 조회 */
		SELECT CT.CUST_NO
		     , CT.CELL_PHNNO
		FROM   TB_CUSTOMER CT
		INNER  JOIN (SELECT CR.CPN_ID
		                  , G.GOODS_CD
		                  , C.CUST_NO
		             FROM   TB_CART C
		             INNER  JOIN TB_GOODS G
		             ON     C.GOODS_CD = G.GOODS_CD
		             INNER  JOIN TB_COUPON_REFVAL CR
		             ON     G.SUPPLY_COMP_CD = CR.REF_VAL
		             INNER  JOIN TB_COUPON CP
		             ON     CP.CPN_ID = CR.CPN_ID
		             WHERE  1=1
		             AND    CP.CPN_ID IN
		             <foreach collection="cpnIdArr" item="item" index="index"  open="(" close=")" separator=",">
		                        #{item}
		             </foreach>
		             AND    CP.APPLY_SCOPE = 'I'
		             AND    G.GOODS_STAT = 'G008_90'
		             AND    CR.DEL_YN = 'N'
		             AND    CR.CPN_TYPE <![CDATA[ <> ]]> 'G230_10'
		             AND    C.CART_GB = 'G026_BC'
		             AND    C.CUST_NO > 0
		             AND    CR.CPN_TARGET = 'G260_13' -- 공급업체
		             UNION  ALL
		             SELECT CR.CPN_ID
		                  , G.GOODS_CD
		                  , C.CUST_NO
		             FROM   TB_CART C
		             INNER  JOIN TB_GOODS G
		             ON     C.GOODS_CD = G.GOODS_CD
		             INNER  JOIN TB_COUPON_REFVAL CR
		             ON     G.BRAND_CD = CR.REF_VAL
		             INNER  JOIN TB_COUPON CP
		             ON     CP.CPN_ID = CR.CPN_ID
		             WHERE  1=1
		             AND    CP.CPN_ID IN
		             <foreach collection="cpnIdArr" item="item" index="index"  open="(" close=")" separator=",">
		                        #{item}
		             </foreach>
		             AND    CP.APPLY_SCOPE = 'I'
		             AND    G.GOODS_STAT = 'G008_90'
		             AND    CR.DEL_YN = 'N'
		             AND    CR.CPN_TYPE <![CDATA[ <> ]]> 'G230_10'
		             AND    C.CART_GB = 'G026_BC'
		             AND    C.CUST_NO > 0
		             AND    CR.CPN_TARGET = 'G260_12' -- 브랜드
		             UNION ALL
		             SELECT CR.CPN_ID
		                  , CG.GOODS_CD
		                  , C.CUST_NO
		             FROM   TB_CART C
		             INNER  JOIN TB_GOODS G
		             ON     C.GOODS_CD = G.GOODS_CD
		             INNER  JOIN TB_CATE_GOODS CG
		             ON     G.GOODS_CD = CG.GOODS_CD
		             INNER  JOIN (SELECT C4.LEAF_CATE_NO
		                               , CASE WHEN X = 1 THEN CATE1_NO
		                                 WHEN X = 2 THEN CATE2_NO
		                                 WHEN X = 3 THEN CATE3_NO
		                                 WHEN X = 4 THEN CATE4_NO
		                                 ELSE CATE5_NO
		                                 END  CATE_NO
		                          FROM   TB_CATE_4SRCH C4
		                          LEFT   OUTER JOIN (SELECT 1 AS X
		                                             UNION  ALL
		                                             SELECT 2 AS X
		                                             UNION  ALL
		                                             SELECT 3 AS X
		                                             UNION  ALL
		                                             SELECT 4 AS X
		                                             UNION  ALL
		                                             SELECT 5 AS X ) B
		                          ON     1=1
		                          WHERE  C4.SITE_CD = 'G000_10'
		                          AND    C4.CATE_TYPE = 'G031_10') CATE
		             ON     CATE.LEAF_CATE_NO = CG.CATE_NO
		             INNER  JOIN TB_COUPON_REFVAL CR
		             ON     CG.CATE_NO = CR.REF_VAL
		             INNER  JOIN TB_COUPON CP
		             ON     CP.CPN_ID = CR.CPN_ID
		             WHERE  1=1
		             AND    CP.CPN_ID IN
		             <foreach collection="cpnIdArr" item="item" index="index"  open="(" close=")" separator=",">
		                        #{item}
		             </foreach>
		             AND    CP.APPLY_SCOPE = 'I'
		             AND    G.GOODS_STAT = 'G008_90'
		             AND    CR.DEL_YN = 'N'
		             AND    CR.CPN_TYPE <![CDATA[ <> ]]> 'G230_10'
		             AND    CR.CPN_TARGET = 'G260_11' -- 카테고리
		             UNION ALL
		             SELECT CR.CPN_ID
		                  , G.GOODS_CD
		                  , C.CUST_NO
		             FROM   TB_CART C
		             INNER  JOIN TB_GOODS G
		             ON     C.GOODS_CD = G.GOODS_CD
		             INNER  JOIN TB_COUPON_REFVAL CR
		             ON     G.GOODS_CD = CR.REF_VAL
		             INNER  JOIN TB_COUPON CP
		             ON     CP.CPN_ID = CR.CPN_ID
		             WHERE  1=1
		             AND    CP.CPN_ID IN
		             <foreach collection="cpnIdArr" item="item" index="index"  open="(" close=")" separator=",">
		                        #{item}
		             </foreach>
		             AND    CP.APPLY_SCOPE = 'I'
		             AND    G.GOODS_STAT = 'G008_90'
		             AND    CR.DEL_YN = 'N'
		             AND    CR.CPN_TYPE <![CDATA[ <> ]]> 'G230_10'
		             AND    C.CART_GB = 'G026_BC'
		             AND    C.CUST_NO > 0
		             AND    CR.CPN_TARGET = 'G260_10' -- 적용상품
		       ) A
		ON     CT.CUST_NO = A.CUST_NO
		INNER  JOIN TB_COUPON_CUST_GRADE CCG
		ON     A.CPN_ID = CCG.CPN_ID
		AND    CT.CUST_GRADE = CCG.USABLE_CUST_GRADE
		LEFT   OUTER JOIN TB_COUPON_NOTICE_SENDED CNS
		ON     A.CPN_ID = CNS.CPN_ID
		AND    CNS.BENEFIT_GB = 'C'
		LEFT   OUTER JOIN TB_COUPON_BAN_GOODS CG
		ON     CG.GOODS_CD = A.GOODS_CD
		AND    CG.DEL_YN = 'N'
		LEFT   OUTER JOIN TB_COUPON_REFVAL CR
		ON     A.CPN_ID = CR.CPN_ID
		AND    A.GOODS_CD = CR.REF_VAL
		AND    CR.CPN_TARGET = 'G260_14'
		AND    CR.DEL_YN = 'N'
		WHERE  CT.SECEDE_DT IS NULL
		AND    CNS.CPN_ID IS NULL
		AND    CG.GOODS_CD IS NULL
		AND    CR.REF_VAL IS NULL
		AND    CT.APP_AGREE_YN = 'Y'
		AND    CT.MK_AGREE_YN = 'Y'
		GROUP  BY CT.CUST_NO
		        , CT.CELL_PHNNO
		UNION  ALL
		SELECT CT.CUST_NO
		     , CT.CELL_PHNNO
		FROM   TB_CUSTOMER CT
		INNER  JOIN (SELECT C.GOODS_CD
		                  , C.CUST_NO
		                  , CP.CPN_ID
		             FROM   TB_CART C
		             INNER  JOIN TB_GOODS G
		             ON     C.GOODS_CD = G.GOODS_CD
		             INNER  JOIN TB_COUPON CP
		             ON    CP.CPN_ID IN
		             <foreach collection="cpnIdArr" item="item" index="index"  open="(" close=")" separator=",">
		                        #{item}
		             </foreach>
		             AND    CP.APPLY_SCOPE = 'A'
		             LEFT   OUTER JOIN TB_COUPON_REFVAL CR
		             ON     CR.CPN_ID = CP.CPN_ID
		             AND    G.GOODS_CD = CR.REF_VAL
		             AND    CR.CPN_TARGET = 'G260_14' -- 제외상품
		             AND    CR.DEL_YN = 'N'
		             AND    CR.CPN_TYPE <![CDATA[ <> ]]> 'G230_10'
		             LEFT   OUTER JOIN TB_COUPON_NOTICE_SENDED CNS
		             ON     CP.CPN_ID = CNS.CPN_ID
		             AND    CNS.BENEFIT_GB = 'C'
		             LEFT   OUTER JOIN TB_COUPON_BAN_GOODS CG
		             ON     CG.GOODS_CD = C.GOODS_CD
		             AND    CG.DEL_YN = 'N'
		             WHERE  1=1
		             AND    CNS.CPN_ID IS NULL
		             AND    CG.GOODS_CD IS NULL
		             AND    G.GOODS_STAT = 'G008_90'
		             AND    C.CART_GB = 'G026_BC'
		             AND    C.CUST_NO > 0
		       ) A
		ON     CT.CUST_NO = A.CUST_NO
		INNER  JOIN TB_COUPON_CUST_GRADE CCG
		ON     A.CPN_ID = CCG.CPN_ID
		AND    CT.CUST_GRADE = CCG.USABLE_CUST_GRADE
		WHERE  CT.SECEDE_DT IS NULL
		AND    CT.APP_AGREE_YN = 'Y'
		AND    CT.MK_AGREE_YN = 'Y'
		GROUP  BY CT.CUST_NO
		     , CT.CELL_PHNNO
	</select>

	<!-- 위시리스트 혜택 추가 푸시 알림 대상 조회 -->
	<select id="getWishlistAddBenefitPushCustomerList" parameterType="Order" resultType="Customer">
		/* TsbCart.getWishlistAddBenefitPushCustomerList : 위시리스트 혜택 추가 푸시 알림 대상 조회 */
		SELECT CT.CUST_NO
		     , CT.CELL_PHNNO
		FROM   TB_CUSTOMER CT
		INNER  JOIN (SELECT CR.CPN_ID
		                  , G.GOODS_CD
		                  , W.CUST_NO
		             FROM   TB_WISHLIST W
		             INNER  JOIN TB_GOODS G
		             ON     W.GOODS_CD = G.GOODS_CD
		             INNER  JOIN TB_COUPON_REFVAL CR
		             ON     G.SUPPLY_COMP_CD = CR.REF_VAL
		             INNER  JOIN TB_COUPON CP
		             ON     CP.CPN_ID = CR.CPN_ID
		             WHERE  1=1
		             AND    CP.CPN_ID IN
		             <foreach collection="cpnIdArr" item="item" index="index"  open="(" close=")" separator=",">
		                        #{item}
		             </foreach>
		             AND    CP.APPLY_SCOPE = 'I'
		             AND    G.GOODS_STAT = 'G008_90'
		             AND    CR.DEL_YN = 'N'
		             AND    CR.CPN_TYPE <![CDATA[ <> ]]> 'G230_10'
		             AND    W.CUST_NO > 0
		             AND    CR.CPN_TARGET = 'G260_13' -- 공급업체
		             UNION  ALL
		             SELECT CR.CPN_ID
		                  , G.GOODS_CD
		                  , W.CUST_NO
		             FROM   TB_WISHLIST W
		             INNER  JOIN TB_GOODS G
		             ON     W.GOODS_CD = G.GOODS_CD
		             INNER  JOIN TB_COUPON_REFVAL CR
		             ON     G.BRAND_CD = CR.REF_VAL
		             INNER  JOIN TB_COUPON CP
		             ON     CP.CPN_ID = CR.CPN_ID
		             WHERE  1=1
		             AND    CP.CPN_ID IN
		             <foreach collection="cpnIdArr" item="item" index="index"  open="(" close=")" separator=",">
		             	#{item}
		             </foreach>
		             AND    CP.APPLY_SCOPE = 'I'
		             AND    G.GOODS_STAT = 'G008_90'
		             AND    CR.DEL_YN = 'N'
		             AND    CR.CPN_TYPE <![CDATA[ <> ]]> 'G230_10'
		             AND    W.CUST_NO > 0
		             AND    CR.CPN_TARGET = 'G260_12' -- 브랜드
		             UNION ALL
		             SELECT CR.CPN_ID
		                  , CG.GOODS_CD
		                  , W.CUST_NO
		             FROM   TB_WISHLIST W
		             INNER  JOIN TB_GOODS G
		             ON     W.GOODS_CD = G.GOODS_CD
		             INNER  JOIN TB_CATE_GOODS CG
		             ON     G.GOODS_CD = CG.GOODS_CD
		             INNER  JOIN (SELECT C4.LEAF_CATE_NO
		                               , CASE WHEN X = 1 THEN CATE1_NO
		                                      WHEN X = 2 THEN CATE2_NO
		                                      WHEN X = 3 THEN CATE3_NO
		                                      WHEN X = 4 THEN CATE4_NO
		                                      ELSE CATE5_NO END  CATE_NO
		                          FROM   TB_CATE_4SRCH C4
		                          LEFT   OUTER JOIN (SELECT 1 AS X
		                                             UNION  ALL
		                                             SELECT 2 AS X
		                                             UNION  ALL
		                                             SELECT 3 AS X
		                                             UNION  ALL
		                                             SELECT 4 AS X
		                                             UNION  ALL
		                                             SELECT 5 AS X ) B
		                          ON     1=1
		                          WHERE  C4.SITE_CD = 'G000_10'
		                          AND    C4.CATE_TYPE = 'G031_10') CATE
		             ON     CATE.LEAF_CATE_NO = CG.CATE_NO
		             INNER  JOIN TB_COUPON_REFVAL CR
		             ON     CG.CATE_NO = CR.REF_VAL
		             INNER  JOIN TB_COUPON CP
		             ON     CP.CPN_ID = CR.CPN_ID
		             WHERE  1=1
		             AND    CP.CPN_ID IN
		             <foreach collection="cpnIdArr" item="item" index="index"  open="(" close=")" separator=",">
		                        #{item}
		             </foreach>
		             AND    CP.APPLY_SCOPE = 'I'
		             AND    G.GOODS_STAT = 'G008_90'
		             AND    CR.DEL_YN = 'N'
		             AND    CR.CPN_TYPE <![CDATA[ <> ]]> 'G230_10'
		             AND    CR.CPN_TARGET = 'G260_11' -- 카테고리
		             UNION ALL
		             SELECT CR.CPN_ID
		                  , G.GOODS_CD
		                  , W.CUST_NO
		             FROM   TB_WISHLIST W
		             INNER  JOIN TB_GOODS G
		             ON     W.GOODS_CD = G.GOODS_CD
		             INNER  JOIN TB_COUPON_REFVAL CR
		             ON     G.GOODS_CD = CR.REF_VAL
		             INNER  JOIN TB_COUPON CP
		             ON     CP.CPN_ID = CR.CPN_ID
		             WHERE  1=1
		             AND    CP.CPN_ID IN
		             <foreach collection="cpnIdArr" item="item" index="index"  open="(" close=")" separator=",">
		                        #{item}
		             </foreach>
		             AND    CP.APPLY_SCOPE = 'I'
		             AND    G.GOODS_STAT = 'G008_90'
		             AND    CR.DEL_YN = 'N'
		             AND    CR.CPN_TYPE <![CDATA[ <> ]]> 'G230_10'
		             AND    W.CUST_NO > 0
		             AND    CR.CPN_TARGET = 'G260_10' -- 적용상품
		             ) A
		ON     CT.CUST_NO = A.CUST_NO
		INNER  JOIN TB_COUPON_CUST_GRADE CCG
		ON     A.CPN_ID = CCG.CPN_ID
		AND    CT.CUST_GRADE = CCG.USABLE_CUST_GRADE
		LEFT   OUTER JOIN TB_COUPON_NOTICE_SENDED CNS
		ON     A.CPN_ID = CNS.CPN_ID
		AND    CNS.BENEFIT_GB = 'W'
		LEFT   OUTER JOIN TB_COUPON_BAN_GOODS CG
		ON     CG.GOODS_CD = A.GOODS_CD
		AND    CG.DEL_YN = 'N'
		LEFT   OUTER JOIN TB_COUPON_REFVAL CR
		ON     A.CPN_ID = CR.CPN_ID
		AND    A.GOODS_CD = CR.REF_VAL
		AND    CR.CPN_TARGET = 'G260_14'
		AND    CR.DEL_YN = 'N'
		WHERE  CT.SECEDE_DT IS NULL
		AND    CNS.CPN_ID IS NULL
		AND    CG.GOODS_CD IS NULL
		AND    CR.REF_VAL IS NULL
		AND    CT.APP_AGREE_YN = 'Y'
		AND    CT.MK_AGREE_YN = 'Y'
		GROUP  BY CT.CUST_NO
		        , CT.CELL_PHNNO
		UNION  ALL
		SELECT CT.CUST_NO
		     , CT.CELL_PHNNO
		FROM   TB_CUSTOMER CT
		INNER  JOIN (SELECT W.GOODS_CD
		                  , W.CUST_NO
		                  , CP.CPN_ID
		             FROM   TB_WISHLIST W
		             INNER  JOIN TB_GOODS G
		             ON     W.GOODS_CD = G.GOODS_CD
		             INNER  JOIN TB_COUPON CP
		             ON    CP.CPN_ID IN
		             <foreach collection="cpnIdArr" item="item" index="index"  open="(" close=")" separator=",">
		                       #{item}
		             </foreach>
		             AND    CP.APPLY_SCOPE = 'A'
		             LEFT   OUTER JOIN TB_COUPON_REFVAL CR
		             ON     CR.CPN_ID = CP.CPN_ID
		             AND    G.GOODS_CD = CR.REF_VAL
		             AND    CR.CPN_TARGET = 'G260_14' -- 제외상품
		             AND    CR.DEL_YN = 'N'
		             AND    CR.CPN_TYPE <![CDATA[ <> ]]> 'G230_10'
		             LEFT   OUTER JOIN TB_COUPON_NOTICE_SENDED CNS
		             ON     CP.CPN_ID = CNS.CPN_ID
		             AND    CNS.BENEFIT_GB = 'W'
		             LEFT   OUTER JOIN TB_COUPON_BAN_GOODS CG
		             ON     CG.GOODS_CD = W.GOODS_CD
		             AND    CG.DEL_YN = 'N'
		             WHERE  1=1
		             AND    CNS.CPN_ID IS NULL
		             AND    CG.GOODS_CD IS NULL
		             AND    G.GOODS_STAT = 'G008_90'
		             AND    W.CUST_NO > 0 ) A
		ON     CT.CUST_NO = A.CUST_NO
		INNER  JOIN TB_COUPON_CUST_GRADE CCG
		ON     A.CPN_ID = CCG.CPN_ID
		AND    CT.CUST_GRADE = CCG.USABLE_CUST_GRADE
		WHERE  CT.SECEDE_DT IS NULL
		AND    CT.APP_AGREE_YN = 'Y'
		AND    CT.MK_AGREE_YN = 'Y'
		GROUP  BY CT.CUST_NO
		        , CT.CELL_PHNNO
	</select>

	<!-- 장바구니 혜택추가 송부 이력 저장 -->
	<insert id="createCouponNoticeSended" parameterType="Order">
		/* TsbCart.createCouponNoticeSended : 장바구니 혜택추가 송부 이력 저장 */
		INSERT INTO TB_COUPON_NOTICE_SENDED (
		      BENEFIT_GB
		    , CPN_ID
		    , REG_NO
		    , REG_DT
		) VALUES (
		      #{benefitGb}
		    , #{cpnId}
		    , #{regNo}
		    , NOW()
		)
	</insert>
</mapper>