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

	<!-- 주문상세 조회 -->
	<select id="getOrderDetailItemList" parameterType="WmsDelivery" resultType="WmsDelivery">
		/* TscDelivery.getOrderDetailItemList */
		SELECT A.ORD_NO 
		     , B.ORD_DTL_NO
		     , B.GOODS_TYPE
		     , C.ORD_DTL_ITEM_SQ
		     , CASE WHEN B.GOODS_TYPE = 'G056_S' THEN #{chgQty} * C.ITEM_QTY ELSE #{chgQty} END  AS ITEM_QTY 
		     , C.PRODUCT_NO
		     , C.OPT_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 
		 WHERE B.ORD_NO     = #{ordNo}
		   AND B.ORD_DTL_NO = #{ordDtlNo}
		
	</select>
	
	<!-- 배송비 정책 코드 별 배송비 조회 -->
	<select id="getDeliveryFee" parameterType="DeliveryFee" resultType="DeliveryFee">
		/* TscDelivery.getDeliveryFee */
		SELECT DELV_FEE_CD
		     , SUM(DELV_FEE) AS DELV_FEE
		     , SUM(DELV_CPN_DC_AMT) AS DELV_CPN_DC_AMT
		     , SUM(REAL_DELV_AMT) AS REAL_DELV_AMT
		  FROM TB_DELIVERY_FEE
		 WHERE ORD_NO = #{ordNo}
		<if test="delvFeeCd != null and delvFeeCd != ''">
		   AND DELV_FEE_CD = #{delvFeeCd}
		</if>
		<if test="paySq != null and paySq != ''">
		   AND PAY_SQ = #{paySq}
		</if>
		<if test="ordChgSq != null and ordChgSq != ''">
		   AND ORD_CHG_SQ = #{ordChgSq}
		</if>
		 GROUP BY DELV_FEE_CD
	</select>

	<!-- 품절 알림톡 조회 -->
	<select id="getSoldoutInfo" parameterType="Order" resultType="Order">
		/* TscDelivery.getSoldoutInfo */
		SELECT
			  D.ORD_NO 
			, D.CUST_NO 
			, D.ORD_PHNNO 
			, D.ORD_NM 
			, (SELECT GOODS_NM FROM TB_GOODS WHERE GOODS_CD= MAX(C.GOODS_CD))  AS GOODS_NM
			, COUNT(C.GOODS_CD) -1 AS GOODS_CNT
			, CASE WHEN MAX((SELECT SUM(OD.ORD_QTY - OD.CNCL_RTN_QTY) FROM TB_ORDER_DETAIL OD WHERE OD.ORD_NO = C.ORD_NO)) = 0 THEN 'Y' ELSE 'N' END  AS CANCEL_ALL_YN
			, MAX((SELECT M.EXTMALL_NM FROM TB_EXTMALL M WHERE M.VENDOR_ID = C.VENDOR_ID AND M.EXTMALL_ID = C.EXTMALL_ID)) AS EXTMALL_NM
			, MAX((SELECT M.EXTMALL_TEL FROM TB_EXTMALL M WHERE M.VENDOR_ID = C.VENDOR_ID AND M.EXTMALL_ID = C.EXTMALL_ID)) AS EXTMALL_TEL
			, MAX((SELECT M.CNCLSMS_SEND_YN FROM TB_EXTMALL M WHERE M.VENDOR_ID = C.VENDOR_ID AND M.EXTMALL_ID = C.EXTMALL_ID)) AS CNCLSMS_SEND_YN
			, D.MALL_GB
		  FROM TB_ORDER_CHANGE A
		  JOIN TB_ORDER_CHANGE_DETAIL B ON A.ORD_CHG_SQ = B.ORD_CHG_SQ 
		  JOIN TB_ORDER_DETAIL C        ON B.ORD_DTL_NO = C.ORD_DTL_NO 
		  JOIN TB_ORDER        D        ON C.ORD_NO     = D.ORD_NO 
		 WHERE A.ORD_CHG_SQ = #{ordChgSq}
		   AND B.DEL_YN ='N'
		 GROUP BY D.ORD_NO, D.CUST_NO, D.ORD_PHNNO, D.ORD_NM
		
	</select>
	
	<!-- 주문상세번호 조회 -->	
	<select id="getOrdNoInfoKakao" resultType="Order">
		/*TscDelivery.getOrdNoInfoKakao*/
		<![CDATA[
		SELECT A.ORD_NO, A.CUST_NO, A.ORD_PHNNO , A.ORD_NM, A.MALL_GB, B.ORD_EXCH_GB, B.ORD_DTL_STAT
		     , CASE WHEN (SELECT COUNT(*) FROM TB_ORDER_DETAIL C WHERE C.ORD_NO = A.ORD_NO AND C.ORD_DTL_STAT='G013_70') > 0 THEN 'N' ELSE 'Y' END  AS ALL_YN  
		  FROM TB_ORDER A
		  JOIN TB_ORDER_DETAIL B ON A.ORD_NO = B.ORD_NO 
		 WHERE B.ORD_DTL_NO = #{ordDtlNo}
		]]>
	</select>
		
</mapper>
