<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.style24.admin.biz.dao.TsaDeliveryDao">

	<!-- 목록 페이징 정보 -->
	<sql id="getListPagingCondition_sql">
		<choose>
		<when test="pageable != null">
		    ) A
		)Z WHERE RNUM BETWEEN  #{pageable.startRow} AND #{pageable.endRow}
		</when>
		<otherwise>
		    ) A
		)Z
		</otherwise>
		</choose>
	</sql>
	
	<!-- 배송목록 건수 -->
	<select id="getDeliveryListCount" parameterType="Delivery" resultType="int">
		/* TsaDelivery.getDeliveryListCount */
		SELECT COUNT(*) AS TOTCNT
		  FROM (		
				SELECT A.* 
				  FROM (
					SELECT 
							  CASE WHEN ORD_DTL_STAT IN('G013_20','G013_30','G013_35','G013_40')
							       THEN FN_GET_BIZDAYS(IF(A.ORD_EXCH_GB='O',DATE_FORMAT(A.PAY_DT,'%Y%m%d'),DATE_FORMAT(A.WD_EDDT,'%Y%m%d')),DATE_FORMAT(NOW(),'%Y%m%d')) - 1 
							       ELSE NULL END AS DELV_DELAY_DAYS
							, FN_GET_CODE_NM('G024', (SELECT B.DELV_LOC_CLSF FROM TB_DELIVERY_LOC B WHERE  A.DELV_LOC_CD = B.DELV_LOC_CD AND A.SUPPLY_COMP_CD = B.SUPPLY_COMP_CD ) ) AS DELV_LOC_CLSF  -- 출고처 분류 G024  물류창고, 일반매장, 직송매장, 입점업체 
					  FROM (
							SELECT 
								  A.PAY_DT         -- 결제일시
								, B.SUPPLY_COMP_CD -- 공급업체
								, B.ORD_DTL_STAT  -- 주문상세상태
								, B.DELV_LOC_CD   -- 출고처
								, (SELECT MAX(CH.WD_EDDT) FROM TB_ORDER_CHANGE CH INNER JOIN TB_ORDER_CHANGE_DETAIL CHD ON CH.ORD_CHG_SQ = CHD.ORD_CHG_SQ WHERE CHD.ORD_DTL_NO =  B.ORG_ORD_DTL_NO) AS WD_EDDT
					            , B.ORD_EXCH_GB
							FROM  TB_ORDER A 
							      INNER JOIN TB_ORDER_DETAIL B        ON A.ORD_NO       = B.ORD_NO
							      INNER JOIN TB_GOODS C               ON B.GOODS_CD     = C.GOODS_CD
							      INNER JOIN TB_DELIVERY_ADDR D       ON B.DELV_ADDR_SQ = D.DELV_ADDR_SQ
							WHERE 1=1
							<include refid="getDeliveryListCondition_sql"/>  
					) A
				) A
				WHERE 1=1
				<if test='delvDelayDays != null and delvDelayDays != ""'>
				<![CDATA[
				AND A.DELV_DELAY_DAYS >= #{delvDelayDays}
				]]>
				</if>
				<if test='delvLocClsf != null and delvLocClsf != ""'>
				AND   A.DELV_LOC_CLSF = #{delvLocClsf}
				</if>
			) A

	</select>
		
	<!-- 배송목록 -->
	<select id="getDeliveryList" parameterType="Delivery" resultType="Delivery">
		/* TsaDelivery.getDeliveryList */
		SELECT Z.*
		    , FN_GET_CODE_NM('G000', Z.SITE_CD) AS SITE_NM
			, ( SELECT SELL_STORE_NM
		      FROM   TB_SELL_STORE
		      WHERE  SUPPLY_COMP_CD = Z.SUPPLY_COMP_CD
		      AND    SELL_STORE_CD = Z.SELL_STORE_CD
		     ) AS SELL_STORE_NM
		    , FN_GET_CODE_NM('G011', Z.MALL_GB) AS MALL_GB_NM
		    , FN_GET_CODE_NM('G013', Z.ORD_DTL_STAT) AS ORD_DTL_STAT_NM
		    , (SELECT B.DELV_LOC_NM FROM TB_DELIVERY_LOC B WHERE  Z.DELV_LOC_CD = B.DELV_LOC_CD AND Z.SUPPLY_COMP_CD = B.SUPPLY_COMP_CD ) AS DELV_LOC_NM   -- 출고처명
		    , (SELECT BD.BRAND_KNM FROM TB_BRAND BD WHERE BD.BRAND_CD = Z.BRAND_CD ) AS BRAND_NM
		    , FN_GET_CODE_NM('G056', Z.GOODS_TYPE ) AS GOODS_TYPE_NM 
		    , (SELECT SUPPLY_COMP_NM FROM TB_SUPPLY_COMPANY S WHERE S.SUPPLY_COMP_CD = Z.SUPPLY_COMP_CD) AS SUPPLY_COMP_NM 
		FROM (
		    SELECT A.*, ROW_NUMBER() OVER(ORDER BY  A.ORD_NO DESC, A.ORD_DTL_NO) RNUM 
		    FROM (
				SELECT A.* 
				  FROM (
					SELECT 
							  CASE WHEN ORD_DTL_STAT IN('G013_20','G013_30','G013_35','G013_40')
							       THEN FN_GET_BIZDAYS(IF(A.ORD_EXCH_GB='O',DATE_FORMAT(A.PAY_DT,'%Y%m%d'),DATE_FORMAT(A.WD_EDDT,'%Y%m%d')),DATE_FORMAT(NOW(),'%Y%m%d')) - 1 
							       ELSE NULL END AS DELV_DELAY_DAYS
							, A.ORD_NO     -- 주문번호
							, A.ORD_DTL_NO -- 주문상세번호
							, A.SITE_CD    -- 사이트
							, A.SELL_STORE_CD -- 판매몰
							, A.EXTMALL_NM  -- 판매몰명     
							, A.ORD_DT         -- 주문일시
							, A.PAY_DT         -- 결제일시
							, A.MALL_GB        -- 몰구분
							, A.ORD_NM         -- 주문자
							, A.RECIP_NM       -- 수령인
							, A.SUPPLY_COMP_CD -- 공급업체
							, A.GOODS_CD       -- 상품코드
							, A.GOODS_TYPE     -- 구성유형 
							, A.GOODS_NM       -- 상품명
							, A.OPT_CD1        -- 옵션1
							, A.OPT_CD2        -- 옵션2 
							, A.OPT_CD         -- SKU 
							, A.SKU_MODEL_NO
							, A.PRODUCT_NO
							, A.PRODUCT_CODE
							, A.ORD_QTY        -- 주문수량
							, A.CNCL_RTN_QTY   -- 취소수량
							, A.SALE_QTY       -- 판매수량
							, A.SALE_AMT       -- 판매금액
							, A.REAL_ORD_AMT   -- 실결제금액
							, A.ORD_DTL_STAT   -- 주문상세상태
							, FN_GET_CODE_NM('G024', (SELECT B.DELV_LOC_CLSF FROM TB_DELIVERY_LOC B WHERE  A.DELV_LOC_CD = B.DELV_LOC_CD AND A.SUPPLY_COMP_CD = B.SUPPLY_COMP_CD ) ) AS DELV_LOC_CLSF           -- 출고처 분류 G024  물류창고, 일반매장, 직송매장, 입점업체 
				            , A.DELV_LOC_CD   -- 출고처
							, A.SHIP_COMP_CD  -- 출고택배사
							, A.INVOICE_NO    -- 출고송장
							-- 송장등록수정
							-- 배송완료처리
							, A.EXTMALL_ORDER_ID -- 외부몰주문번호
							, A.AGENT_ORDER_ID   -- 에이전트주문번호
							, A.SOLDOUT_YN    -- 결품여부
							, A.DELV_MEMO     -- 배송메모
							, CASE WHEN A.ORD_EXCH_GB ='E' THEN 'Y' ELSE 'N' END AS ORD_EXCH_GB   -- 교환여부
							, A.DELV_STDT     -- 배송시작일  
							, A.BRAND_CD      -- 브랜드	
                            , A.CUST_NO
                            , CASE WHEN A.SHOT_DELV_YN = 'Y' THEN '총알배송' ELSE '일반배송' END AS SHOT_DELV_YN
				  			, A.PG_GB 
						    , A.PAY_MEANS 
						    , A.BANK_CD 
						    , A.ACCOUNT_NO 
						    , A.ACCOUNT_NM 
					  FROM (
							SELECT 
								  A.ORD_NO     -- 주문번호
								, B.ORD_DTL_NO -- 주문상세번호
								, A.SITE_CD    -- 사이트
								, B.SELL_STORE_CD -- 판매몰
								, IFNULL((SELECT MAX(EXTMALL_NM) FROM TB_EXTMALL WHERE VENDOR_ID = B.VENDOR_ID
								          AND  EXTMALL_ID = B.EXTMALL_ID), '아이스타일24')  AS EXTMALL_NM  -- 판매몰명
								, A.ORD_DT         -- 주문일시
								, A.PAY_DT         -- 결제일시
								, A.MALL_GB        -- 몰구분
								, A.ORD_NM         -- 주문자
								, D.RECIP_NM       -- 수령인
								, B.SUPPLY_COMP_CD -- 공급업체
								, B.GOODS_CD       -- 상품코드
								, B.GOODS_TYPE     -- 구성유형 
								, C.GOODS_NM       -- 상품명
								, ODI.OPT_CD1      -- 옵션1
								, ODI.OPT_CD2      -- 옵션2 
								, ODI.OPT_CD       -- SKU 
								, ODI.SKU_MODEL_NO
								, ODI.PRODUCT_NO
								, ODI.PRODUCT_CODE
								, ODI.ITEM_QTY * B.ORD_QTY      AS ORD_QTY      -- 주문수량
								, ODI.ITEM_QTY * B.CNCL_RTN_QTY AS CNCL_RTN_QTY -- 취소수량
								, (ODI.ITEM_QTY * B.ORD_QTY  - ODI.ITEM_QTY * B.CNCL_RTN_QTY) SALE_QTY -- 판매수량
								, B.ORD_AMT - B.CNCL_RTN_AMT  AS SALE_AMT       -- 판매금액
								, B.REAL_ORD_AMT  -- 실결제금액
								, B.ORD_DTL_STAT  -- 주문상세상태
								, B.DELV_LOC_CD   -- 출고처
								, B.SHIP_COMP_CD  -- 출고택배사
								, B.INVOICE_NO    -- 출고송장
								, B.EXTMALL_ORDER_ID -- 외부몰주문번호
								, B.AGENT_ORDER_ID
								, B.SOLDOUT_YN    -- 결품여부
								, D.DELV_MEMO     -- 배송메모
								, B.ORD_EXCH_GB   -- 교환여부
								, B.DELV_STDT     -- 배송시작일  
								, (SELECT MAX(CH.WD_EDDT) FROM TB_ORDER_CHANGE CH INNER JOIN TB_ORDER_CHANGE_DETAIL CHD ON CH.ORD_CHG_SQ = CHD.ORD_CHG_SQ WHERE CHD.ORD_DTL_NO =  B.ORG_ORD_DTL_NO) AS WD_EDDT
							    , C.BRAND_CD
							    , A.CUST_NO
							    , B.SHOT_DELV_YN
							    , P.PG_GB 
							    , P.PAY_MEANS 
							    , T.BANK_CD 
							    , T.ACCOUNT_NO 
							    , T.ACCOUNT_NM 
							FROM  TB_ORDER A 
							      INNER JOIN TB_ORDER_DETAIL B        ON A.ORD_NO       = B.ORD_NO
								  INNER JOIN TB_ORDER_DETAIL_ITEM ODI ON B.ORD_DTL_NO   = ODI.ORD_DTL_NO
							      INNER JOIN TB_GOODS C               ON ODI.ITEM_CD    = C.GOODS_CD
							      INNER JOIN TB_DELIVERY_ADDR D       ON B.DELV_ADDR_SQ = D.DELV_ADDR_SQ
								  INNER JOIN TB_PAYMENT       P       ON A.ORD_NO       = P.ORD_NO  AND P.PAY_STAT = 'G016_30'
							 LEFT OUTER JOIN TB_CUST_ACCOUNT  T       ON A.CUST_NO      = T.CUST_NO AND T.DEFAULT_YN = 'Y' AND T.DEL_YN = 'N'
							WHERE 1=1 
							  AND B.GOODS_TYPE NOT IN ('G056_S')
					        <include refid="getDeliveryListCondition_sql"/>
							
							UNION ALL
							
							SELECT 
								  A.ORD_NO     -- 주문번호
								, B.ORD_DTL_NO -- 주문상세번호
								, A.SITE_CD    -- 사이트
								, B.SELL_STORE_CD -- 판매몰
								, IFNULL((SELECT MAX(EXTMALL_NM) FROM TB_EXTMALL WHERE VENDOR_ID = B.VENDOR_ID
								          AND  EXTMALL_ID = B.EXTMALL_ID), '아이스타일24')  AS EXTMALL_NM  -- 판매몰명
								, A.ORD_DT         -- 주문일시
								, A.PAY_DT         -- 결제일시
								, A.MALL_GB        -- 몰구분
								, A.ORD_NM         -- 주문자
								, D.RECIP_NM       -- 수령인
								, B.SUPPLY_COMP_CD -- 공급업체
								, B.GOODS_CD       -- 상품코드
								, B.GOODS_TYPE     -- 구성유형 
								, C.GOODS_NM       -- 상품명
								, '-' AS OPT_CD1   -- 옵션1
								, '-' AS OPT_CD2   -- 옵션2 
								, '-' AS OPT_CD    -- SKU 
								, '-' AS SKU_MODEL_NO
								, NULL  AS PRODUCT_NO
								, '-'  AS PRODUCT_CODE
								, B.ORD_QTY      AS ORD_QTY      -- 주문수량
								, B.CNCL_RTN_QTY AS CNCL_RTN_QTY -- 취소수량
								, B.ORD_QTY  - B.CNCL_RTN_QTY   AS SALE_QTY   -- 판매수량
								, B.ORD_AMT - B.CNCL_RTN_AMT    AS SALE_AMT   -- 판매금액
								, B.REAL_ORD_AMT  -- 실결제금액
								, B.ORD_DTL_STAT  -- 주문상세상태
								, B.DELV_LOC_CD   -- 출고처
								, B.SHIP_COMP_CD  -- 출고택배사
								, B.INVOICE_NO    -- 출고송장
								, B.EXTMALL_ORDER_ID -- 외부몰주문번호
								, B.AGENT_ORDER_ID
								, B.SOLDOUT_YN    -- 결품여부
								, D.DELV_MEMO     -- 배송메모
								, B.ORD_EXCH_GB   -- 교환여부
								, B.DELV_STDT     -- 배송시작일  
								, (SELECT MAX(CH.WD_EDDT) FROM TB_ORDER_CHANGE CH INNER JOIN TB_ORDER_CHANGE_DETAIL CHD ON CH.ORD_CHG_SQ = CHD.ORD_CHG_SQ WHERE CHD.ORD_DTL_NO =  B.ORG_ORD_DTL_NO) AS WD_EDDT
							    , C.BRAND_CD 
							    , A.CUST_NO
							    , B.SHOT_DELV_YN
							    , P.PG_GB 
							    , P.PAY_MEANS 
							    , T.BANK_CD 
							    , T.ACCOUNT_NO 
							    , T.ACCOUNT_NM 
							FROM  TB_ORDER A 
							      INNER JOIN TB_ORDER_DETAIL B        ON A.ORD_NO       = B.ORD_NO
							      INNER JOIN TB_GOODS C               ON B.GOODS_CD     = C.GOODS_CD
							      INNER JOIN TB_DELIVERY_ADDR D       ON B.DELV_ADDR_SQ = D.DELV_ADDR_SQ
								  INNER JOIN TB_PAYMENT       P       ON A.ORD_NO       = P.ORD_NO  AND P.PAY_STAT = 'G016_30' AND P.PAY_GB ='O'
							 LEFT OUTER JOIN TB_CUST_ACCOUNT  T       ON A.CUST_NO      = T.CUST_NO AND T.DEFAULT_YN = 'Y' AND T.DEL_YN = 'N'
							WHERE 1=1
							  AND B.GOODS_TYPE IN ('G056_S')
							<include refid="getDeliveryListCondition_sql"/>  
					) A
				) A
				WHERE 1=1
				<if test='delvDelayDays != null and delvDelayDays != ""'>
				<![CDATA[
				AND A.DELV_DELAY_DAYS >= #{delvDelayDays}
				]]>
				</if>
				<if test='delvLocClsf != null and delvLocClsf != ""'>
				AND   A.DELV_LOC_CLSF = #{delvLocClsf}
				</if>
				ORDER BY A.ORD_NO DESC, A.ORD_DTL_NO
	<include refid="getListPagingCondition_sql"/>
	</select>

    <!-- 배송목록 조회 조건  -->
	<sql id="getDeliveryListCondition_sql">
		<if test='siteCd != null and siteCd != ""'>
		AND  A.SITE_CD = #{siteCd}
		</if>
		<if test='mallGb != null and mallGb != ""'>
		AND  A.MALL_GB = #{mallGb}
		</if>
		<if test='supplyCompCd != null and supplyCompCd != ""'>
		AND B.SUPPLY_COMP_CD = #{supplyCompCd}
		</if>
        <if test="multiBrand != null and multiBrand != ''">
        AND C.BRAND_CD IN
            <foreach collection="multiBrand" item="item" index="index"  open="(" close=")" separator=",">
        	#{item}
            </foreach>
        </if>
		<if test='orderNm != null and orderNm != ""'>
		AND   A.ORD_NM = #{orderNm}
		</if>
		<if test='orderPhnno != null and orderPhnno != ""'>
		AND   REPLACE(A.ORDER_PHNNO, '-', '') = REPLACE(#{orderPhnno}, '-', '')
		</if>
		<if test='ordDtlNo != null and ordDtlNo != ""'>
		AND   B.ORD_DTL_NO = #{ordDtlNo}
		</if>
		<if test='conditions != null and conditions != "" '>
		AND (
		<choose>
		     <when test='search != null and search == "searchOrdNo"'>
		         A.ORD_NO IN 
		          <foreach collection="conditions" item="item" index="index"  open="(" close=")" separator=",">
		      		#{item}
		         </foreach>
		     
		     </when>
		     <when test='search != null and search == "searchExtmallOrderId"'>
		         B.EXTMALL_ORDER_ID IN 
		         <foreach collection="conditions" item="item" index="index"  open="(" close=")" separator=",">
		      		#{item}
		         </foreach>
		      </when>
		</choose>
		      )
		</if>
		<if test='recipNm != null and recipNm != ""'>
		AND   D.RECIP_NM = #{recipNm}
		</if>
		<if test='invoiceNo != null and invoiceNo != ""'>
		AND   B.INVOICE_NO = #{invoiceNo}
		</if>
		<if test='goodsCd != null and goodsCd != ""'>
		AND   C.GOODS_CD = #{goodsCd}
		</if>
		<if test='delvLocCd != null and delvLocCd != ""'>
		AND   B.DELV_LOC_CD = #{delvLocCd}
		</if>
		<if test="multiOrdDtlStat != null and multiOrdDtlStat !=''">
		AND  B.ORD_DTL_STAT IN
		    <foreach collection="multiOrdDtlStat" item="item" index="index"  open="(" close=")" separator=",">
		     #{item}
		    </foreach>
		</if>		
		<if test='soldoutYn != null and soldoutYn != ""'>
		AND  B.SOLDOUT_YN = #{soldoutYn}
		</if>
		<if test='termGb == "ordDt"'>
		    <if test='stDate != null and stDate != ""'>
		AND   A.REG_DT >= STR_TO_DATE(REPLACE(#{stDate},'-',''), '%Y%m%d%H%i%s')    
		    </if>
		    <if test='edDate != null and edDate != ""'>
		AND   A.REG_DT <![CDATA[<]]> STR_TO_DATE(REPLACE(#{edDate},'-','') +1, '%Y%m%d%H%i%s') 
		    </if>
		</if>
		<if test='termGb == "delvDt"'>
		    <if test='stDate != null and stDate != ""'>
		AND   B.DELV_EDDT >= STR_TO_DATE(REPLACE(#{stDate},'-',''), '%Y%m%d%H%i%s')
		    </if>
		    <if test='edDate != null and edDate != ""'>
		AND   B.DELV_EDDT <![CDATA[<]]> STR_TO_DATE(REPLACE(#{edDate},'-','')+1, '%Y%m%d%H%i%s')
		    </if>
		</if>
		<if test='termGb == "delvStDt"'>
		    <if test='stDate != null and stDate != ""'>
		AND   B.DELV_STDT >= STR_TO_DATE(REPLACE(#{stDate},'-',''), '%Y%m%d%H%i%s')
		    </if>
		    <if test='edDate != null and edDate != ""'>
		AND   B.DELV_STDT <![CDATA[<]]> STR_TO_DATE(REPLACE(#{edDate},'-','')+1 , '%Y%m%d%H%i%s') 
		    </if>
		</if>
		AND B.ORD_DTL_STAT != 'G013_00'
	</sql>


	<!-- 주문상세 단품 목록 -->
	<select id="getDelvDetailItemList" parameterType="Delivery" resultType="Delivery">
		/* TsaDelivery.getDelvDetailItemList */
		SELECT 
		     B.ITEM_CD 
		   , (SELECT G.GOODS_NM FROM TB_GOODS G WHERE G.GOODS_CD = B.ITEM_CD) AS GOODS_NM
		   , B.OPT_CD1 
		   , B.OPT_CD2 
		   , B.ITEM_QTY 
		   , B.ITEM_PRICE 
		   , C.CURR_STOCK_QTY
		   , C.BASE_STOCK_QTY
		 FROM TB_ORDER_DETAIL A
		 JOIN TB_ORDER_DETAIL_ITEM B ON A.ORD_DTL_NO = B.ORD_DTL_NO 
		 JOIN VW_STOCK             C ON B.ITEM_CD    = C.GOODS_CD    AND B.OPT_CD = C.OPT_CD 
	    WHERE A.ORD_DTL_NO  = #{ordDtlNo}
	    ORDER BY B.DISP_ORD  
	</select>


	<!-- 주문상세 상태 값 변경 -->
	<update id="updateOrderDetailStat" parameterType="Delivery" >
		/* TsaDelivery.updateOrderDetailStat */
		UPDATE TB_ORDER_DETAIL
		SET    ORD_DTL_STAT = #{ordDtlStat}
		     , DELV_EDDT    = IFNULL(DELV_EDDT, NOW()) 
		     , UPD_NO       = #{updNo}
		     , UPD_DT       = NOW()
		WHERE  ORD_DTL_NO   = #{ordDtlNo}
	</update>

	<!-- 주문상세 -->
	<select id="getOrderDetail" parameterType="Integer" resultType="Delivery">
		/* TsaDelivery.getOrderDetail */
		SELECT 
			  A.ORD_NO
			, A.ORD_DTL_NO
			, A.ORD_DTL_STAT 
			, A.SHIP_COMP_CD 
			, A.INVOICE_NO 
		  FROM TB_ORDER_DETAIL A
		 WHERE ORD_DTL_NO  = #{ordDtlNo}
	</select>
	
	<!-- 송장번호  변경 -->
	<update id="saveChangeInvoice" parameterType="Delivery" >
		/* TsaDelivery.saveChangeInvoice */
		UPDATE TB_ORDER_DETAIL
		SET    SHIP_COMP_CD = #{shipCompCd}
		     , INVOICE_NO   = #{invoiceNo}
		     , UPD_NO       = #{updNo}
		     , UPD_DT       = NOW()
		WHERE  ORD_DTL_NO   = #{ordDtlNo}
	</update>
		

	
	<!-- 출고금지상품 목록 -->
	<select id="getBangoodsList" parameterType="Delivery" resultType="Delivery">
		/* TsaDelivery.getBangoodsList */
		SELECT DBG.DEL_YN
			, G.BRAND_CD
			, B.BRAND_KNM
			, DL.DELV_LOC_NM
			, (SELECT MAX(SYS_IMG_NM) FROM TB_GOODS_IMG  WHERE GOODS_CD = DBG.GOODS_CD AND COLOR_CD = DBG.OPT_CD1 AND DEFAULT_IMG_YN = 'Y' ) AS SYS_IMG_NM
			, G.GOODS_NM
			, DBG.GOODS_CD
			, DBG.OPT_CD1			
			, DBG.OPT_CD2
			, FN_GET_CODE_NM('G008', G.GOODS_STAT) AS GOODS_STAT
			, DBG.DELV_BAN_STDT 
			, DBG.DELV_BAN_EDDT	
			, DBG.DELV_BAN_GOODS_SQ
		FROM  TB_DELIVERY_BAN_GOODS DBG
		INNER JOIN TB_DELIVERY_LOC DL   ON DBG.DELV_LOC_CD = DL.DELV_LOC_CD
		INNER JOIN TB_GOODS        G    ON DBG.GOODS_CD = G.GOODS_CD
		INNER JOIN TB_BRAND        B    ON G.BRAND_CD = B.BRAND_CD
		WHERE 1=1
		<if test='supplyCompCd != null and supplyCompCd != ""'>
			AND G.SUPPLY_COMP_CD = #{supplyCompCd}
		</if>
		<if test='brandCd != null and brandCd != ""'>
			AND G.BRAND_CD = #{brandCd}
		</if>
		<if test='searchTxt != null and searchTxt != ""'>
			<if test='goodsGb == "goodsCd"'>
				AND G.GOODS_CD LIKE UPPER(#{searchTxt} || '%')
			</if>
			<if test='goodsGb == "goodsNm"'>
				AND G.GOODS_NM LIKE #{searchTxt} || '%'
			</if>
		</if>
		<if test='delvLocCd != null and delvLocCd != ""'>
			AND DBG.DELV_LOC_CD = #{delvLocCd}
		</if>
		<if test='delYn != null and delYn != ""'>
			AND DBG.DEL_YN = #{delYn}
		</if>
		<if test="stDate != null and stDate != '' and edDate != null and edDate != ''">
			AND DBG.REG_DT BETWEEN STR_TO_DATE(CONCAT(#{stDate},' 000000'),'%Y-%m-%d %H%i%S') AND STR_TO_DATE(CONCAT(#{edDate},' 235959'), '%Y-%m-%d %H%i%S')
		</if>
	</select>

	<!-- 출고금지상품 삭제 -->
	<update id="deleteBangoods" parameterType="Delivery" >
		/* TsaDelivery.deleteBangoods */
       UPDATE TB_DELIVERY_BAN_GOODS
		  SET DEL_YN = 'Y'
			, UPD_NO = #{updNo}
			, UPD_DT = NOW()
		WHERE DELV_BAN_GOODS_SQ = #{delvBanGoodsSq}
		  AND GOODS_CD          = #{goodsCd}
		  AND OPT_CD1           = #{optCd1}
		  AND OPT_CD2           = #{optCd2}
	</update>

	<!-- 출고금지상품 등록 대상목록 -->
	<select id="getBangoodsRegistList" parameterType="Delivery" resultType="Delivery">
		/* TsaDelivery.getBangoodsRegistList */
		SELECT 
		      G.BRAND_CD
			, B.BRAND_KNM
			, (SELECT MAX(SYS_IMG_NM) FROM TB_GOODS_IMG  WHERE GOODS_CD = S.GOODS_CD AND COLOR_CD = S.OPT_CD1 AND DEFAULT_IMG_YN = 'Y' ) AS SYS_IMG_NM	
			, G.GOODS_NM
			, G.GOODS_CD
			, S.OPT_CD1 
			, S.OPT_CD2 
			, G.GOODS_STAT
			, (SELECT CD_NM FROM TB_COMMON_CODE WHERE CD_GB = 'G008' AND CD =G.GOODS_STAT) AS GOODS_STAT_NM
		FROM  TB_GOODS G
		INNER JOIN TB_BRAND B  ON G.BRAND_CD = B.BRAND_CD
		INNER JOIN TB_OPTION S ON G.GOODS_CD = S.GOODS_CD			
		WHERE 1=1
		<if test='supplyCompCd != null and supplyCompCd != ""'>
			AND G.SUPPLY_COMP_CD = #{supplyCompCd}
		</if>
		<if test='brandCd != null and brandCd != ""'>
			AND G.BRAND_CD = #{brandCd}
		</if>
		<if test='searchTxt != null and searchTxt != ""'>
			<if test='goodsGb == "goodsCd"'>
				AND G.GOODS_CD LIKE UPPER(#{searchTxt} || '%')
			</if>
			<if test='goodsGb == "goodsNm"'>
				AND G.GOODS_NM LIKE #{searchTxt} || '%'
			</if>
		</if>
		<if test='goodsStat != null and goodsStat != ""'>
			AND G.GOODS_STAT = #{goodsStat}
		</if>
	</select>

	<!-- 출고금지상품 카운트 -->
	<select id="getBangoodsListCnt" parameterType="Delivery"	resultType="int">
		/* TsaDelivery.getBangoodsListCnt */
		SELECT COUNT(*)
		  FROM TB_DELIVERY_BAN_GOODS
		 WHERE DELV_LOC_CD = #{delvLocCd}
		   AND GOODS_CD    = #{goodsCd}
		   AND OPT_CD1     = #{optCd1}
		   AND OPT_CD2     = #{optCd2}
		   AND DEL_YN      = 'N'
	</select>

	<!-- 출고금지상품 등록 -->
	<insert id="createBangoods" parameterType="Delivery" >
		/* TsaDelivery.createBangoods */
		INSERT INTO TB_DELIVERY_BAN_GOODS (
			  DELV_LOC_CD
			, GOODS_CD
			, OPT_CD1
			, OPT_CD2
			, DELV_BAN_STDT
			, DELV_BAN_EDDT
			, DEL_YN
			, REG_NO
			, REG_DT
			, UPD_NO
			, UPD_DT
		)
		VALUES (
			  #{delvLocCd}
			, #{goodsCd}
			, #{optCd1}
			, #{optCd2}
			, replace(#{delvBanStdt},'-','')
			, replace(#{delvBanEddt},'-','')
			, 'N'
			, #{regNo}
			, NOW()
			, #{updNo}
			, NOW()
		)
	</insert>

	<!-- 배송/회수지시 목록 건수 -->
	<select id="getDeliveryWithdrawDirectiveListCount" parameterType="Delivery" resultType="int">
		/* TsaDelivery.getDeliveryWithdrawDirectiveListCount */
				SELECT COUNT(*) AS TOTCNT
				  FROM (		
						SELECT 
						       '배송'                              AS DELV_GB_NM  -- 지시구분명
						     , OD.INVOICE_SEND_YN                 AS SEND_YN     -- 전송여부
						     , O.SITE_CD                                         -- 사이트코드
						     , O.ORD_NO                                          -- 주문번호
						     , OD.ORD_DTL_NO                                     -- 주문상세번호
						     , IT.ITEM_CD AS GOODS_CD                            -- 상품코드
						FROM   TB_ORDER O
						 JOIN  TB_ORDER_DETAIL OD      ON O.ORD_NO = OD.ORD_NO
						 JOIN  TB_DELIVERY_ADDR DA     ON OD.DELV_ADDR_SQ = DA.DELV_ADDR_SQ AND OD.DELV_ADDR_SQ = DA.DELV_ADDR_SQ
						 JOIN  TB_ORDER_DETAIL_ITEM IT ON OD.ORD_NO       = IT.ORD_NO       AND OD.ORD_DTL_NO   = IT.ORD_DTL_NO
						 JOIN  TB_GOODS G              ON IT.ITEM_CD      = G.GOODS_CD
						WHERE 1=1 
						<if test="delvGb != null and delvGb != ''">
						AND    'D' = #{delvGb} -- 배송지시
						</if>
						<if test="stDate != null and stDate != ''">
						AND    O.REG_DT <![CDATA[>=]]> STR_TO_DATE(REPLACE(#{stDate},'-','') , '%Y%m%d%H%i%s')
						</if>
						<if test="edDate != null and edDate != ''">
						AND    O.REG_DT <![CDATA[<]]> STR_TO_DATE(REPLACE(#{edDate},'-','')+1 , '%Y%m%d%H%i%s') 
						</if>
						<if test="siteCd != null and siteCd != ''">
						AND    O.SITE_CD = #{siteCd}
						</if>
						AND    OD.ORD_DTL_STAT = 'G013_50' -- 배송중
						<if test="sendYn != null and sendYn != ''">
						AND    OD.INVOICE_SEND_YN = #{sendYn}
						</if>
						AND    G.SELF_GOODS_YN = 'Y' -- 자사상품만
						
						UNION ALL
						
						SELECT 
						       '회수'                 AS DELV_GB_NM      -- 지시구분명
						     , OC.WD_INVOICE_SEND_YN AS SEND_YN         -- 전송여부
						     , O.SITE_CD                                -- 사이트코드
						     , O.ORD_NO                                 -- 주문번호
						     , OCD.ORD_DTL_NO                           -- 주문상세번호
						     , IT.ITEM_CD AS GOODS_CD                   -- 상품코드
						FROM   TB_ORDER_CHANGE OC          
						JOIN   TB_ORDER_CHANGE_DETAIL OCD ON OC.ORD_CHG_SQ  = OCD.ORD_CHG_SQ 
						JOIN   TB_ORDER_DETAIL OD         ON OCD.ORD_DTL_NO = OD.ORD_DTL_NO
						JOIN   TB_ORDER O                 ON OD.ORD_NO      = O.ORD_NO
						JOIN   TB_ORDER_DETAIL_ITEM IT    ON OD.ORD_NO      = IT.ORD_NO     AND OD.ORD_DTL_NO     = IT.ORD_DTL_NO
						JOIN   TB_GOODS G                 ON IT.ITEM_CD     = G.GOODS_CD
						JOIN   TB_DELIVERY_LOC D          ON OD.DELV_LOC_CD = D.DELV_LOC_CD AND OD.SUPPLY_COMP_CD = D.SUPPLY_COMP_CD
						WHERE  1=1 
						<if test="delvGb != null and delvGb != ''">
						AND    'W' = #{delvGb} -- 회수지시
						</if>
						AND    OC.CHG_GB IN ('G680_30','G680_40') -- 반품요청,교환요청
						<if test="sendYn != null and sendYn != ''">
						AND    OC.WD_INVOICE_SEND_YN = #{sendYn}
						</if>
						<if test="stDate != null and stDate != ''">
						AND    OC.REG_DT <![CDATA[>=]]> STR_TO_DATE(REPLACE(#{stDate},'-','') , '%Y%m%d%H%i%s')
						</if>
						<if test="edDate != null and edDate != ''">
						AND    OC.REG_DT <![CDATA[<]]> STR_TO_DATE(REPLACE(#{edDate},'-','')+1 , '%Y%m%d%H%i%s')
						</if>
						AND    OCD.CHG_STAT IN ('G685_30','G685_40') -- 반품요청,교환요청
						<if test="siteCd != null and siteCd != ''">
						AND    O.SITE_CD = #{siteCd}
						</if>
						AND    G.SELF_GOODS_YN = 'Y' -- 자사상품만
						AND    OCD.DEL_YN      = 'N'
					) A

	</select>
	
	<!-- 배송/회수지시 목록 -->
	<select id="getDeliveryWithdrawDirectiveList" parameterType="Delivery" resultType="Delivery">
		/* TsaDelivery.getDeliveryWithdrawDirectiveList */
		SELECT Z.*
		FROM (
		    SELECT A.*, ROW_NUMBER() OVER(ORDER BY  A.DELV_GB_NM, A.ORD_NO DESC, A.ORD_DTL_NO )  RNUM 
		    FROM ( 
				SELECT A.* 
				  FROM (		
						SELECT DISTINCT
						       '배송'                              AS DELV_GB_NM  -- 지시구분명
						     , OD.INVOICE_SEND_YN                 AS SEND_YN     -- 전송여부
						     , O.SITE_CD                                         -- 사이트코드
						     , O.ORD_NO                                          -- 주문번호
						     , OD.ORD_DTL_NO                                     -- 주문상세번호
						     , IT.ITEM_CD AS GOODS_CD                            -- 상품코드
						     , G.GOODS_NM                                        -- 상품명
						     , DA.RECIP_NM                                       -- 수취인명
						     , DA.RECIP_PHNNO                                    -- 수취인휴대전화번호
						     , DA.RECIP_TELNO                                    -- 수취인전화번호
						  --   , DA.RECIP_EMAIL                                    -- 수취인이메일
						     , DA.RECIP_ZIPCODE                                  -- 수취인우편번호
						     , DA.RECIP_BASE_ADDR                                -- 수취인기본주소
						     , DA.RECIP_DTL_ADDR                                 -- 수취인상세주소
						     , O.ORD_NM                                          -- 주문자명
						     , O.ORD_PHNNO                                       -- 주문자휴대전화번호
						     , O.ORD_TELNO                                       -- 주문자전화번호
						     , O.ORD_EMAIL                                       -- 주문자이메일
						FROM   TB_ORDER O
						 JOIN  TB_ORDER_DETAIL OD      ON O.ORD_NO = OD.ORD_NO
						 JOIN  TB_DELIVERY_ADDR DA     ON OD.DELV_ADDR_SQ = DA.DELV_ADDR_SQ AND OD.DELV_ADDR_SQ = DA.DELV_ADDR_SQ
						 JOIN  TB_ORDER_DETAIL_ITEM IT ON OD.ORD_NO       = IT.ORD_NO       AND OD.ORD_DTL_NO   = IT.ORD_DTL_NO
						 JOIN  TB_GOODS G              ON IT.ITEM_CD      = G.GOODS_CD
						WHERE 1=1 
						<if test="delvGb != null and delvGb != ''">
						AND    'D' = #{delvGb} -- 배송지시
						</if>
						<if test="stDate != null and stDate != ''">
						AND    O.REG_DT <![CDATA[>=]]> STR_TO_DATE(REPLACE(#{stDate},'-','') , '%Y%m%d%H%i%s')
						</if>
						<if test="edDate != null and edDate != ''">
						AND    O.REG_DT <![CDATA[<]]> STR_TO_DATE(REPLACE(#{edDate},'-','')+1 , '%Y%m%d%H%i%s') 
						</if>
						<if test="siteCd != null and siteCd != ''">
						AND    O.SITE_CD = #{siteCd}
						</if>
						AND    OD.ORD_DTL_STAT = 'G013_50' -- 배송중
						<if test="sendYn != null and sendYn != ''">
						AND    OD.INVOICE_SEND_YN = #{sendYn}
						</if>
						AND    G.SELF_GOODS_YN = 'Y' -- 자사상품만
						
						UNION ALL
						
						SELECT DISTINCT
						       '회수'                 AS DELV_GB_NM      -- 지시구분명
						     , OC.WD_INVOICE_SEND_YN AS SEND_YN         -- 전송여부
						     , O.SITE_CD                                -- 사이트코드
						     , O.ORD_NO                                 -- 주문번호
						     , OCD.ORD_DTL_NO                           -- 주문상세번호
						     , IT.ITEM_CD AS GOODS_CD                   -- 상품코드
						     , G.GOODS_NM                               -- 상품명
						     , D.RTN_LOC_NM          AS RECIP_NM        -- 수취인명
						     , NULL                  AS RECIP_PHNNO     -- 수취인휴대전화번호
						     , D.RTN_LOC_TELNO       AS RECIP_TELNO     -- 수취인전화번호
						     , D.RTN_LOC_ZIPCODE     AS RECIP_POST_NO   -- 수취인우편번호
						     , D.RTN_LOC_BASE_ADDR   AS RECIP_BASE_ADDR -- 수취인주소
						     , D.RTN_LOC_DTL_ADDR    AS RECIP_DTL_ADDR  -- 수취인상세주소
						     , O.ORD_NM                                 -- 주문자명
						     , O.ORD_PHNNO                              -- 주문자휴대전화번호
						     , O.ORD_TELNO                              -- 주문자전화번호
						     , O.ORD_EMAIL                              -- 주문자이메일
						FROM   TB_ORDER_CHANGE OC          
						JOIN   TB_ORDER_CHANGE_DETAIL OCD ON OC.ORD_CHG_SQ  = OCD.ORD_CHG_SQ 
						JOIN   TB_ORDER_DETAIL OD         ON OCD.ORD_DTL_NO = OD.ORD_DTL_NO
						JOIN   TB_ORDER O                 ON OD.ORD_NO      = O.ORD_NO
						JOIN   TB_ORDER_DETAIL_ITEM IT    ON OD.ORD_NO      = IT.ORD_NO     AND OD.ORD_DTL_NO     = IT.ORD_DTL_NO
						JOIN   TB_GOODS G                 ON IT.ITEM_CD     = G.GOODS_CD
						JOIN   TB_DELIVERY_LOC D          ON OD.DELV_LOC_CD = D.DELV_LOC_CD AND OD.SUPPLY_COMP_CD = D.SUPPLY_COMP_CD
						WHERE  1=1 
						<if test="delvGb != null and delvGb != ''">
						AND    'W' = #{delvGb} -- 회수지시
						</if>
						AND    OC.CHG_GB IN ('G680_30','G680_40') -- 반품요청,교환요청
						<if test="sendYn != null and sendYn != ''">
						AND    OC.WD_INVOICE_SEND_YN = #{sendYn}
						</if>
						<if test="stDate != null and stDate != ''">
						AND    OC.REG_DT <![CDATA[>=]]> STR_TO_DATE(REPLACE(#{stDate},'-','') , '%Y%m%d%H%i%s')
						</if>
						<if test="edDate != null and edDate != ''">
						AND    OC.REG_DT <![CDATA[<]]> STR_TO_DATE(REPLACE(#{edDate},'-','')+1 , '%Y%m%d%H%i%s')
						</if>
						AND    OCD.CHG_STAT IN ('G685_30','G685_40') -- 반품요청,교환요청
						<if test="siteCd != null and siteCd != ''">
						AND    O.SITE_CD = #{siteCd}
						</if>
						AND    G.SELF_GOODS_YN = 'Y' -- 자사상품만
						AND    OCD.DEL_YN      = 'N'
					) A
				ORDER BY A.DELV_GB_NM, A.ORD_NO DESC, A.ORD_DTL_NO
				<include refid="getListPagingCondition_sql"/>
	</select>
	
	<!-- 출고처지정 목록 건수 -->
	<select id="getAssignListCount" parameterType="Delivery" resultType="int">
		/* TsaDelivery.getAssignListCount */
		SELECT COUNT(*) AS TOTCNT
		  FROM (
				SELECT 
					  B.ORD_DTL_NO   -- 주문상세번호
					, B.GOODS_CD     -- 상품코드
				  FROM TB_ORDER A 
				  JOIN TB_ORDER_DETAIL B      ON A.ORD_NO = B.ORD_NO
				  JOIN TB_ORDER_DETAIL_ITEM C ON B.ORD_NO = C.ORD_NO AND B.ORD_DTL_NO = C.ORD_DTL_NO 
				  JOIN TB_GOODS D             ON B.GOODS_CD = D.GOODS_CD 
				  JOIN VW_STOCK E             ON C.ITEM_CD = E.GOODS_CD AND C.OPT_CD = E.OPT_CD 
				 WHERE D.SELF_GOODS_YN  = 'Y'  -- 자사상품만 
				   AND B.ORD_DTL_STAT IN ('G013_20','G013_35')
				   AND B.GOODS_TYPE    != 'G056_S'
				   <include refid="getAssignListCondition_sql"/>
				   
				 UNION ALL
				  
				SELECT 
					  B.ORD_DTL_NO   -- 주문상세번호
					, B.GOODS_CD     -- 상품코드
				  FROM TB_ORDER A 
				  JOIN TB_ORDER_DETAIL B      ON A.ORD_NO = B.ORD_NO
				  JOIN TB_GOODS D             ON B.GOODS_CD = D.GOODS_CD 
				 WHERE D.SELF_GOODS_YN  = 'Y'  -- 자사상품만 
				   AND B.ORD_DTL_STAT IN ('G013_20','G013_35')
				   AND B.GOODS_TYPE    != 'G056_S'				 
				   <include refid="getAssignListCondition_sql"/>
		       ) A 
		
	</select>	
	
	<!-- 출고처지정 목록 -->
	<select id="getAssignList" parameterType="Delivery" resultType="Delivery">
		/* TsaDelivery.getAssignList */
		SELECT Z.*
		     , FN_GET_CODE_NM('G000', Z.SITE_CD) AS SITE_NM
		     , FN_GET_CODE_NM('G011', Z.MALL_GB) AS MALL_NM
		     , FN_GET_CODE_NM('G056', Z.GOODS_TYPE) AS GOODS_TYPE_NM
		     , CASE WHEN ORD_EXCH_GB ='O' THEN '주문' ELSE '교환' END AS ORD_EXCH_NM     -- 상품코드
		     , FN_GET_CODE_NM('G013', Z.ORD_DTL_STAT) AS ORD_DTL_STAT_NM 
			 , (SELECT MAX(EE.EXTMALL_NM)
			      FROM TB_EXTMALL EE
			     WHERE EE.VENDOR_ID  = Z.VENDOR_ID
				   AND EE.EXTMALL_ID = Z.EXTMALL_ID) AS EXTMALL_NM	
			 , (SELECT L.DELV_LOC_NM FROM TB_DELIVERY_LOC L WHERE L.DELV_LOC_CD = Z.DELV_LOC_CD AND L.SUPPLY_COMP_CD = Z.SUPPLY_COMP_CD ) AS DELV_LOC_NM	
			 , (SELECT DL.DELV_LOC_CLSF FROM TB_DELIVERY_LOC DL WHERE DL.DELV_LOC_CD = Z.DELV_LOC_CD AND DL.SUPPLY_COMP_CD = Z.SUPPLY_COMP_CD  ) AS DELV_LOC_CLSF   	      
		  FROM (
				SELECT A.*, ROW_NUMBER() OVER(ORDER BY  A.ORD_NO DESC, A.ORD_DTL_NO) RNUM 
				FROM (  
					SELECT A.* FROM (	
						SELECT 
							  A.SITE_CD      -- 사이트
							, A.ORD_NO       -- 주문번호
							, B.ORD_DTL_NO   -- 주문상세번호
							, B.DELV_LOC_CD  -- 출고처
							, A.ORD_DT       -- 주문일시
							, A.PAY_DT       -- 결제일시
							, A.MALL_GB      -- 몰구분
							, B.EXTMALL_ID   -- 외부몰
							, A.ORD_NM       -- 주문자
							, B.GOODS_TYPE   -- 구성유형  (신규)
							, D.GOODS_NM     -- 상품명 
							, B.GOODS_CD     -- 상품코드
							, C.OPT_CD       -- 옵션 
							, C.OPT_CD1      -- 옵션1	
							, C.OPT_CD2      -- 옵션2  
							, B.ORD_QTY - B.CNCL_RTN_QTY  AS ORD_QTY -- 주문수량
							, E.CURR_STOCK_QTY 	-- 현재고
							, E.BASE_STOCK_QTY 	-- 안전재고
							, B.ORD_AMT - B.CNCL_RTN_AMT AS ORD_AMT -- 주문금액
							, B.ORD_EXCH_GB 	-- 주문교환 
							, B.ORD_DTL_STAT 	-- 주문상세상태
							, B.VENDOR_ID
							, B.SUPPLY_COMP_CD 
						  FROM TB_ORDER A 
						  JOIN TB_ORDER_DETAIL B      ON A.ORD_NO = B.ORD_NO
						  JOIN TB_ORDER_DETAIL_ITEM C ON B.ORD_NO = C.ORD_NO AND B.ORD_DTL_NO = C.ORD_DTL_NO 
						  JOIN TB_GOODS D             ON B.GOODS_CD = D.GOODS_CD 
						  JOIN VW_STOCK E             ON C.ITEM_CD = E.GOODS_CD AND C.OPT_CD = E.OPT_CD 
						 WHERE D.SELF_GOODS_YN  = 'Y'  -- 자사상품만 
						   AND B.ORD_DTL_STAT IN ('G013_20','G013_35')
						   AND B.GOODS_TYPE    != 'G056_S'
						   <include refid="getAssignListCondition_sql"/>
						   
					UNION ALL 				   
						
						SELECT 
							  A.SITE_CD      -- 사이트
							, A.ORD_NO       -- 주문번호
							, B.ORD_DTL_NO   -- 주문상세번호
							, B.DELV_LOC_CD  -- 출고처
							, A.ORD_DT       -- 주문일시
							, A.PAY_DT       -- 결제일시
							, A.MALL_GB      -- 몰구분
							, B.EXTMALL_ID   -- 외부몰
							, A.ORD_NM       -- 주문자
							, B.GOODS_TYPE   -- 구성유형  (신규)
							, D.GOODS_NM     -- 상품명 
							, B.GOODS_CD     -- 상품코드
							, '-' AS OPT_CD       -- 옵션 
							, '-' AS OPT_CD1      -- 옵션1	
							, '-' AS OPT_CD2      -- 옵션2  
							, B.ORD_QTY - B.CNCL_RTN_QTY  AS ORD_QTY -- 주문수량
							, NULL AS CURR_STOCK_QTY 	-- 현재고
							, NULL AS BASE_STOCK_QTY 	-- 안전재고
							, B.ORD_AMT - B.CNCL_RTN_AMT AS ORD_AMT -- 주문금액
							, B.ORD_EXCH_GB 	-- 주문교환 
							, B.ORD_DTL_STAT 	-- 주문상세상태
							, B.VENDOR_ID
							, B.SUPPLY_COMP_CD 
						  FROM TB_ORDER A 
						  JOIN TB_ORDER_DETAIL B      ON A.ORD_NO = B.ORD_NO
						  JOIN TB_GOODS D             ON B.GOODS_CD = D.GOODS_CD 
						 WHERE D.SELF_GOODS_YN  = 'Y'  -- 자사상품만 
						   AND B.ORD_DTL_STAT IN ('G013_20','G013_35')
						   AND B.GOODS_TYPE     = 'G056_S'
						   <include refid="getAssignListCondition_sql"/>  
						 
				       ) A
				       ORDER BY A.ORD_NO, A.ORD_DTL_NO, A.OPT_CD
		<include refid="getListPagingCondition_sql"/>			       
	</select>	
	
    <!-- 출고처지정 조회 조건  -->
	<sql id="getAssignListCondition_sql">
		<if test='ordNo != null and ordNo != ""'>
			AND A.ORD_NO = #{ordNo}
		</if>		
		<if test='ordDtlNo != null and ordDtlNo != ""'>
			AND B.ORD_DTL_NO = #{ordDtlNo}
		</if>	
		<if test='extmallOrderId != null and extmallOrderId != ""'>
			AND LOWER(B.EXTMALL_ORDER_ID) LIKE LOWER(#{extmallOrderId}||'%')
		</if>
		<if test='siteCd != null and siteCd != ""'>
			AND A.SITE_CD = #{siteCd}
		</if>
		<if test='mallGb != null and mallGb != ""'>
			AND A.MALL_GB = #{mallGb}
		</if>	
		<if test='goodsCd != null and goodsCd != ""'>
			AND C.GOODS_CD = #{goodsCd}
		</if>
		<if test='delvAssignStat == "Y"'>
			AND B.DELV_LOC_CD IS NOT NULL
		</if>
		<if test='delvAssignStat == "N"'>
			AND B.DELV_LOC_CD IS NULL
		</if>
		<if test='delvLocCd != null and delvLocCd != ""'>
			AND B.DELV_LOC_CD = #{delvLocCd}
		</if>
		<if test='siteCd != null and siteCd != ""'>
			AND  A.SITE_CD = #{siteCd}
		</if>
		<if test='ordExchGb == "Y"'>
			AND B.ORD_EXCH_GB = 'E'
		</if>
		<if test='ordExchGb == "N"'>
			AND B.ORD_EXCH_GB = 'O'
		</if>		
		<if test='stDate != null and stDate != ""'>
			AND A.ORD_DT >= STR_TO_DATE(replace(#{stDate},'-',''), '%Y%m%d%H%i%S')
		</if>
		<if test='edDate != null and edDate != ""'>
			AND A.ORD_DT <![CDATA[<]]> DATE_ADD(STR_TO_DATE(replace(#{edDate},'-',''), '%Y%m%d%H%i%S'), INTERVAL 1 DAY) 
		</if>		 
	
	</sql>
	
	<!-- 출고처 수동지정 대상 조회  -->
	<select id="getDeliveryAssignPopup" parameterType="Delivery" resultType="Delivery">
		/* TsaDelivery.getDeliveryAssignPopup */
        SELECT
              A.ORD_NO
            , B.ORD_DTL_NO
            , GROUP_CONCAT(G.ITEM_CD ORDER BY G.ITEM_CD SEPARATOR '\r\n') AS GOODS_CD
            , B.CURR_PRICE
        FROM  TB_ORDER A
        JOIN  TB_ORDER_DETAIL B      ON A.ORD_NO = B.ORD_NO
        JOIN  TB_ORDER_DETAIL_ITEM G ON B.ORD_NO = G.ORD_NO AND B.ORD_DTL_NO = G.ORD_DTL_NO
        WHERE B.ORD_DTL_NO     = #{ordDtlNo}
      GROUP BY A.ORD_NO, B.ORD_DTL_NO, B.CURR_PRICE
	</select>
			
	
	<!-- 출고처 수동지정 주문상세 리스트 조회 -->
	<select id="getOrdDtlNoItemList" resultType="Delivery">
		/*TsaDelivery.getOrdDtlNoItemList */
		SELECT 
			  A.ORD_NO 
			, B.ORD_DTL_NO 
			, B.GOODS_CD 
			, C.ITEM_CD 
			, C.OPT_CD 
			, C.OPT_CD1 
			, C.OPT_CD2 
		    , (B.ORD_QTY - B.CNCL_RTN_QTY) * C.ITEM_QTY  AS ORD_QTY
		    , (B.ORD_QTY - B.CNCL_RTN_QTY)               AS ORD_DTL_QTY
		    , C.ITEM_QTY                                 AS ITEM_QTY
		    , B.GOODS_TYPE 
		    , C.PRODUCT_NO 
    		, B.FORMAL_GB  
    		, B.SUPPLY_COMP_CD 
    		, D.BRAND_CD
		  FROM TB_ORDER             A
		  JOIN TB_ORDER_DETAIL      B ON A.ORD_NO = B.ORD_NO 
		  JOIN TB_ORDER_DETAIL_ITEM C ON B.ORD_NO = C.ORD_NO AND B.ORD_DTL_NO = C.ORD_DTL_NO 
		  JOIN TB_GOODS             D ON B.GOODS_CD = D.GOODS_CD 
		 WHERE B.ORD_DTL_NO = #{ordDtlNo}
	</select>
		
	<!-- 출고처지정 임시 테이블 삭제    -->
	<delete id="deleteTbDeliveryLocTmp" parameterType="Delivery"  >
		/*TsaDelivery.deleteTbDeliveryLocTmp*/
		DELETE FROM TB_DELIVERY_LOC_TMP WHERE USE_GB = #{useGb}
	</delete>	
	
	<!-- 출고처지정 임시테이블 등록  -->
	<insert id="insertTbDeliveryLocTmp" parameterType="Delivery" >
		/* TsaDelivery.insertTbDeliveryLocTmp */
		INSERT INTO TB_DELIVERY_LOC_TMP
		(
			  USE_GB
			, DELV_LOC_CD
			, DELV_ASSIGN_ORD
			, LOC_QTY
			, GOODS_LOC_CNT
			, ORD_DTL_NO_LIST
		)
		VALUES
		(
			  #{useGb}
			, #{delvLocCd}
			, (SELECT DELV_ASSIGN_ORD FROM TB_DELIVERY_LOC LC WHERE LC.DELV_LOC_CD = #{delvLocCd} AND LC.SUPPLY_COMP_CD = #{supplyCompCd})
			, TRUNCATE(#{locQty}/#{ordQty},0)
			, 1
			, #{ordDtlNo}
		) ON DUPLICATE KEY
		UPDATE 
			GOODS_LOC_CNT = GOODS_LOC_CNT+1
		  , ORD_DTL_NO_LIST = CONCAT(ORD_DTL_NO_LIST,',',#{ordDtlNo})
		  , LOC_QTY = CASE WHEN  LOC_QTY > TRUNCATE(#{locQty}/#{ordQty},0) THEN  TRUNCATE(#{locQty}/#{ordQty},0) ELSE LOC_QTY END	
	</insert>	
	
	<!-- 출고처지정 임시테이블 세트 등록  -->
	<insert id="insertTbDeliveryLocSetTmp" parameterType="Delivery" >
		/* TsaDelivery.insertTbDeliveryLocSetTmp */
		INSERT INTO TB_DELIVERY_LOC_TMP
		(
			  USE_GB
			, DELV_LOC_CD
			, DELV_ASSIGN_ORD
			, LOC_QTY
			, GOODS_LOC_CNT
			, GOODS_LOC_SET_CNT
		)
		VALUES
		(
			  #{useGb}
			, #{delvLocCd}
			, (SELECT DELV_ASSIGN_ORD FROM TB_DELIVERY_LOC LC WHERE LC.DELV_LOC_CD = #{delvLocCd} AND LC.SUPPLY_COMP_CD = #{supplyCompCd})
			, TRUNCATE(#{locQty}/#{ordQty})
			, 0
			, 1
		) ON DUPLICATE KEY
		UPDATE 
			GOODS_LOC_SET_CNT = A.GOODS_LOC_SET_CNT+1
		  ,	LOC_QTY = CASE WHEN LOC_QTY > TRUNCATE(#{locQty}/#{ordQty})THEN TRUNCATE(#{locQty}/#{ordQty}) ELSE LOC_QTY END 		
	</insert>
	
	<!-- 출고처 지정 임시테이블 세트상품갯수 max값 조회      -->	
	<select id="getDeliveryLocTmpGoodsSetMaxCnt" resultType="int">
		/*TsbDelivery.getDeliveryLocTmpGoodsSetMaxCnt*/
		SELECT CASE WHEN MAX(GOODS_LOC_SET_CNT) IS NULL THEN 0 ELSE MAX(GOODS_LOC_SET_CNT)  END  AS CNT 
		  FROM TB_DELIVERY_LOC_TMP
		 WHERE USE_GB = #{useGb}
	</select>			
	
	<!-- 출고처지정 임시 테이블 수정   -->
	<update id="updateTbDeliveryLocSetTmp" parameterType="Delivery"  >
		/*TsbDelivery.updateTbDeliveryLocSetTmp*/
		UPDATE TB_DELIVERY_LOC_TMP SET
		      GOODS_LOC_CNT = GOODS_LOC_CNT + 1
		    , ORD_DTL_NO_LIST =  CASE WHEN ORD_DTL_NO_LIST IS NULL THEN #{ordDtlNo} ELSE CONCAT(ORD_DTL_NO_LIST,',',#{ordDtlNo}) END
		WHERE USE_GB = #{useGb} AND GOODS_LOC_SET_CNT = #{goodsLocSetCnt}		
	</update>	
		
	<!-- 주문상세 출고처 지정대상 리스트 -->
	<select id="getOrdDtlAssignLocList" resultType="Delivery">
		/*TsaDelivery.getOrdDtlAssignLocList */
		SELECT   (SELECT DELV_LOC_NM FROM TB_DELIVERY_LOC WHERE DELV_LOC_CD = A.DELV_LOC_CD AND SUPPLY_COMP_CD = #{supplyCompCd}) AS DELV_LOC_NM
		       , DELV_LOC_CD
		       , LOC_QTY AS STOCK_QTY
		       , (SELECT DELV_LOC_CLSF FROM TB_DELIVERY_LOC WHERE DELV_LOC_CD = A.DELV_LOC_CD AND SUPPLY_COMP_CD = #{supplyCompCd} ) AS DELV_LOC_CLSF
		       , IFNULL((SELECT CASE WHEN DELV_AR_GB ='N' THEN 'Y' ELSE 'N' END FROM TB_DELIVERY_ASSIGN WHERE DELV_LOC_CD = A.DELV_LOC_CD AND  ORD_DTL_NO = #{ordDtlNo}),'N') AS DELV_AR_GB
		  FROM TB_DELIVERY_LOC_TMP A
		 WHERE USE_GB        = #{useGb}
		   AND GOODS_LOC_CNT = #{goodsLocCnt}

	</select>
			
	<!-- 주문상세 출고처 등록   -->
	<insert id="updateOrdDtlDelvCd" parameterType="Delivery" >
		/*TsbDelivery.updateOrdDtlDelvCd*/
		UPDATE TB_ORDER_DETAIL SET
			  ORD_DTL_STAT     =  #{ordDtlStat}
			, DELV_LOC_CD      =  #{delvLocCd}
			, DELV_ASSIGN_DT   = NOW()
			, DELV_ASSIGN_STAT = #{delvArGb}
			, UPD_NO           = #{updNo}
			, UPD_DT           = NOW()
		WHERE ORD_NO     = #{ordNo}
		  AND ORD_DTL_NO = #{ordDtlNo}
	</insert>		
			
	<!-- 출고처지정 등록    -->
	<insert id="createDeliveryAssign" parameterType="Delivery" keyProperty="delvAssignSq" >
		/*TsbDelivery.insertTbDeliveryAssign*/
		INSERT INTO TB_DELIVERY_ASSIGN
		(
			  DELV_ASSIGN_SQ    --	출고지정일련번호
			, ORD_NO	 		--	주문번호
			, ORD_DTL_NO	 	--	주문상세번호
			, DELV_LOC_CD	    --	출고처코드
			, ASSIGN_QTY	    --	지정수량
			, DELV_AR_GB	    --	출고수락거부구분(P:대기, Y:수락, N:거부)
			, REG_NO	 	    --	등록자NO
			, REG_DT	 	    --	등록일시
			, UPD_NO	 	    --	수정자NO
			, UPD_DT	 	    --	수정일시
		)
		VALUES
		(
			  #{delvAssignSq}	--	출고지정일련번호
			, #{ordNo}	 		--	주문번호
			, #{ordDtlNo}	 	--	주문상세번호
			, #{delvLocCd}	    --	출고처코드
			, #{locQty}	        --	지정수량
			, #{delvArGb}	    --	출고수락거부구분(P:대기, Y:수락, N:거부)
			, #{regNo}	 	    --	등록자NO
			, NOW()	 		    --	등록일시
			, #{regNo}	 	    --	수정자NO
			, NOW()	            --	수정일시
		)		
	</insert>
   
	<!-- 출고처지정HST 등록    -->
	<insert id="createDeliveryAssignHst" parameterType="Delivery" keyProperty="delvAssignHstSq" >
		/*TsbDelivery.insertTbDeliveryAssignHst*/
		INSERT INTO TB_DELIVERY_ASSIGN_HST
		(
			  DELV_ASSIGN_SQ  --  출고지정일련번호
			, ORD_NO	 	  --  주문번호
			, ORD_DTL_NO	  --  주문상세번호
			, DELV_LOC_CD	  --  출고처코드
			, ASSIGN_QTY	  --  지정수량
			, DELV_AR_GB	  --  출고수락거부구분(P:대기, Y:수락, N:거부)
			, REG_NO	 	  --  등록자NO
			, REG_DT	 	  --  등록일시
			, UPD_NO	 	  --  수정자NO
			, UPD_DT	 	  --  수정일시
			, HST_REG_DT      --  이력등록일시
		)
		VALUES
		(
			  #{delvAssignSq}	--	출고지정일련번호
			, #{ordNo}	 		--	주문번호
			, #{ordDtlNo}	 	--	주문상세번호
			, #{delvLocCd}	    --	출고처코드
			, #{locQty}	        --	지정수량
			, #{delvArGb}	    --	출고수락거부구분(P:대기, Y:수락, N:거부)
			, #{regNo}	 	    --	등록자NO
			, NOW()	 		    --	등록일시
			, #{regNo}	 	    --	수정자NO
			, NOW()	            --	수정일시
			, NOW()	            --	수정일시
		)
	</insert>
				
	<!-- 주문상세 아이템목록  -->
	<select id="getOrdDtlItemList" resultType="Delivery">
		/*TsaDelivery.getOrdDtlItemList */
		SELECT 
			  A.ORD_NO 
			, B.ORD_DTL_NO 
			, B.ORD_DTL_STAT 
			, (B.ORD_QTY - B.CNCL_RTN_QTY) * C.ITEM_QTY AS ITEM_QTY
			, B.ORD_QTY - B.CNCL_RTN_QTY AS ORD_QTY
			, C.ITEM_CD 
			, C.OPT_CD 
			, C.OPT_CD1 
			, C.OPT_CD2 
			, C.PRODUCT_NO 
		  FROM TB_ORDER A 
		  JOIN TB_ORDER_DETAIL B      ON A.ORD_NO = B.ORD_NO 
		  JOIN TB_ORDER_DETAIL_ITEM C ON B.ORD_NO = C.ORD_NO AND B.ORD_DTL_NO = C.ORD_DTL_NO 
		 WHERE B.ORD_NO     = #{ordNo}
		   AND B.ORD_DTL_NO = #{ordDtlNo}		
	</select>				
	
	<!-- 출고처지정 삭제   -->
	<delete id="deleteTbDeliveryAssign" parameterType="Delivery" >
		/*TsbDelivery.deleteTbDeliveryAssign*/
		DELETE FROM TB_DELIVERY_ASSIGN
		WHERE ORD_DTL_NO  = #{ordDtlNo}
	</delete>	
	
	<!-- 주문상세 출고처지정 취소   -->
	<update id="cancelAssignDelvLoc" parameterType="Delivery" >
		/*TsbDelivery.cancelAssignDelvLoc*/
		UPDATE TB_ORDER_DETAIL SET
			  ORD_DTL_STAT     = #{ordDtlStat}
			, DELV_LOC_CD      = NULL
			, DELV_ASSIGN_DT   = NULL
			, DELV_ASSIGN_STAT = 'P'
			, UPD_NO           = #{updNo}
			, UPD_DT           = NOW()
		WHERE ORD_DTL_NO = #{ordDtlNo}
	</update>	
		
	
</mapper>