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

	<!-- 무통장입금 입금기한 만료 주문 조회 -->
	<select id="getOrderDepositExpirationList" parameterType="Order" resultType="Order">
		/* TsbOrder.getOrderDepositExpirationList : 무통장입금 입금기한 만료 주문 조회 */
		SELECT OD.ORD_NO
		FROM   TB_ORDER_DETAIL OD
		INNER  JOIN TB_PAYMENT PAY
		ON     OD.ORD_NO = PAY.ORD_NO
		WHERE  OD.ORD_DTL_STAT = 'G013_10'
		AND    PAY.PAY_STAT = 'G016_10'
		AND    PAY.PG_GB = 'KCP'
		AND    PAY.PAY_MEANS = 'G014_20'
		AND    PAY.VA_DEADLINE <![CDATA[ < ]]> NOW()
		GROUP BY OD.ORD_NO
	</select>

	<!-- 선물하기 배송지 등록 기간 만료 주문 취소 -->
	<select id="getOrderGiftExpirationList" parameterType="Order" resultType="Order">
		/* TsbOrder.getOrderGiftExpirationList : 선물하기 배송지 등록 기간 만료 주문 취소 */
		SELECT O.ORD_NO
		     , O.CUST_NO
		FROM   TB_ORDER O
		INNER  JOIN TB_ORDER_DETAIL OD
		ON     OD.ORD_NO = O.ORD_NO
		WHERE  1=1
		AND    OD.GIFT_PACK_YN = 'Y'
		AND    OD.GIFT_ADDR_INP_YN = 'N'
		AND    OD.ORD_DTL_STAT = 'G013_20'
		AND    O.PAY_DT <![CDATA[ < ]]> ADDDATE(NOW(), INTERVAL -3 DAY)
		GROUP  BY O.ORD_NO
	</select>

	<!-- 무통장입금 만료대기자 리스트 조회 -->
	<select id="getOrderDepositWaitingCustomerList" parameterType="Order" resultType="Order">
		/* TsbOrder.getOrderDepositWaitingCustomerList : 무통장입금 만료대기자 리스트 조회 */
		SELECT O.ORD_NO
		     , IFNULL(O.CUST_NO, 0) AS CUST_NO
		     , O.ORD_NM
		     , O.ORD_PHNNO
		     , P.PAY_AMT
		     , P.VA_NO
		     , DATE_FORMAT(P.VA_DEADLINE, '%Y.%m.%d %H:%i:%S') as VA_DEADLINE
		     , FN_GET_CODE_NM('G940', P.VA_BANK) AS BANK_NM
		     , OD.GOODS_CNT
		     , G.GOODS_NM
		FROM   TB_PAYMENT P
		INNER  JOIN TB_ORDER O
		ON     P.ORD_NO = O.ORD_NO
		INNER  JOIN (SELECT OD.ORD_NO
		                  , COUNT(1) - 1 AS GOODS_CNT
		                  , MAX(GOODS_CD) AS GOODS_CD
		             FROM   TB_ORDER_DETAIL OD
		             WHERE  1=1
		             AND    ORD_DTL_STAT = 'G013_10'
		             GROUP  BY ORD_NO ) OD
		ON     OD.ORD_NO = O.ORD_NO
		INNER  JOIN TB_GOODS G
		ON     OD.GOODS_CD = G.GOODS_CD
		LEFT   OUTER JOIN TB_PAYMENT P2
		ON     P.ORD_NO = P2.ORD_NO
		AND    P2.PAY_STAT = 'G016_98'
		WHERE  1=1
		AND    P2.ORD_NO IS NULL
		AND    P.PAY_STAT = 'G016_10'
		AND    CAST(P.VA_DEADLINE AS DATE) = CAST(NOW() AS DATE)
	</select>
</mapper>