<?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.TscFreegiftDao">

	<!-- 사은품 : 1차조건 적용된 사음품 정보, 상품 금액 조회 -->
	<select id="getFreeGiftGoodsApplyAmt" parameterType="Order" resultType="Order">
		/* freegift.getFreeGiftGoodsApplyAmt */
		WITH GOODS_DATA AS (
			SELECT A.GOODS_CD
			     , A.SUPPLY_COMP_CD
			     , A.BRAND_CD
			     , SUM(A.GOODS_QTY) AS GOODS_QTY
			     , FN_GET_APPLY_CPN1_PRICE(A.GOODS_CD, #{frontGb}) AS CURR_PRICE -- 즉시할인 쿠폰 적용가 
			     , A.ADD_PRICE
			FROM (
				SELECT CA.GOODS_CD
				     , G.SUPPLY_COMP_CD
				     , G.BRAND_CD
				     , CA.GOODS_QTY 
				     , SUM(OP.ADD_PRICE) AS ADD_PRICE -- 옵션가
				     , G.GOODS_TYPE
				     , CA.CART_SQ
				FROM   TB_CART CA
				INNER  JOIN TB_CART_DETAIL CD
				ON     CA.CART_SQ = CD.CART_SQ
				INNER  JOIN TB_GOODS G
				ON     CA.GOODS_CD = G.GOODS_CD
				LEFT   OUTER JOIN TB_OPTION OP
				ON     G.GOODS_CD = OP.GOODS_CD
				AND    CD.OPT_CD = OP.OPT_CD
				WHERE  1=1
				<if test="custNo == 0">
				AND    CA.CUST_NO = 0
				AND    CA.JSESSION_ID = #{jsessionId}
				</if>
				<if test="custNo != 0">
				AND    CA.CUST_NO = #{custNo}
				</if>
				AND    CA.CART_SQ IN
				<foreach collection="cartSqArr" item="item" index="index"  open="(" close=")" separator=",">
					#{item}
				</foreach>
				GROUP BY CA.GOODS_CD
				     , G.SUPPLY_COMP_CD
				     , G.BRAND_CD
				     , CA.GOODS_QTY
				     , G.GOODS_TYPE
				     , CA.CART_SQ
			) A
			GROUP BY A.GOODS_CD
			    , A.SUPPLY_COMP_CD
			    , A.BRAND_CD
			    , A.ADD_PRICE
		)
		SELECT FGG.FREEGIFT_SQ
		     , FGG.GOODS_CD
		     , FGG.CURR_PRICE
		     , IFNULL(FGG.ADD_PRICE, 0) AS OPT_ADD_PRICE 
		     , FGG.GOODS_QTY
		FROM   TB_FREEGIFT FG
		INNER  JOIN (
			SELECT FGG.FREEGIFT_SQ
			     , GD.GOODS_CD
			     , GD.CURR_PRICE
			     , GD.ADD_PRICE
			     , GD.GOODS_QTY
			FROM   TB_FREEGIFT_GOODS FGG
			INNER  JOIN GOODS_DATA GD
			ON     FGG.TARGET_VAL = GD.GOODS_CD
			WHERE  1=1
			AND    FGG.GOODS_GB = 'G800_20' -- 적용상품
			AND    FGG.TARGET_GB= 'G260_10' -- 상품
			AND    FGG.DEL_YN = 'N'
			UNION
			SELECT FGG1.FREEGIFT_SQ
			     , GD.GOODS_CD
			     , GD.CURR_PRICE
			     , GD.ADD_PRICE
			     , GD.GOODS_QTY
			FROM   GOODS_DATA GD
			INNER  JOIN TB_FREEGIFT_GOODS_APPLY FGG1
			ON     GD.SUPPLY_COMP_CD = FGG1.TARGET_VAL
			AND    FGG1.TARGET_GB= 'G260_13' -- 공급처
			INNER  JOIN TB_FREEGIFT_GOODS_APPLY FGG2
			ON     GD.BRAND_CD = FGG2.TARGET_VAL
			AND    FGG2.TARGET_GB= 'G260_12' -- 브랜드
			AND    FGG1.FREEGIFT_SQ = FGG2.FREEGIFT_SQ
		) FGG
		ON     FG.FREEGIFT_SQ = FGG.FREEGIFT_SQ 
		LEFT   OUTER JOIN TB_FREEGIFT_GOODS NOT_APPLY_GOODS
		ON     FGG.FREEGIFT_SQ = NOT_APPLY_GOODS.FREEGIFT_SQ
		AND    FGG.GOODS_CD = NOT_APPLY_GOODS.TARGET_VAL 
		AND    NOT_APPLY_GOODS.TARGET_GB = 'G800_30'
		WHERE  1=1
		AND    NOT_APPLY_GOODS.FREEGIFT_SQ IS NULL
		AND    FG.DEL_YN = 'N'
		AND    FG.SELF_YN = 'Y'
		AND    FG.FREEGIFT_STAT = 'G232_11' -- 진행
		AND    FG.FREEGIFT_STDT <![CDATA[<]]> CURRENT_TIMESTAMP 
		AND    FG.FREEGIFT_EDDT > CURRENT_TIMESTAMP
		ORDER  BY FGG.FREEGIFT_SQ
	</select>
	
	<!-- 사은품 : 2차조건 적용된 사은품상품금액 구간정보조회 -->
	<select id="getFreeGiftGoodsApplySection" parameterType="Order" resultType="Order">
		/* freegift.getFreeGiftGoodsApplySection */
		SELECT FGS.FREEGIFT_SECTION_SQ
		     , FGS.FREEGIFT_SQ
		     , FGS.SECTION_VAL
		FROM   TB_FREEGIFT_SECTION FGS
		WHERE  1=1
		AND    FGS.DEL_YN = 'N'
		AND    FGS.SECTION_GB = 'G810_11' -- 금액만 처리
		AND    FGS.FREEGIFT_SQ IN
		<foreach collection="freegiftSqArr" item="item" index="index"  open="(" close=")" separator=",">
			#{item}
		</foreach>
		ORDER  BY FGS.FREEGIFT_SQ
		     , FGS.SECTION_VAL
	</select>
	
	<!-- 사은품 : 3차조건 적용된 구간정보의 사은품정보조회 -->
	<select id="getFreeGiftGoodsApplyVal" parameterType="Order" resultType="Order">
		/* freegift.getFreeGiftGoodsApplyVal */
		SELECT FGV.FREEGIFT_VAL_SQ
		     , FG.FREEGIFT_NM 
		     , FGV.FREEGIFT_SECTION_SQ 
		     , FGV.FREEGIFT_SQ 
		     , FGV.USE_POINT
		     , FGV.ITEM_QTY
		     , FG.ALL_YN
		     , G.PRODUCT_NO
		     , G.PRODUCT_CODE 
		     , G.GOODS_NM 
		     , G.SYS_IMG_NM 
		FROM   TB_FREEGIFT_VAL FGV
		INNER  JOIN TB_FREEGIFT FG
		ON     FGV.FREEGIFT_SQ = FG.FREEGIFT_SQ
		INNER  JOIN TB_FREE_GOODS G
		ON     FGV.PRODUCT_NO = G.PRODUCT_NO 
		WHERE  1=1
		AND    FGV.DEL_YN = 'N'
		AND    FGV.LEFT_QTY <![CDATA[>=]]> FGV.ITEM_QTY
		AND    FGV.FREEGIFT_SECTION_SQ IN
		<foreach collection="freegiftSectionSqArr" item="item" index="index"  open="(" close=")" separator=",">
			#{item}
		</foreach>
		ORDER  BY FG.FREEGIFT_SQ
		        , FGV.FREEGIFT_SECTION_SQ
		        , FGV.USE_POINT
	</select>
	
	
</mapper>















