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

	<!-- 실재고 조회 -->
	<select id="getRealStock" parameterType="WmsDelivery" resultType="WmsDelivery" timeout="300">
		/* TscWmsDelivery.getRealStock */
		SELECT 'ST0001' AS STOCK_LOC
		     , A.PRODUCT_NO
		     , A.SKU_CODE
		     , SUM(A.LOC_QTY) - MAX(A.SLIP_CREATE_QTY) - MAX(A.CVS_QTY) - MAX(A.EOUT_CREATE_QTY)  - SUM(A.Qty) AS LOC_QTY
		  FROM (
		    SELECT PRODUCT_NO
		         , SKU_CODE
		         , LOC_QTY
		         , 0  AS SLIP_CREATE_QTY
		         , 0  AS CVS_QTY
		         , 0  AS EOUT_CREATE_QTY
		         , 0  AS QTY 
		      FROM istyle24_Wms.dbo.VW_Stock_By_LocCode
		     WHERE PRODUCT_NO = #{productNo}
		       AND SKU_CODE   = #{skuCode}
		       AND WH_CODE    = #{whCode}
			 UNION ALL 
			SELECT PRODUCT_NO        
				 , SKU_CODE          
				 , 0                 AS LOC_QTY
				 , SLIP_CREATE_QTY   AS SLIP_CREATE_QTY
				 , CVS_QTY           AS CVS_QTY
				 , EOUT_CREATE_QTY   AS EOUT_CREATE_QTY
				 , 0 
			  FROM istyle24_Wms.dbo.TLOST06M WITH (NOLOCK)
			 WHERE PRODUCT_NO = #{productNo}
			   AND SKU_CODE   = #{skuCode}
			   AND WH_CODE    = #{whCode}
			UNION ALL
			SELECT ProductNo
				 , SKUCode
				 , 0  AS LOC_QTY
				 , 0  AS SLIP_CREATE_QTY
				 , 0  AS CVS_QTY
				 , 0  AS EOUT_CREATE_QTY
			     , Qty
			  FROM iSTYLE24_WmsIf.dbo.TB_IF_DELIFIX_QTY
			 WHERE ProductNo = #{productNo}
			   AND SKUCode   = #{skuCode}
			) A GROUP BY A.PRODUCT_NO, A.SKU_CODE	
	</select>
	
	<!-- 취소가능여부 조회 -->
	<select id="getCancelYn" parameterType="WmsDelivery" resultType="String">
		/* TscWmsDelivery.getCancelYn */
	    <![CDATA[
			SELECT 
			       CASE WHEN COUNT(*) < 1 THEN 'Y' ELSE 'N' END AS CANCEL_YN
		      FROM istyle24_Wmsif.dbo.TB_IF_DeliveryInvoice
		     WHERE orderNo    = #{ordNo}
		       AND orderDtlNo = #{ordDtlNo}
		 ]]>      
   </select>

	<!-- WMS 주문번호상세 존재여부  -->
	<select id="getWmsOrdDtlNoYn" parameterType="WmsDelivery" resultType="String">
		/* TscWmsDelivery.getWmsOrdDtlNoYn */
	    <![CDATA[
			SELECT CASE WHEN COUNT(*) > 0 THEN 'Y' ELSE 'N' END AS CHK_YN 
			  FROM istyle24_Wmsif.dbo.TB_IF_DeliveryOrder A,  
			       istyle24_Wmsif.dbo.TB_IF_DeliveryOrderItem B
			 WHERE A.DeliveryOrderNo = B.DeliveryOrderNo 
			   AND A.OrderNo  = #{ordNo} 
			   AND B.OrderDtlNo IS NULL
			   AND B.GiftYn = 'N'
		 ]]>      
   </select>
      
	<!-- WMS 배송 취소  -->
	<update id="updateWmsIfDeliveryOrderCancel" parameterType="WmsDelivery">
		/* TscWmsDelivery.updateWmsIfDeliveryOrderCancel */
			UPDATE istyle24_Wmsif.dbo.TB_IF_DeliveryOrder SET
			  IssueTypeCd = '취소'
			, StatusCd    = '60'
			, IsCancel    = 1
			WHERE OrderNo = #{ordNo}
			  AND StatusCd !='60'
	</update>

	<!-- WMS 배송상세 취소  -->
	<update id="updateWmsIfDeliveryOrderItemCancel" parameterType="WmsDelivery">
		/* TscWmsDelivery.updateWmsIfDeliveryOrderItemCancel */
			UPDATE istyle24_Wmsif.dbo.TB_IF_DeliveryOrderItem  
			SET    StatusCd = CASE WHEN (Qty - Cqty) = #{itemQty} THEN '60' ELSE StatusCd END 
			     , IsCancel = CASE WHEN (Qty - Cqty) = #{itemQty} THEN 1    ELSE IsCancel END 
			     , Cqty     = CASE WHEN (SELECT COUNT(*) 
			                             FROM   istyle24_Wmsif.dbo.TB_IF_RecallExceptionItem B 
			                             WHERE  B.OrderDtlNo = #{ordDtlNo} 
			                             AND    B.ProductNo = #{productNo} 
			                             AND    B.SKUCode = #{optCd}   
			                             AND    B.ReasonCode IN ('08','10') ) > 0 THEN Cqty 
			                       ELSE Cqty + #{itemQty} 
			                       END
			     , DateLastModified = GETDATE()
			     , ExceptionDesc = #{ExceptionDesc}
			WHERE 1=1
			  AND OrderDtlNo = #{ordDtlNo}
			  AND ProductNo  = #{productNo}
			  AND SKUCode    = #{optCd}
	</update>
	
	<!-- WMS 전체취소 여부 -->
	<select id="getWmsAllCancelYn" parameterType="WmsDelivery" resultType="String">
		/* TscWmsDelivery.getWmsAllCancelYn */
	    <![CDATA[
		SELECT CASE WHEN COUNT(*) > 0 
		            THEN 'N' 
		            ELSE 'Y' 
		            END AS CHK_YN 
		  FROM istyle24_Wmsif.dbo.TB_IF_DeliveryOrder A,  
		       istyle24_Wmsif.dbo.TB_IF_DeliveryOrderItem B
		 WHERE A.DeliveryOrderNo = B.DeliveryOrderNo 
		   AND A.OrderNo   = #{ordNo}
		   AND B.StatusCd != '60'
		   AND (B.GiftYn  != 'Y' OR B.GiftYn IS NULL )
		 ]]>      
   </select>
   	
	<!-- WMS 배송 사은품 취소  -->
	<update id="updateWmsIfDeliveryOrderGiftCancel" parameterType="WmsDelivery">
		/* TscWmsDelivery.updateWmsIfDeliveryOrderGiftCancel */
		UPDATE istyle24_Wmsif.dbo.TB_IF_DeliveryOrderItem 
		SET    StatusCd = '60'
			 , IsCancel = 1
		WHERE  DeliveryOrderNo = (SELECT MAX(A.DeliveryOrderNo) AS DeliveryOrderNo
				                    FROM istyle24_Wmsif.dbo.TB_IF_DeliveryOrder A,  
				                         istyle24_Wmsif.dbo.TB_IF_DeliveryOrderItem B
				                   WHERE A.DeliveryOrderNo = B.DeliveryOrderNo 
				                     AND A.OrderNo   = #{ordNo}
				                     AND B.StatusCd != '60'
				                     AND B.GiftYn    = 'Y')
	</update>   	

	<!-- 출고처지정 지연재고 삭제     -->
	<delete id="deleteWmsTbIfDelifixQty" parameterType="WmsDelivery" >
		/*TscWmsDelivery.deleteWmsTbIfDelifixQty*/
		DELETE FROM iSTYLE24_WmsIf.dbo.TB_IF_DELIFIX_QTY 
		WHERE  OrderNo = #{ordNo}
	</delete>
	
	<!-- 출고처지정 지연재고 변경  -->
	<update id="updateWmsTbIfDelifixQty" parameterType="WmsDelivery">
		/* TscWmsDelivery.updateWmsTbIfDelifixQty */
		UPDATE iSTYLE24_WmsIf.dbo.TB_IF_DELIFIX_QTY 
		SET    Qty = Qty - #{itemQty}
		WHERE  OrderNo    = #{ordNo}
		  AND  OrderDtlNo = #{ordDtlNo}
		  AND  SKUCode    = #{optCd} 
		  AND  ProductNo  = #{productNo}
	</update>
	
	<!-- WMS 회수지시 삭제  -->
	<update id="updateWmsRecallCancel" parameterType="WmsDelivery">
		/* TscWmsDelivery.updateWmsRecallCancel */
		UPDATE iSTYLE24_WmsIf.dbo.TB_IF_RecallDelivery 
		SET    IsDeleted    = 1
			 , DateCreated  = GETDATE()
		WHERE  OrderExceptionNo  = #{ordChgSq}
		  AND  OrderNo           = #{ordNo}
		  AND  IsDeleted         = 0
	</update>

	<!-- WMS 회수지시 여부 조회 -->
	<select id="getWmsWithdrawRequestInfo" parameterType="WmsDelivery" resultType="WmsDelivery" timeout="300">
		/* TscWmsDelivery.getWmsWithdrawRequestInfo */
		SELECT RecallNo
		     , OrderExceptionNo
		     , RecallTypeCd
		     , Receiver
		     , ReceiverTelNum
		     , ReceiverCellNum
		     , DeliveryPostalCode
		     , DeliveryAddr1
		     , DeliveryAddr2
		     , OrderNo
		     , CustomerName
		     , LoginId
		     , CSMemo
		     , DateCSMemoUpdated
		     , WMSMemo
		     , DateWMSMemoUpdated
		     , DateCreated
		     , IsDeleted
		     , DateDeleted
		     , DeliveryAddrNew
		     , RecallRequestYn
		     , IfStat
		     , IfDate
		     , encloseFee
		     , CodFee
		  FROM iSTYLE24_WmsIf.dbo.TB_IF_RECALLDELIVERY
		 WHERE OrderExceptionNo = #{OrderExceptionNo}
	</select>
	
	<!-- WMS 배송 주소변경  -->
	<update id="updateWmsDeliveryChangeAddr" parameterType="Order">
		/* TscWmsDelivery.updateWmsDeliveryChangeAddr */
		UPDATE istyle24_Wmsif.dbo.TB_IF_DeliveryOrder  
		SET    DeliveryPostalCode = #{recipZipcode}
		     , DeliveryAddr1      = #{recipBaseAddr}
		     , DeliveryAddr2      = #{recipDtlAddr}
		     , IsChangeAddress    = 1
		     , DeliveryMemo       = #{delvMemo}
		 WHERE DeliveryOrderNo IN
		<foreach collection="deliveryOrderNoArr" item="item" index="index"  open="(" close=")" separator=",">
			#{item}
		</foreach>
		   AND IsCancel = 0
	</update>

	<!-- WMS 회수 주소변경  -->
	<update id="updateWmsRecallChangeAddr" parameterType="Order">
		/* TscWmsDelivery.updateWmsRecallChangeAddr */
		UPDATE istyle24_Wmsif.dbo.TB_IF_RecallDelivery 
		SET    DeliveryPostalCode = #{chgerZipcode}
		     , DeliveryAddr1      = #{chgerBaseAddr}
		     , DeliveryAddr2      = #{chgerDtlAddr}
		     , chgerRtnMemo       = #{chgerRtnMemo}
		     , DateCSMemoUpdated  = GETDATE() 
		 WHERE OrderExceptionNo = #{ordChgSq}
		   AND OrderNo   = #{ordNo}
		   AND IsDeleted = 0
	</update>

	<!-- WMS 배송메모 변경  -->
	<update id="updateWmsDeliveryMemo" parameterType="Order">
		/* TscWmsDelivery.updateWmsDeliveryMemo */
		UPDATE istyle24_Wmsif.dbo.TB_IF_DeliveryOrder
		   SET IsChangeAddress = 1
		     , DeliveryMemo = #{delvMemo}
		 WHERE DeliveryOrderNo IN
		<foreach collection="deliveryOrderNoArr" item="item" index="index"  open="(" close=")" separator=",">
			#{item}
		</foreach>
		   AND IsCancel = 0
	</update>

	<!-- WMS 회수메모 변경  -->
	<update id="updateWmsRecallChangeMemo" parameterType="Order">
		/* TscWmsDelivery.updateWmsRecallChangeMemo */
		UPDATE istyle24_Wmsif.dbo.TB_IF_RecallDelivery
		   SET chgerRtnMemo = #{chgerRtnMemo}
			 , DateCSMemoUpdated = GETDATE()
		 WHERE OrderExceptionNo = #{ordChgSq}
		   AND OrderNo = #{ordNo}
		   AND IsDeleted = 0
	</update>
	
	<!-- WMS 배송주문번호 조회 -->
	<select id="getDeliveryOrderNo" parameterType="Order" resultType="Order">
		/* TscWmsDelivery.getDeliveryOrderNo */
		SELECT DeliveryOrderNo
		  FROM istyle24_Wmsif.dbo.TB_IF_DeliveryOrderItem
		 WHERE OrderDtlNo IN
			<foreach collection="ordDtlNoArr" item="item" index="index"  open="(" close=")" separator=",">
		       #{item}
			</foreach>
	</select>
	
	<!-- WMS 주소수정 가능 여부 체크 -->
	<select id="getCanModifyAddr" parameterType="Order" resultType="int">
		/* TscWmsDelivery.getCanModifyAddr */
		SELECT CASE StatusCd WHEN '10' THEN 1
		                     ELSE 0
		        END AS CAN_MODIFY_ADDR_YN
		  FROM istyle24_Wmsif.dbo.TB_IF_DeliveryOrder
		 WHERE DeliveryOrderNo IN
		<foreach collection="deliveryOrderNoArr" item="item" index="index"  open="(" close=")" separator=",">
			#{item}
		</foreach>
	</select>
	
	<!-- WMS 택배사 수정 -->
	<update id="updateWmsDeliveryShipComp" parameterType="Order">
		UPDATE istyle24_Wmsif.dbo.TB_IF_DeliveryOrder
		   SET LogisticsNo = #{shipCompCd}
		     , LogisticsName = #{shipCompNm}
		 WHERE DeliveryOrderNo IN
		<foreach collection="deliveryOrderNoArr" item="item" index="index"  open="(" close=")" separator=",">
			#{item}
		</foreach>
		   AND IsCancel = 0
		   AND LogisticsNo != #{shipCompCd}
	</update>
</mapper>

