<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">

<mapper namespace="i2c.biz.admin.renewal.usageStatMng.msgResult.dao.MsgResultDAO">

 	<resultMap    id="msgListVO"  type="i2c.biz.admin.renewal.usageStatMng.vo.MsgListVO" />
    <parameterMap id="msgListVO"  type="i2c.biz.admin.renewal.usageStatMng.vo.MsgListVO" />

 	<resultMap    id="msgSelectVO"  type="i2c.biz.admin.renewal.usageStatMng.vo.MsgSelectVO" />
    <parameterMap id="msgSelectVO"  type="i2c.biz.admin.renewal.usageStatMng.vo.MsgSelectVO" />

	<sql id="listMsgResultSearchSQL">
		<if test='mberNo != null and mberNo != ""'>
          	AND tData.mberNo = #{mberNo}
        </if>
        <if test='orgId != null and orgId != ""'>
          	AND tData.orgId = #{orgId}
        </if>
        <if test='brandSeq != null and brandSeq != ""'>
          	AND tData.brandSeq = #{brandSeq}
        </if>
        <if test='searchGroupId != null and searchGroupId != ""'>
        	AND tData.groupId LIKE CONCAT('%',#{searchGroupId},'%')
        </if>
        <if test='searchExcelDate != null and searchExcelDate != ""'>
        	AND date_format(tData.dateClientReq, '%Y-%m') = #{searchExcelDate}
        </if>
		<if test='searchExcelDate == null or searchExcelDate == ""'>
	        <if test='searchSdate != null and searchSdate != "" and searchEdate != null and searchEdate != ""'>
	          	AND tData.dateClientReq BETWEEN #{searchSdate} AND #{searchEdate}
	        </if>
	        <if test="searchServiceTypeArray != null">
	   			AND tData.serviceType IN
		        <foreach item="item" index="index" collection="searchServiceTypeList"  open="(" separator="," close=")">
		            #{item}
		        </foreach>
			</if>
		</if>
	</sql>

	<sql id = "listMsgResultSubQuerySQL">
		SELECT
		    seq
		     ,'EI' AS agentType
		     ,mber_no AS mberNo
		     ,null AS cmpNm
		     ,org_id AS orgId
		     ,null AS usrNm
		     ,null AS brandSeq
		     ,null AS brandName
		     ,noti_group_id AS groupId
		     ,date_client_req AS dateClientReq
		     ,CASE
		         WHEN msg_type IS NULL THEN service_type
		         ELSE
		             CASE
		                 WHEN kakao_img_url IS NULL OR kakao_img_url = '' THEN msg_type
		                 ELSE '1013'
		                 END
		         END AS serviceType
		     ,CASE
		         WHEN msg_type IS NOT NULL THEN service_type
		         END AS fbServiceType
		     ,msg_cl_type AS msgClType
		     ,tmp_book_yn AS tmpBookYN
		     ,all_cnt AS allCnt
		     ,success_cnt AS successCnt
		     ,failback_cnt AS failbackCnt
		     ,fail_cnt AS failCnt
		     <if test='searchExcelDate != null and searchExcelDate != ""'>
		     	,content AS content
				 </if>
		FROM t_data
		WHERE all_cnt > 0
		  AND date_format(date_client_req, '%Y%m') >= date_format(curdate() + interval -3 month, '%Y%m')
		  AND tmp_book_yn != 'T'
	</sql>

	<select id="cntListMsgResult" parameterMap="msgListVO" resultType="int">
	/* [i2c.biz.admin.renewal.usageStatMng.dao.UsageStatMngDAO.cntMsgResult] 메시지 사용현황 cnt */
		SELECT COUNT(*)
		FROM (
		    <include refid="listMsgResultSubQuerySQL"/>
		    ) tData
		WHERE 1=1
		<include refid="listMsgResultSearchSQL" />
	</select>

	<select id="listMsgResult" parameterMap="msgListVO" resultMap="msgListVO">
	/* [i2c.biz.admin.renewal.usageStatMng.dao.UsageStatMngDAO.listMsgResult] 메시지 사용현황 list */
		SELECT
		    seq
		     ,agentType
		     ,mberNo
		     ,cmpNm
		     ,orgId
		     ,usrNm
		     ,brandSeq
		     ,brandName
		     ,groupId
		     ,dateClientReq
		     ,serviceType
		     ,fbServiceType
		     ,msgClType
		     ,tmpBookYN
		     ,allCnt
		     ,successCnt
		     ,failbackCnt
		     ,failCnt
		<if test='searchExcelDate != null and searchExcelDate != ""'>
		     ,content
		</if>
		FROM (
		    <include refid="listMsgResultSubQuerySQL"/>
		    ) tData
		WHERE 1=1
		<include refid="listMsgResultSearchSQL" />
		ORDER BY tData.dateClientReq DESC
		LIMIT #{firstIndex}, #{recordCountPerPage}
	</select>

	<select id="selectMsgResult" parameterMap="msgSelectVO" resultMap="msgSelectVO">
	/* [i2c.biz.admin.renewal.usageStatMng.dao.UsageStatMngDAO.selectMsgResult] 메시지 상세정보 */
		SELECT
			 seq
			 ,'EI'					AS agentType
			 ,NULL 					AS brandName
			 ,noti_group_id 		AS groupId
			 ,date_client_req		AS dateClientReq
			 ,CASE
					WHEN msg_type IS NULL THEN service_type
					ELSE
						CASE
							WHEN kakao_img_url IS NULL OR kakao_img_url = '' THEN msg_type
							ELSE '1013'
							END
				END AS serviceType
			 ,CASE
				  WHEN msg_type IS NOT NULL THEN service_type
			  END AS fbServiceType
			 ,msg_cl_type			AS msgClType
			 ,tmp_book_yn			AS tmpBookYN
			 ,all_cnt				AS allCnt
			 ,success_cnt			AS successCnt
			 ,failback_cnt			AS failbackCnt
			 ,fail_cnt				AS failCnt
			 ,callback				AS callbackNum
			 ,ars_080_num			AS arsNum
			 ,subject				AS subject
			 ,content				AS content
		FROM
			t_data
		WHERE all_cnt > 0
			AND seq = #{seq}
	</select>

	<sql id="selectMsgResultSearchSQL">
		<if test='agentType == "R"'>
          	AND t_seq = #{seq}
        </if>
		<if test='agentType == "EI"'>
          	AND seq = #{seq}
        </if>
		<if test='searchCondition == "recipientNum" and searchKeyword != null and searchKeyword != ""'>
          	AND recipient_num = #{searchKeyword}
        </if>
	</sql>

	<select id="cntSelectMsgResult" parameterMap="msgSelectVO" resultType="int">
	/* [i2c.biz.admin.renewal.usageStatMng.dao.UsageStatMngDAO.cntSelectMsgResult] 메시지 상세정보 cnt */
		SELECT
			count(0)
		FROM
			${mainTable}
		WHERE 1=1
			<include refid="selectMsgResultSearchSQL" />
	</select>

	<select id="mainListSelectMsgResult" parameterMap="msgSelectVO" resultMap="msgSelectVO">
	/* [i2c.biz.admin.renewal.usageStatMng.dao.UsageStatMngDAO.mainListSelectMsgResult] 메시지 상세정보 1차 list */
		SELECT
			 noti_id				AS notiId
			,recipient_num			AS recipientNum
			<if test='mainTable.startsWith("em_")'>
				,mt_report_code_ib		AS reportCode
				,date_mt_sent			AS startSendDate
			</if>
			<if test='mainTable.startsWith("ita_")'>
				,report_code			AS reportCode
				,date_mt_sent			AS startSendDate
			</if>
			<if test='mainTable.startsWith("rta_")'>
				,report_code			AS reportCode
				,date_sent				AS startSendDate
				,failover_date_sent		AS fbStartSendDate
				,failover_date_rslt		AS fbFinishSendDate
			</if>
			,date_rslt				AS finishSendDate
		FROM
			${mainTable}
		WHERE 1=1
			<include refid="selectMsgResultSearchSQL" />
		ORDER BY date_rslt
		LIMIT #{firstIndex}, #{recordCountPerPage}
	</select>

	<select id="subSelectMsgResult" parameterMap="msgSelectVO" resultMap="msgSelectVO">
	/* [i2c.biz.admin.renewal.usageStatMng.dao.UsageStatMngDAO.mainListSelectMsgResult] 메시지 failback정보 */
		SELECT
			<if test='subTable.startsWith("em_")'>
				 mt_report_code_ib		AS fbReportCode
			</if>
			<if test='subTable.startsWith("ita_")'>
				,report_code			AS fbReportCode
			</if>
			,date_mt_sent			AS fbStartSendDate
			,date_rslt				AS fbFinishSendDate
		FROM
			${subTable}
		WHERE 1=1 AND noti_id = #{notiId}
			<if test='agentType == "R"'>
	          	AND t_seq = #{seq}
	        </if>
			<if test='agentType == "EI"'>
	          	AND seq = #{seq}
	        </if>
	</select>

</mapper>
