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

	<select id="selectTotalSmsCount" parameterType="hashmap" resultType="hashmap">
		SELECT COUNT(*)  AS "totalCount"
		  FROM MU_SMS AS MS
	INNER JOIN MU_USER AS MU
			ON MS.MU_USER_ID = MU.MU_USER_ID
		   AND MS.USE_YN = 'Y'
		   AND MU.USE_YN = 'Y'
		   <if test="smsSearchStartDate != null and smsSearchStartDate != '' and smsSearchEndDate != null and smsSearchEndDate != ''">
		   AND DATE_FORMAT(MS.SEND_DATE, '%Y-%m-%d') &gt;= #{smsSearchStartDate}
		   AND DATE_FORMAT(MS.SEND_DATE, '%Y-%m-%d') &lt;= #{smsSearchEndDate}
		   </if>
		   <if test="smsSearchKeywordParam != null and smsSearchKeywordParam != ''">
		   AND MU.USER_NAME LIKE CONCAT('%',TRIM(#{smsSearchKeywordParam}),'%')
		   </if>
	</select>
	
	<select id="selectListSms" parameterType="hashmap" resultType="hashmap">
		SELECT MS.*
		  FROM (
				SELECT MS.*
					  ,CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
				  FROM (
						SELECT MS.MU_SMS_ID                                   AS "muSmsId"
						      ,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"
							  ,MS.CONTENT                                     AS "smsContent"
							  ,CASE
							   WHEN MS.POPBILL_STATE = '0'
							   THEN '접수'
							   WHEN MS.POPBILL_STATE = '1'
							   THEN '대기중'
							   WHEN MS.POPBILL_STATE = '2'
							   THEN '처리중'
							   WHEN MS.POPBILL_STATE = '3'
							   THEN '완료'
							   WHEN MS.POPBILL_STATE = '4'
							   THEN '취소'
							   ELSE ''
							   END						                      AS "popbillState"
							  ,MS.POPBILL_RESULT                              AS "popbillResult"
							  ,(SELECT MM.NAME
								  FROM MU_MEMBER AS MM
								 WHERE MM.USE_YN = 'Y'
								   AND MM.MU_MEMBER_ID = MS.REG_ID
								 LIMIT 0, 1)                                  AS "memberName"
							  ,DATE_FORMAT(MS.SEND_DATE, '%Y-%m-%d')          AS "sendDate"
						  FROM MU_SMS AS MS
					INNER JOIN MU_USER AS MU
							ON MS.MU_USER_ID = MU.MU_USER_ID
						   AND MS.USE_YN = 'Y'
						   AND MU.USE_YN = 'Y'
						   <if test="smsSearchStartDate != null and smsSearchStartDate != '' and smsSearchEndDate != null and smsSearchEndDate != ''">
						   AND DATE_FORMAT(MS.SEND_DATE, '%Y-%m-%d') &gt;= #{smsSearchStartDate}
						   AND DATE_FORMAT(MS.SEND_DATE, '%Y-%m-%d') &lt;= #{smsSearchEndDate}
						   </if>
						   <if test="smsSearchKeywordParam != null and smsSearchKeywordParam != ''">
						   AND MU.USER_NAME LIKE CONCAT('%',TRIM(#{smsSearchKeywordParam}),'%')
						   </if>
						 <choose>
						 <when test="smsSort != null and smsSort != ''">
					  ORDER BY ${smsSort}
						 </when>
						 <otherwise>
					  ORDER BY MS.SEND_DATE DESC
						 </otherwise>
						 </choose>
						 LIMIT 18446744073709551615
				  ) MS, (SELECT @RNUM:=0) R
				 WHERE 1 = 1
		  ) MS
		 WHERE 1 = 1
		 LIMIT ${smsStart}, ${smsLimit}
	</select>

	<!-- SMS 리스트 조회 TIME-->
	<select id="selectListSmsTime" parameterType="hashmap" resultType="hashmap">
		SELECT MS.SENDER         AS "sender"
			  ,MS.CONTENT        AS "content"
			  ,MS.T_ID           AS "tId"
		      ,MIN(MS.REG_DATE)  AS "minRegDate"
		  FROM MU_SMS AS MS
		 WHERE MS.USE_YN = 'Y'
		   AND MS.STATE  = #{state}
		   AND MS.TYPE   = #{type}
      GROUP BY MS.SENDER
			  ,MS.CONTENT
			  ,MS.T_ID
	  ORDER BY minRegDate ASC
	</select>

	<!-- SMS 리스트 조회 State-->
	<select id="selectListSmsState" parameterType="hashmap" resultType="hashmap">
		SELECT MS.MU_SMS_ID                         AS "muSmsId"
			  ,MS.STATE                             AS "state"
			  ,IFNULL(DATE_FORMAT(MS.SEND_DATE, '%Y-%m-%d'), '')	AS "sendDate"
              ,MS.SENDER                            AS "sender"
			  ,MS.RECEIVER                          AS "receiver"
		      ,MS.USER_NAME                         AS "userName"
			  ,IFNULL(MS.TITLE, '')                 AS "title"
			  ,MS.CONTENT                           AS "content"
		      ,MS.TYPE                              AS "type"
		 FROM MU_SMS AS MS
		WHERE MS.USE_YN = 'Y'
          AND MS.STATE    = #{state}
          AND MS.TYPE     = #{type}
		  AND MS.SENDER   = #{sender}
		  AND MS.CONTENT  = #{content}
	</select>

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

	<select id="selectSms" parameterType="hashmap" resultType="hashmap">
		SELECT MS.MU_SMS_ID    AS "muSmsId"
			  ,MS.TITLE        AS "title"
			  ,MS.CONTENT      AS "content"
		  FROM MU_SMS AS MS
		 WHERE MS.USE_YN        = 'Y'
		   AND MS.MU_SMS_ID  = #{muSmsId}
		 LIMIT 0, 1
	</select>

	<insert id="insertSms" parameterType="hashmap">
		<selectKey resultType="string" keyProperty="id" order="BEFORE">
        	SELECT CONCAT(#{muSmsId},LPAD((SELECT NEXTVAL(MU_SMS_SEQ)), 11, 0))
        </selectKey>
		INSERT INTO MU_SMS(
					 MU_SMS_ID
					,MU_USER_ID
					,USER_NAME
					,RECEIVER
					,TYPE
					,TITLE
					,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}
					,#{smsType, jdbcType=VARCHAR}
		   			,#{title, jdbcType=VARCHAR}
					,#{content, jdbcType=VARCHAR}
					,#{state, jdbcType=VARCHAR}
					,#{popbillState, jdbcType=VARCHAR}
					,#{sender, jdbcType=VARCHAR}
					,NOW()
					,NOW()
					,'C'
					,'Y'
					,#{regId}
					,NOW()
					,#{modId}
					,NOW()
					,#{tId}
					,NOW()
		)
	</insert>

	<insert id="insertSmsConsultingChart" parameterType="hashmap">
		<selectKey resultType="string" keyProperty="id" order="BEFORE">
        	SELECT CONCAT(#{muSmsId},LPAD((SELECT NEXTVAL(MU_SMS_SEQ)), 11, 0))
        </selectKey>
		INSERT INTO MU_SMS(
					 MU_SMS_ID
					,MU_USER_ID
					,USER_NAME
					,RECEIVER
					,TYPE
					,TITLE
					,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}
					,#{smsType, jdbcType=VARCHAR}
		   			,#{title, jdbcType=VARCHAR}
					,#{content, jdbcType=VARCHAR}
					,#{state, jdbcType=VARCHAR}
					,#{popbillState, jdbcType=VARCHAR}
					,#{sender, jdbcType=VARCHAR}
					,NOW()
					,NOW()
					,'C'
					,'Y'
					,#{regId}
					,NOW()
					,#{modId}
					,NOW()
					,#{tId}
					,NOW()
		)
	</insert>

	<update id="updateSms" parameterType="hashmap">
		UPDATE MU_SMS
		   SET MOD_ID              = #{modId}
			  ,MOD_DATE            = NOW()
			  ,CUD_FLAG            = 'U'
			  ,TITLE               = #{title}
			  ,CONTENT             = #{content}
			  ,OPEN_DATE           = #{openDate}
		 WHERE USE_YN        = 'Y'
		   AND MU_SMS_ID  = #{muSmsId}
	</update>

	<update id="updateSmsState" parameterType="hashmap">
		UPDATE MU_SMS
	  	   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>
			  <when test="reserveDateYn eq 'Y'.toString()">
			  ,SEND_DATE = #{reserveDate}
			  </when>
		      <otherwise>
			  </otherwise>
			  </choose>
			  <if test="receiptNum != null and receiptNum != ''">
			  ,RECEIPT_NUMBER = #{receiptNum, jdbcType=VARCHAR}
			  </if>
		WHERE USE_YN	   = 'Y'
		  AND MU_SMS_ID    = #{muSmsId}
		  AND STATE        = #{oldState}
	</update>

	<update id="updateSmsPopbillState" parameterType="hashmap">
		UPDATE MU_SMS
		   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}
	</update>

</mapper>