<?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.admin.biz.dao.TsaPgDao">

	<!-- 주문 샘플 데이터 (추후 삭제 예정) -->
	<select id="getOrderSampleGoodsInfo" parameterType="Goods" resultType="Goods">
		SELECT SYS_IMG_NM
		     , GOODS_CD
		     , GOODS_NM
		     , STOCK_QTY_SUM
		     , GOODS_STAT
		     , STYLE_YEAR
			 , SEASON_CD
		     , LIST_PRICE
		     , CURR_PRICE
		     , PRICE_UPD_DT
		     , ERP_STOCK_LINK_YN
		     , FORMAL_GB
		     , RETURNABLE_YN
		     , REG_DT
		     , REG_NM
			 , UPD_DT
		     , UPD_NM
		     , REP_GOODS_CD
			 , GROUP_CONCAT(OPT_CD2 ORDER BY DISP_ORD DESC, OPT_CD2 SEPARATOR ',')        AS SITE_CD
		  FROM (
			  SELECT GI.SYS_IMG_NM
				   , G.GOODS_CD
				   , G.GOODS_NM
				   , (CASE WHEN G.GOODS_TYPE = 'G056_N' THEN (SELECT IFNULL(SUM(CURR_STOCK_QTY - BASE_STOCK_QTY),0) FROM VW_STOCK WHERE GOODS_CD = G.GOODS_CD)
						   ELSE (SELECT IFNULL(SUM(CURR_STOCK_QTY - BASE_STOCK_QTY),0) FROM VW_STOCK_COMPOSE WHERE GOODS_CD = G.GOODS_CD)
					  END) AS STOCK_QTY_SUM
				   , G.GOODS_STAT
				   , G.STYLE_YEAR
				   , G.SEASON_CD
				   , G.LIST_PRICE
				   , G.CURR_PRICE
				   , G.PRICE_UPD_DT
				   , G.ERP_STOCK_LINK_YN
				   , G.FORMAL_GB
				   , G.RETURNABLE_YN
				   , G.REG_DT
				   , FN_GET_USER_NM(G.REG_NO) AS REG_NM
				   , G.UPD_DT
				   , FN_GET_USER_NM(G.UPD_NO) AS UPD_NM
				   , IFNULL(GC.COMPS_GOODS_CD, G.GOODS_CD) AS REP_GOODS_CD
				   , OP.OPT_CD2
				   , OP.DISP_ORD
				FROM TB_GOODS G
				LEFT OUTER JOIN TB_GOODS_COMPOSE GC
				  ON G.GOODS_CD = GC.GOODS_CD
			   INNER JOIN TB_OPTION OP
				  ON IFNULL(GC.COMPS_GOODS_CD, G.GOODS_CD) = OP.GOODS_CD
				LEFT OUTER JOIN TB_GOODS_IMG GI
				  ON G.GOODS_CD = GI.GOODS_CD
			   INNER JOIN TB_BRAND B
				  ON G.BRAND_CD = B.BRAND_CD
			   WHERE G.GOODS_CD IN
		<foreach collection="arrGoodsCd" item="item" index="index"  open="(" close=")" separator=",">
			#{item}
		</foreach>
		  ) Z
		 GROUP BY SYS_IMG_NM, GOODS_CD, GOODS_NM, STOCK_QTY_SUM, GOODS_STAT, STYLE_YEAR
				, SEASON_CD, LIST_PRICE, CURR_PRICE, PRICE_UPD_DT, ERP_STOCK_LINK_YN, FORMAL_GB, RETURNABLE_YN, REG_DT, REG_NM
				, UPD_DT, UPD_NM, REP_GOODS_CD
	</select>
	
	<select id="getOrderSampleGoodsStock" parameterType="Goods" resultType="Goods">
		SELECT GOODS_CD
		     , OPT_CD2 AS SITE_CD
		     , MAX(CURR_STOCK_QTY - BASE_STOCK_QTY) AS MIN_ORD_QTY
		  FROM VW_STOCK
		 WHERE GOODS_CD = #{goodsCd}
		   AND OPT_CD2 = #{siteCd}
		 GROUP BY GOODS_CD, OPT_CD2
	</select>
	
	<insert id="createOrderSample" parameterType="OrderSample" keyProperty="ordNo">
		INSERT INTO TB_ORDER
			( MALL_GB
			, ORD_DT
			, PAY_DT
			, CUST_NO
			, ORD_NM
			, ORD_PHNNO
			, ORD_TELNO
			, ORD_EMAIL
			, SITE_CD
			, NPAY_ORD_NO
			, FRONT_GB
			, REG_NO
			, REG_DT
			, UPD_NO
			, UPD_DT
			)
		VALUES
			( 'G011_10'			-- MALL_GB
			, NOW()				-- ORD_DT
			, NOW()				-- PAY_DT
			, #{custNo}			-- CUST_NO
			, #{ordNm}			-- ORD_NM
			, #{ordPhnno}		-- ORD_PHNNO
			, NULL				-- ORD_TELNO
			, #{ordEmail}		-- ORD_EMAIL
			, 'G000_10'			-- SITE_CD
			, 0					-- NPAY_ORD_NO
			, 'P'				-- FRONT_GB
			, #{custNo}			-- REG_NO
			, NOW()				-- REG_DT
			, #{custNo}			-- UPD_NO
			, NOW()				-- UPD_DT
			)
	</insert>
	
	<insert id="createDelveryAddrSample" parameterType="OrderSample" keyProperty="delvAddrSq">
		INSERT INTO TB_DELIVERY_ADDR
			( RECIP_NM
			, RECIP_PHNNO
			, RECIP_TELNO
			, RECIP_ZIPCODE
			, RECIP_BASE_ADDR
			, RECIP_DTL_ADDR
			, DELV_MEMO
			, REG_NO
			, REG_DT
			, UPD_NO
			, UPD_DT
			)
		VALUES
			( #{ordNm}				-- RECIP_NM
			, #{ordPhnno}			-- RECIP_PHNNO
			, NULL					-- RECIP_TELNO
			, #{recipZipcode}		-- RECIP_ZIP_NO
			, #{recipBaseAddr}		-- RECIP_BASE_ADDR
			, #{recipDtlAddr}		-- RECIP_DTL_ADDR
			, '배송메모'				-- DELV_MEMO
			, #{custNo}				-- REG_NO
			, NOW()					-- REG_DT
			, #{custNo}				-- UPD_NO
			, NOW()					-- UPD_DT
			)
	</insert>
	
	<insert id="createDeliveryFeeSample" parameterType="OrderSample" keyProperty="delvFeeSq">
		INSERT INTO TB_DELIVERY_FEE
			( PAY_SQ
			, ORD_NO
			, DELV_FEE_GB
			, DELV_FEE_CD
			, DELV_FEE
			, DELV_CPN_SQ
			, DELV_CPN_DC_AMT
			, REAL_DELV_AMT
			, ORD_CHG_SQ
			, SUPPLY_COMP_CD
			, DELV_USAC_YN
			, DELV_USAC_DT
			, REG_NO
			, REG_DT
			, UPD_NO
			, UPD_DT
			)
		SELECT 0               -- PAY_SQ
			 , #{ordNo}        -- ORD_NO
			 , #{delvFeeGb}    -- DELV_FEE_GB
			 , G.DELV_FEE_CD   -- DELV_FEE_CD
			 , 0               -- DELV_FEE
			 , NULL            -- DELV_CPN_SQ
			 , 0               -- DELV_CPN_DC_AMT
			 , 0               -- REAL_DELV_AMT
			 , NULL            -- ORD_CHG_SQ
			 , G.SUPPLY_COMP_CD -- SUPPLY_COMP_CD
			 , 'N'             -- DELV_USAC_YN
			 , NULL            -- DELV_USER_DT
			 , #{custNo}       -- REG_NO
			 , NOW()           -- REG_DT
			 , #{custNo}       -- UPD_NO
			 , NOW()           -- UPO_DT
		  FROM TB_GOODS G
		 INNER JOIN TB_DELV_FEE_POLICY DFP
		    ON G.DELV_FEE_CD = DFP.DELV_FEE_CD
		 WHERE G.GOODS_CD = #{goodsCd}
		   AND NOT EXISTS(SELECT 1
							FROM TB_DELIVERY_FEE
						   WHERE ORD_NO <![CDATA[<>]]> #{ordNo}
							 AND DELV_FEE_GB <![CDATA[<>]]> #{delvFeeGb}
							 AND DELV_FEE_CD <![CDATA[<>]]> G.DELV_FEE_CD)
	</insert>
	
	<insert id="createOrderDetailSample" parameterType="OrderSample" keyProperty="ordDtlNo">
		INSERT INTO TB_ORDER_DETAIL
			( ORD_NO
			, ORD_EXCH_GB
			, ORD_DTL_STAT
			, ORG_ORD_DTL_NO
			, SUPPLY_COMP_CD
			, GOODS_CD
			, PRODUCT_NO
			, PRODUCT_CODE
			, FORMAL_GB
			, GOODS_TYPE
			, LIST_PRICE
			, CURR_PRICE
			, DC_RATE
			, OPT_ADD_PRICE
			, ORD_QTY
			, ORD_AMT
			, CNCL_RTN_QTY
			, CNCL_RTN_AMT
			, CPN1_CPN_SQ
			, CPN1_DC_AMT
			, TMTB1_SQ
			, TMTB1_DC_AMT
			, TMTB2_SQ
			, TMTB2_DC_AMT
			, GOODS_CPN_SQ
			, GOODS_CPN_DC_AMT
			, CART_CPN_SQ
			, CART_CPN_DC_AMT
			, BURDEN_RATE
			, PNT_DC_AMT
			, PRE_PNT_DC_AMT
			, SAVE_PNT_AMT
			, REAL_ORD_AMT
			, GFCD_USE_AMT
			, VENDOR_ID
			, EXTMALL_ID
			, AGENT_ORDER_ID
			, EXTMALL_ORDER_ID
			, CHANGABLE_YN
			, CHANGE_FEE_FREE_YN
			, RETURNABLE_YN
			, RETURN_FEE_FREE_YN
			, SOLDOUT_YN
			, SOLDOUT_MEMO
			, SOLDOUT_REG_NO
			, SOLDOUT_REG_DT
			, DELV_ADDR_SQ
			, DELV_FEE_CD
			, SHOT_DELV_YN
			, GIFT_PACK_YN
			, MAKE_GOODS_YN
			, ENTRY_NO
			, DELV_LOC_CD
			, DELV_ASSIGN_DT
			, DELV_ASSIGN_STAT
			, DSTRBT_NOTE
			, DELV_STDT
			, DELV_EDDT
			, SHIP_COMP_CD
			, INVOICE_NO
			, INVOICE_SEND_YN
			, SELL_STORE_CD
			, SELL_FEE_RATE
			, AF_LINK_CD
			, ITHR_CD
			, CONTENTS_LOC
			, PLAN_DTL_SQ
			, SOCIAL_SQ
			, REG_NO
			, REG_DT
			, UPD_NO
			, UPD_DT
			)
		SELECT #{ordNo} -- ORD_NO
			, 'O' -- ORD_EXCH_GB
			, #{ordDtlStat} -- ORD_DTL_STAT
			, NULL -- ORG_ORD_DTL_NO
			, G.SUPPLY_COMP_CD -- SUPPLY_COMP_CD
			, G.GOODS_CD -- GOODS_CD
			, G.PRODUCT_NO -- PRODUCT_NO
			, G.PRODUCT_CODE -- PRODUCT_CODE
			, G.FORMAL_GB -- FORMAL_GB
			, G.GOODS_TYPE -- GOODS_TYPE
			, G.LIST_PRICE -- LIST_PRICE
			, G.CURR_PRICE -- CURR_PRICE
			, G.DC_RATE -- DC_RATE
			, 0 -- OPT_ADD_PRICE
			, #{ordQty} -- ORD_QTY
			, #{ordQty} * G.CURR_PRICE -- ORD_AMT
			, 0 -- CNCL_RTN_QTY
			, 0 -- CNCL_RTN_AMT
			, 0 -- CPN1_CPN_SQ
			, 0 -- CPN1_DC_AMT
			, 0 -- TMTB1_SQ
			, 0 -- TMTB1_DC_AMT
			, 0 -- TMTB2_SQ
			, 0 -- TMTB2_DC_AMT
			, 0 -- GOODS_CPN_SQ
			, 0 -- GOODS_CPN_DC_AMT
			, 0 -- CART_CPN_SQ
			, 0 -- CART_CPN_DC_AMT
			, 0 -- BURDEN_RATE
			, 0 -- PNT_DC_AMT
			, 0 -- PRE_PNT_DC_AMT
			, 0 -- SAVE_PNT_AMT
			, #{ordQty} * G.CURR_PRICE -- REAL_ORD_AMT
			, 0 -- GFCD_USE_AMT
			, '' -- VENDOR_ID
			, '' -- EXTMALL_ID
			, '' -- AGENT_ORDER_ID
			, '' -- EXTMALL_ORDER_ID
			, 'Y' -- CHANGABLE_YN
			, 'Y' -- CHANGE_FEE_FREE_YN
			, 'Y' -- RETURNABLE_YN
			, 'Y' -- RETURN_FEE_FREE_YN
			, 'N' -- SOLDOUT_YN
			, '' -- SOLDOUT_MEMO
			, NULL -- SOLDOUT_REG_NO
			, NOW() -- SOLDOUT_REG_DT
			, #{delvAddrSq} -- DELV_ADDR_SQ
			, G.DELV_FEE_CD -- DELV_FEE_CD
			, 'N' -- SHOT_DELV_YN
			, 'N' -- GIFT_PACK_YN
			, 'N' -- MAKE_GOODS_YN
			, '' -- ENTRY_NO
			, '' -- DELV_LOC_CD
			, NOW() -- DELV_ASSIGN_DT
			, '' -- DELV_ASSIGN_STAT
			, '' -- DSTRBT_NOTE
			, NOW() -- DELV_STDT
			, NOW() -- DELV_EDDT
			, 'S0001' -- SHIP_COMP_CD
			, '' -- INVOICE_NO
			, '' -- INVOICE_SEND_YN
			, '' -- SELL_STORE_CD
			, 0 -- SELL_FEE_RATE
			, '' -- AF_LINK_CD
			, '' -- ITHR_CD
			, '' -- CONTENTS_LOC
			, NULL -- PLAN_DTL_SQ
			, NULL -- SOCIAL_SQ
			, #{custNo} -- REG_NO
			, NOW() -- REG_DT
			, #{custNo} -- UPD_NO
			, NOW() -- UPD_DT
			FROM TB_GOODS G
		 WHERE G.GOODS_CD = #{goodsCd}
		   AND NOT EXISTS (SELECT 1 FROM TB_ORDER_DETAIL WHERE ORD_NO = #{ordNo} AND GOODS_CD = #{goodsCd})
	</insert>
	
	<insert id="createOrderDetailItemSample" parameterType="OrderSample" keyProperty="">
		INSERT INTO TB_ORDER_DETAIL_ITEM
			( ORD_DTL_NO
			, ORD_NO
			, ORD_DTL_STAT
			, ITEM_CD
			, OPT_CD
			, OPT_CD1
			, OPT_CD2
			, SKU_MODEL_NO
			, PRODUCT_NO
			, PRODUCT_CODE
			, ITEM_QTY
			, ITEM_PRICE
			, OPT_ADD_PRICE
			, DISP_ORD
			, ORD_AMT
			, CNCL_RTN_AMT
			, CPN1_DC_AMT
			, TMTB1_DC_AMT
			, TMTB2_DC_AMT
			, GOODS_CPN_DC_AMT
			, CART_CPN_DC_AMT
			, PNT_DC_AMT
			, PRE_PNT_DC_AMT
			, SAVE_PNT_AMT
			, REAL_ORD_AMT
			, GFCD_USE_AMT
			, REG_NO
			, REG_DT
			, UPD_NO
			, UPD_DT
			)
		SELECT OD.ORD_DTL_NO       -- ORD_DTL_NO
			 , OD.ORD_NO           -- ORD_NO
			 , OD.ORD_DTL_STAT     -- ORD_DTL_STAT
			 , G.GOODS_CD         -- ITEM_CD
			 , OP.OPT_CD          -- OPT_CD
			 , OP.OPT_CD1         -- OPT_CD1
			 , OP.OPT_CD2         -- OPT_CD2
			 , OP.SKU_MODEL_NO    -- SKU_MODEL_NO
			 , OP.PRODUCT_NO      -- PRODUCT_NO
			 , OP.PRODUCT_CODE    -- PRODUCT_CODE
			 , IFNULL(GC.QTY, 1)  -- ITEM_QTY
			 , IFNULL(GC.COMPS_CURR_PRICE, G.CURR_PRICE)       -- ITEM_PRICE
			 , 0                  -- OPT_ADD_PRICE
			 , IFNULL(GC.DISP_ORD, 1)                  -- DISP_ORD
			 , #{ordQty} * IFNULL(GC.COMPS_CURR_PRICE, G.CURR_PRICE)          -- ORD_AMT
			 , 0     -- CNCL_RTN_AMT
			 , 0      -- CPN1_DC_AMT
			 , 0     -- TMTB1_DC_AMT
			 , 0     -- TMTB2_DC_AMT
			 , 0 -- GOODS_CPN_DC_AMT
			 , 0  -- CART_CPN_DC_AMT
			 , 0       -- PNT_DC_AMT
			 , 0   -- PRE_PNT_DC_AMT
			 , 0     -- SAVE_PNT_AMT
			 , #{ordQty} * IFNULL(GC.COMPS_CURR_PRICE, G.CURR_PRICE)     -- REAL_ORD_AMT
			 , 0     -- GFCD_USE_AMT
			 , #{custNo}          -- REG_NO
			 , NOW()              -- REG_DT
			 , #{custNo}          -- UPD_NO
			 , NOW()              -- UPD_DT
		  FROM TB_ORDER_DETAIL OD
		 INNER JOIN TB_GOODS                     G
		    ON OD.GOODS_CD = G.GOODS_CD
		  LEFT OUTER JOIN TB_GOODS_COMPOSE GC
		    ON G.GOODS_CD = GC.GOODS_CD
		   AND GC.COMPS_GOODS_CD = #{compsGoodsCd}
		 INNER JOIN TB_OPTION                    OP
		    ON IFNULL(GC.COMPS_GOODS_CD, G.GOODS_CD) = OP.GOODS_CD
		   AND OP.OPT_CD2 = #{optCd}
		 WHERE OD.ORD_DTL_NO = #{ordDtlNo}
	</insert>
	
	<insert id="createPaymentSample" parameterType="OrderSample" keyProperty="paySq">
		INSERT INTO TB_PAYMENT
			( ORD_NO
			, PAY_DT
			, PAY_MEANS
			, PAY_AMT
			, PG_CPN_AMT
			, NPAY_PNT_AMT
			, PAY_GB
			, PAY_STAT
			, PG_GB
			, PG_TID
			, PG_TRADE_NO
			, PG_SHOP_ID
			, CARD_TYPE
			, CARD_KIND
			, CARD_BANK
			, CARD_NM
			, CARD_MIPS
			, CARD_PCABLE_YN
			, VA_NO
			, VA_NM
			, VA_BANK
			, VA_DEADLINE
			, TELECOM
			, ESCROW_YN
			, ORD_CHG_SQ
			, REG_NO
			, REG_DT
			, UPD_NO
			, UPD_DT
			)
		SELECT OD.ORD_NO   -- ORD_NO
			 , NOW()      -- PAY_DT
			 , 'G014_30'  -- PAY_MEANS
			 , SUM(OD.REAL_ORD_AMT + DF.REAL_DELV_AMT)  -- PAY_AMT
			 , 0          -- PG_CPN_AMT
			 , 0          -- NPAY_CPN_AMT
			 , 'O'        -- PAY_GB
			 , 'G016_30'  -- PAY_STAT
			 , 'KCP'      -- PG_GB
			 , 'TID00001' -- PG_TID
			 , '101010'   -- PG_TRADE_NO
			 , 'STYLE24'  -- PG_SHOP_ID
			 , 'Y'        -- CARD_TYPE
			 , ''         -- CARD_KIND
			 , ''         -- CARD_BANK
			 , '신한카드'     -- CARD_NM
			 , ''         -- CARD_MIPS
			 , ''         -- CARD_PCABLE_YN
			 , ''         -- VA_NO
			 , ''         -- VA_NM
			 , ''         -- VA_BANK
			 , NOW()      -- VA_DEADLINE
			 , ''         -- TELECOM
			 , ''         -- ESCROW_YN
			 , NULL       -- ORD_CHG_SQ
			 , #{custNo}          -- REG_NO
			 , NOW()      -- REG_DT
			 , #{custNo}          -- UPD_NO
			 , NOW()      -- UPD_DT
		  FROM TB_ORDER_DETAIL OD
		 INNER JOIN (SELECT ORD_NO
						  , SUM(REAL_DELV_AMT) AS REAL_DELV_AMT
					   FROM TB_DELIVERY_FEE
					  WHERE ORD_NO = #{ordNo}
					  GROUP BY ORD_NO) DF
		    ON OD.ORD_NO = DF.ORD_NO
		 WHERE OD.ORD_NO = #{ordNo}
		 GROUP BY OD.ORD_NO
	</insert>
	
	<update id="updateDeliveryFeeSample" parameterType="OrderSample">
		UPDATE TB_DELIVERY_FEE
		   SET PAY_SQ = #{paySq}
		 WHERE ORD_NO = #{ordNo}
	</update>
	<!-- //주문 샘플 데이터 (추후 삭제 예정) -->
	
</mapper>















