<?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="kr.co.i2talk.notification">

	<insert id="insertNotification" parameterType="i2c.biz.notification.vo.NotificationVO">
	/* i2c.biz.notification.insertNotification */
		INSERT INTO sns_schedule_message
			(schdl_msg_id, msg_cntn, sndr_id, crt_dttm, link_url, biz_tp_cd, biz_sub_tp_cd
			, tar_obj_id /* 20170608 lsol927 타겟 ID */
			, SEND_SE )
		VALUES 
			(#{schdl_msg_id}, #{msg_cntn}, #{sndr_id}, DATE_FORMAT(SYSDATE(),'%Y%m%d%H%i%s'), #{link_url}, #{biz_tp_cd}, #{biz_sub_tp_cd}
			, #{tar_obj_id} /* 20170608 lsol927 타겟 ID */ 
			, #{send_se})		
	</insert>
	
	<insert id="insertMessage" parameterType="i2c.biz.notification.vo.NotificationVO">
	/* i2c.biz.notification.insertMessage */
		INSERT INTO sns_message			
			(msg_id, msg_cntn, crt_usr_id, crt_dttm, link_url, biz_tp_cd, biz_sub_tp_cd
			, biz_id  /* 20170314 lsol927 추가 메일 및 sms 연결 */ 
			)
		VALUES 
			(#{schdl_msg_id}, #{msg_cntn}, #{sndr_id}, DATE_FORMAT(SYSDATE(),'%Y%m%d%H%i%s'), #{link_url}, #{biz_tp_cd}, #{biz_sub_tp_cd}
			, #{biz_id}  /* 20170314 lsol927 추가 메일 및 sms 연결 */
			)		
	</insert>		
	
	<update id="insertNotificationUser" parameterType="i2c.biz.notification.vo.NotificationVO">
	/* i2c.biz.notification.insertNotificationUser */
<!-- 		MERGE INTO SNS_SCHEDULE_RECV_USER
        USING DUAL
        ON (SCHDL_MSG_ID = #{schdl_msg_id} AND RCVR_ID = #{rcvr_id})
        WHEN MATCHED THEN
            UPDATE SET
            		SND_DTTM = NULL          	
        WHEN NOT MATCHED THEN
            INSERT (
				SCHDL_MSG_ID, RCVR_ID
			) VALUES (
				#{schdl_msg_id}, #{rcvr_id}
			)	 -->	
	    INSERT INTO SNS_SCHEDULE_RECV_USER(SCHDL_MSG_ID, RCVR_ID) 
		         VALUES(#{schdl_msg_id}, #{rcvr_id})
         ON DUPLICATE KEY 
               UPDATE SND_DTTM = NULL
	</update>
	
	<update id="insertAllNotificationUser" parameterType="java.util.Map">
		/* i2c.biz.notification.insertAllNotificationUser */
		INSERT INTO SNS_SCHEDULE_RECV_USER
		(
		    SCHDL_MSG_ID
		    ,RCVR_ID
		)
		SELECT DISTINCT A.*
		FROM 
		(
		  <foreach collection="insertMap" item="item" index="index" separator="UNION ALL">
		  <![CDATA[
		  SELECT 
		    #{item.schdl_msg_id} SCHDL_MSG_ID
		    ,#{item.rcvr_id} RCVR_ID 
		  FROM DUAL
		  ]]>
		  </foreach>
		) A
    </update>
    
    <update id="insertRecvUser" parameterType="i2c.biz.notification.vo.NotificationVO">
<!-- 	/* i2c.biz.notification.insertRecvUser */
		MERGE INTO SNS_RECV_USER
        USING DUAL
        ON (MSG_ID = #{schdl_msg_id} AND USR_ID = #{rcvr_id})
        WHEN MATCHED THEN
            UPDATE SET
            		CONFIRM_DTTM = NULL          	
        WHEN NOT MATCHED THEN
            INSERT (
				RECV_ID, MSG_ID, USR_ID
			) VALUES (
				#{recv_id}, #{schdl_msg_id}, #{rcvr_id}
			)	 -->	
			
	   INSERT INTO SNS_RECV_USER(RECV_ID, MSG_ID, USR_ID) 
	        VALUES (#{recv_id}, #{schdl_msg_id}, #{rcvr_id})
	    ON DUPLICATE KEY 
	        UPDATE CONFIRM_DTTM = NULL	
	</update>

	
	<update id="insertAllRecvUser" parameterType="java.util.Map">
		/* i2c.biz.notification.insertAllRecvUser */
		INSERT INTO SNS_RECV_USER
		(
		    RECV_ID
		    ,MSG_ID
		    ,USR_ID
		)
		SELECT DISTINCT A.*
		FROM 
		(
		  <foreach collection="insertMap" item="item" index="index" separator="UNION ALL">
		  <![CDATA[
		  SELECT 
		    #{item.recv_id} RECV_ID
		    ,#{item.schdl_msg_id} MSG_ID 
		    ,#{item.rcvr_id} USR_ID 
		  FROM DUAL
		  ]]>
		  </foreach>
		) A
    </update>
</mapper>