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

	<!-- 주문목록 카운트 -->
	<select id="getOrderListCount" parameterType="Order" resultType="int">
		/* order.getOrderListCnt */
		SELECT COUNT(1)  
		FROM   TB_ORDER O
		INNER  JOIN TB_ORDER_DETAIL OD
		ON     O.ORD_NO = OD.ORD_NO
		INNER  JOIN TB_GOODS G1
		ON     OD.GOODS_CD = G1.GOODS_CD
		INNER  JOIN TB_ORDER_DETAIL_ITEM ODI
		ON     OD.ORD_NO = ODI.ORD_NO
		AND    OD.ORD_DTL_NO = ODI.ORD_DTL_NO
		INNER  JOIN TB_GOODS G2
		ON     ODI.ITEM_CD = G2.GOODS_CD
		INNER  JOIN TB_GOODS_IMG GI
		ON     ODI.ITEM_CD = GI.GOODS_CD
		AND    ODI.OPT_CD1 = GI.COLOR_CD
		INNER  JOIN TB_PAYMENT P
		ON     O.ORD_NO = P.ORD_NO
		AND    P.PAY_GB = 'O'
		INNER  JOIN TB_DELIVERY_ADDR DA
		ON     OD.DELV_ADDR_SQ = DA.DELV_ADDR_SQ
		LEFT   OUTER JOIN (
			SELECT OC.WD_INVOICE_NO
			     , OC.WD_INVOICE_SEND_YN
			     , OC.SHIP_COMP_CD
			     , OCD.ORD_DTL_NO 
			     , OCD.CHG_QTY
			     , OCD.CHG_STAT
			FROM   TB_ORDER_CHANGE OC
			INNER  JOIN TB_ORDER_CHANGE_DETAIL OCD
			ON     OC.ORD_CHG_SQ  = OCD.ORD_CHG_SQ 
		) OCD
		ON    OD.ORD_DTL_NO = OCD.ORD_DTL_NO
		WHERE  1=1
		<if test='siteCd != null and siteCd != ""'>
		AND    O.SITE_CD = #{siteCd}
		</if>
		<if test='mallCd != null and mallCd != ""'>
		AND    O.MALL_GB = #{mallCd}
		</if>
		<if test='supplyCompCd != null and supplyCompCd != ""'>
		AND    OD.SUPPLY_COMP_CD = #{supplyCompCd}
		</if>
		<if test="multiBrand != null and multiBrand != ''">
        AND    G1.BRAND_CD IN
			<foreach collection="multiBrand" item="item" index="index"  open="(" close=")" separator=",">
        		#{item}
            </foreach>
        </if>
        <if test="multiOrdDtlStat != null and multiOrdDtlStat != ''">
        AND    OD.ORD_DTL_STAT IN
			<foreach collection="multiOrdDtlStat" item="item" index="index"  open="(" close=")" separator=",">
        		#{item}
            </foreach>
        </if>
        <if test="multiOrdDtlStat == null or multiOrdDtlStat == ''">
        	<if test="multiOrdStat != null and multiOrdStat != ''">
        AND    OD.ORD_DTL_STAT IN
				<foreach collection="multiOrdStat" item="item" index="index"  open="(" close=")" separator=",">
	        		#{item}
	            </foreach>
            </if>
        </if>
		<if test="chgStat != null and chgStat != ''">
        AND    OCD.CHG_STAT = #{chgStat}
        </if>
        <if test="chgStat == null or chgStat == ''">
        	<if test="chgGb != null and chgGb != ''">
        AND    OCD.CHG_STAT = #{chgGb}
        	</if>
        </if>
		<if test="chgStat != null and chgStat != ''">
        AND    OCD.CHG_STAT = #{chgStat}
        </if>
        <if test="chgStat == null or chgStat == ''">
        AND    OCD.CHG_STAT = #{chgGb}
        </if>
        <if test='search != null and search == "searchOrdNo"'>
        	<if test="condition != null and condition != ''">
		AND    O.ORD_NO = #{condition}
			</if>
		</if>
		<if test='search != null and search == "searchExtmallOrderId"'>
        	<if test="condition != null and condition != ''">
		AND    OD.EXTMALL_ORDER_ID = #{condition}
			</if>
		</if>
		<if test='search != null and search == "searchAgentOrderId"'>
        	<if test="condition != null and condition != ''">
		AND    OD.AGENT_ORDER_ID = #{condition}
			</if>
		</if>
		<if test='payMeans != null and payMeans != ""'>
		AND    P.PAY_MEANS = #{payMeans}
		</if>
		<if test='orderNm != null and orderNm != ""'>
		AND    O.ORD_NM = #{orderNm}
		</if>
		<if test='custId != null and custId != ""'>
		AND    O.CUST_NO = #{custId}
		</if>
		<if test='orderPhnno != null and orderPhnno != ""'>
		AND    O.ORD_PHNNO = #{orderPhnno}
		</if>
		<if test='goodsCd != null and goodsCd != ""'>
		AND    G1.GOODS_CD = #{goodsCd}
		</if>
		<if test='sizeCd != null and sizeCd != ""'>
		AND    ODI.OPT_CD2 = #{sizeCd}
		</if>
		<if test='goodsNm != null and goodsNm != ""'>
		AND    G1.GOODS_NM = #{goodsNm}
		</if>
		<if test='payMeans != null and payMeans != ""'>
		AND    P.PAY_MEANS = #{payMeans}
		</if>
		<if test='orderNm != null and orderNm != ""'>
		AND    O.ORD_NM = #{orderNm}
		</if>
		<if test='custId != null and custId != ""'>
		AND    O.CUST_NO = #{custId}
		</if>
		<if test='orderPhnno != null and orderPhnno != ""'>
		AND    O.ORD_PHNNO = #{orderPhnno}
		</if>
		<if test='goodsCd != null and goodsCd != ""'>
		AND    G2.GOODS_CD = #{goodsCd}
		</if>
		<if test='sizeCd != null and sizeCd != ""'>
		AND    ODI.OPT_CD2 = #{sizeCd}
		</if>
		<if test='goodsNm != null and goodsNm != ""'>
		AND    G2.GOODS_NM = #{goodsNm}
		</if>
		<if test='recipNm != null and recipNm != ""'>
		AND    DA.RECIP_NM = #{recipNm}
		</if>
		<if test='invoiceNo != null and invoiceNo != ""'>
		AND    OD.INVOICE_NO = #{invoiceNo}
		</if>
		<if test='wdInvoiceSendYn != null and wdInvoiceSendYn == "Y"'>
		AND    OD.INVOICE_SEND_YN = 'Y'
		</if>
		<if test='wdInvoiceSendYn != null and wdInvoiceSendYn == "N"'>
		AND    OD.INVOICE_SEND_YN = 'N'
		</if>
		<if test='searchDateGb != null and searchDateGb == "ordDt"'>
		AND    O.ORD_DT >= DATE_FORMAT(#{stDate}, '%Y-%m-%d %H:%i:%S')
		AND    O.ORD_DT <![CDATA[<]]> DATE_FORMAT(DATE_ADD(#{edDate}, INTERVAL 1 DAY), '%Y-%m-%d %H:%i:%S')
		</if>
		<if test='searchDateGb != null and searchDateGb == "delvStdt"'>
		AND    OD.DELV_STDT >= DATE_FORMAT(#{stDate}, '%Y-%m-%d %H:%i:%S')
		AND    OD.DELV_STDT <![CDATA[<]]> DATE_FORMAT(DATE_ADD(#{edDate}, INTERVAL 1 DAY), '%Y-%m-%d %H:%i:%S')
		</if>
		<if test='searchDateGb != null and searchDateGb == "soldoutDt"'>
		AND    OD.SOLDOUT_REG_DT >= DATE_FORMAT(#{stDate}, '%Y-%m-%d %H:%i:%S')
		AND    OD.SOLDOUT_REG_DT <![CDATA[<]]> DATE_FORMAT(DATE_ADD(#{edDate}, INTERVAL 1 DAY), '%Y-%m-%d %H:%i:%S')
		</if>
		<if test="ordNoList != null and ordNoList != ''">
        AND    OD.ORD_NO IN
			<foreach collection="ordNoList" item="item" index="index"  open="(" close=")" separator=",">
        		#{item}
            </foreach>
        </if>
	</select>

	<!-- 주문목록 -->
	<select id="getOrderList" parameterType="Order" resultType="Order">
		/* order.getOrderList */
		SELECT Z.*
		FROM (
			SELECT @rownum := @rownum + 1 AS RNUM
			     , A.*
			FROM (
				SELECT O.ORD_NO
				     , OD.ORD_DTL_NO
				     , ODI.ORD_DTL_ITEM_SQ
				     , O.ORD_NM
				     , O.CUST_NO
				     , OD.VENDOR_ID
				     , O.FRONT_GB
				     , DATE_FORMAT(O.ORD_DT,'%Y%m%d%H%i%S') AS ORD_DT
				     , OD.ORD_EXCH_GB
				     , OD.ORD_DTL_STAT
				     , OD.DELV_LOC_CD
				     , G1.GOODS_CD 
				     , G1.GOODS_NM
				     , G1.GOODS_TYPE
				     , FN_GET_CODE_NM('G056', G1.GOODS_TYPE) AS GOODS_TYPE_NM
				     , GI.SYS_IMG_NM
				     , G2.BRAND_CD
				     , (SELECT Z.BRAND_ENM
				        FROM   TB_BRAND Z
				        WHERE  Z.BRAND_CD = G2.BRAND_CD) AS BRAND_ENM
				     , G2.GOODS_CD AS ITEM_CD
				     , G2.GOODS_NM AS ITEM_NM
				     , ODI.OPT_CD1
				     , ODI.OPT_CD2
				     , OD.EXTMALL_ORDER_ID
				     , OD.AGENT_ORDER_ID 
				     , ODI.ITEM_QTY * OD.ORD_QTY      AS ORD_QTY
				     , ODI.ITEM_QTY * OD.CNCL_RTN_QTY AS CNCL_RTN_QTY
				     , ODI.ORD_AMT
				     , ODI.CNCL_RTN_AMT
				     , ODI.CPN1_DC_AMT + ODI.TMTB1_DC_AMT + ODI.TMTB2_DC_AMT +  ODI.GOODS_CPN_DC_AMT + ODI.CART_CPN_DC_AMT + ODI.PNT_DC_AMT + ODI.PRE_PNT_DC_AMT AS TOT_DC_AMT
				     , ODI.REAL_ORD_AMT
				     , G2.SUPPLY_GOODS_CD
				     , OD.FORMAL_GB
				     , DATE_FORMAT(OD.DELV_ASSIGN_DT,'%Y%m%d%H%i%S') AS DELV_ASSIGN_DT 
				     , OD.INVOICE_NO
				     , OD.DELV_STDT
				     , OD.SOLDOUT_YN
				     , OD.SOLDOUT_MEMO
				     , DATE_FORMAT(OD.SOLDOUT_REG_DT,'%Y%m%d%H%i%S') AS SOLDOUT_REG_DT 
				     , OD.SOLDOUT_REG_NO     
				FROM   TB_ORDER O
				INNER  JOIN TB_ORDER_DETAIL OD
				ON     O.ORD_NO = OD.ORD_NO
				INNER  JOIN TB_GOODS G1
				ON     OD.GOODS_CD = G1.GOODS_CD
				INNER  JOIN TB_ORDER_DETAIL_ITEM ODI
				ON     OD.ORD_NO = ODI.ORD_NO
				AND    OD.ORD_DTL_NO = ODI.ORD_DTL_NO
				INNER  JOIN TB_GOODS G2
				ON     ODI.ITEM_CD = G2.GOODS_CD
				INNER  JOIN TB_GOODS_IMG GI
				ON     ODI.ITEM_CD = GI.GOODS_CD
				AND    ODI.OPT_CD1 = GI.COLOR_CD
				INNER  JOIN TB_PAYMENT P
				ON     O.ORD_NO = P.ORD_NO
				AND    P.PAY_GB = 'O'
				INNER  JOIN TB_DELIVERY_ADDR DA
				ON     OD.DELV_ADDR_SQ = DA.DELV_ADDR_SQ
				LEFT   OUTER JOIN (
					SELECT OC.WD_INVOICE_NO
					     , OC.WD_INVOICE_SEND_YN
					     , OC.SHIP_COMP_CD
					     , OCD.ORD_DTL_NO 
					     , OCD.CHG_QTY
					     , OCD.CHG_STAT
					FROM   TB_ORDER_CHANGE OC
					INNER  JOIN TB_ORDER_CHANGE_DETAIL OCD
					ON     OC.ORD_CHG_SQ  = OCD.ORD_CHG_SQ 
				) OCD
				ON    OD.ORD_DTL_NO = OCD.ORD_DTL_NO
				WHERE  1=1
				<if test='siteCd != null and siteCd != ""'>
				AND    O.SITE_CD = #{siteCd}
				</if>
				<if test='mallCd != null and mallCd != ""'>
				AND    O.MALL_GB = #{mallCd}
				</if>
				<if test='supplyCompCd != null and supplyCompCd != ""'>
				AND    OD.SUPPLY_COMP_CD = #{supplyCompCd}
				</if>
				<if test="multiBrand != null and multiBrand != ''">
		        AND    G1.BRAND_CD IN
					<foreach collection="multiBrand" item="item" index="index"  open="(" close=")" separator=",">
		        		#{item}
		            </foreach>
		        </if>
		        <if test="multiOrdDtlStat != null and multiOrdDtlStat != ''">
		        AND    OD.ORD_DTL_STAT IN
					<foreach collection="multiOrdDtlStat" item="item" index="index"  open="(" close=")" separator=",">
		        		#{item}
		            </foreach>
		        </if>
		        <if test="multiOrdDtlStat == null or multiOrdDtlStat == ''">
		        	<if test="multiOrdStat != null and multiOrdStat != ''">
		        AND    OD.ORD_DTL_STAT IN
						<foreach collection="multiOrdStat" item="item" index="index"  open="(" close=")" separator=",">
			        		#{item}
			            </foreach>
		            </if>
		        </if>
				<if test="chgStat != null and chgStat != ''">
		        AND    OCD.CHG_STAT = #{chgStat}
		        </if>
		        <if test="chgStat == null or chgStat == ''">
		        	<if test="chgGb != null and chgGb != ''">
		        AND    OCD.CHG_STAT = #{chgGb}
		        	</if>
		        </if>
		        <if test='search != null and search == "searchOrdNo"'>
		        	<if test="condition != null and condition != ''">
				AND    O.ORD_NO = #{condition}
					</if>
				</if>
				<if test='search != null and search == "searchExtmallOrderId"'>
		        	<if test="condition != null and condition != ''">
				AND    OD.EXTMALL_ORDER_ID = #{condition}
					</if>
				</if>
				<if test='search != null and search == "searchAgentOrderId"'>
		        	<if test="condition != null and condition != ''">
				AND    OD.AGENT_ORDER_ID = #{condition}
					</if>
				</if>
				<if test='payMeans != null and payMeans != ""'>
				AND    P.PAY_MEANS = #{payMeans}
				</if>
				<if test='orderNm != null and orderNm != ""'>
				AND    O.ORD_NM = #{orderNm}
				</if>
				<if test='custId != null and custId != ""'>
				AND    O.CUST_NO = #{custId}
				</if>
				<if test='orderPhnno != null and orderPhnno != ""'>
				AND    O.ORD_PHNNO = #{orderPhnno}
				</if>
				<if test='goodsCd != null and goodsCd != ""'>
				AND    G1.GOODS_CD = #{goodsCd}
				</if>
				<if test='sizeCd != null and sizeCd != ""'>
				AND    ODI.OPT_CD2 = #{sizeCd}
				</if>
				<if test='goodsNm != null and goodsNm != ""'>
				AND    G1.GOODS_NM = #{goodsNm}
				</if>
				<if test='payMeans != null and payMeans != ""'>
				AND    P.PAY_MEANS = #{payMeans}
				</if>
				<if test='orderNm != null and orderNm != ""'>
				AND    O.ORD_NM = #{orderNm}
				</if>
				<if test='custId != null and custId != ""'>
				AND    O.CUST_NO = #{custId}
				</if>
				<if test='orderPhnno != null and orderPhnno != ""'>
				AND    O.ORD_PHNNO = #{orderPhnno}
				</if>
				<if test='goodsCd != null and goodsCd != ""'>
				AND    G2.GOODS_CD = #{goodsCd}
				</if>
				<if test='sizeCd != null and sizeCd != ""'>
				AND    ODI.OPT_CD2 = #{sizeCd}
				</if>
				<if test='goodsNm != null and goodsNm != ""'>
				AND    G2.GOODS_NM = #{goodsNm}
				</if>
				<if test='recipNm != null and recipNm != ""'>
				AND    DA.RECIP_NM = #{recipNm}
				</if>
				<if test='invoiceNo != null and invoiceNo != ""'>
				AND    OD.INVOICE_NO = #{invoiceNo}
				</if>
				<if test='wdInvoiceSendYn != null and wdInvoiceSendYn == "Y"'>
				AND    OD.INVOICE_SEND_YN = 'Y'
				</if>
				<if test='wdInvoiceSendYn != null and wdInvoiceSendYn == "N"'>
				AND    OD.INVOICE_SEND_YN = 'N'
				</if>
				<if test='searchDateGb != null and searchDateGb == "ordDt"'>
				AND    O.ORD_DT >= DATE_FORMAT(#{stDate}, '%Y-%m-%d %H:%i:%S')
				AND    O.ORD_DT <![CDATA[<]]> DATE_FORMAT(DATE_ADD(#{edDate}, INTERVAL 1 DAY), '%Y-%m-%d %H:%i:%S')
				</if>
				<if test='searchDateGb != null and searchDateGb == "delvStdt"'>
				AND    OD.DELV_STDT >= DATE_FORMAT(#{stDate}, '%Y-%m-%d %H:%i:%S')
				AND    OD.DELV_STDT <![CDATA[<]]> DATE_FORMAT(DATE_ADD(#{edDate}, INTERVAL 1 DAY), '%Y-%m-%d %H:%i:%S')
				</if>
				<if test='searchDateGb != null and searchDateGb == "soldoutDt"'>
				AND    OD.SOLDOUT_REG_DT >= DATE_FORMAT(#{stDate}, '%Y-%m-%d %H:%i:%S')
				AND    OD.SOLDOUT_REG_DT <![CDATA[<]]> DATE_FORMAT(DATE_ADD(#{edDate}, INTERVAL 1 DAY), '%Y-%m-%d %H:%i:%S')
				</if>
				<if test="ordNoList != null and ordNoList != ''">
		        AND    OD.ORD_NO IN
					<foreach collection="ordNoList" item="item" index="index"  open="(" close=")" separator=",">
		        		#{item}
		            </foreach>
		        </if>
		        ORDER  BY OD.ORD_NO DESC
		                , OD.ORD_DTL_NO DESC
		                , ODI.ORD_DTL_ITEM_SQ DESC
	        ) A
			JOIN (SELECT @rownum := 0) R
		) Z
		WHERE  1=1
        AND    Z.RNUM BETWEEN  #{pageable.startRow} AND #{pageable.endRow}
        ORDER  BY Z.ORD_NO DESC
                , Z.ORD_DTL_NO DESC
                , Z.ORD_DTL_ITEM_SQ DESC
	</select>
	
	<!-- 주문 목록 금액, 수량 정보 : 사용안함 -->
	<select id="getOrderListSumInfo" parameterType="Order" resultType="Order">
		/* order.getOrderListSumInfo */
		SELECT SUM(ORD_AMT)                   AS SUM_ORD_AMT
		     , SUM(ORD_AMT - CNCL_RTN_AMT)    AS SUM_ORD_CNCL_AMT
		     , SUM(REAL_ORD_AMT)              AS SUM_REAL_PAY_AMT
		     , COUNT(DISTINCT ORD_NO)         AS ORD_NO_CNT
		     , SUM(ORD_QTY)                   AS SUM_ORD_QTY
		     , SUM(ORD_QTY - CNCL_RTN_QTY)    AS SUM_ORD_CNCL_QTY
		FROM   (
			SELECT O.ORD_NO
			     , OD.GOODS_CD
			     , OD.ORD_AMT
			     , OD.CNCL_RTN_AMT
			     , OD.CNCL_RTN_QTY
			     , OD.ORD_QTY
			     , OD.REAL_ORD_AMT
			FROM   TB_ORDER O
			INNER  JOIN TB_ORDER_DETAIL OD
			ON     O.ORD_NO = OD.ORD_NO
			INNER  JOIN TB_ORDER_DETAIL_ITEM ODI
			ON     OD.ORD_NO = ODI.ORD_NO
			AND    OD.ORD_DTL_NO = ODI.ORD_DTL_NO
			INNER  JOIN TB_GOODS G
			ON     OD.GOODS_CD = G.GOODS_CD
			INNER  JOIN TB_GOODS_IMG GI
			ON     OD.GOODS_CD = GI.GOODS_CD
			AND    ODI.OPT_CD1 = GI.COLOR_CD
			INNER  JOIN TB_PAYMENT P
			ON     O.ORD_NO = P.ORD_NO
			AND    P.PAY_GB = 'O'
			INNER  JOIN TB_DELIVERY_ADDR DA
			ON     OD.DELV_ADDR_SQ = DA.DELV_ADDR_SQ
			WHERE  1=1
			<if test='siteCd != null and siteCd != ""'>
			AND    O.SITE_CD = #{siteCd}
			</if>
			<if test='mallCd != null and mallCd != ""'>
			AND    O.MALL_GB = #{mallCd}
			</if>
			<if test='supplyCompCd != null and supplyCompCd != ""'>
			AND    OD.SUPPLY_COMP_CD = #{supplyCompCd}
			</if>
			<if test="multiBrand != null and multiBrand != ''">
	        AND    G.BRAND_CD IN
				<foreach collection="multiBrand" item="item" index="index"  open="(" close=")" separator=",">
	        		#{item}
	            </foreach>
	        </if>
	        <if test="multiOrdDtlStat != null and multiOrdDtlStat != ''">
	        AND    OD.ORD_DTL_STAT IN
				<foreach collection="multiOrdDtlStat" item="item" index="index"  open="(" close=")" separator=",">
	        		#{item}
	            </foreach>
	        </if>
	        <if test='search != null and search == "searchOrdNo"'>
	        	<if test="condition != null and condition != ''">
			AND    O.ORD_NO = #{condition}
				</if>
			</if>
			<if test='search != null and search == "searchExtmallOrderId"'>
	        	<if test="condition != null and condition != ''">
			AND    O.ORD_NO = #{condition}
				</if>
			</if>
			<if test='payMeans != null and payMeans != ""'>
			AND    P.PAY_MEANS = #{payMeans}
			</if>
			<if test='orderNm != null and orderNm != ""'>
			AND    O.ORD_NM = #{orderNm}
			</if>
			<if test='custId != null and custId != ""'>
			AND    O.CUST_NO = #{custId}
			</if>
			<if test='orderPhnno != null and orderPhnno != ""'>
			AND    O.ORD_PHNNO = #{orderPhnno}
			</if>
			<if test='goodsCd != null and goodsCd != ""'>
			AND    G.GOODS_CD = #{goodsCd}
			</if>
			<if test='sizeCd != null and sizeCd != ""'>
			AND    ODI.OPT_CD2 = #{sizeCd}
			</if>
			<if test='goodsNm != null and goodsNm != ""'>
			AND    G.GOODS_NM = #{goodsNm}
			</if>
			<if test='recipNm != null and recipNm != ""'>
			AND    DA.RECIP_NM = #{recipNm}
			</if>
			<if test='invoiceNo != null and invoiceNo != ""'>
			AND    OD.INVOICE_NO = #{invoiceNo}
			</if>
			<if test='wdInvoiceSendYn != null and wdInvoiceSendYn == "Y"'>
			AND    OD.INVOICE_SEND_YN = 'Y'
			</if>
			<if test='wdInvoiceSendYn != null and wdInvoiceSendYn == "N"'>
			AND    OD.INVOICE_SEND_YN = 'N'
			</if>
			<if test='searchDateGb != null and searchDateGb == "ordDt"'>
			AND    O.ORD_DT >= DATE_FORMAT(#{stDate}, '%Y-%m-%d %H:%i:%S')
			AND    O.ORD_DT <![CDATA[<]]> DATE_FORMAT(DATE_ADD(#{edDate}, INTERVAL 1 DAY), '%Y-%m-%d %H:%i:%S')
			</if>
			<if test='searchDateGb != null and searchDateGb == "delvStdt"'>
			AND    OD.DELV_STDT >= DATE_FORMAT(#{stDate}, '%Y-%m-%d %H:%i:%S')
			AND    OD.DELV_STDT <![CDATA[<]]> DATE_FORMAT(DATE_ADD(#{edDate}, INTERVAL 1 DAY), '%Y-%m-%d %H:%i:%S')
			</if>
			<if test='searchDateGb != null and searchDateGb == "soldoutDt"'>
			AND    OD.SOLDOUT_REG_DT >= DATE_FORMAT(#{stDate}, '%Y-%m-%d %H:%i:%S')
			AND    OD.SOLDOUT_REG_DT <![CDATA[<]]> DATE_FORMAT(DATE_ADD(#{edDate}, INTERVAL 1 DAY), '%Y-%m-%d %H:%i:%S')
			</if>
		) X
	</select>
	
	<!-- 주문상세 > 주문 정보 -->
	<select id="getOrderInfoList" parameterType="Order" resultType="Order">
		/* order.getOrderInfoList */
		SELECT O.SITE_CD
		     , O.MALL_GB
		     , FN_GET_CODE_NM('G011', O.MALL_GB) AS MALL_GB_NM
		     , O.FRONT_GB
		     , CASE WHEN O.FRONT_GB = 'P'
		            THEN 'PC'
		            ELSE 'MOBILE'
		             END FRONT_GB_NM
		     , OD.EXTMALL_ID 
		     , DATE_FORMAT(O.ORD_DT,'%Y%m%d%H%i%S') AS ORD_DT
		     , O.ORD_NM
		     , O.CUST_NO
		     , O.ORD_NO
		     , O.ORD_EMAIL
		     , O.ORD_PHNNO
		     , C.CUST_GB 
		     , CASE WHEN C.CUST_GB IS NULL 
		            THEN '비회원'
		            ELSE FN_GET_CODE_NM('G100', C.CUST_GB) 
		             END CUST_GB_NM
		     , C.MANAGED_RSN
		     , CASE WHEN C.MANAGED_RSN IS NULL 
		            THEN '부'
		            ELSE FN_GET_CODE_NM('G120', C.MANAGED_RSN) 
		             END MANAGED_RSN_NM
		     , C.CUST_GRADE
		     , CASE WHEN C.CUST_GRADE IS NULL 
		            THEN '등급없음'
		            ELSE FN_GET_CODE_NM('G110', C.CUST_GRADE) 
		             END CUST_GRADE_NM
		FROM   TB_ORDER O
		INNER  JOIN TB_ORDER_DETAIL OD
		ON     O.ORD_NO = OD.ORD_NO
		LEFT   OUTER JOIN TB_CUSTOMER C
		ON     O.CUST_NO = C.CUST_NO
		WHERE  1=1
		AND    O.ORD_NO = #{ordNo}
		LIMIT  1
	</select>
	
	<!-- 주문상세 > 상품상세 목록 -->
	<select id="getOrderDetailList" parameterType="Order" resultType="Order">
		/* order.getOrderDetailList */
		SELECT OD.ORD_NO
		     , OD.ORD_DTL_NO
		     , G1.GOODS_CD 
		     , G1.GOODS_NM
		     , G1.GOODS_TYPE
		     , FN_GET_CODE_NM('G056', G1.GOODS_TYPE) AS GOODS_TYPE_NM
		     , GI.SYS_IMG_NM
		     , G2.BRAND_CD
		     , (SELECT Z.BRAND_ENM
		        FROM   TB_BRAND Z
		        WHERE  Z.BRAND_CD = G2.BRAND_CD) AS BRAND_ENM
		     , G2.GOODS_CD AS ITEM_CD
		     , G2.GOODS_NM AS ITEM_NM
		     , ODI.OPT_CD1
		     , ODI.OPT_CD2
		     , ODI.ITEM_QTY
		     , ODI.ITEM_PRICE
		     , ODI.ITEM_QTY * OD.ORD_QTY      AS ORD_QTY
		     , ODI.ITEM_QTY * OD.CNCL_RTN_QTY AS CNCL_RTN_QTY
		     , ODI.OPT_ADD_PRICE 
		     , ODI.CPN1_DC_AMT
		     , ODI.GOODS_CPN_DC_AMT
		     , ODI.CART_CPN_DC_AMT
		     , ODI.TMTB1_DC_AMT
		     , ODI.TMTB2_DC_AMT
		     , ODI.PNT_DC_AMT
		     , ODI.PRE_PNT_DC_AMT
		     , ODI.SAVE_PNT_AMT
		     , ODI.GFCD_USE_AMT
			 , ODI.ORD_AMT
		     , ODI.REAL_ORD_AMT
		     , ODI.CNCL_RTN_AMT
		     , OD.ORD_DTL_STAT
		     , FN_GET_CODE_NM('G013', OD.ORD_DTL_STAT) AS ORD_DTL_STAT_NM 
		     , OD.SOLDOUT_YN
		     , OD.SOLDOUT_REG_NO
		     , OD.SOLDOUT_MEMO
		     , OD.INVOICE_NO
		     , OD.DELV_LOC_CD 
		     , X.G20
		     , X.G30
		     , X.G40
		     , X.G50
		     , X.G55
		     , X.G60
		FROM   TB_ORDER O
		INNER  JOIN TB_ORDER_DETAIL OD
		ON     O.ORD_NO = OD.ORD_NO
		INNER  JOIN TB_GOODS G1
		ON     OD.GOODS_CD = G1.GOODS_CD
		INNER  JOIN TB_ORDER_DETAIL_ITEM ODI
		ON     OD.ORD_NO = ODI.ORD_NO
		AND    OD.ORD_DTL_NO = ODI.ORD_DTL_NO
		INNER  JOIN TB_GOODS G2
		ON     ODI.ITEM_CD = G2.GOODS_CD
		INNER  JOIN TB_GOODS_IMG GI
		ON     ODI.ITEM_CD = GI.GOODS_CD
		AND    ODI.OPT_CD1 = GI.COLOR_CD
		INNER  JOIN TB_PAYMENT P
		ON     O.ORD_NO = P.ORD_NO
		AND    P.PAY_GB = 'O'
		INNER  JOIN TB_DELIVERY_ADDR DA
		ON     OD.DELV_ADDR_SQ = DA.DELV_ADDR_SQ
		LEFT   OUTER JOIN (
			SELECT MAX(Z.G013_20) AS G20
			     , MAX(Z.G013_30) AS G30
			     , MAX(Z.G013_40) AS G40
			     , MAX(Z.G013_50) AS G50
			     , MAX(Z.G013_55) AS G55
			     , MAX(Z.G013_60) AS G60
			     , Z.ORD_NO
			     , Z.ORD_DTL_NO
			FROM (
				SELECT CASE WHEN ODH.ORD_DTL_STAT = 'G013_20' THEN 'G013_20' END G013_20
				     , CASE WHEN ODH.ORD_DTL_STAT = 'G013_30' THEN 'G013_30' END G013_30
				     , CASE WHEN ODH.ORD_DTL_STAT = 'G013_40' THEN 'G013_40' END G013_40
				     , CASE WHEN ODH.ORD_DTL_STAT = 'G013_50' THEN 'G013_50' END G013_50
				     , CASE WHEN ODH.ORD_DTL_STAT = 'G013_55' THEN 'G013_55' END G013_55
				     , CASE WHEN ODH.ORD_DTL_STAT = 'G013_60' THEN 'G013_60' END G013_60
				     , ODH.ORD_NO
				     , ODH.ORD_DTL_NO
				FROM   TB_ORDER_DETAIL_HST ODH
				INNER  JOIN TB_COMMON_CODE CC
				ON     ODH.ORD_DTL_STAT = CC.CD
				WHERE  ODH.ORD_NO = #{ordNo}
				GROUP  BY ODH.ORD_DTL_STAT
			) Z
		) X
		ON     OD.ORD_NO = X.ORD_NO
		AND    OD.ORD_DTL_NO = X.ORD_DTL_NO
		WHERE  1=1
		AND    OD.ORD_NO = #{ordNo}
	</select>
	
	<!-- 주문상세 > 사은품목록 -->
	<select id="getOrderFreeGiftList" parameterType="Order" resultType="Order">
		/* order.getOrderFreeGiftList */
		SELECT OFV.ORD_FREEGIFT_SQ
		     , OFV.ORD_NO
		     , OFV.FREEGIFT_SQ 
		     , FG.FREEGIFT_NM 
		     , OFV.FREEGIFT_VAL_SQ 
		     , OFV.USE_POINT
		     , OFV.ITEM_CD 
		     , G.GOODS_NM AS ITEM_NM
		     , OFV.ITEM_QTY 
		     , OFV.OPT_CD 
		     , OFV.OPT_CD1 
		     , OFV.OPT_CD2 
		     , OFV.DEL_YN
		FROM   TB_ORD_FREEGIFT_VAL OFV
		INNER  JOIN TB_FREEGIFT FG
		ON     OFV.FREEGIFT_SQ = FG.FREEGIFT_SQ
		INNER  JOIN TB_GOODS G
		ON     OFV.ITEM_CD = G.GOODS_CD 
		WHERE 1=1
		AND  OFV.ORD_NO = #{ordNo}
		AND  OFV.DEL_YN = 'N'
	</select>
	
	<!-- 주문상세 > 배송정보 -->
	<select id="getDeliveryAddrList" parameterType="Order" resultType="Order">
		/* order.getDeliveryAddrList */
		SELECT Z.*
		FROM   (
			SELECT OD.DELV_ADDR_SQ 
			     , DA.RECIP_NM 
			     , DA.RECIP_PHNNO 
			     , DA.RECIP_TELNO 
			     , DA.RECIP_ZIP_NO 
			     , DA.RECIP_BASE_ADDR
			     , DA.RECIP_DTL_ADDR
			     , O.ORD_EMAIL
			     , DA.DELV_MEMO
			FROM   TB_ORDER_DETAIL OD
			INNER  JOIN TB_ORDER O
			ON     OD.ORD_NO = O.ORD_NO
			INNER  JOIN TB_DELIVERY_ADDR DA
			ON     OD.DELV_ADDR_SQ  = DA.DELV_ADDR_SQ
			WHERE  1=1
			AND    OD.ORD_NO = #{ordNo}
			GROUP  BY OD.DELV_ADDR_SQ 
			     , DA.RECIP_NM 
			     , DA.RECIP_PHNNO 
			     , DA.RECIP_TELNO 
			     , DA.RECIP_ZIP_NO 
			     , DA.RECIP_BASE_ADDR
			     , DA.RECIP_DTL_ADDR
			     , O.ORD_EMAIL
			     , DA.DELV_MEMO
		) Z
		ORDER  BY Z.DELV_ADDR_SQ
	</select>
	
	<!-- 주문상세 > 결제정보 -->
	<select id="getOrderPaymentBasicInfoList" parameterType="Order" resultType="Order">
		/* order.getOrderPaymentBasicInfoList */
		SELECT P.ESCROW_YN
		     , P.PAY_MEANS
		     , FN_GET_CODE_NM('G014', P.PAY_MEANS) AS PAY_MEANS_NM
		     , P.CARD_NM 
		     , P.VA_BANK
		     , P.PG_TRADE_NO
		     , DATE_FORMAT(P.PAY_DT,'%Y%m%d%H%i%S') AS PAY_DT  
		     , P.PAY_STAT 
		     , FN_GET_CODE_NM('G016', P.PAY_STAT) AS PAY_STAT_NM
		     , P.PAY_AMT 
		     , DATE_FORMAT(P.VA_DEADLINE,'%Y%m%d%H%i%S') AS VA_DEADLINE 
		FROM   TB_PAYMENT P
		WHERE  1=1
		AND    P.ORD_NO = #{ordNo}
	</select>
	
	<!-- 주문상세 > 배송비정보 -->
	<select id="getDeliveryFeeList" parameterType="Order" resultType="Order">
		/* order.getDeliveryFeeList */
		SELECT Z.*
		FROM   (
		SELECT DF.DELV_FEE_GB 
		     , FN_GET_CODE_NM('G018', DF.DELV_FEE_GB) AS DELV_FEE_GB_NM
		     , DF.DELV_FEE
		     , DF.DELV_USAC_YN 
		     , DATE_FORMAT(DF.DELV_USAC_DT,'%Y%m%d%H%i%S') AS DELV_USAC_DT 
		     , DF.SUPPLY_COMP_CD 
		     , DF.DELV_FEE_SQ 
		FROM   TB_DELIVERY_FEE DF
		WHERE  1=1
		AND    DF.ORD_NO = #{ordNo}
		) Z
		GROUP  BY Z.DELV_FEE_GB 
		     , Z.DELV_FEE_GB_NM
		     , Z.DELV_FEE
		     , Z.DELV_USAC_YN 
		     , Z.DELV_USAC_DT 
		     , Z.SUPPLY_COMP_CD 
		     , Z.DELV_FEE_SQ
	</select>
	
	<!-- 주문상세 > 취소/반품/교환요청 정보 -->
	<select id="getOrderChangeList" parameterType="Order" resultType="Order">
		/* order.getOrderChangeList */
		SELECT OCD.ORD_CHG_SQ
		     , OC.CHG_GB
		     , FN_GET_CODE_NM('G680', OC.CHG_GB) AS CHG_NM
		     , OD.GOODS_CD
		     , ODI.OPT_CD1
		     , ODI.OPT_CD2
		     , OCD.CHG_STAT 
		     , FN_GET_CODE_NM('G685', OCD.CHG_STAT) AS CHG_STAT_NM
		     , DATE_FORMAT(OCD.REG_DT,'%Y%m%d%H%i%S') AS REG_DT 
		     , OC.CHG_REASON 
		     , CASE WHEN OC.CHG_GB = 'G680_20' THEN FN_GET_CODE_NM('G686', OC.CHG_REASON)
		            WHEN OC.CHG_GB = 'G680_30' THEN FN_GET_CODE_NM('G688', OC.CHG_REASON)
		            WHEN OC.CHG_GB = 'G680_40' THEN FN_GET_CODE_NM('G689', OC.CHG_REASON)
		            ELSE '결저전취소'
		            END  CHG_REASON_NM
		     , OC.CHG_MEMO
		     , OC.CHGER_NM
		     , OC.CHGER_PHNNO 
		     , OC.CHGER_ZIP_NO
		     , OC.CHGER_BASE_ADDR
		     , OC.CHGER_DTL_ADDR
		     , OC.WD_INVOICE_SEND_YN
		     , OC.WD_INVOICE_NO
		FROM   TB_ORDER_CHANGE_DETAIL OCD
		INNER  JOIN TB_ORDER_CHANGE OC
		ON     OC.ORD_CHG_SQ = OCD.ORD_CHG_SQ 
		INNER  JOIN TB_ORDER_DETAIL OD
		ON     OCD.ORD_DTL_NO = OD.ORD_DTL_NO 
		INNER  JOIN TB_ORDER_DETAIL_ITEM ODI
		ON     OD.ORD_DTL_NO = ODI.ORD_DTL_NO 
		WHERE  1=1
		AND    OD.ORD_NO = #{ordNo}
	</select>
	
	<!-- 주문상세 > 환불정보 -->
	<select id="orderRefundInfo" parameterType="Order" resultType="Order">
		/* order.orderRefundInfo */
		SELECT P.PAY_MEANS
		     , FN_GET_CODE_NM('G014', P.PAY_MEANS) AS PAY_MEANS_NM
		     , P.CARD_NM
		     , R.RA_BANK 
		     , R.RA_NO
		     , R.RA_NM 
		     , R.REFUND_AMT 
		     , P.UPD_DT
		     , P.UPD_NO
		FROM   TB_REFUND R
		INNER  JOIN TB_PAYMENT P
		ON     R.ORD_CHG_SQ = P.ORD_CHG_SQ 
		WHERE  1=1
		AND    R.ORD_NO = #{ordNo}
	</select>
	
	<!-- 주문상세 > 상담내역 -->
	<select id="orderCounselInfo" parameterType="Order" resultType="Order">
		/* order.orderCounselInfo */
		SELECT CS.COUNSEL_CLSF
			 , FN_GET_CODE_NM('G059', CS.COUNSEL_CLSF) AS COUNSEL_CLSF_NM
		     , CS.REL_GOODS_CD
		     , CS.QUEST_TITLE
		     , DATE_FORMAT(CS.QUEST_DT,'%Y%m%d%H%i%S') AS QUEST_DT
		     , DATE_FORMAT(CS.ANS_DT,'%Y%m%d%H%i%S') AS ANS_DT
		     , CS.ANS_NO
		     , (SELECT USER_NM
		        FROM   TB_USER X
		        WHERE  X.USER_NO = CS.ANS_NO) AS ANS_NM
		     , CS.DEL_YN
		FROM   TB_COUNSEL CS
		WHERE  1=1
		AND    CS.REL_ORD_NO = #{ordNo}
		ORDER  BY CS.REG_DT DESC
	</select>
	
	<!-- 주문상세 > 관리자메모 -->
	<select id="getOrderTsainMemoList" parameterType="Order" resultType="Order">
		/* order.getOrderTsainMemoList */
		SELECT ORDER_MEMO_SQ 
		     , OM.ORD_NO
		     , OM.MEMO
		     , OM.REG_NO
		     , OM.REG_DT
		     , OM.UPD_NO
		     , OM.UPD_DT
		     , (SELECT USER_NM
		        FROM   TB_USER X
		        WHERE  X.USER_NO = OM.REG_NO) AS REG_NM
		FROM  TB_ORDER_MEMO OM
		WHERE 1=1
		AND   OM.ORD_NO = #{ordNo}
		AND   OM.DEL_YN = #{delYn}
		ORDER BY OM.REG_DT DESC
	</select>
	
	<!-- 주문/배송 > 입점업체미발주목록 -->
	<select id="getOrderSellerUnorderList" parameterType="Order" resultType="Order">
		/* order.getOrderSellerUnorderList */
		SELECT OD.SUPPLY_COMP_CD
		     , (SELECT SC.SUPPLY_COMP_NM
		        FROM   TB_SUPPLY_COMPANY SC
		        WHERE  SC.SUPPLY_COMP_CD = OD.SUPPLY_COMP_CD) AS SUPPLY_COMP_NM
		     , G1.BRAND_CD 
		     , (SELECT B.BRAND_KNM 
		        FROM   TB_BRAND B
		        WHERE  B.BRAND_CD = G1.BRAND_CD) AS BRAND_KNM
		     , G1.GOODS_TYPE 
		     , FN_GET_CODE_NM('G056', G1.GOODS_TYPE) AS GOODS_TYPE_NM
		     , G1.GOODS_CD
		     , G1.GOODS_NM
		     , OD.ORD_NO
		     , OD.ORD_DTL_NO
		     , OD.ORD_DTL_STAT 
		     , FN_GET_CODE_NM('G013', OD.ORD_DTL_STAT) AS ORD_DTL_STAT_NM
		     , O.ORD_NM 
		     , DATE_FORMAT(O.ORD_DT,'%Y%m%d%H%i%S') AS ORD_DT
		     , P.PAY_MEANS 
		     , FN_GET_CODE_NM('G014', P.PAY_MEANS) AS PAY_MEANS_NM
		     , O.ORD_PHNNO 
		     , DA.RECIP_NM 
		     , DA.RECIP_PHNNO 
		     , DA.RECIP_ZIP_NO 
		     , DA.RECIP_BASE_ADDR 
		     , DA.RECIP_DTL_ADDR 
		     , G2.SUPPLY_GOODS_CD
		     , G2.GOODS_CD AS ITEM_CD
		     , G2.GOODS_NM AS ITEM_NM
		     , ODI.OPT_CD1
		     , ODI.OPT_CD2
		     , ODI.ORD_AMT
		     , ODI.CPN1_DC_AMT + ODI.TMTB1_DC_AMT + ODI.TMTB2_DC_AMT +  ODI.GOODS_CPN_DC_AMT + ODI.CART_CPN_DC_AMT + ODI.PNT_DC_AMT + ODI.PRE_PNT_DC_AMT AS TOT_DC_AMT
		     , ODI.REAL_ORD_AMT 
		     , OD.INVOICE_NO 
		     , OD.SHIP_COMP_CD
		FROM   TB_ORDER O
		INNER  JOIN TB_ORDER_DETAIL OD
		ON     O.ORD_NO = OD.ORD_NO
		INNER  JOIN TB_GOODS G1
		ON     OD.GOODS_CD = G1.GOODS_CD
		INNER  JOIN TB_ORDER_DETAIL_ITEM ODI
		ON     OD.ORD_NO = ODI.ORD_NO 
		AND    OD.ORD_DTL_NO = ODI.ORD_DTL_NO 
		INNER  JOIN TB_GOODS G2
		ON     ODI.ITEM_CD = G2.GOODS_CD
		INNER  JOIN TB_GOODS_IMG GI
		ON     ODI.ITEM_CD = GI.GOODS_CD
		AND    ODI.OPT_CD1 = GI.COLOR_CD
		INNER  JOIN TB_PAYMENT P
		ON     O.ORD_NO = P.ORD_NO 
		AND    P.PAY_GB = 'O'
		INNER  JOIN TB_DELIVERY_ADDR DA
		ON     OD.DELV_ADDR_SQ = DA.DELV_ADDR_SQ 
		INNER  JOIN TB_DELIVERY_FEE DF		
		ON     DF.SUPPLY_COMP_CD = OD.SUPPLY_COMP_CD
		AND    DF.DELV_FEE_CD = OD.DELV_FEE_CD 
		AND    DF.ORD_NO = OD.ORD_NO
		WHERE  1=1
		<if test='supplyCompCd != null and supplyCompCd != ""'>
		AND    OD.SUPPLY_COMP_CD = #{supplyCompCd}
		</if>
		<if test="multiBrand != null and multiBrand != ''">
        AND    G.BRAND_CD IN
			<foreach collection="multiBrand" item="item" index="index"  open="(" close=")" separator=",">
        		#{item}
            </foreach>
        </if>
        <if test="ordDtlStat != null and ordDtlStat != ''">
        AND    OD.ORD_DTL_STAT = #{ordDtlStat}
        </if>
        <if test="ordNo != null and ordNo != ''">
		AND    O.ORD_NO = #{ordNo}
		</if>
		<if test="ordNm != null and ordNm != ''">
		AND    O.ORD_NM = #{ordNm}
		</if>
		<if test='goodsCd != null and goodsCd != ""'>
		AND    G.GOODS_CD = #{goodsCd}
		</if>
		<if test='searchDateGb != null and searchDateGb == "ordDt"'>
		AND    O.ORD_DT >= DATE_FORMAT(#{stDate}, '%Y-%m-%d %H:%i:%S')
		AND    O.ORD_DT <![CDATA[<]]> DATE_FORMAT(DATE_ADD(#{edDate}, INTERVAL 1 DAY), '%Y-%m-%d %H:%i:%S')
		</if>
	</select>
	
	<!-- 입점업체 엑셀다운용 임시테이블 삭제-->
	<delete id="deleteEntryExcelDownTmp" parameterType="Order">
		/* TsbOrder.deleteEntryExcelDownTmp */
		DELETE FROM TB_ENTRY_UPLOAD_EXCEL
		WHERE  1=1
	</delete>
	
	<!-- 주문상세정보 변경 (입점) -->
	<update id="updateOrderDetailStat" parameterType="Order">
		/* Order.updateOrderDetailStat */
		UPDATE TB_ORDER_DETAIL
		SET    ORD_DTL_STAT = 'G013_30'
		     , UPD_NO = #{updNo}
		     , UPD_DT = CURRENT_TIMESTAMP()
		WHERE  ORD_DTL_NO = #{ordDtlNo}
		AND    ORD_DTL_STAT in ('G013_20')
		AND    SUPPLY_COMP_CD = #{supplyCompCd}
	</update>
	
	<!-- 주문상세이력 생성-->
	<insert id="createOrderDetailHst" parameterType="Order">
		/* Order.createOrderDetailHst */
		INSERT INTO TB_ORDER_DETAIL_HST (
				ORD_DTL_NO
				, ORD_NO
				, ORD_EXCH_GB
				, ORD_DTL_STAT
				, ORG_ORD_DTL_NO
				, SUPPLY_COMP_CD
				, GOODS_CD
				, 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 ORD_DTL_NO
				, ORD_NO
				, ORD_EXCH_GB
				, ORD_DTL_STAT
				, ORG_ORD_DTL_NO
				, SUPPLY_COMP_CD
				, GOODS_CD
				, 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
				, #{regNo} AS REG_ID
				, CURRENT_TIMESTAMP() AS REG_DT
				, #{regNo} AS REG_ID
				, CURRENT_TIMESTAMP() AS UPD_DT
		FROM   TB_ORDER_DETAIL
		WHERE  ORD_DTL_NO = #{ordDtlNo}
	</insert>
	
	<!-- 입점업체 엑셀다운 임시파일 체크-->
	<select id="getEntryUploadExcelCheck" parameterType="Order" resultType="int">
		SELECT COUNT(1)
		FROM   TB_ENTRY_UPLOAD_EXCEL X
		WHERE  1=1
		AND    X.ORD_NO = #{ordNo}
		AND    X.ORD_DTL_NO = #{ordDtlNo}
	</select>
	
	<!-- 입점업체 엑셀다운용 임시테이블 생성-->
	<insert id="createEntryExcelDownTmp" parameterType="Order">
		INSERT INTO TB_ENTRY_UPLOAD_EXCEL (
			ORD_NO
			, ORD_DTL_NO
			, REG_NO
			, REG_DT
		) VALUES (
			#{ordNo}
			, #{ordDtlNo}
			, #{regNo}
			, CURRENT_TIMESTAMP()
		)
	</insert>
	
	<!-- 주문/배송 > 입점업체미발주목록 > 엑셀다운로드 -->
	<select id="getSellerUnorderList" parameterType="Order" resultType="Order">
		/* Order.getSellerUnorderList */
		SELECT (SELECT SC.SUPPLY_COMP_NM
		        FROM   TB_SUPPLY_COMPANY SC
		        WHERE  SC.SUPPLY_COMP_CD = OD.SUPPLY_COMP_CD) AS SUPPLY_COMP_NM
		     , (SELECT B.BRAND_KNM 
		        FROM   TB_BRAND B
		        WHERE  B.BRAND_CD = G1.BRAND_CD) AS BRAND_KNM
	         , G1.GOODS_CD 
		     , G1.GOODS_NM
			 , G1.GOODS_TYPE
		     , FN_GET_CODE_NM('G056', G1.GOODS_TYPE) AS GOODS_TYPE_NM
		     , OD.ORD_NO
		     , OD.ORD_DTL_NO
		     , FN_GET_CODE_NM('G013', OD.ORD_DTL_STAT) AS ORD_DTL_STAT_NM
		     , O.ORD_NM 
		     , DATE_FORMAT(O.ORD_DT,'%Y%m%d%H%i%S') AS ORD_DT
		     , FN_GET_CODE_NM('G014', P.PAY_MEANS) AS PAY_MEANS_NM
		     , O.ORD_PHNNO 
		     , DA.RECIP_NM 
		     , DA.RECIP_PHNNO 
		     , DA.RECIP_ZIP_NO 
		     , DA.RECIP_BASE_ADDR 
		     , DA.RECIP_DTL_ADDR 
		     , G2.SUPPLY_GOODS_CD
		     , G2.GOODS_CD AS ITEM_CD
		     , G2.GOODS_NM AS ITEM_NM 
		     , ODI.OPT_CD1
		     , ODI.OPT_CD2
		     , ODI.ORD_AMT
		     , ODI.CPN1_DC_AMT + ODI.TMTB1_DC_AMT + ODI.TMTB2_DC_AMT +  ODI.GOODS_CPN_DC_AMT + ODI.CART_CPN_DC_AMT + ODI.PNT_DC_AMT + ODI.PRE_PNT_DC_AMT AS TOT_DC_AMT
		     , ODI.REAL_ORD_AMT 
		     , OD.INVOICE_NO 
		FROM   TB_ORDER O
		INNER  JOIN TB_ORDER_DETAIL OD
		ON     O.ORD_NO = OD.ORD_NO
		INNER  JOIN TB_GOODS G1
		ON     OD.GOODS_CD = G1.GOODS_CD
		INNER  JOIN TB_ORDER_DETAIL_ITEM ODI
		ON     OD.ORD_NO = ODI.ORD_NO 
		AND    OD.ORD_DTL_NO = ODI.ORD_DTL_NO
		INNER  JOIN TB_GOODS G2
		ON     ODI.ITEM_CD = G2.GOODS_CD
		INNER  JOIN TB_GOODS_IMG GI
		ON     ODI.ITEM_CD = GI.GOODS_CD
		AND    ODI.OPT_CD1 = GI.COLOR_CD 
		INNER  JOIN TB_PAYMENT P
		ON     O.ORD_NO = P.ORD_NO 
		AND    P.PAY_GB = 'O' 
		INNER  JOIN TB_DELIVERY_ADDR DA
		ON     OD.DELV_ADDR_SQ = DA.DELV_ADDR_SQ
		INNER  JOIN TB_DELIVERY_FEE DF		
		ON     DF.SUPPLY_COMP_CD = OD.SUPPLY_COMP_CD
		AND    DF.DELV_FEE_CD = OD.DELV_FEE_CD 
		AND    DF.ORD_NO = OD.ORD_NO
		INNER  JOIN TB_ENTRY_UPLOAD_EXCEL X
		ON     OD.ORD_NO = X.ORD_NO
		AND    OD.ORD_DTL_NO = X.ORD_DTL_NO
		WHERE  1=1
		<if test='supplyCompCd != null and supplyCompCd != ""'>
		AND    OD.SUPPLY_COMP_CD = #{supplyCompCd}
		</if>
	</select>
	
	<!-- 주문/배송 > 입점업체미발주목록 > 주문상세 목록 -->
	<select id="getOrderDetailInfo" parameterType="Order" resultType="Order">
		/* TsaOrder.getOrderDetailInfo */
		SELECT O.ORD_NO
		     , O.MALL_GB
		     , DATE_FORMAT(O.ORD_DT,'%Y%m%d%H%i%S') AS ORD_DT
		     , DATE_FORMAT(O.PAY_DT,'%Y%m%d%H%i%S') AS PAY_DT
		     , O.CUST_NO
		     , O.ORD_NM
		     , O.ORD_PHNNO
		     , O.ORD_TELNO
		     , O.ORD_EMAIL
		     , OD.ORD_DTL_STAT
		     , O.SITE_CD
		     , O.FRONT_GB
		     , OD.ORD_DTL_NO
		     , OD.ORD_EXCH_GB
		     , OD.ORG_ORD_DTL_NO
		     , OD.VENDOR_ID
		     , OD.SUPPLY_COMP_CD
		     , OD.GOODS_CD
		     , OD.FORMAL_GB
		     , OD.GOODS_TYPE
		     , OD.LIST_PRICE
		     , OD.CURR_PRICE
		     , OD.DC_RATE
		     , OD.ORD_QTY
		     , OD.ORD_AMT
		     , OD.CNCL_RTN_QTY
		     , OD.CNCL_RTN_AMT
		     , OD.GOODS_CPN_DC_AMT
		     , OD.CART_CPN_DC_AMT
		     , OD.PNT_DC_AMT
		     , OD.PRE_PNT_DC_AMT
		     , OD.SAVE_PNT_AMT
		     , OD.REAL_ORD_AMT
		     , OD.AGENT_ORDER_ID
		     , OD.EXTMALL_ORDER_ID
		     , OD.ORD_DTL_STAT
		     , FN_GET_CODE_NM('G013', OD.ORD_DTL_STAT)    AS ORD_DTL_STAT_NM
		     , OD.SOLDOUT_YN
		     , OD.SOLDOUT_MEMO
		     , OD.SOLDOUT_REG_NO
		     , OD.SOLDOUT_REG_DT
		     , OD.SHIP_COMP_CD
		     , OD.INVOICE_NO
		     , OD.DELV_STDT
		     , OD.DELV_EDDT
		     , OD.SELL_STORE_CD
		     , OD.SELL_FEE_RATE
		     , OD.DELV_ADDR_SQ
		     , OD.REG_NO
		     , OD.REG_DT
		     , OD.UPD_NO
		     , OD.UPD_DT
		     , (SELECT SELF_GOODS_YN FROM TB_GOODS WHERE GOODS_CD = OD.GOODS_CD) AS SELF_YN
		     , ODI.ITEM_CD
		     , ODI.OPT_CD1
		     , ODI.OPT_CD2 
		FROM   TB_ORDER O
		INNER  JOIN TB_ORDER_DETAIL OD
		ON     O.ORD_NO = OD.ORD_NO
		INNER  JOIN TB_ORDER_DETAIL_ITEM ODI
		ON     OD.ORD_NO = ODI.ORD_NO
		AND    OD.ORD_DTL_NO = ODI.ORD_DTL_NO
		WHERE  O.ORD_NO = OD.ORD_NO
		AND    OD.ORD_DTL_NO = #{ordDtlNo}
		AND    ODI.ITEM_CD = #{itemCd}
		AND    ODI.OPT_CD2 = #{optCd2}
		AND    OD.ORD_DTL_NO = ODI.ORD_DTL_NO
	</select>
	
	<!-- 주문상세정보 변경 - 송장번호 적용(입점)  -->
	<update id="updateOrderDetailInvoice" parameterType="Order">
		/* TsaOrder.updateOrderDetailInvoice */
		UPDATE TB_ORDER_DETAIL
		SET    ORD_DTL_STAT = 'G013_55'
		     , INVOICE_NO = REPLACE(#{invoiceNo},'-','')
		     , INVOICE_SEND_YN = 'Y'
		     , SHIP_COMP_CD = #{shipCompCd}
		     , UPD_NO = #{updNo}
		     , UPD_DT = CURRENT_TIMESTAMP()
		WHERE  ORD_DTL_NO = #{ordDtlNo}
		AND    ORD_DTL_STAT IN ('G013_30', 'G013_55')
		AND    SUPPLY_COMP_CD = #{supplyCompCd}
	</update>
	
	<!-- 주문상세 > 주문메오 > 주문메모정보조회 -->
	<select id="getOrderMemoInfo" parameterType="Order" resultType="Order">
		/* order.getOrderMemoInfo */
		SELECT ORDER_MEMO_SQ
		     , OM.ORD_NO
		     , OM.MEMO
		     , OM.REG_NO
		     , OM.REG_DT
		     , OM.UPD_NO
		     , OM.UPD_DT
		     , (SELECT USER_NM
		        FROM   TB_USER X
		        WHERE  X.USER_NO = OM.UPD_NO) AS USER_NM
		FROM  TB_ORDER_MEMO OM
		WHERE 1=1
		AND   OM.ORD_NO = #{ordNo}
		AND   OM.DEL_YN = 'N'
	</select>
	
	<!-- 주문상세 > 주문요청 관리자 메모 > 메모등록 -->
	<insert id="createOrderMemo" parameterType="Order">
		/* order.createOrderMemo */
		INSERT INTO TB_ORDER_MEMO (
			ORD_NO
			,MEMO
			,ORG_FILE_NM
			,SYS_FILE_NM
			,DEL_YN
			,REG_NO
			,REG_DT
			,UPD_NO
			,UPD_DT
		) VALUES (
			#{ordNo}
			,#{memo}
			,#{orgFileNm}
			,#{sysFileNm}
			,'N'
			,#{regNo}
			,CURRENT_TIMESTAMP()
			,#{updNo}
			,CURRENT_TIMESTAMP()
		)
	</insert>
	
	<!-- 주문상세 > 주문요청 관리자 메모 > 메모수정 -->
	<update id="updateOrderMemo" parameterType="Order">
		/* order.updateOrderMemo */
		UPDATE TB_ORDER_MEMO
		SET    MEMO = #{memo}
		     , UPD_NO = #{updNo}
		     , UPD_DT = CURRENT_TIMESTAMP() 
		WHERE  1=1
		AND    ORD_NO = #{ordNo}
		AND    ORDER_MEMO_SQ = #{seq}
	</update>
	
	<!-- 주문상세 > 주문요청 관리자 메모 > 메모수정 -->
	<update id="deleteOrderMemo" parameterType="Order">
		/* order.deleteOrderMemo */
		UPDATE TB_ORDER_MEMO
		SET    DEL_YN = 'Y'
		     , UPD_NO = #{updNo}
		     , UPD_DT = CURRENT_TIMESTAMP() 
		WHERE  1=1
		AND    ORD_NO = #{ordNo}
		AND    ORDER_MEMO_SQ = #{seq}
	</update>
	
	<!-- 주문상세 > 주문상품 상세 변경 이력 -->
	<select id="getOrderDetailHistoryList" parameterType="Order" resultType="Order">
		/* order.getOrderDetailHistoryList */
		SELECT DATE_FORMAT(ODH.UPD_DT,'%Y%m%d%H%i%S') AS UPD_DT
		     , (SELECT Z.USER_ID
		        FROM   TB_USER Z
		        WHERE  Z.USER_NO = ODH.UPD_NO) 				AS UPD_ID
		     , FN_GET_CODE_NM('G013', ODH.ORD_DTL_STAT)    	AS ORD_DTL_STAT_NM 
		     , ODH.ORD_QTY
		     , ODH.CNCL_RTN_QTY 
		     , ODH.DELV_LOC_CD 
		     , ODH.INVOICE_NO 
		     , ODH.SHIP_COMP_CD
		     , (SELECT SHIP_COMP_NM
		        FROM   TB_SHIP_COMPANY Z
		        WHERE  Z.SHIP_COMP_CD = ODH.SHIP_COMP_CD) 	AS SHIP_COMP_NM
		FROM   TB_ORDER_DETAIL_HST ODH 
		WHERE  1=1
		AND    ODH.ORD_DTL_NO = #{ordDtlNo}
		ORDER  BY ODH.REG_DT DESC
	</select>
	
	<!-- 주문상세 > 쿠폰사용내역 팝업 화면 -->
	<select id="getOrderDiscountCouponList" parameterType="Order" resultType="Order">
		/* order.getOrderDiscountCouponList */
		WITH COUPON_DATA AS (
			SELECT OD.ORD_NO
			     , OD.ORD_DTL_NO
			     , OD.GOODS_CD
			     , OD.ORD_AMT
			     , OD.SUPPLY_COMP_CD
			     , OD.DELV_FEE_CD
			     , DF.DELV_FEE 
			     , OD.GOODS_CPN_SQ 
			     , OD.GOODS_CPN_DC_AMT 
			     , OD.CART_CPN_SQ
			     , OD.CART_CPN_DC_AMT
			     , DF.DELV_CPN_SQ
			     , DF.DELV_CPN_DC_AMT
			FROM   TB_ORDER_DETAIL OD
			INNER  JOIN TB_DELIVERY_FEE DF
			ON     OD.SUPPLY_COMP_CD = DF.SUPPLY_COMP_CD
			AND    OD.DELV_FEE_CD = DF.DELV_FEE_CD
			AND    OD.ORD_NO = DF.ORD_NO
			WHERE  1=1
			AND    OD.ORD_NO = #{ordNo}
		) 
		SELECT X.*
		     , CP.CPN_ID
		     , CP.CPN_NM 
		     , CP.DC_WAY 
		     , CP.DC_PVAL 
		     , CP.DC_MVAL 
		     , CP.DC_AVAL
		FROM   (
			SELECT Z.*
			FROM   (
				SELECT 1                       AS DISP_ORD
				     , CD.GOODS_CPN_SQ         AS CPN_SQ
				     , CD.GOODS_CPN_DC_AMT     AS CPN_DC_AMT
				     , '상품쿠폰'                 AS CPN_TYPE
				     , CD.GOODS_CD             AS TARGET_CD1
				     , CD.ORD_DTL_NO           AS TARGET_CD2
				     , CD.ORD_NO
				     , CD.ORD_AMT
				FROM   COUPON_DATA CD
				UNION  ALL
				SELECT 2                       AS DISP_ORD 
				     , CD.CART_CPN_SQ          AS CPN_SQ
				     , CD.CART_CPN_DC_AMT      AS CPN_DC_AMT
				     , '장바구니쿠폰'              AS CPN_TYPE
				     , CD.GOODS_CD             AS TARGET_CD1
				     , CD.ORD_DTL_NO           AS TARGET_CD2
				     , CD.ORD_NO
				     , CD.ORD_AMT
				FROM   COUPON_DATA CD
				UNION  ALL
				SELECT 3                       AS DISP_ORD 
				     , CD.DELV_CPN_SQ 	       AS CPN_SQ
				     , CD.DELV_CPN_DC_AMT      AS CPN_DC_AMT
				     , '배송비쿠폰'               AS CPN_TYPE
				     , CD.SUPPLY_COMP_CD       AS TARGET_CD1
				     , CD.DELV_FEE_CD          AS TARGET_CD2
				     , CD.ORD_NO
				     , CD.DELV_FEE             AS ORD_AMT
				FROM   COUPON_DATA CD
				GROUP  BY CD.DELV_CPN_SQ
				     , CD.DELV_CPN_DC_AMT
				     , CD.SUPPLY_COMP_CD
				     , CD.DELV_FEE_CD
				     , CD.ORD_NO
			) Z
			WHERE  1=1
			AND    Z.CPN_SQ IS NOT NULL
		) X
		INNER  JOIN TB_CUST_COUPON CC
		ON     X.CPN_SQ = CC.CUST_CPN_SQ
		LEFT   OUTER JOIN TB_COUPON CP
		ON     CC.CPN_ID = CP.CPN_ID
		ORDER  BY DISP_ORD
	</select>
	
	<!-- 주문상세 > 포인트사용내역 -->
	<select id="getOrderDiscountPointList" parameterType="Order" resultType="Order">
		/* order.getOrderDiscountPointList */
		SELECT OD.ORD_NO
		     , OD.ORD_DTL_NO
		     , OD.GOODS_CD
		     , G.PNT_PRATE 
		     , G.PNT_MRATE 
		     , OD.CURR_PRICE
		     , OD.REAL_ORD_AMT 
		     , CPH.PNT_AMT
		     , CPH.OCCUR_GB 
		     , CPH.OCCUR_DTL_DESC 
		     , DATE_FORMAT(CPH.REG_DT,'%Y%m%d%H%i%S') AS REG_DT
		FROM   TB_ORDER_DETAIL OD
		INNER  JOIN TB_CUST_POINT_HST CPH
		ON     OD.ORD_DTL_NO = CPH.ORD_DTL_NO
		AND    OD.ORD_NO = CPH.ORD_NO
		INNER  JOIN TB_GOODS G
		ON     OD.GOODS_CD = G.GOODS_CD
		WHERE  1=1
		AND    OD.ORD_NO = #{ordNo}
		ORDER  BY CPH.REG_DT DESC
	</select>
	
	<!-- 주문상세 > 상품권사용내역 -->
	<select id="getOrderGiftcardHstList" parameterType="Order" resultType="Order">
		/* order.getOrderGiftcardHstList */
		SELECT CG.CUST_GFCD_SQ 
		     , CG.CHG_GFCD_AMT 
		     , CG.GFCD_NM 
		     , CG.GFCD_NO 
		     , CG.CHG_GFCD_AMT 
		     , CG.US_GFCD_AMT 
		     , CG.RM_GFCD_AMT 
		     , OD.ORD_NO
		     , OD.ORD_DTL_NO
		     , OD.GOODS_CD 
		     , CGH.GFCD_AMT
		     , CGH.OCCUR_GB 
		     , CGH.OCCUR_DTL_DESC 
		     , DATE_FORMAT(CGH.REG_DT,'%Y%m%d%H%i%S') AS REG_DT
		     , CG.CUST_GFCD_SQ 
		FROM   TB_ORDER_DETAIL OD
		INNER  JOIN TB_CUST_GIFTCARD_HST CGH
		ON     OD.ORD_DTL_NO = CGH.ORD_DTL_NO
		AND    OD.ORD_NO = CGH.ORD_NO
		INNER  JOIN TB_CUST_GIFTCARD CG
		ON     CGH.CUST_GFCD_SQ  = CG.CUST_GFCD_SQ 
		INNER  JOIN TB_GOODS G
		ON     OD.GOODS_CD = G.GOODS_CD
		WHERE  1=1
		AND    OD.ORD_NO = #{ordNo}
		ORDER  BY CUST_GFCD_SQ
		     , OD.ORD_NO
		     , OD.ORD_DTL_NO
	</select>

	<!-- 주문상세 > 다다익선적용내역 -->
	<select id="getOrderTmtbHstList" parameterType="Order" resultType="Order">
		/* order.getOrderTmtbHstList */
		WITH TMTB_DATA AS (
			SELECT TM1.TMTB_SQ			AS  TMTB1_SQ
			     , TM1.TMTB_NM			AS  TMTB1_NM
			     , OD.TMTB1_DC_AMT
			     , OD.ORD_NO
			     , OD.ORD_DTL_NO
			     , OD.REG_NO
			     , OD.REG_DT
			     , OD.GOODS_CD
			     , TM2.TMTB_SQ			AS  TMTB2_SQ
			     , TM2.TMTB_NM			AS  TMTB2_NM
			     , OD.TMTB2_DC_AMT
			FROM   TB_ORDER_DETAIL OD
			INNER  JOIN TB_TMTB TM1
			ON     OD.TMTB1_SQ = TM1.TMTB_SQ 
			INNER  JOIN TB_TMTB TM2
			ON     OD.TMTB2_SQ = TM2.TMTB_SQ 
			WHERE  1=1
			AND    OD.ORD_NO = 9
		)
		SELECT Z.*
		FROM (
			SELECT 1                    AS DISP_ORD
			     , TD.TMTB1_SQ        	AS TMTB_SQ
			     , TD.TMTB1_NM			AS TMTB_NM
			     , TD.TMTB1_DC_AMT      AS TMTB_DC_AMT
			     , TD.ORD_NO
			     , TD.ORD_DTL_NO
			     , TD.GOODS_CD
			     , TD.REG_NO
			     , DATE_FORMAT(TD.REG_DT,'%Y%m%d%H%i%S') AS REG_DT
			FROM   TMTB_DATA TD
			UNION  ALL
			SELECT 2                    AS DISP_ORD
			     , TD.TMTB2_SQ        	AS TMTB_SQ
			     , TD.TMTB2_NM			AS TMTB_NM
			     , TD.TMTB2_DC_AMT      AS TMTB_DC_AMT
			     , TD.ORD_NO
			     , TD.ORD_DTL_NO
			     , TD.GOODS_CD
			     , TD.REG_NO
			     , DATE_FORMAT(TD.REG_DT,'%Y%m%d%H%i%S') AS REG_DT
			FROM   TMTB_DATA TD
		) Z
		WHERE  1=1
		AND    Z.TMTB_SQ IS NOT NULL
		ORDER  BY Z.DISP_ORD
	</select>
	
	<!-- 주문상세 > 주문상세상태변경 -->
	<update id="changedOrdDtlStat" parameterType="Order">
		/* order.changedOrdDtlStat */
		UPDATE TB_ORDER_DETAIL 
		SET    ORD_DTL_STAT = #{ordDtlStat}
		     , UPD_NO = #{updNo}
		     , UPD_DT = CURRENT_TIMESTAMP() 
		WHERE  1=1
		AND    ORD_NO = #{ordNo}
		AND    ORD_DTL_NO = #{ordDtlNo}
	</update>
	
	<!-- 주문정보 -->
	<select id="getOrderInfo" parameterType="Order" resultType="Order">
		SELECT O.ORD_NO
		     , O.MALL_GB
		     , FN_GET_CODE_NM('G011', O.MALL_GB) AS MALL_GB_NM
		     , DATE_FORMAT(O.ORD_DT,'%Y%m%d%H%i%S') AS ORD_DT
		     , O.CUST_NO
		     , O.ORD_NM
		     , O.ORD_PHNNO
		     , O.ORD_TELNO
		     , O.ORD_EMAIL
		     , O.SITE_CD
		     , FN_GET_CODE_NM('G000', O.SITE_CD) AS SITE_NM
		     , O.FRONT_GB
		     , O.REG_NO
		     , DATE_FORMAT(O.REG_DT,'%Y%m%d%H%i%S') AS REG_DT
		     , O.UPD_NO
		     , DATE_FORMAT(O.UPD_DT,'%Y%m%d%H%i%S') AS UPD_DT
		     , P.PAY_MEANS
		     , FN_GET_CODE_NM('G014', P.PAY_MEANS) AS PAY_MEANS_NM
		     , DATE_FORMAT(O.PAY_DT,'%Y%m%d%H%i%S') AS PAY_DT
		     , P.PAY_AMT
		     , P.PAY_SQ
		     , P.PAY_STAT
		     , P.PG_GB
		     , P.PG_TID
		     , P.ESCROW_YN
		     , C.CUST_GB
		     , C.CUST_GRADE
		     , S.SUM_ORD_QTY
		     , S.SUM_CNCL_RTN_QTY
		     , S.SUM_REAL_ORD_AMT
		     , S.SUM_CNCL_RTN_AMT
		     , S.SUM_CART_CPN_DC_AMT
		FROM   TB_ORDER O
		INNER  JOIN TB_PAYMENT P
		ON     O.ORD_NO = P.ORD_NO
		INNER  JOIN (
			SELECT MAX(PAY_SQ) AS PAY_SQ 
			FROM   TB_PAYMENT 
			WHERE  ORD_NO = #{ordNo}
		) PS
		ON     P.PAY_SQ = PS.PAY_SQ
		LEFT   OUTER JOIN TB_CUSTOMER C
		ON     O.CUST_NO = C.CUST_NO
		INNER  JOIN (
			SELECT ORD_NO
			     , NVL(MIN(CART_CPN_SQ),0) AS CART_CPN_SQ
			     , SUM(OD.ORD_QTY)         AS SUM_ORD_QTY
			     , SUM(OD.CNCL_RTN_QTY)    AS SUM_CNCL_RTN_QTY
			     , SUM(OD.REAL_ORD_AMT)    AS SUM_REAL_ORD_AMT
			     , SUM(OD.CNCL_RTN_AMT)    AS SUM_CNCL_RTN_AMT
			     , SUM(OD.CART_CPN_DC_AMT) AS SUM_CART_CPN_DC_AMT
			FROM  TB_ORDER_DETAIL OD
			WHERE ORD_NO = #{ordNo}
			GROUP BY ORD_NO
		) S
		ON    1=1
		LEFT  OUTER JOIN TB_CUST_COUPON CC
		ON    S.CART_CPN_SQ = CC.CUST_CPN_SQ 
		WHERE O.ORD_NO = P.ORD_NO
		AND   O.ORD_NO = #{ordNo}
	</select>
	
	<!-- 주문상세 > 주문취소대상목록 -->
	<select id="getCancelRequestTargetList" parameterType="Order" resultType="Order">
		SELECT OD.ORD_NO
		     , OD.ORD_DTL_NO
		     , G1.GOODS_CD
		     , G1.GOODS_NM
		     , G1.GOODS_TYPE
		     , FN_GET_CODE_NM('G056', G1.GOODS_TYPE) AS GOODS_TYPE_NM
		     , ODI.ORD_DTL_ITEM_SQ
		     , G2.GOODS_CD AS ITEM_CD
		     , G2.GOODS_NM AS ITEM_NM
		     , ODI.OPT_CD
		     , ODI.OPT_CD1
		     , ODI.OPT_CD2
		     , ODI.ITEM_QTY
		     , OD.ORD_QTY
             , OD.CNCL_RTN_QTY 
		     , CASE WHEN OCD.ORD_REQ_CHG_QTY IS NULL
		            THEN 0
		            ELSE OCD.ORD_REQ_CHG_QTY * ODI.ITEM_QTY
		            END  ORD_REQ_CHG_QTY
		     , ODI.ITEM_PRICE
		     , ODI.OPT_ADD_PRICE
		     , ODI.ORD_AMT
		     , ODI.CNCL_RTN_AMT
		     , ODI.CPN1_DC_AMT 
		     , ODI.TMTB1_DC_AMT 
		     , ODI.TMTB2_DC_AMT 
		     , ODI.GOODS_CPN_DC_AMT 
		     , ODI.CART_CPN_DC_AMT
		     , ODI.PNT_DC_AMT 
		     , ODI.PRE_PNT_DC_AMT 
		     , ODI.GFCD_USE_AMT
		     , ODI.REAL_ORD_AMT
		     , DF.DELV_FEE
		     , DF.SUPPLY_COMP_CD
		     , DF.DELV_FEE_CD 
		     , DFP.MIN_ORD_AMT 
		     , DFP.DELV_FEE AS ORG_DELV_FEE
		     , 0			AS ORD_CAN_CHG_QTY
		FROM   TB_ORDER_DETAIL OD
		INNER  JOIN TB_GOODS G1
		ON     OD.GOODS_CD = G1.GOODS_CD
		INNER  JOIN TB_ORDER_DETAIL_ITEM ODI
		ON     OD.ORD_NO = ODI.ORD_NO
		AND    OD.ORD_DTL_NO = ODI.ORD_DTL_NO
		INNER  JOIN TB_GOODS G2
		ON     ODI.ITEM_CD = G2.GOODS_CD
		LEFT   OUTER JOIN (
			SELECT OD.ORD_DTL_NO
			     , SUM(OCD.CHG_QTY) AS ORD_REQ_CHG_QTY
			FROM   TB_ORDER_CHANGE_DETAIL OCD
			INNER  JOIN TB_ORDER_DETAIL OD
			ON     OCD.ORD_DTL_NO = OD.ORD_DTL_NO
			WHERE  1=1
			AND    OD.ORD_NO = 9
			AND    OCD.CHG_STAT IN (
				'G685_20', 'G685_30', 'G685_40'
			)
			GROUP  BY OCD.ORD_DTL_NO
		) OCD
		ON     OD.ORD_DTL_NO = OCD.ORD_DTL_NO
		INNER  JOIN TB_DELIVERY_FEE DF
		ON     OD.SUPPLY_COMP_CD = DF.SUPPLY_COMP_CD 
		AND    OD.DELV_FEE_CD = DF.DELV_FEE_CD 
		INNER  JOIN TB_DELV_FEE_POLICY DFP
		ON     DF.SUPPLY_COMP_CD  = DFP.SUPPLY_COMP_CD 
		AND    DF.DELV_FEE_CD = DFP.DELV_FEE_CD 
		WHERE  1=1
		AND    OD.ORD_DTL_STAT IN (
			'G013_10', 'G013_20', 'G013_30', 'G013_40'
		)
		AND    OD.ORD_NO = 9
		GROUP  BY OD.ORD_NO
		     , OD.ORD_DTL_NO
	    ORDER  BY OD.ORD_NO
	         , OD.ORD_DTL_NO
	</select>
</mapper>









