<?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.batch.biz.dao.TsbShoplinkerDao">

	<!-- 샵링커 전송 이력 -->
	<insert id="insertShoplinerApiHst" parameterType="ShoplinkerGoods">
		/* TsaShoplinkerDao.insertShoplinerApiHst */
		INSERT INTO SHOPLINKER_SYNC_HST (
		      LOG_SQ
			, API_TYPE
			, API_RESULT
			, API_MESSAGE
			, API_PRODUCT_ID
			, GOODS_CD
			, OPT_CD
			, QTY
			, ORD_DTL_NO
			, XML_TXT
			, REG_NO
			, REG_DT
		)
		VALUES (
		      null
		    , #{apiType}
			, #{apiResult}
			, #{apiMessage}
			, #{apiProductId}
			, #{goodsCd}
			, #{optCd}
			, #{quantity}
			, #{ordDtlNo}
			, #{xmlTxt}
			, #{regNo}
		    , NOW()
		)
	</insert>

	<!-- 재고 전송예정 목록-->
	<select id="getSyncStockList" parameterType="ShoplinkerGoods" resultType="ShoplinkerGoods">
		/*TsbCustomerDao.getSyncStockList*/
		SELECT
			TG.ITEMKIND_CD
			, A.GOODS_CD
			, A.OPT_CD
			, A.STOCK_QTY AS QUANTITY
		FROM   SHOPLINKER_STOCK A
		 INNER JOIN TB_GOODS TG ON A.GOODS_CD = TG.GOODS_CD
		WHERE  A.SEND_YN = 'N'

	</select>

	<!-- 재고 전체 동기화 -->
	<update id="updateSyncStock" timeout="600">
		UPDATE SHOPLINKER_STOCK A
		SET UPD_DT = now()
			, SEND_YN = 'N'
			, STOCK_QTY = ( SELECT VW.CURR_STOCK_QTY  FROM VW_STOCK VW
							WHERE VW.GOODS_CD = A.GOODS_CD  AND VW.OPT_CD = A.OPT_CD)
		WHERE 1=1
		AND EXISTS ( SELECT VW.OPT_CD
		            FROM VW_STOCK VW
		            WHERE VW.GOODS_CD = A.GOODS_CD
		            AND VW.OPT_CD = A.OPT_CD
		            AND VW.CURR_STOCK_QTY != A.STOCK_QTY
		       )
	</update>

	<!-- 재고 정보 수정 -->
	<update id="updateStockInfo" parameterType="ShoplinkerGoods">
		UPDATE SHOPLINKER_STOCK A
		SET SEND_DT = NOW()
			, SEND_YN = #{sendYn}
		WHERE 1=1
		<if test='allUpdYn != null and allUpdYn == "Y"'>
			AND SEND_YN = 'N'
		</if>
		<if test="goodsCd != null and goodsCd != ''">
			AND GOODS_CD = #{goodsCd}
		</if>
		<if test="optCd != null and optCd != ''">
			AND OPT_CD = #{optCd}
		</if>
	</update>

	<!-- 주문수집 배치 - 마지막 배치실행시간 -->
	<select id="getShoplinkerOrderLastBatchInfo" resultType="ShoplinkerSearch">
		SELECT
			DATE_FORMAT(BATCH_STDT, '%H') AS BATCH_ORD_LAST_HOUR
		FROM TB_BATCH_LOG A
		WHERE BATCH_ID = 'cron.shoplinker.order.receive'
		AND DATE(REG_DT) = DATE(NOW())
		AND DATE_FORMAT(REG_DT,'%Y%m%d%H') <![CDATA[<]]> DATE_FORMAT(NOW(),'%Y%m%d%H')
		ORDER BY REG_DT DESC
		LIMIT 1
	</select>

	<!-- 주문수집 날짜 정보-->
	<select id="getOrdDateInfo" resultType="ShoplinkerSearch">
		SELECT
			(SELECT MAX(S.BIZDAY) FROM TB_BIZDAY S WHERE S.BIZDAY <![CDATA[<]]> DATE(NOW()) AND S.BIZDAY_YN = 'Y') AS START_DT
			, A.BIZDAY AS TO_DT
			, A.BIZDAY_YN
		FROM TB_BIZDAY A
		WHERE BIZDAY = DATE(NOW())
	</select>

	<!-- 주문수집 날짜 정보 -테스트기간 30일전 -->
	<select id="getOrdDateInfoTest30" resultType="ShoplinkerSearch">
		SELECT
			(SELECT MAX(S.BIZDAY) FROM TB_BIZDAY S WHERE S.BIZDAY <![CDATA[<]]> A.BIZDAY AND S.BIZDAY_YN = 'Y') AS START_DT
			, A.BIZDAY AS TO_DT
			, A.BIZDAY_YN
		FROM TB_BIZDAY A
		WHERE BIZDAY = DATE(DATE_ADD(NOW(), INTERVAL -30 DAY))
	</select>


</mapper>