<?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="kr.co.uplus.ez.api.channelMgt.ChannelMgtMapper">

	<select id="selectTmpltTotalCnt" parameterType="kr.co.uplus.ez.api.channelMgt.dto.TmpltListReqDto" resultType="int">
		SELECT COUNT(*)
		  from hubez_common.EZ_ATLK_TMPLT eat
			 inner join hubez_common.EZ_KKO_CHNL ekc
				on ekc.SNDRPROF_KEY = eat.SNDRPROF_KEY
			 inner join hubez_common.EZ_SVC_USER esu
				on esu.USER_SEQ = eat.USER_SEQ
			 inner join hubez_common.EZ_CUST_INFO eci
				on eci.CUST_SEQ = esu.CUST_SEQ
		  <include refid="tmpltListCondition"></include>
	</select>
	<select id="selectTmpltList" parameterType="kr.co.uplus.ez.api.channelMgt.dto.TmpltListReqDto" resultType="kr.co.uplus.ez.api.channelMgt.dto.TmpltInfo">
		SELECT
		  @ROWNUM := @ROWNUM + 1 AS NO,
		  A.custNm,
		  A.userInfo,
		  A.bRegNo,
		  A.tmpltCd,
		  A.tmpltNm,
		  (SELECT  DTL_CD_NM  FROM hubez_common.EZ_CD_DTL  WHERE GRP_CD = 'TMPLT_TP_CD' AND DTL_CD=A.TMPLT_TP_CD) AS tmpltType,
		  (SELECT  DTL_CD_NM  FROM hubez_common.EZ_CD_DTL WHERE GRP_CD = 'TMPLT_STTUS_CD' AND DTL_CD=A.TMPLT_STTUS_CD) AS stat,
		  A.returnReason,
		  A.sendProfile,
		  DATE_FORMAT(A.CHG_DT, '%Y-%m-%d') As lastChgDt,
		  A.sndrprofKey,
		  A.tmpltKey
	   FROM (
		  SELECT eci.CUST_NM AS custNm,
		  	 CONCAT(esu.USER_ID,"(",esu.USER_NM,")")  AS userInfo,
		  	 esu.USER_NM AS userNm,
			 eci.BIZRNO AS bRegNo,
			 eat.TMPLT_CD  AS tmpltCd,
			 eat.TMPLT_NM  AS tmpltNm,
			 eat.TMPLT_TP_CD,
			 eat.TMPLT_STTUS_CD,
			 eat.REJCT_RSN  AS returnReason,
			 eat.SNDRPROF_KEY AS sendProfile,
			 eat.CHG_DT,
			 eat.SNDRPROF_KEY AS sndrprofKey,
			 eat.TMPLT_KEY AS tmpltKey
		  from hubez_common.EZ_ATLK_TMPLT eat
			 inner join hubez_common.EZ_KKO_CHNL ekc
				on ekc.SNDRPROF_KEY = eat.SNDRPROF_KEY
			 inner join hubez_common.EZ_SVC_USER esu
				on esu.USER_SEQ = eat.USER_SEQ
			 inner join hubez_common.EZ_CUST_INFO eci
				on eci.CUST_SEQ = esu.CUST_SEQ
		  <include refid="tmpltListCondition"></include>
		  ORDER BY eat.CHG_DT DESC
		  LIMIT #{page}, #{pagePerRows}) A,
		  ( SELECT @ROWNUM := #{page} ) AS R
	</select>

	<sql id="tmpltListCondition">
		<if test='searchType1 != null and searchType1 != ""'>
			AND eat.TMPLT_STTUS_CD = #{searchType1}
		</if>
		<if test='searchText1 != null and searchText1 != ""'>
				<choose>
					<when test='searchType2 == "custNm"'><!-- 검색조건 - 고객사명 -->
						and UPPER(eci.CUST_NM) LIKE CONCAT('%' , UPPER(#{searchText1}) , '%')
					</when>
					<when test='searchType2 == "bizNo"'><!-- 검색조건 - 사업자번호 -->
						AND eci.BIZRNO LIKE CONCAT('%', #{searchText1}, '%')
					</when>
					<when test='searchType2 == "tmpltNm"'><!-- 검색조건 - 인증코드 -->
						and UPPER(eat.TMPLT_NM) LIKE CONCAT('%' , UPPER(#{searchText1}) , '%')
					</when>
				</choose>
			</if>
	</sql>

	<select id="selectTmpltListExcel" parameterType="kr.co.uplus.ez.api.channelMgt.dto.TmpltListReqDto" resultType="kr.co.uplus.ez.api.channelMgt.dto.TmpltInfo">
		SELECT
         @ROWNUM := @ROWNUM + 1 AS NO,
         A.custNm,
         A.userInfo,
         A.bRegNo,
         A.tmpltCd,
         A.tmpltNm,
         (SELECT  DTL_CD_NM  FROM hubez_common.EZ_CD_DTL  WHERE GRP_CD = 'TMPLT_TP_CD' AND DTL_CD=A.TMPLT_TP_CD) AS tmpltType,
         (SELECT  DTL_CD_NM  FROM hubez_common.EZ_CD_DTL WHERE GRP_CD = 'TMPLT_STTUS_CD' AND DTL_CD=A.TMPLT_STTUS_CD) AS stat,
         A.returnReason,
         A.sendProfile,
         DATE_FORMAT(A.CHG_DT, '%Y-%m-%d') As lastChgDt
      FROM (
         SELECT eci.CUST_NM AS custNm,
         	CONCAT(esu.USER_ID,"(",esu.USER_NM,")")  AS userInfo,
            eci.BIZRNO AS bRegNo,
            eat.TMPLT_CD  AS tmpltCd,
            eat.TMPLT_NM  AS tmpltNm,
            eat.TMPLT_TP_CD,
            eat.TMPLT_STTUS_CD,
            eat.REJCT_RSN  AS returnReason,
            eat.SNDRPROF_KEY AS sendProfile,
            eat.CHG_DT
         from hubez_common.EZ_ATLK_TMPLT eat
            inner join hubez_common.EZ_KKO_CHNL ekc
               on ekc.SNDRPROF_KEY = eat.SNDRPROF_KEY
            inner join hubez_common.EZ_SVC_USER esu
               on esu.USER_SEQ = eat.USER_SEQ
            inner join hubez_common.EZ_CUST_INFO eci
               on eci.CUST_SEQ = esu.CUST_SEQ
         <include refid="tmpltListCondition"></include>
         ORDER BY eat.CHG_DT DESC
       ) A, ( SELECT @ROWNUM := #{page} ) AS R
	</select>

</mapper>