<?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="MobileKakao">

	<select id="selectTotalKakaoCount" parameterType="hashmap" resultType="hashmap">
		SELECT COUNT(*)  AS "totalCount"
		  FROM MU_KAKAO AS MK
	INNER JOIN MU_USER AS MU
			ON MK.MU_USER_ID = MU.MU_USER_ID
		   AND MK.USE_YN = 'Y'
		   AND MU.USE_YN = 'Y'
		   <if test="kakaoSearchStartDate != null and kakaoSearchStartDate != '' and kakaoSearchEndDate != null and kakaoSearchEndDate != ''">
		   AND DATE_FORMAT(MK.SEND_DATE, '%Y-%m-%d') &gt;= #{kakaoSearchStartDate}
		   AND DATE_FORMAT(MK.SEND_DATE, '%Y-%m-%d') &lt;= #{kakaoSearchEndDate}
		   </if>
		   <if test="kakaoSearchKeywordParam != null and kakaoSearchKeywordParam != ''">
		   AND MU.USER_NAME LIKE CONCAT('%',TRIM(#{kakaoSearchKeywordParam}),'%')
		   </if>
	</select>
	
	<select id="selectListKakao" parameterType="hashmap" resultType="hashmap">
		SELECT MK.*
		  FROM (
				SELECT MK.*
					  ,CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
				  FROM (
						SELECT MK.MU_KAKAO_ID                                 AS "muKakaoId"
						      ,MU.USER_NAME                                   AS "userName"
							  ,(SELECT MCI.CATEGORY_ITEM_NAME
								  FROM MU_CATEGORY_ITEM AS MCI
								 WHERE MCI.USE_YN = 'Y'
								   AND MCI.MU_CATEGORY_ITEM_ID = MU.NATIONALITY
								 LIMIT 0, 1)                                  AS "nationality"
							  ,DATE_FORMAT(MU.BIRTHDAY, '%Y-%m-%d')           AS "birthday"
							  ,TIMESTAMPDIFF(YEAR, MU.BIRTHDAY, CURDATE()) - (DATE_FORMAT(CURDATE(), '%m%d') <![CDATA[ < ]]> DATE_FORMAT(MU.BIRTHDAY, '%m%d'))
                                                                              AS "age"
							  ,CASE
							   WHEN MU.GENDER = 'M'
							   THEN '남'
							   WHEN MU.GENDER = 'F'
							   THEN '여'
							   ELSE ''
							   END                                            AS "gender"
							  ,IFNULL((SELECT MCI.FILE_PATH
										 FROM MU_CATEGORY_ITEM AS MCI
										WHERE MCI.USE_YN = 'Y'
										  AND MCI.MU_CATEGORY_ITEM_ID = MU.USER_TYPE
										LIMIT 0, 1), MU.USER_TYPE2)           AS "userType"
							  ,(SELECT MKT.TITLE
								  FROM MU_KAKAO_TEMPLATE AS MKT
								 WHERE MKT.USE_YN = 'Y'
								   AND MKT.TEMPLATE_CODE = MK.TEMPLATE_CODE
								 LIMIT 0, 1)                                  AS "kakaoTemplateTitle"
							  ,CASE
							   WHEN MK.POPBILL_STATE = '0'
							   THEN '접수'
							   WHEN MK.POPBILL_STATE = '1'
							   THEN '대기중'
							   WHEN MK.POPBILL_STATE = '2'
							   THEN '처리중'
							   WHEN MK.POPBILL_STATE = '3'
							   THEN '완료'
							   WHEN MK.POPBILL_STATE = '4'
							   THEN '취소'
							   ELSE ''
							   END						                      AS "popbillState"
							  ,MK.POPBILL_RESULT                              AS "popbillResult"
							  ,(SELECT MM.NAME
								  FROM MU_MEMBER AS MM
								 WHERE MM.USE_YN = 'Y'
								   AND MM.MU_MEMBER_ID = MK.REG_ID
								 LIMIT 0, 1)                                  AS "memberName"
							  ,DATE_FORMAT(MK.SEND_DATE, '%Y-%m-%d')          AS "sendDate"
						  FROM MU_KAKAO AS MK
					INNER JOIN MU_USER AS MU
							ON MK.MU_USER_ID = MU.MU_USER_ID
						   AND MK.USE_YN = 'Y'
						   AND MU.USE_YN = 'Y'
						   <if test="kakaoSearchStartDate != null and kakaoSearchStartDate != '' and kakaoSearchEndDate != null and kakaoSearchEndDate != ''">
						   AND DATE_FORMAT(MK.SEND_DATE, '%Y-%m-%d') &gt;= #{kakaoSearchStartDate}
						   AND DATE_FORMAT(MK.SEND_DATE, '%Y-%m-%d') &lt;= #{kakaoSearchEndDate}
						   </if>
						   <if test="kakaoSearchKeywordParam != null and kakaoSearchKeywordParam != ''">
						   AND MU.USER_NAME LIKE CONCAT('%',TRIM(#{kakaoSearchKeywordParam}),'%')
						   </if>
						 <choose>
						 <when test="kakaoSort != null and kakaoSort != ''">
					  ORDER BY ${kakaoSort}
						 </when>
						 <otherwise>
					  ORDER BY MK.SEND_DATE DESC
						 </otherwise>
						 </choose>
						 LIMIT 18446744073709551615
				  ) MK, (SELECT @RNUM:=0) R
				 WHERE 1 = 1
		  ) MK
		 WHERE 1 = 1
		 LIMIT ${kakaoStart}, ${kakaoLimit}
	</select>

	<!-- Kakao 리스트 조회 Template -->
	<select id="selectListKakaoTemplate" parameterType="hashmap" resultType="hashmap">
		SELECT MK.SENDER          AS "sender"
			  ,MK.TEMPLATE_CODE   AS "templateCode"
		      ,MIN(MK.REG_DATE)   AS "minRegDate"
		  FROM MU_KAKAO AS MK
		 WHERE MK.USE_YN = 'Y'
           AND MK.STATE  = #{state}
      GROUP BY MK.SENDER
      		  ,MK.TEMPLATE_CODE
	  ORDER BY minRegDate ASC
	</select>

	<!-- Kakao 리스트 조회  State -->
	<select id="selectListKakaoState" parameterType="hashmap" resultType="hashmap">
		SELECT MK.MU_KAKAO_ID                       AS "muKakaoId"
			  ,MK.STATE                             AS "state"
			  ,MK.USER_NAME                         AS "userName"
			  ,IFNULL(DATE_FORMAT(MK.SEND_DATE, '%Y-%m-%d'), '')	AS "sendDate"
			  ,MK.RECEIVER                          AS "receiver"
			  ,MK.SENDER							AS "sender"
			  ,MK.CONTENT                           AS "content"
			  ,MK.TEMPLATE_CODE						AS "templateCode"
		  FROM MU_KAKAO AS MK
		 WHERE MK.USE_YN = 'Y'
           AND MK.STATE  = #{state}
           AND MK.SENDER = #{sender}
           AND MK.TEMPLATE_CODE = #{templateCode}
	</select>

	<select id="selectListPopbillState" parameterType="hashmap" resultType="hashmap">
	    SELECT MK.RECEIPT_NUMBER		AS "receiptNumber"
	      FROM MU_KAKAO AS MK
	     WHERE MK.USE_YN		 = 'Y'
	       AND MK.STATE		 = 'Y'
	       AND MK.POPBILL_STATE = #{popbillState}
	       AND MK.RECEIVER IS NOT NULL
	       AND MK.RECEIVER NOT IN ('', '010-')
	       AND MK.RECEIPT_NUMBER IS NOT NULL
	  GROUP BY MK.RECEIPT_NUMBER
	</select>

	<select id="selectKakao" parameterType="hashmap" resultType="hashmap">
		SELECT MK.MU_KAKAO_ID                        AS "muKakaoId"
			  ,MK.MU_MEMBER_ID                        AS "muMemberId"
			  ,MK.TITLE                               AS "title"
			  ,MK.CONTENT                             AS "content"
			  ,DATE_FORMAT(MK.OPEN_DATE, '%Y-%m-%d')  AS "openDate"
			  ,DATE_FORMAT(MK.OPEN_DATE, '%H:%i')     AS "openTime"
			  ,DATE_FORMAT(MK.WRITE_DATE, '%Y-%m-%d') AS "writeDate"
		  FROM MU_KAKAO AS MK
		 WHERE MK.USE_YN        = 'Y'
		   AND MK.MU_KAKAO_ID  = #{muKakaoId}
		 LIMIT 0, 1
	</select>

	<insert id="insertKakao" parameterType="hashmap">
		<selectKey resultType="string" keyProperty="id" order="BEFORE">
        	SELECT CONCAT(#{muKakaoId},LPAD((SELECT NEXTVAL(MU_KAKAO_SEQ)), 11, 0))
        </selectKey>
		INSERT INTO MU_KAKAO(
					 MU_KAKAO_ID
					,MU_USER_ID
					,USER_NAME
					,RECEIVER
					,TEMPLATE_CODE
					,CONTENT
					,STATE
					,POPBILL_STATE
					,SENDER
					,WRITE_DATE
					,WRITE_TIME
					,CUD_FLAG
					,USE_YN
					,REG_ID
					,REG_DATE
					,MOD_ID
					,MOD_DATE
					,T_ID
					,T_DATE
		)VALUES(
					 #{id}
					,#{muUserId, jdbcType=VARCHAR}
					,#{userName, jdbcType=VARCHAR}
					,#{phoneNumber, jdbcType=VARCHAR}
		   			,#{templateCode, jdbcType=VARCHAR}
					,#{content, jdbcType=VARCHAR}
					,#{state, jdbcType=VARCHAR}
					,#{popbillState, jdbcType=VARCHAR}
					,#{sender, jdbcType=VARCHAR}
					,NOW()
					,NOW()
					,'C'
					,'Y'
					,#{regId}
					,NOW()
					,#{modId}
					,NOW()
					,#{tId}
					,#{tDate}
		)
	</insert>
	
	<update id="updateKakao" parameterType="hashmap">
		UPDATE MU_KAKAO
		   SET MOD_ID              = #{modId}
			  ,MOD_DATE            = NOW()
			  ,CUD_FLAG            = 'U'
			  ,TITLE               = #{title}
			  ,CONTENT             = #{content}
			  ,OPEN_DATE           = #{openDate}
		 WHERE USE_YN        = 'Y'
		   AND MU_KAKAO_ID  = #{muKakaoId}
	</update>

	<update id="updateKakaoState" parameterType="hashmap">
		UPDATE MU_KAKAO
	  	   SET USE_YN	= 'Y'
		      ,MOD_ID	= #{modId}
		      ,MOD_DATE	= NOW()
		      ,CUD_FLAG	= 'U'
		      ,STATE    = #{chgState, jdbcType=VARCHAR}
		      <choose>
			  <when test="sendDateYn eq 'Y'.toString()">
			  ,SEND_DATE = NOW()
			  </when>
		      <otherwise>
			  </otherwise>
			  </choose>
			  <if test="receiptNum != null and receiptNum != ''">
			  ,RECEIPT_NUMBER = #{receiptNum, jdbcType=VARCHAR}
			  </if>
		WHERE USE_YN	     = 'Y'
		  AND MU_KAKAO_ID    = #{muKakaoId}
		  AND STATE          = #{oldState}
	</update>

	<update id="updateKakaoPopbillState" parameterType="hashmap">
		UPDATE MU_KAKAO
		   SET MOD_ID		  = #{modId}
		   	  ,MOD_DATE		  = NOW()
		   	  ,CUD_FLAG		  = 'U'
		   	  ,POPBILL_STATE  = #{state, jdbcType=VARCHAR}
		   	  ,POPBILL_RESULT = #{result, jdbcType=VARCHAR}
		 WHERE USE_YN		              = 'Y'
		   AND POPBILL_STATE              = #{popbillState}
		   AND RECEIPT_NUMBER             = #{receiptNumber}
		   AND REPLACE(RECEIVER, '-', '') = #{receiveNumber}
		   AND USER_NAME                  = #{receiveName}
		   AND TEMPLATE_CODE              = #{templateCode}
	</update>

</mapper>