<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTO Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.icomsys.main_vm.db.mybatis.mapper.ConversationMarkMapper">

	<select id="selectBookmarkList" resultType="java.util.HashMap">
	/* ConversationMarkMapper.selectBookmarkList */
	<![CDATA[
	    SELECT
	        (SELECT @ROWNUM := @ROWNUM + 1) AS 'rownum'
	    	, A.*
	    FROM (
				SELECT
					z.SEQ AS seq
					, z.OPR_MNG_CODE AS oprMngCode
					, z.LOG_TYPE AS logType
					, z.TOKEN AS token
					, z.TALK_SEQ AS talkSeq
					, z.TALK_TEXT AS talkText
					, z.BOOKMARK_ERROR_CODE AS bookmarkErrCode
					, IFNULL((SELECT x.CODE_NAME FROM TB_BOT_COMMON_CODE x WHERE x.major_code = 'BOOKMARK_ERR_CODE' AND x.minor_code = z.BOOKMARK_ERROR_CODE AND x.CUST_CODE = SUBSTRING_INDEX(z.OPR_MNG_CODE, '_', 1)), '') AS bookmarkErrCodeName
					, z.MEASURE_TYPE AS measureType
					, IFNULL((SELECT w.CODE_NAME FROM TB_BOT_COMMON_CODE w WHERE w.major_code = 'MEASURE_TYPE' AND w.minor_code = z.MEASURE_TYPE AND w.CUST_CODE = SUBSTRING_INDEX(z.OPR_MNG_CODE, '_', 1)), '미조치') AS measureTypeName
					, DATE_FORMAT(y.call_init_date, '%Y-%m-%d %H:%i') AS callInitDate
	    			, DATE_FORMAT(z.REGIST_DATE, '%Y-%m-%d %H:%i') AS registDate
				FROM
					TB_TALK_BOOKMARK z
				LEFT JOIN TB_CALL_INFO y ON y.token = z.token
				WHERE y.IN_CHANNEL_TYPE = 'VOICE'
				]]>
					<choose>
						<when test="callInitDateFrom != null and callInitDateFrom != '' and callInitDateTo != null and callInitDateTo != ''">
							<![CDATA[
							  AND y.call_init_date >= #{callInitDateFrom} AND y.call_init_date <= #{callInitDateTo}
							]]>
						</when>
						<otherwise>
							<![CDATA[
								AND y.call_init_date > CURDATE() AND y.call_init_date < DATE_ADD(CURDATE(), INTERVAL 1 DAY)
							]]>
						</otherwise>
					</choose>
					<if test="bookmarkErrCode != null and bookmarkErrCode != ''">
						<![CDATA[
							AND z.BOOKMARK_ERROR_CODE = #{bookmarkErrCode}
						]]>
					</if>
					<if test="measureType != null and measureType != ''">
						<choose>
							<when test="measureType == 'incomplete'">
								<![CDATA[
									AND (z.MEASURE_TYPE IS NULL OR z.MEASURE_TYPE != 'COMPLETE' AND z.MEASURE_TYPE != 'DEL')
	      						]]>
							</when>
							<otherwise>
								<![CDATA[
									AND z.MEASURE_TYPE = #{measureType}
								]]>
							</otherwise>
						</choose>
					</if>
					<if test="talkText != null and talkText != ''">
						<![CDATA[
							AND z.TALK_TEXT like CONCAT('%', #{talkText}, '%')
						]]>
					</if>
				<![CDATA[
				GROUP BY z.SEQ
				ORDER BY y.call_init_date DESC
				]]>
				<choose>
					<when test="page != null and page != ''">
						<if test="page != -1">
							LIMIT ${limit}, ${offset}
						</if>
					</when>
					<otherwise>
						LIMIT 0, 100
					</otherwise>
				</choose>
		<![CDATA[
	    ) A, (SELECT @ROWNUM :=
	      ]]>
	    <choose>
			<when test="page != null and page != ''">
				<choose>
					<when test="page != -1">
						${limit}
					</when>
					<otherwise>
						0
					</otherwise>
				</choose>
			</when>
			<otherwise>
				0
			</otherwise>
		</choose>
		<![CDATA[
	    ) B
		]]>
	</select>

	<select id="selectBookmarkListCnt" resultType="int">
		<![CDATA[
		SELECT
		    COUNT(*)
		FROM (
			SELECT
				DISTINCT z.SEQ
			FROM TB_TALK_BOOKMARK z
				LEFT JOIN TB_CALL_INFO y
					ON y.token = z.token
			WHERE y.IN_CHANNEL_TYPE = 'VOICE'
			]]>
			<choose>
				<when test="callInitDateFrom != null and callInitDateFrom != '' and callInitDateTo != null and callInitDateTo != ''">
					<![CDATA[
					  AND y.call_init_date >= #{callInitDateFrom} AND y.call_init_date <= #{callInitDateTo}
					]]>
				</when>
				<otherwise>
					<![CDATA[
						AND y.call_init_date > CURDATE() AND y.call_init_date < DATE_ADD(CURDATE(), INTERVAL 1 DAY)
					]]>
				</otherwise>
			</choose>
			<if test="bookmarkErrCode != null and bookmarkErrCode != ''">
				<![CDATA[
					AND z.BOOKMARK_ERROR_CODE = #{bookmarkErrCode}
				]]>
			</if>
			<if test="measureType != null and measureType != ''">
				<choose>
					<when test="measureType == 'incomplete'">
						<![CDATA[
							AND z.MEASURE_TYPE IS NULL OR (z.MEASURE_TYPE != 'COMPLETE' AND z.MEASURE_TYPE != 'DEL')
						]]>
					</when>
					<otherwise>
						<![CDATA[
							AND z.MEASURE_TYPE = #{measureType}
						]]>
					</otherwise>
				</choose>
			</if>
			<if test="talkText != null and talkText != ''">
				<![CDATA[
					AND z.TALK_TEXT like CONCAT('%', #{talkText}, '%')
				]]>
			</if>
			LIMIT 1000001
		) A
	</select>

	<insert id="insertBookmark">
		INSERT INTO TB_TALK_BOOKMARK (
			LOG_TYPE
			, OPR_MNG_CODE
			, TOKEN
			, TALK_SEQ
			, TALK_TEXT
			, BOOKMARK_ERROR_CODE
			, REGIST_DATE
			, REGIST_ID
			, UPDATE_DATE
			, UPDATE_ID
		) VALUES (
		          #{logType}
		          , #{oprMngCode}
		          , #{token}
		          , #{talkSeq}
		          , #{talkText}
		          , #{bookmarkErrCode}
		          , NOW()
		          , #{registId}
		          , NOW()
		          , #{updateId}
				)
	</insert>

	<update id="updateBookmark">
		<![CDATA[
		UPDATE TB_TALK_BOOKMARK
		SET
		    MEASURE_TYPE = #{measureType}
			, UPDATE_DATE = NOW()
			, UPDATE_ID = #{updateId}
		]]>
		<if test="bookmarkErrCode != null and bookmarkErrCode != ''">
			, BOOKMARK_ERROR_CODE = #{bookmarkErrCode}
		</if>
		<![CDATA[
		WHERE
		    SEQ = #{seq}
		]]>
	</update>

	<select id="checkBookmarkCnt" resultType="java.util.HashMap">
		<![CDATA[
		SELECT
		    COUNT(*) AS CNT
			, SEQ AS SEQ
		FROM
			TB_TALK_BOOKMARK
		WHERE 1=1
		]]>
		<if test="token != null and token != ''">
			<![CDATA[
			  AND TOKEN = #{token}
			]]>
		</if>
		<if test="talkSeq != null and talkSeq != ''">
			<![CDATA[
			  AND TALK_SEQ = #{talkSeq}
			]]>
		</if>
		<if test="logType != null and logType != ''">
			<![CDATA[
			  AND LOG_TYPE = #{logType}
			]]>
		</if>
		<if test="chkMeasure != null and chkMeasure != ''">
			<![CDATA[
			  AND (MEASURE_TYPE IS NULL OR MEASURE_TYPE = '')
			]]>
		</if>
	</select>

</mapper>
