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

	<!-- 제휴채널정산 목록 -->
	<select id="getAfLinkFeeList" parameterType="AfLinkFee" resultType="AfLinkFee">
		/* TsaSettle.getAfLinkFeeList */
		SELECT AF_CHANNEL                                         /*상위채널코드*/
		     , FN_GET_CODE_NM('G053',AF_CHANNEL) AS AF_CHANNEL_NM /*상위채널명*/
		     , AF_LINK_CD                                         /*채널코드*/
		     , AF_LINK_NM                                         /*채널명*/
		     , OCCUR_DT                                           /*발생일시*/
		     , ORD_NO                                             /*주문번호*/
		     , ORD_DTL_STAT                                       /*주문상세상태*/
		     , SUM(SELL_AMT)                     AS SELL_AMT      /*판매금액(상품금액.배송비제외)*/
		     , SUM(CPN1_DC_AMT +
		           TMTB1_DC_AMT + TMTB2_DC_AMT +
		           GOODS_CPN_DC_AMT +
		           CART_CPN_DC_AMT)              AS CPN_DC_AMT     /*쿠폰할인금액*/
		     , SUM(PNT_DC_AMT + PRE_PNT_DC_AMT)  AS PNT_DC_AMT     /*포인트할인금액*/
		     , SUM(GFCD_USE_AMT)                 AS GFCD_USE_AMT   /*상품권사용금액*/
		     , SUM(SELL_AMT - (CPN1_DC_AMT + TMTB1_DC_AMT + TMTB2_DC_AMT + GOODS_CPN_DC_AMT + CART_CPN_DC_AMT + PG_CPN_AMT)
		                    - (PNT_DC_AMT + PRE_PNT_DC_AMT)
		                    - GFCD_USE_AMT)      AS PAY_AMT        /*결제금액(VAT포함)*/
		     , SUM(TRUNCATE((SELL_AMT - (CPN1_DC_AMT + TMTB1_DC_AMT + TMTB2_DC_AMT + GOODS_CPN_DC_AMT + CART_CPN_DC_AMT + PG_CPN_AMT)
		                              - (PNT_DC_AMT + PRE_PNT_DC_AMT)
		                              - GFCD_USE_AMT
		                    ) / 1.1,0))          AS SALES_AMT      /*매출금액(VAT제외). 건별로 1.1로 나누고 소숫점을 절사하여 합계를 낸다.*/
		FROM   (
		        SELECT AL.AF_CHANNEL
		             , ODH.AF_LINK_CD                                              /*제휴링크코드*/
		             , AL.AF_LINK_NM
		             , DATE_FORMAT(ODH.REG_DT,'%Y-%m-%d %m:%h:%s') AS OCCUR_DT     /*발생일자*/
		             , ODH.ORD_NO
		             , ODH.ORD_DTL_NO
		             , CASE WHEN ODH.ORD_DTL_STAT = 'G013_20' THEN
		                        FN_GET_CODE_NM('G013',ODH.ORD_DTL_STAT)
		                    ELSE
		                        IFNULL(FN_GET_CODE_NM('G685',OCD.CHG_STAT),'주문취소')
		               END                                         AS ORD_DTL_STAT /*주문상세상태*/
		             , (ODH.ORD_AMT - ODH.CNCL_RTN_AMT)            AS SELL_AMT     /*판매금액(상품금액.배송비제외)*/
		             , ODH.CPN1_DC_AMT                                             /*1차쿠폰(즉시할인쿠폰)할인금액*/
		             , ODH.TMTB1_DC_AMT                                            /*다다익선할인금액(수량)*/
		             , ODH.TMTB2_DC_AMT                                            /*다다익선할인금액(금액)*/
		             , ODH.GOODS_CPN_DC_AMT                                        /*상품쿠폰할인금액*/
		             , ODH.CART_CPN_DC_AMT                                         /*장바구니할인금액*/
		             , 0                                           AS PG_CPN_AMT   /*PG쿠폰금액.TODO:바로 아래 쿼리문으로 대체해야 함*/
		             -- , ODH.PG_CPN_AMT                                              /*PG쿠폰금액*/
		             , ODH.PNT_DC_AMT                                              /*포인트할인금액*/
		             , ODH.PRE_PNT_DC_AMT                                          /*선포인트할인금액*/
		             , ODH.GFCD_USE_AMT                                            /*상품권사용금액*/
		        FROM   TB_ORDER_DETAIL_HST ODH
		        INNER JOIN TB_AF_LINK AL
		                ON ODH.AF_LINK_CD = AL.AF_LINK_CD
		        LEFT OUTER JOIN TB_ORDER_CHANGE_DETAIL OCD
		                     ON ODH.ORD_DTL_NO = OCD.ORD_DTL_NO
		                    AND OCD.CHG_STAT IN ('G685_21','G685_32','G685_42')	/*취소완료,교환완료,반품완료*/
		        WHERE  ODH.ORD_DTL_STAT IN ('G013_20','G013_99') /*결제완료,주문취소*/
		        AND    ODH.REG_DT <![CDATA[>=]]> STR_TO_DATE(#{startDt},'%Y-%m-%d')
		        AND    ODH.REG_DT <![CDATA[<]]> DATE_ADD(STR_TO_DATE(#{endDt},'%Y-%m-%d'),INTERVAL 1 DAY)
		        <if test="ordNo != null and ordNo != ''">
		        AND    ODH.ORD_NO = #{ordNo}
		        </if>
		        <if test="afLinkCd != null and afLinkCd != ''">
		        AND    AL.AF_LINK_CD = #{afLinkCd}
		        </if>
		        <if test="afChannel != null and afChannel != ''">
		        AND    AL.AF_CHANNEL = #{afChannel}
		        </if>
		       ) U
		GROUP  BY AF_CHANNEL, AF_LINK_CD, AF_LINK_NM, OCCUR_DT, ORD_NO, ORD_DTL_STAT
		ORDER  BY AF_CHANNEL, AF_LINK_CD, AF_LINK_NM, OCCUR_DT, ORD_NO, ORD_DTL_STAT
	</select>

</mapper>