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

	<!-- 배송목록 -->
	<select id="getDeliveryList" parameterType="Delivery" resultType="Delivery">
		/* TsaDelivery.getDeliveryList */
		SELECT  CASE WHEN ORD_DTL_STAT IN('50','55','60')
		        THEN NULL
		        ELSE FN_GET_BIZDAYS(DECODE(ORD_EXCH_GB,'O',TO_CHAR(A.PAY_DT,'YYYYMMDD'),TO_CHAR(WD_EDDT,'YYYYMMDD')),TO_CHAR(SYSDATE,'YYYYMMDD')) - 1
		        END AS DELV_DELAY_DAYS
		      , ORD_NO
		      , ORD_DTL_NO
		      , SITE_CD
		      , FN_GET_CODE_NM('G000', SITE_CD) AS SITE_NM
		      , SELL_STORE_CD
		      , (
		          SELECT SELL_STORE_NM
		          FROM   TB_SELL_STORE
		          WHERE  SUPPLY_COMP_CD = A.SUPPLY_COMP_CD
		          AND    SELL_STORE_CD = A.SELL_STORE_CD
		         ) AS SELL_STORE_NM
		      , ORD_DT
		      , PAY_DT
		      , MALL_GB
		      , FN_GET_CODE_NM('G011', MALL_GB) AS MALL_GB_NM
		      , CUST_NO
		      , ORDER_NM
		      , RECIP_NM
		      , GOODS_CD
		      , GOODS_NM
		      , (
		         SELECT COLOR_KNM
		         FROM TB_COLOR
		         WHERE SUPPLY_COMP_CD = A.SUPPLY_COMP_CD
		         AND COLOR_CD = A.COLOR_CD AND BRAND_CD = A.BRAND_CD
		         ) AS COLOR_KNM
		      , SIZE_CD
		      , SUPPLY_GOODS_CD
		      , ORD_QTY
		      , CNCL_RTN_QTY
		      , SALE_QTY
		      , SALE_AMT
		      , ORD_DTL_STAT
		      , FN_GET_CODE_NM('G013', ORD_DTL_STAT) AS ORD_DTL_STAT_NM
		      , A.DELV_LOC_CD
		      , B.DELV_LOC_NM
		      , SBAG_ENCLOSE_YN
		      , INVOICE_NO
		      , SOLDOUT_YN
		      , DSTRBT_NOTE
		      , DECODE(ORD_EXCH_GB, 'E', 'Y', '') AS ORD_EXCH_GB
		      , EXTMALL_ORDER_ID
		      , EXTMALL_ORD_DTL_NO
		      , AGENT_ORDER_ID
		      , A.SUPPLY_COMP_CD
		      , (
		         SELECT SUPPLY_COMP_NM
		         FROM TB_SUPPLY_COMPANY
		         WHERE SUPPLY_COMP_CD = A.SUPPLY_COMP_CD
		         ) AS SUPPLY_COMP_NM
		      , DELV_MEMO
		      , A.SHIP_COMP_CD
		      , A.DELV_STDT
		      , A.REAL_ORD_AMT
		      , A.EXTMALL_NM
		      , A.ORDER_PHNNO
		      , A.ORDER_TELNO
		      , A.ORDER_EMAIL
		      , B.DELV_LOC_CLSF
		FROM
		(
		 SELECT A.ORD_NO
		      , ODI.ORD_DTL_NO
		      , A.SITE_CD
		      , B.SELL_STORE_CD
		      , A.ORD_DT
		      , A.PAY_DT
		      , A.MALL_GB
		      , A.CUST_NO
		      , A.ORDER_NM
		      , D.RECIP_NM
		      , ODI.ITEM_CD     AS GOODS_CD
		      , C.GOODS_NM
		      , C.COLOR_CD
		      , ODI.SIZE_CD
		      , C.SUPPLY_GOODS_CD
		      , B.ORD_QTY
		      , B.CNCL_RTN_QTY
		      , (B.ORD_QTY - B.CNCL_RTN_QTY) AS SALE_QTY
		      , (B.ORD_AMT - B.CNCL_RTN_AMT) AS SALE_AMT
		      , B.ORD_DTL_STAT
		      , B.DELV_LOC_CD
		      , A.SBAG_ENCLOSE_YN
		      , B.INVOICE_NO
		      , B.SOLDOUT_YN
		      , B.DSTRBT_NOTE
		      , B.ORD_EXCH_GB
		      , B.EXTMALL_ORDER_ID
		      , B.EXTMALL_ORD_DTL_NO
		      , B.AGENT_ORDER_ID
		      , B.SUPPLY_COMP_CD
		      , D.DELV_MEMO
		      , B.SHIP_COMP_CD
		      , B.DELV_STDT
		      , B.REAL_ORD_AMT
		      , NVL((SELECT MAX(EXTMALL_NM)
		              FROM TB_EXTMALL WHERE VENDOR_ID = B.VENDOR_ID
		              AND  EXTMALL_ID = B.EXTMALL_ID), 'PASTELmall'
		            )  AS EXTMALL_NM
		      , C.BRAND_CD
		      , (SELECT MAX(WD_EDDT) FROM TB_ORDER_CHANGE CH, TB_ORDER_CHANGE_DETAIL CHD WHERE CH.ORD_CHG_SQ = CHD.ORD_CHG_SQ AND CHD.ORD_DTL_NO =  B.ORG_ORD_DTL_NO) AS WD_EDDT
		      , A.ORDER_PHNNO
		      , A.ORDER_TELNO
		      , A.ORDER_EMAIL
		FROM  TB_ORDER A
			, TB_ORDER_DETAIL_ITEM ODI
		    , TB_ORDER_DETAIL B
		    , TB_GOODS C
		    , TB_DELIVERY_ADDR D
		WHERE A.ORD_NO = B.ORD_NO
		AND   B.ORD_DTL_NO = ODI.ORD_DTL_NO
		AND   ODI.ITEM_CD = C.GOODS_CD
		AND   B.DELV_ADDR_SQ = D.DELV_ADDR_SQ
		<if test='ordDtlNo != null and ordDtlNo != ""'>
		AND   ODI.ORD_DTL_NO = #{ordDtlNo}
		</if>
		<if test='termGb == "ordDt"'>
		    <if test='stDate != null and stDate != ""'>
		AND   A.REG_DT >= TO_DATE(#{stDate}, 'YYYY/MM/DD HH24 MI SS')
		    </if>
		    <if test='edDate != null and edDate != ""'>
		AND   A.REG_DT <![CDATA[<]]> TO_DATE(#{edDate}, 'YYYY/MM/DD HH24 MI SS') + 1
		    </if>
		</if>
		<if test='termGb == "delvDt"'>
		    <if test='stDate != null and stDate != ""'>
		AND   B.DELV_EDDT >= TO_DATE(#{stDate}, 'YYYY/MM/DD HH24 MI SS')
		    </if>
		    <if test='edDate != null and edDate != ""'>
		AND   B.DELV_EDDT <![CDATA[<]]> TO_DATE(#{edDate}, 'YYYY/MM/DD HH24 MI SS') + 1
		    </if>
		</if>
		<if test='termGb == "delvStDt"'>
		    <if test='stDate != null and stDate != ""'>
		AND   B.DELV_STDT >= TO_DATE(#{stDate}, 'YYYY/MM/DD HH24 MI SS')
		    </if>
		    <if test='edDate != null and edDate != ""'>
		AND   B.DELV_STDT <![CDATA[<]]> TO_DATE(#{edDate}, 'YYYY/MM/DD HH24 MI SS') + 1
		    </if>
		</if>
		<if test='orderNm != null and orderNm != ""'>
		AND   A.ORDER_NM = #{orderNm}
		</if>
		<if test='recipNm != null and recipNm != ""'>
		AND   D.RECIP_NM = #{recipNm}
		</if>
		<if test='orderPhnno != null and orderPhnno != ""'>
		AND   REPLACE(A.ORDER_PHNNO, '-', '') = REPLACE(#{orderPhnno}, '-', '')
		</if>
		<if test='conditions != null and conditions != "" '>
		AND (
		<choose>
		     <when test='search != null and search == "searchOrdNo"'>
		         <foreach collection="conditions" item="item" index="index" separator="or">
		      REGEXP_LIKE(UPPER(A.ORD_NO), UPPER(#{item}))
		         </foreach>
		     </when>
		     <when test='search != null and search == "searchExtmallOrderId"'>
		         <foreach collection="conditions" item="item" index="index" separator="or">
		      REGEXP_LIKE(UPPER(B.EXTMALL_ORDER_ID), UPPER(#{item}))
		         </foreach>
		      </when>
		</choose>
		      )
		</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='frontGb != null and frontGb != ""'>
		AND   A.FRONT_GB = #{frontGb}
		</if>
		<if test='siteCd != null and siteCd != ""'>
		AND  A.SITE_CD = #{siteCd}
		</if>
		<if test='soldoutYn != null and soldoutYn != ""'>
		AND  B.SOLDOUT_YN = #{soldoutYn}
		</if>
		<if test='mallGb != null and mallGb != ""'>
		AND  A.MALL_GB = #{mallGb}
		</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='supplyCompCd != null and supplyCompCd != ""'>
		AND  B.SUPPLY_COMP_CD = #{supplyCompCd}
		</if>
		<if test='vendorId != null and vendorId != ""'>
		AND  B.VENDOR_ID = #{vendorId}
		</if>
		<if test='extmallId != null and extmallId != ""'>
		AND  B.EXTMALL_ID = #{extmallId}
		</if>
		<choose>
			<when test='soldoutYn == "Y"'>
				AND B.ORD_DTL_STAT = '99'
			</when>
			<otherwise>
				<choose>
					<when test='delvDelayDays != null and delvDelayDays != ""'>
						AND B.ORD_DTL_STAT IN ('20', '30', '34', '35', '40')
					</when>
					<when test='multiOrdDtlStat == null or multiOrdDtlStat == ""'>
						AND B.ORD_DTL_STAT != '00'
					</when>
				</choose>
			</otherwise>
		</choose>
		) A
		, TB_DELIVERY_LOC B
		WHERE A.DELV_LOC_CD = B.DELV_LOC_CD (+)
		AND A.SUPPLY_COMP_CD = B.SUPPLY_COMP_CD(+)
		<if test='delvDelayDays != null and delvDelayDays != ""'>
		AND   CASE WHEN ORD_DTL_STAT IN('50','55','60') THEN NULL
		      ELSE FN_GET_BIZDAYS(DECODE(ORD_EXCH_GB,'O',TO_CHAR(A.PAY_DT,'YYYYMMDD'),TO_CHAR(WD_EDDT,'YYYYMMDD')),TO_CHAR(SYSDATE,'YYYYMMDD')) END >= #{delvDelayDays}
		</if>
		<if test='delvLocClsf != null and delvLocClsf != ""'>
		AND   B.DELV_LOC_CLSF = #{delvLocClsf}
		</if>
		ORDER BY A.ORD_NO DESC, A.ORD_DTL_NO
		
	</select>

	<!-- 주문상세 -->
	<select id="getOrderDetail" parameterType="Delivery" resultType="Delivery">
		/* TsaDelivery.getOrderDetail */
		SELECT '1' FROM DUAL
	</select>

	<!-- 주문상세 상태 값 변경 -->
	<update id="updateOrderDetailStat" parameterType="Delivery" >
		/* TsaDelivery.updateOrderDetailStat */
		SELECT '1' FROM DUAL
	</update>
	
	<!-- 주문 상세 이력 등록-->
	<update id="createOrderDetailHst" parameterType="Delivery" >
		/* TsaDelivery.createOrderDetailHst */
		SELECT '1' FROM DUAL
	</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.COLOR_CD AND DEFAULT_IMG_YN = 'Y' ) AS SYS_IMG_NM
			, G.GOODS_NM
			, DBG.GOODS_CD
			, DBG.COLOR_CD			
			, DBG.SIZE_CD
			, 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 COLOR_CD          = #{colorCd}
		  AND SIZE_CD           = #{sizeCd}
	</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 AS COLOR_CD 
			, S.OPT_CD2 AS SIZE_CD
			, 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 SIZE_CD     = #{sizeCd}
		   AND DEL_YN      = 'N'
	</select>

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

	<!-- 배송/회수지시 목록 -->
	<select id="getDeliveryWithdrawDirectiveList" parameterType="Delivery" resultType="Delivery">
		/* TsaDelivery.getDeliveryWithdrawDirectiveList */
		SELECT '1' FROM DUAL
	</select>
	
</mapper>