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

	<!-- 제휴몰주문등록 - 주문업로드 그룹별 목록 -->
	<select id="getRegExtmallMasterOrderList" parameterType="ExtmallOrder" resultType="ExtmallOrder">
	/* TscOrderOcm.getExtmallMasterOrderList */
		SELECT
			A.VENDOR_ID, A.EXTMALL_ID, A.EXTMALL_ORDER_ID
			, IFNULL(A.ORD_NM, '') AS ORD_NM
			, IFNULL(A.ORD_PHNNO, '') AS ORD_PHNNO
			, IFNULL(A.ORD_TELNO, '') AS ORD_TELNO
			, IFNULL(A.ORD_EMAIL, '') AS ORD_EMAIL
			, IFNULL(A.DELV_MEMO, '') AS DELV_MEMO
			, IFNULL(A.RECIP_NM, '') AS RECIP_NM
			, IFNULL(A.RECIP_PHNNO, '') AS RECIP_PHNNO
			, IFNULL(A.RECIP_TELNO, '') AS RECIP_TELNO
			, IFNULL(A.RECIP_ZIPCODE, '') AS RECIP_ZIPCODE
			, IFNULL(A.RECIP_BASE_ADDR, '') AS RECIP_BASE_ADDR
			, IFNULL(A.RECIP_DTL_ADDR, '') AS RECIP_DTL_ADDR
			, FN_GET_SHOPLINKER_DEVL_FEE(#{uploadGb}, A.EXTMALL_ID, A.EXTMALL_ORDER_ID) AS DELV_FEE
			, SUM(A.ORD_AMT) AS REAL_ORD_SUM_AMT
			, (SELECT CNCLSMS_SEND_YN FROM TB_EXTMALL S WHERE S.VENDOR_ID = A.VENDOR_ID
				AND S.EXTMALL_ID = A.EXTMALL_ID AND S.USE_YN = 'Y') AS CNCLSMS_SEND_YN
		FROM TB_EXTMALL_ORDER_UPLOAD A
		WHERE A.UPLOAD_STAT = 'G021_00'
		AND RECIP_BASE_ADDR IS NOT NULL AND RECIP_BASE_ADDR != ''
		AND A.UPLOAD_GB = #{uploadGb}
		<if test="regNo != null and regNo != ''">
	      AND A.REG_NO = #{regNo}
	    </if>
		GROUP BY A.VENDOR_ID, A.EXTMALL_ID, A.EXTMALL_ORDER_ID, A.ORD_NM, A.ORD_PHNNO, A.ORD_TELNO, A.ORD_EMAIL, A.DELV_MEMO
				 , A.RECIP_NM, A.RECIP_PHNNO, A.RECIP_TELNO, A.RECIP_ZIPCODE, A.RECIP_BASE_ADDR, A.RECIP_DTL_ADDR
		ORDER BY A.VENDOR_ID, A.EXTMALL_ID, A.EXTMALL_ORDER_ID
	</select>

	<!-- 제휴몰주문등록 - 주문업로드 등록 목록 -->
	<select id="getRegExtmallOrderList" parameterType="ExtmallOrder" resultType="ExtmallOrder">
	/* TscOrderOcm.getRegExtmallOrderList */
		select
			distinct B.GOODS_CD
			, B.GOODS_NM
			, B.GOODS_TYPE
			, B.GOODS_STAT
			, B.SUPPLY_COMP_CD -- 공급업체코드
			, B.DELV_FEE_CD -- 배송비정책코드
			, B.FORMAL_GB
			, B.LIST_PRICE
			, B.DC_RATE
			, B.CHANGEABLE_YN
			, B.CHANGE_FEE_FREE_YN
			, B.RETURNABLE_YN
			, B.RETURN_FEE_FREE_YN
			, B.ORDER_MADE_YN
			, C.EXTMALL_NM
			, C.EXTMALL_TEL
			, C.SELL_FEE_RATE
			, C.EXTMALL_ID
			, A.VENDOR_ID
			, A.AGENT_ORDER_ID
			, A.EXTMALL_ORDER_ID
			, A.EXTMALL_PROD_ID
			, A.EXTMALL_PROD_NM
			, A.SKU
			, A.CURR_PRICE
			, A.ORD_QTY
			, A.ORD_AMT
			, A.CPN_DC_AMT
			, A.PNT_DC_AMT
			, A.DELV_FEE
			, IFNULL(A.ORD_NM, '') AS ORD_NM
			, IFNULL(A.ORD_PHNNO, '') AS ORD_PHNNO
			, A.ORD_TELNO
			, A.ORD_EMAIL
			, IFNULL(A.RECIP_NM, '') AS RECIP_NM
			, IFNULL(A.RECIP_PHNNO, '') AS RECIP_PHNNO
			, A.RECIP_TELNO
			, A.RECIP_ZIPCODE
			, IFNULL(A.RECIP_BASE_ADDR, '') AS RECIP_BASE_ADDR
			, IFNULL(A.RECIP_DTL_ADDR, '') AS RECIP_DTL_ADDR
			, A.DELV_MEMO
			, A.UPLOAD_STAT
			, A.UPLOAD_FAIL_CD
			, A.UPLOAD_FAIL_REASON
			, A.ORD_NO
			, A.DELV_ADDR_SQ
			, A.OPT_CD
			, A.UPLOAD_GB
		FROM TB_EXTMALL_ORDER_UPLOAD A
			LEFT OUTER JOIN TB_OPTION OP ON A.OPT_CD = OP.OPT_CD
			LEFT OUTER JOIN TB_GOODS B ON OP.GOODS_CD = B.GOODS_CD
			LEFT OUTER JOIN TB_EXTMALL C ON A.VENDOR_ID = C.VENDOR_ID AND A.EXTMALL_ID = C.EXTMALL_ID AND C.USE_YN = 'Y'
		WHERE 1=1
		AND RECIP_BASE_ADDR IS NOT NULL AND RECIP_BASE_ADDR != ''
		AND A.UPLOAD_GB = #{uploadGb}
		AND A.UPLOAD_STAT = #{uploadStat}
		<if test="regNo != null and regNo != ''">
	      AND A.REG_NO = #{regNo}
	    </if>
	    order by A.VENDOR_ID, C.EXTMALL_ID, A.EXTMALL_ORDER_ID, A.AGENT_ORDER_ID
	</select>

	<!-- 제휴몰주문등록 - 주문등록  -->
	<insert id="insertExtmallOrder" parameterType="ExtmallOrder">
	/* TscOrderOcm.insertExtmallOrder */
		INSERT INTO TB_EXTMALL_ORDER_UPLOAD
		(
			VENDOR_ID
			, EXTMALL_ID
			, EXTMALL_NM
			, AGENT_ORDER_ID
			, EXTMALL_ORDER_ID
			, EXTMALL_PROD_ID
			, EXTMALL_PROD_NM
			, GOODS_CD
			, SKU
			, CURR_PRICE
			, ORD_QTY
			, ORD_AMT
			, CPN_DC_AMT
			, PNT_DC_AMT
			, DELV_FEE
			, ORD_RECV_DT
			, ORD_DT
			, ORD_NM
			, ORD_PHNNO
			, ORD_TELNO
			, ORD_EMAIL
			, RECIP_NM
			, RECIP_PHNNO
			, RECIP_TELNO
			, RECIP_ZIPCODE
			, RECIP_BASE_ADDR
			, RECIP_DTL_ADDR
			, DELV_MEMO
			, UPLOAD_STAT
			, UPLOAD_FAIL_CD
			, UPLOAD_FAIL_REASON
			, ORD_NO
			, DELV_ADDR_SQ
			, OPT_CD
			, UPLOAD_GB
			, REG_NO
			, REG_DT
		)
		SELECT
			#{vendorId}
			, #{extmallId}
			, #{extmallNm}
			, #{agentOrderId}
			, #{extmallOrderId}
			, #{extmallProdId}
			, #{extmallProdNm}
			, #{goodsCd}
			, #{sku}
			, IFNULL(#{currPrice}, 0)
			, IFNULL(#{ordQty}, 0)
			, IFNULL(#{ordAmt}, 0)
			, IFNULL(#{cpnDcAmt}, 0)
			, IFNULL(#{pntDcAmt}, 0)
			, IFNULL(#{delvFee}, 0)
			, STR_TO_DATE(#{ordRecvDt}, '%Y%m%d%H%i%s')
			, STR_TO_DATE(#{ordDt}, '%Y%m%d%H%i%s')
			, IFNULL(#{ordNm}, #{recipNm})
			, IFNULL(#{ordPhnno}, #{recipPhnno})
			, IFNULL(#{ordTelno}, #{recipTelno})
			, #{ordEmail}
			, #{recipNm}
			, #{recipPhnno}
			, #{recipTelno}
			, replace(#{recipZipcode}, '-', '')
			, #{recipBaseAddr}
			, #{recipDtlAddr}
			, #{delvMemo}
			, #{uploadStat}
			, #{uploadFailCd}
			, #{uploadFailReason}
			, #{ordNo}
			, #{delvAddrSq}
			, #{optCd}
			, #{uploadGb}
			, #{regNo}
			, now()
		FROM DUAL
		WHERE NOT EXISTS(
				SELECT 1 FROM TB_EXTMALL_ORDER_UPLOAD
				WHERE VENDOR_ID = #{vendorId}
				AND EXTMALL_ID = #{extmallId}
				AND AGENT_ORDER_ID = #{agentOrderId}
				AND EXTMALL_ORDER_ID = #{extmallOrderId}
				AND UPLOAD_GB = #{uploadGb}
			  )
	</insert>

	<!-- 제휴몰주문등록 - 주문등록 상태값 업데이트 -->
	<update id="updateExtmallOrder" parameterType="ExtmallOrder">
	/* TscOrderOcm.updateExtmallOrder */
		UPDATE TB_EXTMALL_ORDER_UPLOAD
		SET
			ORD_NO = #{ordNo}
			, DELV_ADDR_SQ = #{delvAddrSq}
			, UPLOAD_STAT = #{uploadStat}
			, UPLOAD_FAIL_CD = #{uploadFailCd}
			, UPLOAD_FAIL_REASON = #{uploadFailReason}

		WHERE VENDOR_ID = #{vendorId}
		AND EXTMALL_ID = #{extmallId}
		AND AGENT_ORDER_ID = #{agentOrderId}
		AND EXTMALL_ORDER_ID = #{extmallOrderId}
		AND UPLOAD_STAT != 'G021_30'

	</update>

	<!-- 제휴몰주문등록 - 주문등록 체크 -->
	<select id="getExtmallOrderInfo" parameterType="ExtmallOrder" resultType="ExtmallOrder">
	/* TscOrderOcm.getExtmallOrderInfo */
		SELECT
			A.AGENT_ORDER_ID
			, A.EXTMALL_ORDER_ID
			, OD.ORD_NO
			, GS.GOODS_CD
			, GS.GOODS_NM
			, GS.GOODS_STAT
			, (SELECT COUNT(UPLOAD_STAT) FROM TB_EXTMALL_ORDER_UPLOAD S WHERE A.VENDOR_ID = S.VENDOR_ID
				AND A.EXTMALL_ID = S.EXTMALL_ID AND A.AGENT_ORDER_ID = S.AGENT_ORDER_ID AND A.EXTMALL_ORDER_ID = S.EXTMALL_ORDER_ID
				AND S.UPLOAD_STAT = 'G021_40' AND UPLOAD_FAIL_CD = 'G022_60') AS STOCK_FAIL_HST_CNT
			, VS.OPT_CD
			, VS.SOLDOUT_YN
			, VS.OPT_CD2
			, VS.CURR_STOCK_QTY
		FROM TB_EXTMALL_ORDER_UPLOAD A
			LEFT OUTER JOIN TB_ORDER_DETAIL OD ON A.VENDOR_ID = OD.VENDOR_ID AND A.EXTMALL_ID = OD.EXTMALL_ID
					AND A.AGENT_ORDER_ID = OD.AGENT_ORDER_ID AND A.EXTMALL_ORDER_ID = OD.EXTMALL_ORDER_ID
			LEFT OUTER JOIN VW_STOCK VS ON VS.OPT_CD = A.OPT_CD
			LEFT OUTER JOIN TB_GOODS GS ON VS.GOODS_CD = GS.GOODS_CD

		WHERE A.VENDOR_ID = #{vendorId}
		AND A.EXTMALL_ID = #{extmallId}
		AND A.AGENT_ORDER_ID = #{agentOrderId}
		AND A.EXTMALL_ORDER_ID = #{extmallOrderId}
		AND A.UPLOAD_GB = #{uploadGb}
		ORDER BY A.REG_DT DESC
		LIMIT 1
	</select>

	<!-- 제휴몰주문등록 - 주문업로드 목록 -->
	<select id="getExtmallOrderList" parameterType="ExtmallOrder" resultType="ExtmallOrder">
	/* TscOrderOcm.getExtmallOrderList */
		SELECT
			 B.GOODS_NM
			, B.SUPPLY_COMP_CD -- 공급업체코드
			, B.DELV_FEE_CD -- 배송비정책코드
			, B.FORMAL_GB
			, B.GOODS_TYPE
			, B.LIST_PRICE
			, B.DC_RATE
			, B.CHANGEABLE_YN
			, B.CHANGE_FEE_FREE_YN
			, B.RETURNABLE_YN
			, B.RETURN_FEE_FREE_YN
			, B.ORDER_MADE_YN
			, C.EXTMALL_NM
			, C.EXTMALL_TEL
			, C.SELL_FEE_RATE
			, A.VENDOR_ID
			, A.EXTMALL_ID
			, A.AGENT_ORDER_ID
			, A.EXTMALL_ORDER_ID
			, A.EXTMALL_PROD_ID
			, A.EXTMALL_PROD_NM
			, A.GOODS_CD
			, A.SKU
			, A.CURR_PRICE
			, A.ORD_QTY
			, A.ORD_AMT
			, A.CPN_DC_AMT
			, A.PNT_DC_AMT
			, A.DELV_FEE
			, A.ORD_RECV_DT
			, A.ORD_DT
			, IFNULL(A.ORD_NM, '') AS ORD_NM
			, IFNULL(A.ORD_PHNNO, '') AS ORD_PHNNO
			, A.ORD_TELNO
			, A.ORD_EMAIL
			, IFNULL(A.RECIP_NM, '') AS RECIP_NM
			, IFNULL(A.RECIP_PHNNO, '') AS RECIP_PHNNO
			, A.RECIP_TELNO
			, A.RECIP_ZIPCODE
			, IFNULL(A.RECIP_BASE_ADDR, '') AS RECIP_BASE_ADDR
			, IFNULL(A.RECIP_DTL_ADDR, '') AS RECIP_DTL_ADDR
			, A.DELV_MEMO
			, A.UPLOAD_STAT
			, A.UPLOAD_FAIL_CD
			, A.UPLOAD_FAIL_REASON
			, A.ORD_NO
			, A.DELV_ADDR_SQ
			, A.OPT_CD
			, A.UPLOAD_GB
			, A.REG_NO
			, A.REG_DT
		FROM TB_EXTMALL_ORDER_UPLOAD A
			LEFT OUTER JOIN TB_OPTION OP ON A.OPT_CD = OP.OPT_CD
			LEFT OUTER JOIN TB_GOODS B ON OP.GOODS_CD = B.GOODS_CD
			LEFT OUTER JOIN TB_EXTMALL C ON A.VENDOR_ID = C.VENDOR_ID AND A.EXTMALL_ID = C.EXTMALL_ID AND C.USE_YN = 'Y'
		WHERE 1=1
		AND RECIP_BASE_ADDR IS NOT NULL AND RECIP_BASE_ADDR != ''
		AND A.UPLOAD_GB = #{uploadGb}
		<if test="regNo != null and regNo != ''">
	      AND A.REG_NO = #{regNo}
	    </if>
		<if test="uploadStat != null and uploadStat != ''">
	      AND A.UPLOAD_STAT = #{uploadStat}
	    </if>

		<if test="stDate != null and stDate != ''">
	      AND A.REG_DT >= DATE_FORMAT(#{stDate}, '%Y-%m-%d %H:%i:%S')
        </if>
	    <if test="edDate != null and edDate != ''">
	      <![CDATA[
	        AND A.REG_DT < DATE_FORMAT(DATE_ADD(#{edDate}, INTERVAL 1 DAY), '%Y-%m-%d %H:%i:%S')
	      ]]>
	    </if>
		order by A.VENDOR_ID, A.EXTMALL_ID, A.EXTMALL_ORDER_ID, A.AGENT_ORDER_ID
	</select>


<!--***** 주문정보등록 **************************************************************** -->
<!--***** 주문정보등록 **************************************************************** -->
	<!-- 1. 주문기본정보등록 -->
	<insert id="createOrder" parameterType="ExtmallOrder" keyProperty="ordNo">
		/* TscOrderOcm.createOrder : 주문기본정보등록 */
		INSERT INTO TB_ORDER (
		    MALL_GB
		    , ORD_DT
		    , PAY_DT
		    , CUST_NO
		    , ORD_NM
		    , ORD_PHNNO
		    , ORD_TELNO
		    , ORD_EMAIL
		    , SITE_CD
		    , NPAY_ORD_NO
		    , FRONT_GB
		    , REG_NO
		    , REG_DT
		    , UPD_NO
		    , UPD_DT
		) VALUES (
		     'G011_20'
		     , NOW()
		     , NOW()
		     , #{custNo}
		     , #{custNm}
		     , #{ordPhnno}
		     , #{ordTelno}
		     , #{email}
		     , 'G000_10'
		     , #{npayOrdNo}
		     , 'P'
		     , #{custNo}
		     , NOW()
		     , #{custNo}
		     , NOW()
		)
	</insert>

	<!-- 2. 주문배송주소정보등록-->
	<insert id="createDeliveryAddr" parameterType="ExtmallOrder" keyProperty="delvAddrSq">
		/* TscOrderOcm.createDeliveryAddr : 주문배송주소정보등록 */
		INSERT INTO TB_DELIVERY_ADDR (
		    RECIP_NM
		    , RECIP_PHNNO
		    , RECIP_TELNO
		    , RECIP_ZIPCODE
		    , RECIP_BASE_ADDR
		    , RECIP_DTL_ADDR
		    , DELV_MEMO
		    , GIFT_MSG
		    , REG_NO
		    , REG_DT
		    , UPD_NO
		    , UPD_DT
		) VALUES (
		    #{recipNm}
		    , #{recipPhnno}
		    , #{recipTelno}
		    , #{recipZipcode}
		    , #{recipBaseAddr}
		    , #{recipDtlAddr}
		    , #{delvMemo}
		    , #{giftMsg}
		    , #{custNo}
		    , NOW()
		    , #{custNo}
		    , NOW()
		)
	</insert>

	<!-- 3. 주문배송비정보등록-->
	<insert id="createDeliveryFee" parameterType="ExtmallOrder">
		/* TscOrderOcm.createDeliveryFee : 주문배송비정보등록 */
		INSERT INTO TB_DELIVERY_FEE (
		    PAY_SQ
		    , ORD_NO
		    , DELV_FEE_GB
		    , DELV_FEE_CD
		    , DELV_FEE
		    , DELV_CPN_SQ
		    , DELV_CPN_DC_AMT
		    , REAL_DELV_AMT
		    , ORD_CHG_SQ
		    , SUPPLY_COMP_CD
		    , DELV_USAC_YN
		    , DELV_USAC_DT
		    , REG_NO
		    , REG_DT
		    , UPD_NO
		    , UPD_DT
		) VALUES (
		    NULL
		    , #{ordNo}
		    , #{delvFeeGb}
		    , #{delvFeeCd}
		    , #{delvFee}
		    , #{delvCpnSq}
		    , #{delvCpnDcAmt}
		    , #{realDelvAmt}
		    , NULL
		    , #{supplyCompCd}
		    , 'N'
		    , NULL
		    , #{custNo}
		    , NOW()
		    , #{custNo}
		    , NOW()
		)
	</insert>

	<!-- 4. 주문상세정보등록-->
	<insert id="createOrderDetail" parameterType="ExtmallOrder" keyProperty="ordDtlNo">
		/* TscOrderOcm.createOrderDetail : 주문상세정보등록 */
		INSERT INTO TB_ORDER_DETAIL (
		    ORD_NO
		    , ORD_EXCH_GB
		    , ORD_DTL_STAT
		    , ORG_ORD_DTL_NO
		    , SUPPLY_COMP_CD
		    , GOODS_CD
		    , DEAL_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
		    , CHANGEABLE_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
		    , GIFT_ADDR_INP_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
		) VALUES
		(
		    #{ordNo}
		     , #{ordExchGb}
		     , #{ordDtlStat}
		     , NULL
		     , #{supplyCompCd}
		     , #{goodsCd}
		     , null
		     , #{formalGb}
		     , #{goodsType}
		     , #{listPrice}
		     , #{currPrice}
		     , #{dcRate}
		     , #{optAddPrice}
		     , #{ordQty}
		     , #{ordAmt}
		     , 0
		     , 0
		     , #{cpn1CpnSq}
		     , #{cpn1DcAmt}
		     , #{tmtb1Sq}
		     , #{tmtb1DcAmt}
		     , #{tmtb2Sq}
		     , #{tmtb2DcAmt}
		     , #{goodsCpnSq}
		     , #{goodsCpnDcAmt}
		     , #{cartCpnSq}
		     , #{cartCpnDcAmt}
		     , 0
		     , #{pntDcAmt}
		     , #{prePntDcAmt}
		     , #{savePntAmt}
		     , #{realOrdAmt}
		     , #{gfcdUseAmt}
		     , #{vendorId}
		     , #{extmallId}
		     , #{agentOrderId}
		     , #{extmallOrderId}
		     , #{changeableYn}
		     , #{changeFeeFreeYn}
		     , #{returnableYn}
		     , #{returnFeeFreeYn}
		     , 'N'
		     , NULL
		     , NULL
		     , NULL
		     , #{delvAddrSq}
		     , #{delvFeeCd}
		     , #{shotDelvYn}
		     , #{giftPackYn}
		     , 'N'
		     , #{orderMadeYn}
		     , #{entryNo}
		     , NULL
		     , NULL
		     , 'P'
		     , NULL
		     , NULL
		     , NULL
		     , NULL
		     , NULL
		     , 'N'
		     , NULL
		     , #{sellFeeRate}
		     , null
		     , null
		     , null
		     , null
		     , NULL
		     , #{custNo}
		     , NOW()
		     , #{custNo}
		     , NOW()
		)
	</insert>

	<!-- 4-2. 주문상세 이력 등록-->
	<insert id="createOrderDetailHst" parameterType="ExtmallOrder">
		/* TscOrderOcm.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
		    , DEAL_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
		    , CHANGEABLE_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
		    , GIFT_ADDR_INP_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
		)
		 VALUES
		(
			#{ordDtlNo}
		     , #{ordNo}
		     , #{ordExchGb}
		     , #{ordDtlStat}
		     , NULL
		     , #{supplyCompCd}
		     , #{goodsCd}
		     , null
		     , #{formalGb}
		     , #{goodsType}
		     , #{listPrice}
		     , #{currPrice}
		     , #{dcRate}
		     , #{optAddPrice}
		     , #{ordQty}
		     , #{ordAmt}
		     , 0
		     , 0
		     , #{cpn1CpnSq}
		     , #{cpn1DcAmt}
		     , #{tmtb1Sq}
		     , #{tmtb1DcAmt}
		     , #{tmtb2Sq}
		     , #{tmtb2DcAmt}
		     , #{goodsCpnSq}
		     , #{goodsCpnDcAmt}
		     , #{cartCpnSq}
		     , #{cartCpnDcAmt}
		     , 0
		     , #{pntDcAmt}
		     , #{prePntDcAmt}
		     , #{savePntAmt}
		     , #{realOrdAmt}
		     , #{gfcdUseAmt}
		     , #{vendorId}
		     , #{extmallId}
		     , #{agentOrderId}
		     , #{extmallOrderId}
		     , #{changeableYn}
		     , #{changeFeeFreeYn}
		     , #{returnableYn}
		     , #{returnFeeFreeYn}
		     , 'N'
		     , NULL
		     , NULL
		     , NULL
		     , #{delvAddrSq}
		     , #{delvFeeCd}
		     , #{shotDelvYn}
		     , #{giftPackYn}
		     , 'N'
		     , #{orderMadeYn}
		     , #{entryNo}
		     , NULL
		     , NULL
		     , 'P'
		     , NULL
		     , NULL
		     , NULL
		     , NULL
		     , NULL
		     , 'N'
		     , NULL
		     , #{sellFeeRate}
		     , null
		     , null
		     , null
		     , null
		     , null
		     , #{custNo}
		     , NOW()
		     , #{custNo}
		     , NOW()
		)
	</insert>

	<!-- 5. 주문상세단품정보등록을 위한 조회 -->
	<select id="getOrderDetailItem" parameterType="ExtmallOrder" resultType="ExtmallOrder">
		/* TscOrderOcm.getOrderDetailItem : 주문상세단품정보등록을 위한 조회 */
		SELECT
			#{ordDtlNo}                                  AS ORD_DTL_NO
		     , #{ordNo}                                     AS ORD_NO
		     , #{ordDtlStat}                                AS ORD_DTL_STAT
			 , OP.GOODS_CD
		     , OP.GOODS_CD as ITEM_CD
		     , OP.OPT_CD
		     , OP.OPT_CD1
		     , OP.OPT_CD2
		     , OP.SKU_MODEL_NO
		     , OP.PRODUCT_NO
		     , OP.PRODUCT_CODE
		     , G.CURR_PRICE
		     , IFNULL(GC.QTY, 1)                            AS ITEM_QTY
		     , IFNULL(GC.COMPS_CURR_PRICE, G.CURR_PRICE)    AS ITEM_PRICE
		     , OP.ADD_PRICE                                 AS OPT_ADD_PRICE
		    /* , CD.DISP_ORD*/
		     , 0                                            AS ORD_AMT
		     , 0                                            AS CNCL_RTN_AMT
		     , 0                                            AS CPN1_DC_AMT
		     , 0                                            AS TMTB1_DC_AMT
		     , 0                                            AS TMTB2_DC_AMT
		     , 0                                            AS GOODS_CPN_DC_AMT
		     , 0                                            AS CART_CPN_DC_AMT
		     , 0                                            AS PNT_DC_AMT
		     , 0                                            AS PRE_PNT_DC_AMT
		     , 0                                            AS SAVE_PNT_AMT
		     , 0                                            AS REAL_ORD_AMT
		     , 0                                            AS GFCD_USE_AMT
		FROM   TB_OPTION OP
		INNER  JOIN TB_GOODS G
		ON     op.GOODS_CD = G.GOODS_CD
		LEFT   OUTER JOIN TB_GOODS_COMPOSE GC
		ON    G.GOODS_CD = GC.GOODS_CD
		WHERE 1=1
		and OP.OPT_CD= #{optCd}

	</select>

	<!-- 6. 주문상세단품정보등록 -->
	<insert id="createOrderDetailItem" parameterType="ExtmallOrder" keyProperty="ordDtlItemSq">
		/* TscOrderOcm.createOrderDetailItem : 주문상세단품정보등록 */
		INSERT INTO TB_ORDER_DETAIL_ITEM (
		    ORD_DTL_NO
		    , ORD_NO
		    , ORD_DTL_STAT
		    , ITEM_CD
		    , OPT_CD
		    , OPT_CD1
		    , OPT_CD2
		    , SKU_MODEL_NO
		    , PRODUCT_NO
		    , PRODUCT_CODE
		    , ITEM_QTY
		    , ITEM_PRICE
		    , OPT_ADD_PRICE
		    , DISP_ORD
		    , ORD_AMT
		    , CNCL_RTN_AMT
		    , CPN1_DC_AMT
		    , TMTB1_DC_AMT
		    , TMTB2_DC_AMT
		    , GOODS_CPN_DC_AMT
		    , CART_CPN_DC_AMT
		    , PNT_DC_AMT
		    , PRE_PNT_DC_AMT
		    , SAVE_PNT_AMT
		    , REAL_ORD_AMT
		    , GFCD_USE_AMT
		    , REG_NO
		    , REG_DT
		    , UPD_NO
		    , UPD_DT
		) VALUES (
		    #{ordDtlNo}
		    , #{ordNo}
		    , #{ordDtlStat}
		    , #{itemCd}
		    , #{optCd}
		    , #{optCd1}
		    , #{optCd2}
		    , #{skuModelNo}
		    , #{productNo}
		    , #{productCode}
		    , #{itemQty}
		    , #{itemPrice}
		    , #{optAddPrice}
		    , #{dispOrd}
		    , #{ordAmt}
		    , #{cnclRtnAmt}
		    , #{cpn1DcAmt}
		    , #{tmtb1DcAmt}
		    , #{tmtb2DcAmt}
		    , #{goodsCpnDcAmt}
		    , #{cartCpnDcAmt}
		    , #{pntDcAmt}
		    , #{prePntDcAmt}
		    , #{savePntAmt}
		    , #{realOrdAmt}
		    , #{gfcdUseAmt}
		    , #{custNo}
		    , NOW()
		    , #{custNo}
		    , NOW()
		)
	</insert>

	<!-- 7. 주문상세단품이력정보 등록 -->
	<insert id="createOrderDetailItemHst" parameterType="ExtmallOrder">
		/* TscOrderOcm.createOrderDetailItemHst */
		INSERT INTO TB_ORDER_DETAIL_ITEM_HST (
		    ORD_DTL_ITEM_SQ
		    , ORD_DTL_NO
		    , ORD_NO
		    , ORD_DTL_STAT
		    , ITEM_CD
		    , OPT_CD
		    , OPT_CD1
		    , OPT_CD2
		    , SKU_MODEL_NO
		    , PRODUCT_NO
		    , PRODUCT_CODE
		    , ITEM_QTY
		    , ITEM_PRICE
		    , OPT_ADD_PRICE
		    , DISP_ORD
		    , ORD_AMT
		    , ORD_QTY
		    , CNCL_RTN_AMT
		    , CNCL_RTN_QTY
		    , CPN1_DC_AMT
		    , TMTB1_DC_AMT
		    , TMTB2_DC_AMT
		    , GOODS_CPN_DC_AMT
		    , CART_CPN_DC_AMT
		    , PNT_DC_AMT
		    , PRE_PNT_DC_AMT
		    , SAVE_PNT_AMT
		    , REAL_ORD_AMT
		    , GFCD_USE_AMT
		    , REG_NO
		    , REG_DT
		    , UPD_NO
		    , UPD_DT
		) VALUES (
			#{ordDtlItemSq}
		    , #{ordDtlNo}
		    , #{ordNo}
		    , #{ordDtlStat}
		    , #{itemCd}
		    , #{optCd}
		    , #{optCd1}
		    , #{optCd2}
		    , #{skuModelNo}
		    , #{productNo}
		    , #{productCode}
		    , #{itemQty}
		    , #{itemPrice}
		    , #{optAddPrice}
		    , #{dispOrd}
		    , #{ordAmt}
		    , #{ordQty}
		    , #{cnclRtnAmt}
		    , 0
		    , #{cpn1DcAmt}
		    , #{tmtb1DcAmt}
		    , #{tmtb2DcAmt}
		    , #{goodsCpnDcAmt}
		    , #{cartCpnDcAmt}
		    , #{pntDcAmt}
		    , #{prePntDcAmt}
		    , #{savePntAmt}
		    , #{realOrdAmt}
		    , #{gfcdUseAmt}
		    , #{custNo}
		    , NOW()
		    , #{custNo}
		    , NOW()
		)

	</insert>

	<!-- 8. 주문사은품 마스터 정보 등록 -->
	<insert id="createOrdFreegift" parameterType="ExtmallOrder">
		/* TscOrderOcm.createOrdFreegift */
		INSERT INTO TB_ORD_FREEGIFT (
		    ORD_NO
		    , FREEGIFT_SQ
		    , DEL_YN
		    , REG_NO
		    , REG_DT
		    , UPD_NO
		    , UPD_DT
		)
		SELECT #{ordNo}
		     , FG.FREEGIFT_SQ
		     , 'N'
		     , #{custNo}
		     , NOW()
		     , #{custNo}
		     , NOW()
		FROM   TB_FREEGIFT_VAL FGV
		INNER  JOIN TB_FREEGIFT FG
		ON     FGV.FREEGIFT_SQ = FG.FREEGIFT_SQ
		WHERE  1=1
		AND    FREEGIFT_VAL_SQ IN
		<foreach collection="freegiftValArr" item="item" index="index"  open="(" close=")" separator=",">
			#{item}
		</foreach>
		GROUP  BY FREEGIFT_SQ
	</insert>

	<!-- 9. 주문사은품 상품 정보 등록 -->
	<insert id="createOrdFreegiftVal" parameterType="ExtmallOrder">
		/* TscOrderOcm.createOrdFreegiftVal */
		INSERT INTO TB_ORD_FREEGIFT_VAL (
		    ORD_FREEGIFT_SQ
		    , ORD_NO
		    , FREEGIFT_SQ
		    , FREEGIFT_VAL_SQ
		    , ORD_DTL_STAT
		    , USE_POINT
		    , ITEM_QTY
		    , PRODUCT_NO
		    , PRODUCT_CODE
		    , BRAND_CD
		    , DEL_YN
		    , REG_NO
		    , REG_DT
		    , UPD_NO
		    , UPD_DT
		)
		SELECT OFG.ORD_FREEGIFT_SQ
		    , #{ordNo}
		    , FGV.FREEGIFT_SQ
		    , FGV.FREEGIFT_VAL_SQ
		    , #{ordDtlStat}
		    , CASE WHEN FG.ALL_YN IN ('Y')
		           THEN 0
		           ELSE FGV.USE_POINT
		           END USE_POINT
		    , FGV.ITEM_QTY
		    , FGV.PRODUCT_NO
		    , FRG.PRODUCT_CODE
		    , FRG.BRAND_CD
		    , 'N'
		    , #{custNo}
		    , NOW()
		    , #{custNo}
		    , NOW()
		FROM   TB_FREEGIFT_VAL FGV
		INNER  JOIN TB_FREEGIFT FG
		ON     FGV.FREEGIFT_SQ = FG.FREEGIFT_SQ
		INNER  JOIN TB_FREE_GOODS FRG
		ON     FGV.PRODUCT_NO = FRG.PRODUCT_NO
		INNER  JOIN TB_ORD_FREEGIFT OFG
		ON     FGV.FREEGIFT_SQ = OFG.FREEGIFT_SQ
		WHERE  1=1
		AND    OFG.ORD_NO = #{ordNo}
		AND    FGV.FREEGIFT_VAL_SQ IN
		<foreach collection="freegiftValArr" item="item" index="index"  open="(" close=")" separator=",">
			#{item}
		</foreach>
	</insert>

	<!-- 10. 주문사은품 주문상세 정보 등록 -->
	<insert id="createOrdFreegiftDtl" parameterType="ExtmallOrder">
		/* TscOrderOcm.createOrdFreegiftDtl */
		INSERT INTO TB_ORD_FREEGIFT_DTL (
		    ORD_FREEGIFT_SQ
		    , FREEGIFT_SQ
		    , ORD_NO
		    , ORD_DTL_NO
		    , DEL_YN
		    , REG_NO
		    , REG_DT
		    , UPD_NO
		    , UPD_DT
		)
		SELECT OFR.ORD_FREEGIFT_SQ
		     , OFR.FREEGIFT_SQ
		     , OD.ORD_NO
		     , OD.ORD_DTL_NO
		     , 'N'
		     , #{custNo}
		     , NOW()
		     , #{custNo}
		     , NOW()
		FROM   TB_ORDER_DETAIL OD
		INNER  JOIN TB_ORD_FREEGIFT OFR
		ON     OD.ORD_NO = OFR.ORD_NO
		INNER  JOIN TB_ORD_FREEGIFT_VAL OFV
		ON     OFR.ORD_FREEGIFT_SQ = OFV.ORD_FREEGIFT_SQ
		WHERE  1=1
		AND    OD.GOODS_CD = #{goodsCd}
		AND    OFR.FREEGIFT_SQ = #{freegiftSq}
		AND    OD.ORD_NO = #{ordNo}
		GROUP  BY OD.ORD_DTL_NO
		     , OFR.FREEGIFT_SQ
		     , OFR.ORD_FREEGIFT_SQ
	</insert>

	<!-- 11. 결제 PAYMENT INSERT-->
	<insert id="insertPayment" parameterType="Payment" keyProperty="paySq">
		/* TscOrderOcm.insertPayment : PAYMENT INSERT */
		INSERT INTO TB_PAYMENT (
			  ORD_NO
			, PAY_DT
			, PAY_MEANS
			, PAY_AMT
			, PG_CPN_AMT
			, NPAY_PNT_AMT
			, PAY_GB
			, PAY_STAT
			, CARD_PCABLE_YN
			, REG_NO
			, REG_DT
			, UPD_NO
			, UPD_DT
		) VALUES (
			  #{ordNo}
			, NOW()
			, #{payMeans}
			, #{payAmt}
			, #{pgCpnAmt}
			, #{npayPntAmt}
			, #{payGb}
			, #{payStat}
			, IFNULL(#{cardPcableYn}, 'Y')
			, #{regNo}
			, NOW()
			, #{updNo}
			, NOW()
		)
	</insert>

	<!-- 12. 재고정보등록 -->
	<insert id="createSellQty" parameterType="ExtmallOrder">
		/* TscOrderOcm.createSellQty */
		INSERT INTO TB_SELL_QTY (
		    GOODS_CD
		    , OPT_CD
		    , SELL_GB
		    , ORD_DTL_NO
		    , ORD_CHG_SQ
		    , AGENT_ORDER_ID
		    , EXTMALL_ORDER_ID
		    , SELL_QTY
		    , REG_NO
		    , REG_DT
		) VALUES
		(
			#{goodsCd}
			, #{optCd}
			, #{sellGb}
			, #{ordDtlNo}
			, NULL
			, #{agentOrderId}
			, #{extmallOrderId}
			, #{ordQty}
			, #{custNo}
		    , NOW()
		 )
	</insert>

	<!-- 13. 사은품 남은수량 업데이트 -->
	<update id="updateFreegiftValLeftQty" parameterType="ExtmallOrder">
		/* TscOrderOcm.updateFreegiftValLeftQty */
		UPDATE TB_FREEGIFT_VAL FV
		INNER  JOIN TB_ORD_FREEGIFT_VAL OFV
		ON     FV.FREEGIFT_VAL_SQ = OFV.FREEGIFT_VAL_SQ
		SET    FV.LEFT_QTY = FV.LEFT_QTY - FV.ITEM_QTY
		WHERE  1=1
		AND    OFV.ORD_NO = #{ordNo}
		AND    FV.DEL_YN = 'N'
	</update>

	<!-- 상품 재고 조회 -->
	<select id="getGoodsStockInfo" parameterType="ShoplinkerStock" resultType="ShoplinkerStock">
		/* TscOrderOcm.getGoodsStockInfo */
		SELECT VS.GOODS_CD
			 , VS.OPT_CD
			 , VS.OPT_CD1
			 , VS.OPT_CD2
			 , VS.SOLDOUT_YN
			 , VS.CURR_STOCK_QTY
		     , VS.ADD_PRICE
			 , O.SKU_MODEL_NO
		  FROM VW_STOCK VS
		 INNER JOIN TB_OPTION O
		    ON VS.GOODS_CD = O.GOODS_CD
		   AND VS.OPT_CD = O.OPT_CD
		 WHERE VS.GOODS_CD = #{goodsCd}
		   AND VS.OPT_CD = #{optCd}
	</select>

	<!-- 사은품 : 1차조건 적용된 사음품 정보, 상품 금액 조회 -->
	<select id="getFreeGiftGoodsApplyAmt" parameterType="ExtmallOrder" resultType="ExtmallOrder">
		/* TscOrderOcm.getFreeGiftGoodsApplyAmt */
		WITH GOODS_DATA AS (
			SELECT G.GOODS_CD
			     , G.SUPPLY_COMP_CD
			     , G.BRAND_CD
			     , SUM(OD.ORD_QTY) as GOODS_QTY	 /*동일상품의 옵션이 다른경우, 수량을 합산*/
			     , OD.CURR_PRICE
			     , G.GOODS_TYPE
			FROM   TB_GOODS G
			INNER JOIN TB_ORDER_DETAIL OD ON OD.GOODS_CD = G.GOODS_CD
			WHERE OD.ORD_NO = #{ordNo}
			AND OD.ORD_DTL_NO IN
			<foreach collection="itemCdArr" item="item" index="index"  open="(" close=")" separator=",">
				#{item}
			</foreach>
			GROUP BY G.GOODS_CD, G.SUPPLY_COMP_CD, G.BRAND_CD, OD.ORD_NO, OD.CURR_PRICE, G.GOODS_TYPE
		)
		SELECT FGG.FREEGIFT_SQ
		     , FGG.GOODS_CD
		     , FGG.CURR_PRICE
		     , FGG.GOODS_QTY
		     , FG.FREEGIFT_STDT
		     , FG.FREEGIFT_EDDT
		     , FE.*
		FROM   TB_FREEGIFT FG
		INNER JOIN TB_FREEGIFT_EXTMALL FE ON FG.FREEGIFT_SQ = FE.FREEGIFT_SQ
		INNER  JOIN (
			SELECT FGG.FREEGIFT_SQ
			    , GD.GOODS_CD
			    , GD.CURR_PRICE
			    , GD.GOODS_QTY
			FROM   TB_FREEGIFT_GOODS FGG
			INNER  JOIN GOODS_DATA GD
			ON     FGG.TARGET_VAL = GD.GOODS_CD
			WHERE  1=1
			AND    FGG.GOODS_GB = 'G800_20' -- 적용상품
			AND    FGG.TARGET_GB= 'G260_10' -- 상품
			AND    FGG.DEL_YN = 'N'
			UNION
			SELECT FGG1.FREEGIFT_SQ
			     , GD.GOODS_CD
			     , GD.CURR_PRICE
			     , GD.GOODS_QTY
			FROM   GOODS_DATA GD
			INNER  JOIN TB_FREEGIFT_GOODS_APPLY FGG1
			ON     GD.SUPPLY_COMP_CD = FGG1.TARGET_VAL
			AND    FGG1.TARGET_GB= 'G260_13' -- 공급처
			INNER  JOIN TB_FREEGIFT_GOODS_APPLY FGG2
			ON     GD.BRAND_CD = FGG2.TARGET_VAL
			AND    FGG2.TARGET_GB= 'G260_12' -- 브랜드
			AND    FGG1.FREEGIFT_SQ = FGG2.FREEGIFT_SQ
		) FGG
		ON     FG.FREEGIFT_SQ = FGG.FREEGIFT_SQ
		LEFT   OUTER JOIN TB_FREEGIFT_GOODS NOT_APPLY_GOODS
		ON     FGG.FREEGIFT_SQ = NOT_APPLY_GOODS.FREEGIFT_SQ
		AND    FGG.GOODS_CD = NOT_APPLY_GOODS.TARGET_VAL
		AND    NOT_APPLY_GOODS.TARGET_GB = 'G800_30'
		WHERE  1=1
		AND    NOT_APPLY_GOODS.FREEGIFT_SQ IS NULL
		AND    FG.DEL_YN = 'N'
		AND    FG.SELF_YN = 'N' -- 외부몰
		AND    FG.FREEGIFT_STAT = 'G232_11' -- 진행
		AND	   FE.EXTMALL_ID = #{extmallId}
		AND	   FE.DEL_YN = 'N'
		AND    FG.FREEGIFT_STDT <![CDATA[<]]> CURRENT_TIMESTAMP
		AND    FG.FREEGIFT_EDDT > CURRENT_TIMESTAMP
		ORDER  BY FGG.FREEGIFT_SQ

	</select>

	<!-- 사은품 : 2차조건 적용된 사은품상품금액 구간정보조회 -->
	<select id="getFreeGiftGoodsApplySection" parameterType="ExtmallOrder" resultType="ExtmallOrder">
		/* TscOrderOcm.getFreeGiftGoodsApplySection */
		SELECT FGS.FREEGIFT_SECTION_SQ
		     , FGS.FREEGIFT_SQ
		     , FGS.SECTION_VAL
		FROM   TB_FREEGIFT_SECTION FGS
		WHERE  1=1
		AND    FGS.DEL_YN = 'N'
		AND    FGS.SECTION_GB = 'G810_11' -- 금액만 처리
		AND    FGS.FREEGIFT_SQ IN
		<foreach collection="freegiftSqArr" item="item" index="index"  open="(" close=")" separator=",">
			#{item}
		</foreach>
		ORDER  BY FGS.FREEGIFT_SQ
		     , FGS.SECTION_VAL
	</select>

	<!-- 사은품 : 3차조건 적용된 구간정보의 사은품정보조회 -->
	<select id="getFreeGiftGoodsApplyVal" parameterType="ExtmallOrder" resultType="ExtmallOrder">
		/* TscOrderOcm.getFreeGiftGoodsApplyVal */
		SELECT FGV.FREEGIFT_VAL_SQ
		     , FG.FREEGIFT_NM
		     , FGV.FREEGIFT_SECTION_SQ
		     , FGV.FREEGIFT_SQ
		     , FGV.USE_POINT
		     , FGV.ITEM_QTY
		     , FG.ALL_YN
		     , G.PRODUCT_NO
		     , G.PRODUCT_CODE
		     , G.GOODS_NM
		     , G.SYS_IMG_NM
		FROM   TB_FREEGIFT_VAL FGV
		INNER  JOIN TB_FREEGIFT FG
		ON     FGV.FREEGIFT_SQ = FG.FREEGIFT_SQ
		INNER  JOIN TB_FREE_GOODS G
		ON     FGV.PRODUCT_NO = G.PRODUCT_NO
		WHERE  1=1
		AND    FGV.DEL_YN = 'N'
		AND    FGV.LEFT_QTY <![CDATA[>=]]> FGV.ITEM_QTY
		AND    FGV.FREEGIFT_SECTION_SQ IN
		<foreach collection="freegiftSectionSqArr" item="item" index="index"  open="(" close=")" separator=",">
			#{item}
		</foreach>
		ORDER  BY FG.FREEGIFT_SQ
		        , FGV.FREEGIFT_SECTION_SQ
		        , FGV.USE_POINT
	</select>
<!--***** 주문정보등록 **************************************************************** -->
<!--***** 주문정보등록 **************************************************************** -->


	<!--제휴몰주문등록 엑셀 업로드 대기건 수(본인)-->
	<select id="getExtmallOrderExceluploadWateCnt" parameterType="Integer" resultType="Integer">
		SELECT COUNT(1) 
		FROM TB_EXTMALL_ORDER_UPLOAD
		WHERE UPLOAD_STAT = 'G021_00'
		AND UPLOAD_GB = 'E'
		AND REG_NO = #{regNo}
	</select>
	
	<!--제휴몰주문등록 엑셀 업로드 임시테이블 수(본인)-->
	<select id="getExtmallOrderExceluploadTmpCnt" parameterType="Integer" resultType="Integer">
		SELECT COUNT(1) 
		FROM TB_EXTMALL_ORDER_UPLOAD_TMP
		WHERE 1=1
		AND REG_NO = #{regNo}
	</select>

	<!--외부몰주문등록 전 임시 테이블 삭제-->
	<delete id="deleteExtmallOrderUploadTmp" parameterType="Integer">
		/* TscOrderOcm.deleteExtmallOrderUploadTmp */
		DELETE
		FROM  TB_EXTMALL_ORDER_UPLOAD_TMP
		WHERE REG_NO = #{regNo}
	</delete>

	<!--외부몰주문등록 테이블 실패목록 데이터 삭제 -->
	<delete id="deleteExtmallOrderUploadFail" parameterType="Integer">
		/* TscOrderOcm.deleteExtmallOrderUploadFail */
		DELETE
		FROM  TB_EXTMALL_ORDER_UPLOAD A
		WHERE REG_NO = #{regNo}
		AND UPLOAD_GB = 'E'
		AND UPLOAD_STAT = 'G021_40'
		AND EXISTS (
                      SELECT 1
                      FROM   TB_EXTMALL_ORDER_UPLOAD_TMP B
                      WHERE  B.VENDOR_ID = A.VENDOR_ID
                      AND    B.EXTMALL_ID = A.EXTMALL_ID
                      AND    B.AGENT_ORDER_ID = A.AGENT_ORDER_ID
                      AND    B.EXTMALL_ORDER_ID = A.EXTMALL_ORDER_ID
                    )
	</delete>

	<!-- 외부몰주문등록 (TB_EXTMALL_ORDER_UPLOAD_TMP) 인픽스-->
	<insert id="createExtmallOrderUploadTmp" parameterType="ExtmallOrder">
		/* TscOrderOcm.createExtmallOrderUploadTmp */
		INSERT INTO TB_EXTMALL_ORDER_UPLOAD_TMP (
		      VENDOR_ID
			, EXTMALL_ID
			, EXTMALL_NM
			, AGENT_ORDER_ID
			, EXTMALL_ORDER_ID
			, EXTMALL_PROD_ID
			, EXTMALL_PROD_NM
			, GOODS_CD
			, SKU
			, CURR_PRICE
			, ORD_QTY
			, ORD_AMT
			, CPN_DC_AMT
			, PNT_DC_AMT
			, DELV_FEE
			, ORD_RECV_DT
			, ORD_DT
			, ORD_NM
			, ORD_PHNNO
			, ORD_TELNO
			, ORD_EMAIL
			, RECIP_NM
			, RECIP_PHNNO
			, RECIP_TELNO
			, RECIP_ZIPCODE
			, RECIP_BASE_ADDR
			, RECIP_DTL_ADDR
			, DELV_MEMO
			, UPLOAD_STAT
			, UPLOAD_FAIL_CD
			, UPLOAD_FAIL_REASON
			, ORD_NO
			, DELV_ADDR_SQ
			, OPT_CD
			, UPLOAD_GB
			, REG_NO
			, REG_DT
		)
		VALUES (
		     #{vendorId}
			, #{extmallId}
			, '-'
			, #{agentOrderId}
			, #{extmallOrderId}
			, #{extmallProdId}
			, #{extmallProdNm}
			, (SELECT S.GOODS_CD FROM TB_OPTION S WHERE S.OPT_CD = REPLACE(REPLACE(#{optCd}, '[', ''), ']', ''))
			, #{sku}
			, IFNULL(#{currPrice}, 0)
			, IFNULL(#{ordQty}, 0)
			, IFNULL(#{ordAmt}, 0)
			, IFNULL(#{cpnDcAmt}, 0)
			, IFNULL(#{pntDcAmt}, 0)
			, IFNULL(#{delvFee}, 0)
			, now()
			, now()
			, IFNULL(#{ordNm}, #{recipNm})
			, IFNULL(#{ordPhnno}, #{recipPhnno})
			, IFNULL(#{ordTelno}, #{recipTelno})
			, #{ordEmail}
			, #{recipNm}
			, #{recipPhnno}
			, #{recipTelno}
			, replace(#{recipZipcode}, '-', '')
			, #{recipBaseAddr}
			, #{recipDtlAddr}
			, #{delvMemo}
			, #{uploadStat}
			, #{uploadFailCd}
			, #{uploadFailReason}
			, #{ordNo}
			, #{delvAddrSq}
			, replace(replace(#{optCd}, '[', ''), ']', '')
			, #{uploadGb}
			, #{regNo}
			, now()
		)
	</insert>

	<!-- 외부몰주문등록 업로드 후 목록 -->
	<select id="getExtmallOrderUploadTmpList" parameterType="ExtmallOrder" resultType="ExtmallOrder">
	/* TscOrderOcm.getExtmallOrderUploadTmpList */
		SELECT T.VENDOR_ID
			, T.EXTMALL_ID
			, IFNULL(A.EXTMALL_NM, '(미매핑)') as EXTMALL_NM
			, T.AGENT_ORDER_ID
			, T.EXTMALL_ORDER_ID
			, T.EXTMALL_PROD_ID
			, T.EXTMALL_PROD_NM
			, CASE
				WHEN A.EXTMALL_NM IS NULL OR B.GOODS_CD IS NULL OR B.OPT_CD IS NULL THEN 'Y'
				ELSE 'N'
			  END WARNING_YN
			, CASE
				WHEN B.GOODS_CD IS NULL THEN CONCAT(T.GOODS_CD , '(미매핑)')
				ELSE T.GOODS_CD
			  END AS GOODS_CD
			, CASE
				WHEN B.OPT_CD IS NULL THEN CONCAT(T.OPT_CD , '(미매핑)')
				ELSE T.OPT_CD
			  END AS OPT_CD
			, T.SKU
			, T.CURR_PRICE
			, T.ORD_QTY
			, T.ORD_AMT
			, T.CPN_DC_AMT
			, T.PNT_DC_AMT
			, T.DELV_FEE
			, T.ORD_RECV_DT
			, T.ORD_DT
			, T.ORD_NM
			, T.ORD_PHNNO
			, T.ORD_TELNO
			, T.ORD_EMAIL
			, T.RECIP_NM
			, T.RECIP_PHNNO
			, T.RECIP_TELNO
			, T.RECIP_ZIPCODE
			, T.RECIP_BASE_ADDR
			, T.RECIP_DTL_ADDR
			, T.DELV_MEMO
			, T.UPLOAD_STAT
			, T.UPLOAD_FAIL_CD
			, T.UPLOAD_FAIL_REASON
			, T.ORD_NO
			, T.DELV_ADDR_SQ
			, T.UPLOAD_GB
			, T.REG_NO
			, T.REG_DT
		FROM  TB_EXTMALL_ORDER_UPLOAD_TMP T
				LEFT OUTER JOIN TB_EXTMALL A ON T.VENDOR_ID = A.VENDOR_ID AND T.EXTMALL_ID = A.EXTMALL_ID AND A.USE_YN = 'Y'
				LEFT OUTER JOIN TB_OPTION B ON T.GOODS_CD = B.GOODS_CD AND T.OPT_CD = B.OPT_CD
		WHERE 1=1
		AND   T.REG_NO = #{regNo}

	</select>

	<select id="getVendorId" parameterType="String" resultType="String">
		/* TscOrderOcm.getVendorId */
		SELECT VENDOR_ID
		FROM   TB_EXTMALL_ORDER_UPLOAD_TMP
		WHERE  1=1
		AND    REG_ID = #{regId}
		AND    ROWNUM = 1
	</select>

	<!-- 외부몰 주문업로드 생성 -->
	<insert id="createExtmallOrderUploadSecond" parameterType="ExtmallOrder" timeout="100">
		/* TscOrderOcm.createExtmallOrderUploadSecond */
		INSERT INTO TB_EXTMALL_ORDER_UPLOAD (
		       VENDOR_ID
				, EXTMALL_ID
				, EXTMALL_NM
				, AGENT_ORDER_ID
				, EXTMALL_ORDER_ID
				, EXTMALL_PROD_ID
				, EXTMALL_PROD_NM
				, GOODS_CD
				, SKU
				, CURR_PRICE
				, ORD_QTY
				, ORD_AMT
				, CPN_DC_AMT
				, PNT_DC_AMT
				, DELV_FEE
				, ORD_RECV_DT
				, ORD_DT
				, ORD_NM
				, ORD_PHNNO
				, ORD_TELNO
				, ORD_EMAIL
				, RECIP_NM
				, RECIP_PHNNO
				, RECIP_TELNO
				, RECIP_ZIPCODE
				, RECIP_BASE_ADDR
				, RECIP_DTL_ADDR
				, DELV_MEMO
				, UPLOAD_STAT
				, UPLOAD_FAIL_CD
				, UPLOAD_FAIL_REASON
				, ORD_NO
				, DELV_ADDR_SQ
				, OPT_CD
				, UPLOAD_GB
				, REG_NO
				, REG_DT
		)
		SELECT
			   A.VENDOR_ID
				, A.EXTMALL_ID
				, A.EXTMALL_NM
				, A.AGENT_ORDER_ID
				, A.EXTMALL_ORDER_ID
				, A.EXTMALL_PROD_ID
				, B.GOODS_NM AS EXTMALL_PROD_NM
				, A.GOODS_CD
				, A.SKU
				, A.CURR_PRICE
				, A.ORD_QTY
				, A.ORD_AMT
				, A.CPN_DC_AMT
				, A.PNT_DC_AMT
				, A.DELV_FEE
				, A.ORD_RECV_DT
				, A.ORD_DT
				, A.ORD_NM
				, A.ORD_PHNNO
				, A.ORD_TELNO
				, A.ORD_EMAIL
				, A.RECIP_NM
				, A.RECIP_PHNNO
				, A.RECIP_TELNO
				, A.RECIP_ZIPCODE
				, A.RECIP_BASE_ADDR
				, A.RECIP_DTL_ADDR
				, A.DELV_MEMO
				, A.UPLOAD_STAT
				, A.UPLOAD_FAIL_CD
				, A.UPLOAD_FAIL_REASON
				, A.ORD_NO
				, A.DELV_ADDR_SQ
				, A.OPT_CD
				, A.UPLOAD_GB
				, A.REG_NO
				, NOW()
		 FROM   TB_EXTMALL_ORDER_UPLOAD_TMP A
		 		LEFT OUTER JOIN TB_GOODS B ON A.GOODS_CD = B.GOODS_CD
		 WHERE 	A.REG_NO = #{regNo}
		 AND 	DATE(A.REG_DT) = DATE(NOW())

	</insert>

	<!-- 외부몰주문등록 업로드 후 목록 -->
	<select id="getExtmallOrderUploadSecondList" parameterType="ExtmallOrder" resultType="ExtmallOrder">
	/* TscOrderOcm.getExtmallOrderUploadSecondList */
		SELECT A.*
		FROM
		(
			SELECT
					ROW_NUMBER() OVER(PARTITION BY A.VENDOR_ID, A.EXTMALL_ID, A.AGENT_ORDER_ID, A.EXTMALL_ORDER_ID ORDER BY A.REG_DT DESC) AS RANKING
					, A.VENDOR_ID
					, A.EXTMALL_ID
					, (SELECT S.EXTMALL_NM FROM TB_EXTMALL S WHERE S.VENDOR_ID = A.VENDOR_ID AND S.EXTMALL_ID = A.EXTMALL_ID) AS EXTMALL_NM
					, A.AGENT_ORDER_ID
					, A.EXTMALL_ORDER_ID
					, A.EXTMALL_PROD_ID
					, A.EXTMALL_PROD_NM
					, A.GOODS_CD
					, A.SKU
					, A.CURR_PRICE
					, A.ORD_QTY
					, A.ORD_AMT
					, A.CPN_DC_AMT
					, A.PNT_DC_AMT
					, A.DELV_FEE
					, A.ORD_RECV_DT
					, A.ORD_DT
					, A.ORD_NM
					, A.ORD_PHNNO
					, A.ORD_TELNO
					, A.ORD_EMAIL
					, A.RECIP_NM
					, A.RECIP_PHNNO
					, A.RECIP_TELNO
					, A.RECIP_ZIPCODE
					, A.RECIP_BASE_ADDR
					, A.RECIP_DTL_ADDR
					, A.DELV_MEMO
					, A.UPLOAD_STAT
					, FN_GET_CODE_NM('G021', A.UPLOAD_STAT) AS UPLOAD_STAT_NM
					, A.UPLOAD_FAIL_CD
					, FN_GET_CODE_NM('G022', A.UPLOAD_FAIL_CD) AS UPLOAD_FAIL_NM
					, A.UPLOAD_FAIL_REASON
					, IFNULL(A.ORD_NO, '') AS ORD_NO
					, A.DELV_ADDR_SQ
					, A.OPT_CD
					, A.UPLOAD_GB
					, A.REG_NO
					, A.REG_DT
			FROM   TB_EXTMALL_ORDER_UPLOAD A
			WHERE  1=1
			AND    A.UPLOAD_GB = 'E'
			AND    A.REG_NO = #{regNo}
			AND    DATE(A.REG_DT) = DATE(NOW())
			AND    DATE_FORMAT(A.REG_DT, '%Y%m%d%H%i%S') >= #{resultDtTime}
		) A WHERE A.RANKING = 1
	</select>


	<!-- 외부몰주문등록 업로드 후 결과 수량 -->
	<select id="getExtmallOrderUploadResultInfo" parameterType="ExtmallOrder" resultType="ExtmallOrder">
	/* TscOrderOcm.getExtmallOrderUploadResultInfo */
		SELECT
			 COUNT(IF(UPLOAD_STAT ='G021_40', 1, NULL)) AS FAIL_CNT
			, COUNT(IF(UPLOAD_STAT = 'G021_30', 1, NULL)) AS SUCC_CNT
		FROM
		(
			SELECT
				ROW_NUMBER() OVER(PARTITION BY A.VENDOR_ID, A.EXTMALL_ID, A.AGENT_ORDER_ID, A.EXTMALL_ORDER_ID ORDER BY A.REG_DT DESC) AS RANKING
				, A.VENDOR_ID
				, A.EXTMALL_ID
				, A.EXTMALL_NM
				, A.AGENT_ORDER_ID
				, A.EXTMALL_ORDER_ID
				, A.UPLOAD_STAT
				, A.UPLOAD_FAIL_CD
			FROM   TB_EXTMALL_ORDER_UPLOAD A
			     , TB_EXTMALL_ORDER_UPLOAD_TMP B
			WHERE  A.VENDOR_ID = B.VENDOR_ID
			AND    A.EXTMALL_ID = B.EXTMALL_ID
			AND    A.AGENT_ORDER_ID = B.AGENT_ORDER_ID
			AND    A.EXTMALL_ORDER_ID = B.EXTMALL_ORDER_ID
			AND    A.UPLOAD_GB = 'E'
			AND    B.REG_NO = #{regNo}
		) A  WHERE A.RANKING = 1
	</select>
	
	<!-- 주문상세 > 주문요청 관리자 메모 > 메모등록 -->
	<insert id="createOrderMemo" parameterType="ExtmallOrder">
		/* TscOrderOcm.createOrderMemo */
		INSERT INTO TB_ORDER_MEMO (
		    ORD_NO
		  , MEMO
		  , DEL_YN
		  , REG_NO
		  , REG_DT
		  , UPD_NO
		  , UPD_DT
		) VALUES (
		    #{ordNo}
		  , #{memo}
		  , 'N'
		  , #{regNo}
		  , NOW()
		  , #{regNo}
		  , NOW()
		)
	</insert>

	<!-- 주문상세 최초주문상세번호 수정 -->
	<update id="updateOrderDetail" parameterType="ExtmallOrder">
		/* TscOrderOcm.updateOrderDetail */
		UPDATE TB_ORDER_DETAIL
		   SET FIRST_ORD_DTL_NO = ORD_DTL_NO
		 WHERE ORD_DTL_NO = #{ordDtlNo}
	</update>
</mapper>