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

	<insert id="insertSms" parameterType="hashmap">
		<selectKey resultType="string" keyProperty="id" order="BEFORE">
        	SELECT CONCAT(#{id},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="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>