<?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
				     , 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
				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>
</mapper>















