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

	<!-- 페이징을 위한 select절 상단 -->
	<sql id="selectForPagingHeader">
		SELECT *
		  FROM (
	</sql>

	<!-- 페이징을 위한 select절 하단 -->
	<sql id="selectForPagingFooter">
		) ORIGINAL
		WHERE NUMB BETWEEN #{pageable.startRow} AND #{pageable.endRow}
	</sql>

	<!-- 주문/배송 > 입점업체주문목록 -->
	<select id="getOrderSellerUnorderListCount" parameterType="Order" resultType="int">
		/* TscScm.getOrderSellerUnorderListCount */
		SELECT COUNT(1)
		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_PAYMENT P
		ON     O.ORD_NO = P.ORD_NO
		AND    P.PAY_GB = 'O'
		AND    P.PAY_STAT = 'G016_30'
		INNER  JOIN TB_GOODS G
		ON     OD.GOODS_CD = G.GOODS_CD
		INNER  JOIN TB_DELIVERY_ADDR DA
		ON     OD.DELV_ADDR_SQ = DA.DELV_ADDR_SQ
		LEFT   OUTER JOIN (SELECT OD.ORD_NO
		                        , OD.DELV_FEE_CD
		                   FROM   TB_ORDER_CHANGE_DETAIL OCD
		                   INNER  JOIN TB_ORDER_DETAIL OD
		                   ON     OD.ORD_DTL_NO = OCD.ORD_DTL_NO
		                   WHERE  1=1
		                   AND    OCD.CHG_STAT IN ('G685_10', 'G685_11', 'G685_50', 'G685_51')
		                   AND    OCD.CHG_QTY > 0
		                   AND    OCD.DEL_YN = 'N'
		                   GROUP  BY OD.ORD_NO, OD.DELV_FEE_CD) OCD
		ON     OCD.ORD_NO = OD.ORD_NO
		AND    OCD.DELV_FEE_CD = OD.DELV_FEE_CD
		WHERE  1=1
		AND    DA.RECIP_BASE_ADDR IS NOT NULL
		AND    G.SELF_GOODS_YN = 'N'
		<if test='ifYn == null or ifYn == ""'>
		AND    OCD.ORD_NO IS NULL
		</if>
		<if test='supplyCompCd != null and supplyCompCd != ""'>
		AND    OD.SUPPLY_COMP_CD = #{supplyCompCd}
		</if>
		<if test="multiSupplyCompCd != null and multiSupplyCompCd != ''">
		AND    OD.SUPPLY_COMP_CD IN
			<foreach collection="multiSupplyCompCd" item="item" index="index"  open="(" close=")" separator=",">
		           #{item}
			</foreach>
		</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"'>
		  <if test="stDate != null and stDate != ''">
		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>
		AND    OD.ORD_QTY - OD.CNCL_RTN_QTY > 0
		AND    NOT EXISTS (
		    SELECT 1
		    FROM   TB_EXCHANGE_DELV_FAIL EDF
		    WHERE  1=1
		    AND    EDF.ORD_DTL_NO = OD.ORD_DTL_NO
		    AND    EDF.DELV_CNCL_YN = 'N'
		)
	</select>

	<!-- 주문/배송 > 입점업체주문목록 -->
	<select id="getOrderSellerUnorderList" parameterType="Order" resultType="Order">
		/* tscScm.getOrderSellerUnorderList */
		SELECT X.*
		FROM (SELECT @rownum := @rownum + 1 AS RNUM
		           , Z.*
		      FROM (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
		                 , G.BRAND_CD
		                 , (SELECT B.BRAND_KNM
		                    FROM   TB_BRAND B
		                    WHERE  B.BRAND_CD = G.BRAND_CD) AS BRAND_KNM
		                 , 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.CUST_NO
		                 , O.ORD_NM
		                 , DATE_FORMAT(O.ORD_DT,'%Y%m%d%H%i%S') AS ORD_DT
		                 , P.PG_GB
		                 , P.PAY_MEANS
		                 , CASE WHEN P.PG_GB = 'KCP' THEN FN_GET_CODE_NM('G014', P.PAY_MEANS)
		                        WHEN P.PG_GB = 'ISTYLE' THEN '상품권'
		                        ELSE FN_GET_CODE_NM('G015', P.PG_GB) END AS PAY_MEANS_NM
		                 , O.ORD_PHNNO
		                 , DA.RECIP_NM
		                 , DA.RECIP_PHNNO
		                 , DA.RECIP_ZIPCODE
		                 , DA.RECIP_BASE_ADDR
		                 , DA.RECIP_DTL_ADDR
		                 , DA.DELV_MEMO
		                 , G.SUPPLY_GOODS_CD
		                 , G.GOODS_CD
		                 , G.GOODS_NM
		                 , ODI.OPT_CD1
		                 , ODI.OPT_CD2
		                 , OD.ORD_QTY
		                 , OD.CNCL_RTN_QTY
		                 , OD.ORD_AMT
		                 , OD.CPN1_DC_AMT + OD.TMTB1_DC_AMT + OD.TMTB2_DC_AMT + OD.GOODS_CPN_DC_AMT + OD.CART_CPN_DC_AMT + OD.PNT_DC_AMT + OD.PRE_PNT_DC_AMT AS TOT_DC_AMT
		                 , OD.REAL_ORD_AMT
		                 , OD.INVOICE_NO
		                 , OD.SHIP_COMP_CD
		                 , OD.ORD_EXCH_GB
		                 , CAC.BANK_CD
		                 , CAC.ACCOUNT_NO
		                 , CAC.ACCOUNT_NM
		                 , OD.REG_DT
		                 , 0 AS CANCEL_QTY		/*수정X > 입점발주확정 리스트 미출고수량 디폴트값을 위해 지정*/
		                 , OD.ORD_QTY - OD.CNCL_RTN_QTY AS DELV_QTY		/*수정X > 입점발주확정 리스트 출고수량 디폴트값을 위해 지정*/
		                 , OD.DELV_FEE_CD
		                 , (SELECT IFNULL(OCD2.ORD_CHG_SQ, 0) FROM TB_ORDER_CHANGE_DETAIL OCD2 WHERE OCD2.CHG_ORD_DTL_NO = OD.ORD_DTL_NO) AS ORD_CHG_SQ
		            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_PAYMENT P
		            ON     O.ORD_NO = P.ORD_NO
		            AND    P.PAY_GB = 'O'
		            AND    P.PAY_STAT = 'G016_30'
		            INNER  JOIN TB_GOODS G
		            ON     OD.GOODS_CD = G.GOODS_CD
		            INNER  JOIN TB_DELIVERY_ADDR DA
		            ON     OD.DELV_ADDR_SQ = DA.DELV_ADDR_SQ
		            LEFT   OUTER JOIN TB_CUST_ACCOUNT CAC
		            ON     CAC.CUST_NO = O.CUST_NO
		            AND    CAC.DEL_YN = 'N'
		            LEFT   OUTER JOIN (SELECT OD.ORD_NO
		                                    , OD.DELV_FEE_CD
		                               FROM   TB_ORDER_CHANGE_DETAIL OCD
		                               INNER  JOIN TB_ORDER_DETAIL OD
		                               ON     OD.ORD_DTL_NO = OCD.ORD_DTL_NO
		                               WHERE  1=1
		                               AND    OCD.CHG_STAT IN ('G685_10', 'G685_11', 'G685_50', 'G685_51')
		                               AND    OCD.CHG_QTY > 0
		                               AND    OCD.DEL_YN = 'N'
		                               GROUP  BY OD.ORD_NO, OD.DELV_FEE_CD) OCD
		            ON     OCD.ORD_NO = OD.ORD_NO
		            AND    OCD.DELV_FEE_CD = OD.DELV_FEE_CD
		            WHERE  1=1
		            AND    DA.RECIP_BASE_ADDR IS NOT NULL
		            AND    G.SELF_GOODS_YN = 'N'
		            <if test='ifYn == null or ifYn == ""'>
		            AND    OCD.ORD_NO IS NULL
		            </if>
		            <if test='supplyCompCd != null and supplyCompCd != ""'>
		            AND    OD.SUPPLY_COMP_CD = #{supplyCompCd}
		            </if>
		            <if test="multiSupplyCompCd != null and multiSupplyCompCd != ''">
		                   AND OD.SUPPLY_COMP_CD IN
						<foreach collection="multiSupplyCompCd" item="item" index="index"  open="(" close=")" separator=",">
		                       #{item}
						</foreach>
					</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"'>
		              <if test="stDate != null and stDate != ''">
		            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>
		            AND    OD.ORD_QTY - OD.CNCL_RTN_QTY > 0 
		            AND    NOT EXISTS (
		                        SELECT 1
		                        FROM   TB_EXCHANGE_DELV_FAIL EDF
		                        WHERE  1=1
		                        AND    EDF.ORD_DTL_NO = OD.ORD_DTL_NO
		                        AND    EDF.DELV_CNCL_YN = 'N'
		                   )
		            ) Z
		      JOIN (SELECT @rownum := 0) R
		      ORDER  BY Z.REG_DT ASC ) X
		WHERE  1=1
		AND    X.RNUM BETWEEN  #{pageable.startRow} AND #{pageable.endRow}
		ORDER  BY X.REG_DT ASC
	</select>

	<!-- 입점업체 엑셀다운용 임시테이블 삭제-->
	<delete id="deleteEntryExcelDownTmp" parameterType="Order">
		/* tscScm.deleteEntryExcelDownTmp */
		DELETE FROM TB_ENTRY_UPLOAD_EXCEL WHERE 1=1
	</delete>

	<!-- 주문상세정보 변경 (입점) -->
	<update id="updateOrderDetailStat" parameterType="Order">
		/* tscScm.updateOrderDetailStat */
		UPDATE TB_ORDER_DETAIL
		SET    ORD_DTL_STAT   = #{ordDtlStat}
		     , UPD_NO         = #{updNo}
		     , UPD_DT         = NOW()
		WHERE  ORD_DTL_NO     = #{ordDtlNo}
	</update>

	<!-- 주문 상품 정보 상태 변경  -->
	<update id="updateOrderDetailItemStat" parameterType="Order">
		/* tscScm.updateOrderDetailItemStat : 주문 상품 정보 상태 변경 */
		UPDATE TB_ORDER_DETAIL_ITEM
		SET    ORD_DTL_STAT   = #{ordDtlStat}
		     , UPD_NO         = #{updNo}
		     , UPD_DT         = NOW()
		WHERE  ORD_DTL_NO     = #{ordDtlNo}
	</update>

	<!-- 주문/배송 > 입점업체 엑셀다운 임시파일 체크-->
	<select id="getEntryUploadExcelCheck" parameterType="Order" resultType="int">
		/* tscScm.getEntryUploadExcelCheck */
		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">
		/* tscScm.createEntryExcelDownTmp */
		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">
		/* tscScm.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 = G.BRAND_CD) AS BRAND_KNM
		     , OD.ORD_NO
		     , OD.ORD_DTL_NO
		     , CASE WHEN DF.REAL_DELV_AMT IS NULL THEN 0 ELSE DF.REAL_DELV_AMT END AS REAL_DELV_AMT
		     , 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_ZIPCODE
		     , DA.RECIP_BASE_ADDR
		     , DA.RECIP_DTL_ADDR
		     , G.SUPPLY_GOODS_CD
		     , G.GOODS_CD
		     , G.GOODS_NM
		     , ODI.OPT_CD1
		     , ODI.OPT_CD2
		     , CONCAT(ODI.OPT_CD1, ',', ODI.OPT_CD2) AS OPT
		     , OD.ORD_QTY
		     , OD.CNCL_RTN_QTY
		     , OD.ORD_AMT
		     , OD.CNCL_RTN_AMT
		     , OD.CPN1_DC_AMT + OD.TMTB1_DC_AMT + OD.TMTB2_DC_AMT +  OD.GOODS_CPN_DC_AMT + OD.CART_CPN_DC_AMT + OD.PNT_DC_AMT + OD.PRE_PNT_DC_AMT AS TOT_DC_AMT
		     , OD.REAL_ORD_AMT
		     , OD.SHIP_COMP_CD
		     , OD.INVOICE_NO
		     , DA.DELV_MEMO
		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_PAYMENT P
		ON     O.ORD_NO = P.ORD_NO
		AND    P.PAY_GB = 'O'
		AND    P.PAY_STAT = 'G016_30'
		INNER  JOIN TB_GOODS G
		ON     OD.GOODS_CD = G.GOODS_CD
		INNER  JOIN TB_DELIVERY_ADDR DA
		ON     OD.DELV_ADDR_SQ = DA.DELV_ADDR_SQ
		LEFT   OUTER JOIN ( SELECT OD.ORD_NO
		                         , DF.ORD_CHG_SQ
		                         , DF.DELV_FEE_GB
		                         , DF.DELV_FEE_CD
		                         , IF(DF.ORD_CHG_SQ IS NULL, MIN(OD.ORD_DTL_NO), MIN(OCD.CHG_ORD_DTL_NO)) AS ORD_DTL_NO
		                         , MAX(IF(DF.REAL_DELV_AMT IS NULL, 0, DF.REAL_DELV_AMT)) AS REAL_DELV_AMT
		                    FROM   TB_ORDER O
		                    INNER  JOIN TB_ORDER_DETAIL OD
		                    ON     O.ORD_NO = OD.ORD_NO
		                    INNER  JOIN TB_DELIVERY_FEE DF
		                    ON     O.ORD_NO = DF.ORD_NO
		                    AND    DF.DELV_FEE_CD = OD.DELV_FEE_CD
		                    LEFT   OUTER JOIN TB_ORDER_CHANGE OC
		                    ON     DF.ORD_CHG_SQ = OC.ORD_CHG_SQ
		                    LEFT   OUTER JOIN TB_ORDER_CHANGE_DETAIL OCD
		                    ON     OC.ORD_CHG_SQ = OCD.ORD_CHG_SQ
		                    AND    OD.ORD_DTL_NO = OCD.CHG_ORD_DTL_NO
		                    WHERE  OD.ORD_QTY - CNCL_RTN_QTY > 0
		                    AND    DF.DELV_FEE_GB IN ('G018_10', 'G018_30')
		                    <if test="ordDtlNoArr != null and ordDtlNoArr.length > 0">
		                    AND    OD.ORD_DTL_NO IN
		                        <foreach collection="ordDtlNoArr" item="item" index="index"  open="(" close=")" separator=",">
		                            #{item}
		                        </foreach>
		                    </if>
		                    GROUP  BY OD.ORD_NO, OD.DELV_FEE_CD, DF.ORD_CHG_SQ, DF.DELV_FEE_CD, DF.DELV_FEE_GB) DF
		ON     DF.ORD_DTL_NO = OD.ORD_DTL_NO
		WHERE  1=1
		AND    DA.RECIP_BASE_ADDR IS NOT NULL
		<if test='supplyCompCd != null and supplyCompCd != ""'>
		AND    OD.SUPPLY_COMP_CD = #{supplyCompCd}
		</if>
		<if test="ordDtlNoArr != null and ordDtlNoArr.length > 0">
		AND	   OD.ORD_DTL_NO IN
			<foreach collection="ordDtlNoArr" item="item" index="index"  open="(" close=")" separator=",">
		       #{item}
			</foreach>
		</if>
	</select>

	<!-- 주문/배송 > 입점업체미발주목록 > 주문상세 목록 -->
	<select id="getOrderDetailInfo" parameterType="Order" resultType="Order">
		/* tscScm.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
		INNER  JOIN TB_GOODS G
		ON     OD.GOODS_CD = G.GOODS_CD
		AND    G.SELF_GOODS_YN = 'N'
		WHERE  O.ORD_NO = OD.ORD_NO
		AND    OD.ORD_DTL_NO = #{ordDtlNo}
	</select>

	<!-- 주문/배송 > 주문상세정보 변경 - 송장번호 적용(입점)  -->
	<update id="updateOrderDetailInvoice" parameterType="Order">
		/* tscScm.updateOrderDetailInvoice */
		UPDATE TB_ORDER_DETAIL
		SET ORD_DTL_STAT    = #{ordDtlStat}
		  , INVOICE_NO      = REPLACE(#{invoiceNo}, '-', '')
		  , SHIP_COMP_CD    = #{shipCompCd}
		  , UPD_NO          = #{updNo}
		  , UPD_DT          = NOW()
		WHERE ORD_DTL_NO    = #{ordDtlNo}
	</update>

	<!-- 주문 상세 송장번호 등록 -->
	<insert id="createOrderDetailInvoice" parameterType="Order">
		/* tscScm.createOrderDetailInvoice : 주문 상세 송장번호 등록 */
		INSERT INTO TB_ORDER_DETAIL_INVOICE (
		          ORD_NO
		        , ORD_DTL_NO
		        , INVOICE_NO
		        , DEL_YN
		        , REG_NO
		        , REG_DT
		        , UPD_NO
		        , UPD_DT
		) VALUES (
		          #{ordNo}
		        , #{ordDtlNo}
		        , #{invoiceNo}
		        , 'N'
		        , #{regNo}
		        , NOW()
		        , #{updNo}
		        , NOW()
		)
	</insert>
	
	<!-- 입점클레임목록조회(교환,반품접수) -->
	<select id="getOrderSellerClaimCount" parameterType="Order" resultType="int">
		/* tscScm.getOrderSellerClaimCount */
		SELECT COUNT(1)
		FROM   TB_ORDER_CHANGE_DETAIL OCD
		INNER  JOIN TB_ORDER_CHANGE OC
		ON     OCD.ORD_CHG_SQ = OC.ORD_CHG_SQ
		INNER  JOIN TB_ORDER_DETAIL OD
		ON     OCD.ORD_DTL_NO = OD.ORD_DTL_NO
		INNER  JOIN TB_ORDER O
		ON     OD.ORD_NO = O.ORD_NO
		INNER  JOIN TB_GOODS G
		ON     OD.GOODS_CD = G.GOODS_CD
		INNER  JOIN TB_ORDER_DETAIL_ITEM ODI
		ON     OD.ORD_DTL_NO = ODI.ORD_DTL_NO
		LEFT   OUTER JOIN TB_ORDER_DETAIL OD2
		ON     OCD.CHG_ORD_DTL_NO = OD2.ORD_DTL_NO
		LEFT   OUTER JOIN TB_ORDER_DETAIL_ITEM ODI2
		ON     OD2.ORD_DTL_NO = ODI2.ORD_DTL_NO
		WHERE  1=1
		AND    G.SELF_GOODS_YN = 'N'
		<if test='supplyCompCd != null and supplyCompCd != ""'>
		AND    OD.SUPPLY_COMP_CD = #{supplyCompCd}
		</if>
		<if test="multiSupplyCompCd != null and multiSupplyCompCd != ''">
		AND    OD.SUPPLY_COMP_CD IN
			<foreach collection="multiSupplyCompCd" item="item" index="index"  open="(" close=")" separator=",">
		    #{item}
			</foreach>
		</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='chgGb != null and chgGb != ""'>
		AND    OC.CHG_GB = #{chgGb}
		</if>
		<if test="multiChgStat != null and multiChgStat != ''">
		AND    OCD.CHG_STAT IN
			<foreach collection="multiChgStat" item="item" index="index"  open="(" close=")" separator=",">
				#{item}
			</foreach>
		</if>
		<if test="ordNo != null and ordNo != ''">
		AND    O.ORD_NO = #{ordNo}
		</if>
		<if test="chgerNm != null and chgerNm != ''">
		AND    OC.CHGER_NM = #{chgerNm}
		</if>
		<if test='goodsCd != null and goodsCd != ""'>
		AND    G.GOODS_CD = #{goodsCd}
		</if>
		<if test='ordChgSq != null and ordChgSq != ""'>
		AND    OCD.ORD_CHG_SQ = #{ordChgSq}
		</if>
		<if test='stDate != null and stDate != ""'>
			<if test='searchDateGb != null and searchDateGb == "ordDt"'>
		AND    OC.REG_DT >= DATE_FORMAT(#{stDate}, '%Y-%m-%d %H:%i:%S')
		AND    OC.REG_DT <![CDATA[<]]> DATE_FORMAT(DATE_ADD(#{edDate}, INTERVAL 1 DAY), '%Y-%m-%d %H:%i:%S')
			</if>
		</if>
		AND    OCD.CHG_STAT NOT IN ('G685_19', 'G685_49', 'G685_69')
		AND    OCD.CHG_QTY > 0
	</select>
	
	<!-- 입점클레임목록조회(교환,반품접수) -->
	<select id="getOrderSellerClaimList" parameterType="Order" resultType="Order">
		/* tscScm.getOrderSellerClaimList */
		SELECT X.*
		FROM (
		    SELECT @rownum := @rownum + 1 AS RNUM
		         , Z.*
		    FROM (
		        SELECT OD.SUPPLY_COMP_CD
		             , OCD.ORD_CHG_SQ
		             , (SELECT X.SUPPLY_COMP_NM
		                FROM   TB_SUPPLY_COMPANY X
		                WHERE  X.SUPPLY_COMP_CD = OD.SUPPLY_COMP_CD) AS SUPPLY_COMP_NM
		             , G.BRAND_CD
		             , (SELECT X.BRAND_KNM
		                FROM   TB_BRAND X
		                WHERE  X.BRAND_CD = G.BRAND_CD) AS BRAND_KNM
		             , OD.ORD_NO
		             , OCD.ORD_DTL_NO
		             , OC.CHG_GB
		             , FN_GET_CODE_NM('G680', OC.CHG_GB) AS CHG_GB_NM
		             , OCD.CHG_STAT
		             , FN_GET_CODE_NM('G685', OCD.CHG_STAT) AS CHG_STAT_NM
		             , OC.REG_DT
		             , OC.CHGER_NM
		             , OC.CHGER_PHNNO 
		             , OC.CHGER_ZIPCODE 
		             , OC.CHGER_BASE_ADDR 
		             , OC.CHGER_DTL_ADDR
		             , G.SUPPLY_GOODS_CD 
		             , OD.GOODS_CD
		             , G.GOODS_NM
		             , ODI.OPT_CD1 
		             , ODI.OPT_CD2
		             , OD.ORD_QTY
		             , OCD.CHG_QTY
		             , OCD.CHG_ORD_DTL_NO AS EXCHANGE_ORD_DTL_NO
		             , ODI2.OPT_CD1 AS OPT_CD11 
		             , ODI2.OPT_CD2 AS OPT_CD12
		             , OC.WD_INVOICE_NO 
		             , OC.WD_INVOICE_SEND_YN 
		             , OD.DELV_FEE_CD
		             , CASE WHEN G.SELF_GOODS_YN = 'Y'
		                    THEN 'WMS'
		                    ELSE OD.DELV_FEE_CD
		                    END  DELV_FEE_CD_GRP
		             , OC.WD_GB
		             , OC.CHG_REASON
		             , OC.CHG_MEMO
		             , OCD.WH_MEMO
		             , O.ORD_NM
		             , O.ORD_PHNNO
		             , OD.ORD_AMT
		             , OD.CNCL_RTN_AMT
		             , OD.CPN1_DC_AMT
		             , OD.TMTB1_DC_AMT
		             , OD.TMTB2_DC_AMT
		             , OD.GOODS_CPN_DC_AMT
		             , OD.CART_CPN_DC_AMT
		             , OD.PRE_PNT_DC_AMT
		             , OD.PNT_DC_AMT
		             , OD.REAL_ORD_AMT
		             , OD.GFCD_USE_AMT
		             , OD.SAVE_PNT_AMT
		        FROM   TB_ORDER_CHANGE_DETAIL OCD
		        INNER  JOIN TB_ORDER_CHANGE OC
		        ON     OCD.ORD_CHG_SQ = OC.ORD_CHG_SQ
		        INNER  JOIN TB_ORDER_DETAIL OD
		        ON     OCD.ORD_DTL_NO = OD.ORD_DTL_NO
		        INNER  JOIN TB_ORDER O
		        ON     OD.ORD_NO = O.ORD_NO
		        INNER  JOIN TB_GOODS G
		        ON     OD.GOODS_CD = G.GOODS_CD
		        INNER  JOIN TB_ORDER_DETAIL_ITEM ODI
		        ON     OD.ORD_DTL_NO = ODI.ORD_DTL_NO
		        LEFT   OUTER JOIN TB_ORDER_DETAIL OD2
		        ON     OCD.CHG_ORD_DTL_NO = OD2.ORD_DTL_NO
		        LEFT   OUTER JOIN TB_ORDER_DETAIL_ITEM ODI2
		        ON     OD2.ORD_DTL_NO = ODI2.ORD_DTL_NO
		        WHERE  1=1
		        AND    G.SELF_GOODS_YN = 'N'
				<if test='supplyCompCd != null and supplyCompCd != ""'>
		        AND    OD.SUPPLY_COMP_CD = #{supplyCompCd}
				</if>
				<if test="multiSupplyCompCd != null and multiSupplyCompCd != ''">
		        AND    OD.SUPPLY_COMP_CD IN
					<foreach collection="multiSupplyCompCd" item="item" index="index"  open="(" close=")" separator=",">
		               #{item}
					</foreach>
				</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='chgGb != null and chgGb != ""'>
		        AND    OC.CHG_GB = #{chgGb}
				</if>
				<if test="multiChgStat != null and multiChgStat != ''">
		        AND    OCD.CHG_STAT IN
					<foreach collection="multiChgStat" item="item" index="index"  open="(" close=")" separator=",">
		                #{item}
					</foreach>
				</if>
				<if test="ordNo != null and ordNo != ''">
		        AND    O.ORD_NO = #{ordNo}
				</if>
				<if test="chgerNm != null and chgerNm != ''">
		        AND    OC.CHGER_NM = #{chgerNm}
				</if>
				<if test='goodsCd != null and goodsCd != ""'>
		        AND    G.GOODS_CD = #{goodsCd}
				</if>
				<if test='ordChgSq != null and ordChgSq != ""'>
		        AND    OCD.ORD_CHG_SQ = #{ordChgSq}
				</if>
				<if test='stDate != null and stDate != ""'>
					<if test='searchDateGb != null and searchDateGb == "ordDt"'>
		        AND    OC.REG_DT >= DATE_FORMAT(#{stDate}, '%Y-%m-%d %H:%i:%S')
		        AND    OC.REG_DT <![CDATA[<]]> DATE_FORMAT(DATE_ADD(#{edDate}, INTERVAL 1 DAY), '%Y-%m-%d %H:%i:%S')
					</if>
				</if>
		        AND    OCD.CHG_STAT NOT IN ('G685_19', 'G685_49', 'G685_69')
		        AND    OCD.CHG_QTY > 0
		    ) Z
		    JOIN (SELECT @rownum := 0) R
		    ORDER  BY Z.REG_DT ASC
		) X
		WHERE  1=1
		AND    X.RNUM BETWEEN  #{pageable.startRow} AND #{pageable.endRow}
		ORDER  BY X.REG_DT ASC
	</select>
	
	<!-- 입점상품 변경사유 체크 -->
	<select id="getChgReasonCheck" parameterType="Order" resultType="int">
		/* tscScm.getChgReasonCheck */
		SELECT COUNT(1)
		FROM   TB_ORDER_CHANGE
		WHERE  1=1
		AND    ORD_CHG_SQ = #{ordChgSq}
		AND    CHG_REASON = #{chgReason}
	</select>
	
	<!-- 주문변경신청 완료 정보 조회(취소,반품) -->
	<select id="getCnclRtnCompleteList" parameterType="Order" resultType="Order">
		/* TscScm.getCnclRtnCompleteList */
		SELECT A.ORD_NO
		     , A.ORD_DTL_NO
		     , A.GOODS_CD
		     , A.GOODS_NM
		     , A.GOODS_TYPE_NM
		     , A.CHG_REASON 
		     , A.CHG_REASON_NM
		     , A.CHG_MEMO
		     , A.ADD_PAY_COST
		     , A.RA_BANK 
		     , A.RA_BANK_NM
		     , A.RA_NM 
		     , A.RA_NO
		     , A.REAL_DELV_AMT
		     , A.DELV_CPN_DC_AMT
		     , A.ORD_QTY
		     , A.CNCL_RTN_QTY
		     , A.ORD_AMT
		     , A.OPT_CD
		     , A.OPT_CD1
		     , A.OPT_CD2
		     , A.COLOR_NM
		     , A.CNCL_RTN_AMT
		     , A.CPN1_DC_AMT
		     , A.TMTB1_DC_AMT
		     , A.TMTB2_DC_AMT
		     , A.GOODS_CPN_DC_AMT
		     , A.CART_CPN_DC_AMT
		     , A.PNT_DC_AMT
		     , A.PRE_PNT_DC_AMT
		     , A.GFCD_USE_AMT
		     , A.REAL_ORD_AMT
		     , A.SAVE_PNT_AMT
		     , A.REFUND_AMT
		     , A.DEPOSIT_AMT
		     , A.RF_CPN1_AMT
		     , A.RF_TMTB1_AMT
		     , A.RF_TMTB2_AMT
		     , A.RF_GOODS_CPN_AMT
		     , A.RF_CART_CPN_AMT
		     , A.RF_DELV_CPN_AMT
		     , A.RF_PNT_AMT
		     , A.RF_PRE_PNT_AMT
		     , A.RF_GFCD_USE_AMT
		     , A.RF_DELV_GFCD_USE_AMT
		     , A.RF_CNCL_AMT
		     , A.RF_DELIVERY_FEE
		     , A.RF_REAL_CNCL_AMT
		     , A.SUPPLY_COMP_NM
		     , A.BRAND_GROUP_NM
		     , A.CHG_GB_NM
		     , A.CHG_STAT_NM
		     , A.REG_DT
		FROM (
		    SELECT ODIH.ORD_NO
		         , ODIH.ORD_DTL_NO
		         , G1.GOODS_CD
		         , G1.GOODS_NM
		         , CASE WHEN G1.GOODS_TYPE IN ('G056_N')
		                THEN '일반'
		                ELSE '셋트'
		                END GOODS_TYPE_NM
		         , 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)
  		                ELSE FN_GET_CODE_NM('G689', OC.CHG_REASON)
  		                END  CHG_REASON_NM
		         , CASE WHEN OC.CHG_MEMO IS NULL
		                THEN ''
		                ELSE OC.CHG_MEMO
		                END  CHG_MEMO
		         , OC.ADD_PAY_COST
		         , RF.RA_BANK 
		         , FN_GET_CODE_NM('G940', RF.RA_BANK) AS RA_BANK_NM
		         , RF.RA_NM 
		         , RF.RA_NO
		         , IFNULL(DDF.REAL_DELV_AMT, 0) * -1 AS REAL_DELV_AMT
		         , RF.RF_DELV_CPN_AMT AS DELV_CPN_DC_AMT
		         , IFNULL((SELECT COLOR_KNM FROM TB_COLOR C WHERE COLOR_CD = IFNULL(NULLIF(ODIH.OPT_CD1,'') , G1.MAIN_COLOR_CD ) AND USE_YN  = 'Y'), ODIH.OPT_CD1) AS COLOR_NM
		         , ODIH.ORD_DTL_ITEM_SQ
		         , ODIH.OPT_CD
		         , ODIH.OPT_CD1
		         , ODIH.OPT_CD2
		         , ODIH.ORD_QTY
		         , ODIH.CNCL_RTN_QTY
		         , ODIH.ORD_AMT
		         , ODIH.CNCL_RTN_AMT
		         , ODIH.CPN1_DC_AMT
		         , ODIH.TMTB1_DC_AMT 
		         , ODIH.TMTB2_DC_AMT 
		         , ODIH.GOODS_CPN_DC_AMT
		         , ODIH.CART_CPN_DC_AMT
		         , ODIH.PNT_DC_AMT
		         , ODIH.PRE_PNT_DC_AMT 
		         , ODIH.GFCD_USE_AMT 
		         , ODIH.REAL_ORD_AMT 
		         , ODIH.SAVE_PNT_AMT
		         , RF.REFUND_AMT
		         , RF.DEPOSIT_AMT
		         , RF.RF_CPN1_AMT
		         , RF.RF_TMTB1_AMT
		         , RF.RF_TMTB2_AMT
		         , RF.RF_GOODS_CPN_AMT
		         , RF.RF_CART_CPN_AMT
		         , RF.RF_DELV_CPN_AMT
		         , RF.RF_PNT_AMT
		         , RF.RF_PRE_PNT_AMT
		         , RF.RF_GFCD_USE_AMT
		         , RF.RF_DELV_GFCD_USE_AMT
		         , RF.RF_CNCL_AMT
		         , RF.RF_DELIVERY_FEE
		         , RF.RF_REAL_CNCL_AMT
		         , FN_GET_SUPPLY_COMP_NM(OD.SUPPLY_COMP_CD) AS SUPPLY_COMP_NM
		         , (SELECT CASE WHEN BG.DISP_NM_LANG = 'EN'
		                        THEN BG.BRAND_GROUP_ENM
		                        ELSE BG.BRAND_GROUP_KNM
		                        END  BRAND_GROUP_NM
		            FROM   TB_BRAND BR
		            INNER  JOIN TB_BRAND_GROUP BG
		            ON     BR.BRAND_GROUP_NO = BG.BRAND_GROUP_NO
		            WHERE  1=1
		            AND    BR.BRAND_CD = G1.BRAND_CD
		            AND    BG.USE_YN = 'Y') AS BRAND_GROUP_NM
		         , FN_GET_CODE_NM('G680', OC.CHG_GB) AS CHG_GB_NM
		         , FN_GET_CODE_NM('G685', OCD.CHG_STAT) AS CHG_STAT_NM
		         , OC.REG_DT
		    FROM   TB_ORDER_DETAIL_ITEM_HST ODIH
		    INNER  JOIN TB_ORDER_DETAIL OD
		    ON     ODIH.ORD_DTL_NO = OD.ORD_DTL_NO
		    INNER  JOIN TB_GOODS G1
		    ON     OD.GOODS_CD = G1.GOODS_CD
		    INNER  JOIN TB_ORDER_CHANGE OC
		    ON     ODIH.ORD_CHG_SQ = OC.ORD_CHG_SQ
		    INNER  JOIN TB_ORDER_CHANGE_DETAIL OCD
		    ON     OC.ORD_CHG_SQ = OCD.ORD_CHG_SQ
		    INNER  JOIN TB_REFUND RF
		    ON     ODIH.ORD_CHG_SQ = RF.ORD_CHG_SQ
		    LEFT   OUTER JOIN (
		        SELECT SUM(DF.REAL_DELV_AMT) AS REAL_DELV_AMT
		             , DF.ORD_CHG_SQ
		        FROM   TB_DELIVERY_FEE DF
		        WHERE  DF.ORD_CHG_SQ = #{ordChgSq}
		        GROUP  BY DF.ORD_CHG_SQ
		    ) DDF
		    ON     DDF.ORD_CHG_SQ = ODIH.ORD_CHG_SQ
		    WHERE  1=1
		    AND    ODIH.ORD_CHG_SQ = #{ordChgSq}
		<choose>
			<when test="chgStat == 'G720_30'">
		    AND    ODIH.ORD_DTL_STAT IN ('G720_30', 'G720_40')
			</when>
			<otherwise>
		    AND    ODIH.ORD_DTL_STAT = #{chgStat}
			</otherwise>
		</choose>
		) A
		WHERE  1=1
		GROUP  BY A.ORD_NO
		     , A.ORD_DTL_NO
		     , A.GOODS_CD
		     , A.GOODS_NM
		     , A.GOODS_TYPE_NM
		     , A.CHG_REASON 
		     , A.CHG_REASON_NM
		     , A.CHG_MEMO
		     , A.ADD_PAY_COST
		     , A.RA_BANK 
		     , A.RA_BANK_NM
		     , A.RA_NM 
		     , A.RA_NO
		     , A.REAL_DELV_AMT
		     , A.DELV_CPN_DC_AMT
		     , A.ORD_QTY
		     , A.CNCL_RTN_QTY
		     , A.ORD_AMT
		     , A.OPT_CD
		     , A.OPT_CD1
		     , A.OPT_CD2
		     , A.COLOR_NM
		     , A.CNCL_RTN_AMT
		     , A.CPN1_DC_AMT
		     , A.TMTB1_DC_AMT
		     , A.TMTB2_DC_AMT
		     , A.GOODS_CPN_DC_AMT
		     , A.CART_CPN_DC_AMT
		     , A.PNT_DC_AMT
		     , A.PRE_PNT_DC_AMT
		     , A.GFCD_USE_AMT
		     , A.REAL_ORD_AMT
		     , A.SAVE_PNT_AMT
		     , A.REFUND_AMT
		     , A.DEPOSIT_AMT
		     , A.RF_CPN1_AMT
		     , A.RF_TMTB1_AMT
		     , A.RF_TMTB2_AMT
		     , A.RF_GOODS_CPN_AMT
		     , A.RF_CART_CPN_AMT
		     , A.RF_DELV_CPN_AMT
		     , A.RF_PNT_AMT
		     , A.RF_PRE_PNT_AMT
		     , A.RF_GFCD_USE_AMT
		     , A.RF_DELV_GFCD_USE_AMT
		     , A.RF_CNCL_AMT
		     , A.RF_DELIVERY_FEE
		     , A.RF_REAL_CNCL_AMT
		     , A.SUPPLY_COMP_NM
		     , A.BRAND_GROUP_NM
		     , A.CHG_GB_NM
		     , A.CHG_STAT_NM
		     , A.REG_DT
		ORDER  BY A.ORD_NO
		     , A.ORD_DTL_NO
	</select>
	
	<!-- 취소,반품,교환 대상목록 조회 -->
	<select id="getCancelRequestTargetList2" parameterType="Order" resultType="Order">
		/* TscScm.getCancelRequestTargetList2 */
		SELECT Z.*
		     , DF.DELV_FEE
		FROM (
		    SELECT A.ORD_NO
		         , A.ORD_DTL_NO
		         , A.ORD_DTL_STAT
		         , A.ORD_DTL_STAT_NM
		         , A.ALL_CAN_YN
		         , A.GOODS_CD
		         , A.GOODS_NM
		         , A.GOODS_TYPE
		         , A.GOODS_TYPE_NM
		         , GROUP_CONCAT(A.ITEM_CD ORDER BY A.ORD_DTL_ITEM_SQ) AS ITEM_CD
		         , GROUP_CONCAT(A.ITEM_NM ORDER BY A.ORD_DTL_ITEM_SQ) AS ITEM_NM
		         , GROUP_CONCAT(A.OPT_CD ORDER BY A.ORD_DTL_ITEM_SQ) AS OPT_CD
		         , GROUP_CONCAT(A.OPT_CD1 ORDER BY A.ORD_DTL_ITEM_SQ) AS OPT_CD1
		         , GROUP_CONCAT(A.OPT_CD2 ORDER BY A.ORD_DTL_ITEM_SQ) AS OPT_CD2
		         , GROUP_CONCAT(A.ITEM_QTY ORDER BY A.ORD_DTL_ITEM_SQ) AS ITEM_QTYR
		         , GROUP_CONCAT(A.COLOR_NM ORDER BY A.ORD_DTL_ITEM_SQ) AS COLOR_NM
		         , A.LIST_PRICE
		         , A.CURR_PRICE
		         , SUM(A.OPT_ADD_PRICE) AS OPT_ADD_PRICE
		         , A.ORD_QTY
		         , A.CNCL_RTN_QTY
		         , A.ORD_REQ_CHG_QTY
		         , A.CPN1_DC_AMT
		         , A.ORD_AMT
		         , A.CNCL_RTN_AMT
		         , A.TMTB1_DC_AMT
		         , A.TMTB2_DC_AMT
		         , A.GOODS_CPN_DC_AMT
		         , A.CART_CPN_DC_AMT
		         , A.PRE_PNT_DC_AMT
		         , A.PNT_DC_AMT
		         , A.GFCD_USE_AMT
		         , A.REAL_ORD_AMT
		         , A.SUPPLY_COMP_CD
		         , A.SUPPLY_COMP_NM
		         , A.DELV_FEE_CRITE
		         , A.MIN_ORD_AMT
		         , A.ORG_DELV_FEE
		         , A.RTN_DELV_FEE
		         , A.EXC_DELV_FEE
		         , A.SELF_GOODS_YN
		         , A.DELV_FEE_CD
		         , A.ORD_CAN_CHG_QTY
		         , A.CHG_STAT
		         , A.WD_GB
		         , A.ADD_PAY_COST
		         , A.DELV_FAIL_REASON
		         , A.BRAND_GROUP_NM
		         , A.CHG_GB_NM
		         , A.CHG_STAT_NM
		         , A.REG_DT
		    FROM (
		        SELECT 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
		             , CASE WHEN OD.ORD_DTL_STAT IN ('G013_10', 'G013_20', 'G013_30')
		                    THEN 'Y'
		                    ELSE 'N'
		                    END  ALL_CAN_YN
		             , 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
		             , ODI.ITEM_CD
		             , G2.GOODS_NM AS ITEM_NM
		             , ODI.OPT_CD
		             , ODI.OPT_CD1
		             , ODI.OPT_CD2
		             , ODI.ITEM_QTY
		             , IFNULL((SELECT COLOR_KNM FROM TB_COLOR WHERE COLOR_CD = IFNULL(NULLIF(ODI.OPT_CD1,'') , G1.MAIN_COLOR_CD ) AND USE_YN  = 'Y'), ODI.OPT_CD1) AS COLOR_NM
		             , OD.LIST_PRICE
		             , OD.CURR_PRICE
		             , ODI.OPT_ADD_PRICE
		             , OD.ORD_QTY
		             , OD.CNCL_RTN_QTY
		             , IFNULL(OCD.ORD_REQ_CHG_QTY, 0) AS ORD_REQ_CHG_QTY
		             , OD.CPN1_DC_AMT
		             , OD.ORD_AMT
		             , OD.CNCL_RTN_AMT
		             , OD.TMTB1_DC_AMT
		             , OD.TMTB2_DC_AMT
		             , OD.GOODS_CPN_DC_AMT
		             , OD.CART_CPN_DC_AMT
		             , OD.PRE_PNT_DC_AMT
		             , OD.PNT_DC_AMT
		             , OD.GFCD_USE_AMT
		             , OD.REAL_ORD_AMT
		             , OD.SUPPLY_COMP_CD
		             , FN_GET_SUPPLY_COMP_NM(OD.SUPPLY_COMP_CD) AS SUPPLY_COMP_NM
		             , DFP.DELV_FEE_CRITE
		             , CASE DFP.DELV_FEE_CRITE WHEN 'G078_30' THEN 99999999
		                                       WHEN 'G078_20' THEN 0
		                                       ELSE DFP.MIN_ORD_AMT
		                END AS MIN_ORD_AMT
		             , DFP.DELV_FEE AS ORG_DELV_FEE
		             , DFP.RTN_DELV_FEE
		             , DFP.RTN_DELV_FEE + DFP.RTN_DELV_FEE AS EXC_DELV_FEE
		             , G1.SELF_GOODS_YN
		             , CASE WHEN G1.SELF_GOODS_YN = 'Y'
		                    THEN 'WMS'
		                    ELSE OD.DELV_FEE_CD
		                    END  DELV_FEE_CD
		             , CASE WHEN X.CHG_STAT IS NOT NULL 
		                    THEN X.CHG_QTY
		                    ELSE Y.CNCL_RTN_QTY
		                    END ORD_CAN_CHG_QTY
		             , CASE WHEN X.CHG_STAT IS NOT NULL 
		                    THEN X.CHG_STAT
		                    ELSE Y.CHG_STAT
		                    END CHG_STAT
		             , Y.WD_GB
		             , Y.ADD_PAY_COST
		             , Y.DELV_FAIL_REASON
		             , (SELECT CASE WHEN BG.DISP_NM_LANG = 'EN'
		                       THEN BG.BRAND_GROUP_ENM
		                       ELSE BG.BRAND_GROUP_KNM
		                       END  BRAND_GROUP_NM
		                FROM   TB_BRAND BR
		                INNER  JOIN TB_BRAND_GROUP BG
		                ON     BR.BRAND_GROUP_NO = BG.BRAND_GROUP_NO
		                WHERE  1=1
		                AND    BR.BRAND_CD = G1.BRAND_CD
		                AND    BG.USE_YN = 'Y') AS BRAND_GROUP_NM
		             , FN_GET_CODE_NM('G680', X.CHG_GB) AS CHG_GB_NM
		             , FN_GET_CODE_NM('G685', X.CHG_STAT) AS CHG_STAT_NM
		             , X.REG_DT
		        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_DTL_NO = ODI.ORD_DTL_NO
		        INNER  JOIN TB_GOODS G2
		        ON     ODI.ITEM_CD = G2.GOODS_CD
		        LEFT   OUTER JOIN (
		            SELECT OCD.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
		            INNER  JOIN TB_ORDER_CHANGE OC
		            ON     OC.ORD_CHG_SQ = OCD.ORD_CHG_SQ
		            WHERE  1=1
		            AND    OD.ORD_NO = #{ordNo}
		            AND    OCD.DEL_YN = 'N'
		            AND    OCD.CHG_STAT IN ('G685_10', 'G685_11','G685_20', 'G685_21', 'G685_30', 'G685_31', 'G685_32', 'G685_33', 'G685_50', 'G685_51')
		            AND    OC.WD_BF_SEND_YN = 'N'
		            GROUP  BY OCD.ORD_DTL_NO
		        ) OCD
		        ON     OD.ORD_DTL_NO = OCD.ORD_DTL_NO
		        LEFT   OUTER JOIN TB_DELV_FEE_POLICY DFP
		        ON     OD.DELV_FEE_CD = DFP.DELV_FEE_CD
		        AND    OD.SUPPLY_COMP_CD = DFP.SUPPLY_COMP_CD
		        AND    DFP.USE_YN = 'Y'
		        LEFT OUTER JOIN (
		            SELECT OCD.ORD_DTL_NO
		                 , OCD.CHG_QTY
		                 , OCD.CHG_STAT
		                 , OC.CHG_GB
		                 , OC.REG_DT
		              FROM TB_ORDER_CHANGE_DETAIL OCD
		             INNER JOIN TB_ORDER_CHANGE OC
		                ON OCD.ORD_CHG_SQ = OC.ORD_CHG_SQ
		             WHERE OCD.DEL_YN = 'N'
		               AND OCD.ORD_CHG_SQ = #{ordChgSq}
		        ) X
		        ON OD.ORD_DTL_NO = X.ORD_DTL_NO
		        LEFT OUTER JOIN (
		            SELECT EDF.ORD_DTL_NO
		                 , EDF.CNCL_RTN_QTY
		                 , OCD.CHG_STAT
		                 , OC.WD_GB
		                 , OC.ADD_PAY_COST
		                 , EDF.DELV_FAIL_REASON
		              FROM TB_EXCHANGE_DELV_FAIL EDF
		             INNER JOIN TB_ORDER_CHANGE_DETAIL OCD
		                ON EDF.ORD_DTL_NO = OCD.CHG_ORD_DTL_NO
		             INNER JOIN TB_ORDER_CHANGE OC
		                ON OC.ORD_CHG_SQ = OCD.ORD_CHG_SQ
		             WHERE OCD.DEL_YN = 'N'
		               AND OCD.ORD_CHG_SQ = #{ordChgSq}
		               AND EDF.DELV_CNCL_YN = 'N'
		        ) Y
		        ON OD.ORD_DTL_NO = Y.ORD_DTL_NO
		        WHERE  1=1
		        AND    OD.ORD_NO = #{ordNo}
		        <if test="ordDtlStatArr != null and ordDtlStatArr != ''">
		        AND OD. ORD_DTL_STAT IN
					<foreach collection="ordDtlStatArr" item="item" index="index"  open="(" close=")" separator=",">
		            #{item}
					</foreach>
				</if>
				<if test="ordDtlNoArr != null and ordDtlNoArr != ''">
		        AND OD.ORD_DTL_NO IN
					<foreach collection="ordDtlNoArr" item="item" index="index"  open="(" close=")" separator=",">
		            #{item}
					</foreach>
				</if>
				<choose>
					<when test="chgGb == 'G680_30'">
		        AND    OD.RETURNABLE_YN = 'Y'
					</when>
					<when test="chgGb == 'G680_40'">
		        AND    OD.CHANGEABLE_YN = 'Y'
					</when>
				</choose>
		    ) A
		    GROUP  BY A.ORD_NO
		         , A.ORD_DTL_NO
		         , A.ORD_DTL_STAT
		         , A.ORD_DTL_STAT_NM
		         , A.ALL_CAN_YN
		         , A.GOODS_CD
		         , A.GOODS_NM
		         , A.GOODS_TYPE
		         , A.GOODS_TYPE_NM
		         , A.LIST_PRICE
		         , A.CURR_PRICE
		         , A.ORD_QTY
		         , A.CNCL_RTN_QTY
		         , A.ORD_REQ_CHG_QTY
		         , A.CPN1_DC_AMT
		         , A.ORD_AMT
		         , A.CNCL_RTN_AMT
		         , A.TMTB1_DC_AMT
		         , A.TMTB2_DC_AMT
		         , A.GOODS_CPN_DC_AMT
		         , A.CART_CPN_DC_AMT
		         , A.PRE_PNT_DC_AMT
		         , A.PNT_DC_AMT
		         , A.GFCD_USE_AMT
		         , A.REAL_ORD_AMT
		         , A.SUPPLY_COMP_CD
		         , A.SUPPLY_COMP_NM
		         , A.DELV_FEE_CRITE
		         , A.MIN_ORD_AMT
		         , A.ORG_DELV_FEE
		         , A.RTN_DELV_FEE
		         , A.EXC_DELV_FEE
		         , A.SELF_GOODS_YN
		         , A.DELV_FEE_CD
		         , A.ORD_CAN_CHG_QTY
		         , A.CHG_STAT
		         , A.WD_GB
		         , A.ADD_PAY_COST
		         , A.DELV_FAIL_REASON
		         , A.BRAND_GROUP_NM
		         , A.CHG_GB_NM
		         , A.CHG_STAT_NM
		         , A.REG_DT
		) Z
		INNER  JOIN (
		    SELECT X.DELV_FEE_CD
		         , SUM(X.DELV_FEE) AS DELV_FEE
		    FROM   TB_DELIVERY_FEE X
		    WHERE  1=1
		    AND    X.ORD_NO = #{ordNo}
		    AND    X.DELV_FEE_GB = 'G018_10'
		    GROUP  BY X.DELV_FEE_CD
		) DF
		ON     Z.DELV_FEE_CD = DF.DELV_FEE_CD
		WHERE  1=1
		AND    Z.DELV_FEE_CD = #{delvFeeCdGrp}
		ORDER  BY Z.DELV_FEE_CD
		     , Z.ORD_NO
		     , Z.ORD_DTL_NO
	</select>
	
	<!-- 취소,반품,교환 대상목록 조회 -->
	<select id="getCancelRequestTargetList" parameterType="Order" resultType="Order">
		/* TscScm.getCancelRequestTargetList */
		SELECT Y.*
		FROM (
		    SELECT Z.*
		         , DF.DELV_FEE
		      FROM (
		        SELECT 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
		             , CASE WHEN OD.ORD_DTL_STAT IN ('G013_20', 'G013_30', 'G013_40')
		                    THEN 'Y'
		                    ELSE 'N'
		               END                                      AS ALL_CAN_YN
		             , O.CUST_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                                      AS 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
		             , OD.SUPPLY_COMP_CD
		             , FN_GET_SUPPLY_COMP_NM(OD.SUPPLY_COMP_CD) AS SUPPLY_COMP_NM
		             , DFP.DELV_FEE_CRITE
		             , CASE DFP.DELV_FEE_CRITE WHEN 'G078_30' THEN 99999999
		                                       WHEN 'G078_20' THEN 0
		                                       ELSE DFP.MIN_ORD_AMT
		                END                                     AS MIN_ORD_AMT
		             , DFP.DELV_FEE                             AS ORG_DELV_FEE
		             , DFP.RTN_DELV_FEE
		             , DFP.RTN_DELV_FEE + DFP.RTN_DELV_FEE      AS EXC_DELV_FEE
		             , X.CHG_QTY                                AS ORD_CAN_CHG_QTY
		             , X.OPT_CD                                 AS ORD_CHG_OPT
		             , VS.OPT_CDS
		             , VS.OPT_CD2S
		             , VS.CURR_STOCK_QTYS
		             , G1.SELF_GOODS_YN
		             , CASE WHEN G1.SELF_GOODS_YN = 'Y'
		                    THEN 'WMS'
		                    ELSE OD.DELV_FEE_CD
		                     END DELV_FEE_CD
		             , X.CHG_GB
		             , (SELECT CASE WHEN BG.DISP_NM_LANG = 'EN'
		                       THEN BG.BRAND_GROUP_ENM
		                       ELSE BG.BRAND_GROUP_KNM
		                       END  BRAND_GROUP_NM
		                FROM   TB_BRAND BR
		                INNER  JOIN TB_BRAND_GROUP BG
		                ON     BR.BRAND_GROUP_NO = BG.BRAND_GROUP_NO
		                WHERE  1=1
		                AND    BR.BRAND_CD = G1.BRAND_CD
		                AND    BG.USE_YN = 'Y') AS BRAND_GROUP_NM
		             , FN_GET_CODE_NM('G680', X.CHG_GB) AS CHG_GB_NM
		             , FN_GET_CODE_NM('G685', X.CHG_STAT) AS CHG_STAT_NM
		             , X.REG_DT
		          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
		          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
		               INNER JOIN TB_ORDER_CHANGE OC
		                  ON OC.ORD_CHG_SQ = OCD.ORD_CHG_SQ
		               WHERE 1 = 1
		                 AND OD.ORD_NO = #{ordNo}
		                 AND OCD.DEL_YN = 'N'
		                 AND OCD.CHG_STAT IN ('G685_20', 'G685_21','G685_30', 'G685_31', 'G685_32', 'G685_33', 'G685_34', 'G685_50', 'G685_51')
		                 AND OC.WD_BF_SEND_YN = 'N'
		               GROUP BY OCD.ORD_DTL_NO
		          ) OCD
		            ON OD.ORD_DTL_NO = OCD.ORD_DTL_NO
		          LEFT OUTER JOIN TB_DELV_FEE_POLICY DFP
		            ON OD.DELV_FEE_CD = DFP.DELV_FEE_CD
		           AND OD.SUPPLY_COMP_CD = DFP.SUPPLY_COMP_CD
		           AND DFP.USE_YN = 'Y'
		          LEFT OUTER JOIN (
		              SELECT GOODS_CD
		                   , OPT_CD1
		                   , GROUP_CONCAT(OPT_CD ORDER BY DISP_ORD, OPT_CD2 SEPARATOR ',')                                 AS OPT_CDS
		                   , GROUP_CONCAT(OPT_CD2 ORDER BY DISP_ORD, OPT_CD2 SEPARATOR ',')                                AS OPT_CD2S
		                   , GROUP_CONCAT((CURR_STOCK_QTY - BASE_STOCK_QTY) ORDER BY DISP_ORD DESC, OPT_CD2 SEPARATOR ',') AS CURR_STOCK_QTYS
		                FROM VW_STOCK
		               WHERE GOODS_CD IN (SELECT ITEM_CD FROM TB_ORDER_DETAIL_ITEM WHERE ORD_NO = #{ordNo})
		               GROUP BY GOODS_CD
		                   , OPT_CD1
		          ) VS
		            ON ODI.ITEM_CD = VS.GOODS_CD
		           AND ODI.OPT_CD1 = VS.OPT_CD1
		          LEFT OUTER JOIN (
		              SELECT OCD.ORD_DTL_NO
		                   , OCD.CHG_QTY
		                   , ODI.OPT_CD
		                   , ODI.OPT_CD2
		                   , OC.CHG_GB
		                   , OCD.CHG_STAT
		                   , OC.REG_DT
		                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_ITEM ODI
		                  ON OCD.ORD_DTL_NO = ODI.ORD_DTL_NO
		               WHERE OCD.DEL_YN = 'N'
		                 AND OCD.ORD_CHG_SQ = #{ordChgSq}
		          ) X
		            ON ODI.ORD_DTL_NO = X.ORD_DTL_NO
		           AND ODI.OPT_CD = X.OPT_CD
		         WHERE 1 = 1
		<if test="ordDtlStatArr != null and ordDtlStatArr != ''">
		           AND OD. ORD_DTL_STAT IN
			<foreach collection="ordDtlStatArr" item="item" index="index"  open="(" close=")" separator=",">
					#{item}
			</foreach>
		</if>
		<if test="ordDtlNoArr != null and ordDtlNoArr != ''">
		           AND OD.ORD_DTL_NO IN
			<foreach collection="ordDtlNoArr" item="item" index="index"  open="(" close=")" separator=",">
					#{item}
			</foreach>
		</if>
		           AND OD.ORD_NO = #{ordNo}
		<choose>
			<when test="chgGb == 'G680_30'">
		           AND OD.RETURNABLE_YN = 'Y'
			</when>
			<when test="chgGb == 'G680_40'">
		           AND OD.CHANGEABLE_YN = 'Y'
			</when>
		</choose>          
		    ) Z
		    INNER JOIN (
		        SELECT X.DELV_FEE_CD
		             , SUM(X.DELV_FEE) AS DELV_FEE
		          FROM TB_DELIVERY_FEE X
		         WHERE 1 = 1
		           AND X.ORD_NO = #{ordNo}
		           AND X.DELV_FEE_GB = 'G018_10'
		         GROUP BY X.DELV_FEE_CD
		    ) DF
		    ON Z.DELV_FEE_CD = DF.DELV_FEE_CD
		    AND Z.DELV_FEE_CD = #{delvFeeCdGrp}
		) Y
		 GROUP BY Y.ORD_NO, Y.ORD_DTL_NO, Y.ORD_DTL_STAT, Y.ORD_DTL_STAT_NM, Y.ALL_CAN_YN, Y.GOODS_CD, Y.GOODS_NM, Y.GOODS_TYPE, Y.GOODS_TYPE_NM
		        , Y.ORD_DTL_ITEM_SQ, Y.ITEM_CD, Y.ITEM_NM, Y.OPT_CD, Y.OPT_CD1, Y.OPT_CD2, Y.ITEM_QTY, Y.ORD_QTY, Y.CNCL_RTN_QTY, Y.ORD_REQ_CHG_QTY
		        , Y.ITEM_PRICE, Y.OPT_ADD_PRICE, Y.ORD_AMT, Y.CNCL_RTN_AMT, Y.CPN1_DC_AMT, Y.TMTB1_DC_AMT, Y.TMTB2_DC_AMT, Y.GOODS_CPN_DC_AMT
		        , Y.CART_CPN_DC_AMT, Y.PNT_DC_AMT, Y.PRE_PNT_DC_AMT, Y.GFCD_USE_AMT, Y.REAL_ORD_AMT, Y.DELV_FEE, Y.SUPPLY_COMP_CD, Y.SUPPLY_COMP_NM
		        , Y.DELV_FEE_CD, Y.DELV_FEE_CRITE, Y.MIN_ORD_AMT, Y.ORG_DELV_FEE, Y.RTN_DELV_FEE, Y.EXC_DELV_FEE, Y.ORD_CAN_CHG_QTY, Y.ORD_CHG_OPT
		        , Y.OPT_CD2S, Y.CURR_STOCK_QTYS, Y.SELF_GOODS_YN, Y.CHG_GB, Y.BRAND_GROUP_NM, Y.CHG_GB_NM, Y.CHG_STAT_NM, Y.REG_DT
		 ORDER BY Y.DELV_FEE_CD, Y.ORD_NO, Y.ORD_DTL_NO
	</select>
	
	<!-- 추가배송비 결제여부 조회 -->
	<select id="getAddDelvFee" parameterType="Order" resultType="Order">
		/* TscScm.getAddDelvFee */
		SELECT OC.ADD_PAY_COST 
		     , P.PAY_STAT
		     , P.PG_GB
		     , P.PAY_MEANS
		     , FN_GET_CODE_NM('G014', P.PAY_MEANS) AS PAY_MEANS_NM
		FROM   TB_PAYMENT P
		INNER  JOIN TB_ORDER_CHANGE OC
		ON     P.ORD_CHG_SQ = OC.ORD_CHG_SQ 
		WHERE  1=1
		AND    OC.ORD_CHG_SQ = #{ordChgSq}
		AND    P.PAY_GB = 'D'
		AND    P.PAY_STAT NOT IN ('G016_00')
		LIMIT  1
	</select>
	
</mapper>















