<?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.push">

	<select id="kr.co.i2talk.push.listSnsGroupChat"
		parameterType="Map"
		resultType="i2c.cmm.base.CommonResultMap">
/* kr.co.i2talk.push.listSnsGroupChat */
SELECT
	chat_id,
	chat_room_id,
	occupant_id,
	FN_GET_USR_NM(occupant_id) usr_nm,
	msg_type,
	cntn,
	snd_dttm
FROM
	sns_group_chat
WHERE
	chat_room_id = #{chat_room_id}
ORDER BY
	chat_room_id,
	snd_dttm
	</select>
	
	
	<select id="kr.co.i2talk.push.listPrevGroupChat"
		parameterType="Map"
		resultType="i2c.cmm.base.CommonResultMap">
/* kr.co.i2talk.push.listPrevGroupChat */
WITH AA AS (
	SELECT
		ROW_NUMBER() OVER(ORDER BY SND_DTTM DESC) RN,/* 역순 주의 : 최근 대화 10개 */
		chat_id,
		chat_room_id,
		occupant_id,
		FN_GET_USR_NM(occupant_id) usr_nm,
		msg_type,
		cntn,
		snd_dttm
	FROM
		sns_group_chat
	WHERE
		chat_room_id = #{chat_room_id}
	ORDER BY
		SND_DTTM, RN DESC
)
SELECT
*
FROM
	AA
WHERE
	<if test="@i2c.cmm.util.StringUtil@isEmpty(continue_key)">
		RN &lt;= #{limit}
	</if>
	<if test="@i2c.cmm.util.StringUtil@isNotEmpty(continue_key)">
		RN &lt;= (SELECT RN FROM AA WHERE chat_id = #{continue_key}) + #{limit}
		AND RN &gt; (SELECT RN FROM AA WHERE chat_id = #{continue_key})
	</if>
	
	</select>
</mapper>
