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

	<select id="selectConsultingList" resultType="java.util.HashMap">
		/* MonitoringConsultingMapper.selectConsultingList */
		<![CDATA[
		SELECT
			(SELECT @ROWNUM := @ROWNUM + 1) AS 'rownum'
			, A.*
		FROM
		(
			SELECT
				Z.TOKEN AS 'token'
				, Z.OPR_MNG_CODE AS 'oprMngCode'
				, (SELECT IFNULL(u.SERVICE_GROUP_NAME, '') FROM TB_SERVICE_GROUP u WHERE u.SERVICE_GROUP = SUBSTRING_INDEX(Z.OPR_MNG_CODE, '_', -1)) AS 'oprMngCodeName'
				, CONCAT(IFNULL(Z.CALL_TIME, ''), '초') AS 'callTime'
				, DATE_FORMAT(Z.CALL_INIT_DATE, '%Y-%m-%d %H:%i') AS 'callInitDate'
				, IFNULL((SELECT v.CODE_NAME FROM TB_BOT_COMMON_CODE v WHERE v.MAJOR_CODE = 'CALL_END_CONVERSION' AND v.MINOR_CODE = Z.CALL_END_CONVERSION AND v.CUST_CODE = SUBSTRING_INDEX(Z.OPR_MNG_CODE, '_', 1)), '') AS 'callEndConversionName'
				, IFNULL((SELECT t.CODE_NAME FROM TB_BOT_COMMON_CODE t WHERE t.MAJOR_CODE = 'CALL_END_REASON' AND t.MINOR_CODE = Z.CALL_END_REASON AND t.CUST_CODE = SUBSTRING_INDEX(Z.OPR_MNG_CODE, '_', 1)), '') AS 'callEndReasonName'
				, (SELECT CONCAT(callEndConversionName, '/', callEndReasonName) FROM DUAL) AS 'callEndName'
				, Z.TEL_NO AS 'telNo'
				, Z.DNIS_NO AS 'dnisNo'
				, (SELECT IFNULL(GROUP_CONCAT(DISTINCT(SCENARIO_NAME)), '') FROM TB_CALL_USER_LOG WHERE TOKEN = Z.TOKEN AND TALK_SEQ <> 1) AS 'scenarioName'
				, (SELECT COUNT(*) FROM TB_CALL_USER_LOG WHERE TOKEN = Z.TOKEN AND TALK_SEQ <> 1) AS 'userTalkCnt'
				, (SELECT COUNT(*) FROM TB_CALL_BOT_LOG WHERE TOKEN = Z.TOKEN) AS 'botTalkCnt'
			FROM TB_CALL_INFO Z
			LEFT JOIN TB_CALL_USER_LOG X
				ON X.TOKEN = Z.TOKEN
			WHERE Z.IN_CHANNEL_TYPE = 'VOICE'
			]]>
		<choose>
			<when test="callInitDateFrom != null and callInitDateFrom != '' and callInitDateTo != null and callInitDateTo != ''">
				<![CDATA[
					  AND Z.CALL_INIT_DATE >= #{callInitDateFrom} AND Z.CALL_INIT_DATE <= #{callInitDateTo}
					]]>
			</when>
			<otherwise>
				<![CDATA[
						AND Z.CALL_INIT_DATE > CURDATE() AND Z.CALL_INIT_DATE < DATE_ADD(CURDATE(), INTERVAL 1 DAY)
					]]>
			</otherwise>
		</choose>
		<if test="oprMngCode != null and oprMngCode != ''">
			<choose>
				<when test="oprMngCode == 'all'">
					<![CDATA[
							AND Z.OPR_MNG_CODE IN
						]]>
					<foreach item="item" index="index" collection="oprMngCodeList" open="(" separator="," close=")">
						#{item}
					</foreach>
				</when>
				<otherwise>
					<![CDATA[
							AND Z.OPR_MNG_CODE = #{oprMngCode}
						]]>
				</otherwise>
			</choose>
		</if>
		<if test="callEndConversion != null and callEndConversion != ''">
			<![CDATA[
					AND Z.CALL_END_CONVERSION = #{callEndConversion}
				]]>
		</if>
		<if test="callEndReason != null and callEndReason != ''">
			<![CDATA[
					AND Z.CALL_END_REASON = #{callEndReason}
				]]>
		</if>
		<if test="telNo != null and telNo != ''">
			<![CDATA[
					AND Z.TEL_NO = #{telNo}
				]]>
		</if>
		<if test="dnisNo != null and dnisNo != ''">
			<![CDATA[
					AND Z.DNIS_NO like CONCAT('%', #{dnisNo}, '%')
				]]>
		</if>
		<if test="scenarioSeq != null and scenarioSeq != ''">
			<![CDATA[
					AND X.SCENARIO_SEQ IN (#{scenarioSeq})
				]]>
		</if>
		<![CDATA[
			GROUP BY Z.TOKEN
			ORDER BY Z.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="selectConsultingListCnt" resultType="int">
		<![CDATA[
		SELECT
		    COUNT(*)
		FROM (
			SELECT
				DISTINCT Z.TOKEN
			FROM TB_CALL_INFO Z
			LEFT JOIN TB_CALL_USER_LOG X ON X.TOKEN = Z.TOKEN
			WHERE Z.IN_CHANNEL_TYPE = 'VOICE'
			]]>
		<choose>
			<when test="callInitDateFrom != null and callInitDateFrom != '' and callInitDateTo != null and callInitDateTo != ''">
				<![CDATA[
					  AND Z.CALL_INIT_DATE >= #{callInitDateFrom} AND Z.CALL_INIT_DATE <= #{callInitDateTo}
					]]>
			</when>
			<otherwise>
				<![CDATA[
						AND Z.CALL_INIT_DATE > CURDATE() AND Z.CALL_INIT_DATE < DATE_ADD(CURDATE(), INTERVAL 1 DAY)
					]]>
			</otherwise>
		</choose>
		<if test="oprMngCode != null and oprMngCode != ''">
			<choose>
				<when test="oprMngCode == 'all'">
					<![CDATA[
							AND Z.OPR_MNG_CODE IN
						]]>
					<foreach item="item" index="index" collection="oprMngCodeList" open="(" separator="," close=")">
						#{item}
					</foreach>
				</when>
				<otherwise>
					<![CDATA[
							AND Z.OPR_MNG_CODE = #{oprMngCode}
						]]>
				</otherwise>
			</choose>
		</if>
		<if test="callEndConversion != null and callEndConversion != ''">
			<![CDATA[
					AND Z.CALL_END_CONVERSION = #{callEndConversion}
				]]>
		</if>
		<if test="callEndReason != null and callEndReason != ''">
			<![CDATA[
					AND Z.CALL_END_REASON = #{callEndReason}
				]]>
		</if>
		<if test="telNo != null and telNo != ''">
			<![CDATA[
					AND Z.TEL_NO = #{telNo}
				]]>
		</if>
		<if test="dnisNo != null and dnisNo != ''">
			<![CDATA[
					AND Z.DNIS_NO like CONCAT('%', #{dnisNo}, '%')
				]]>
		</if>
		<if test="scenarioSeq != null and scenarioSeq != ''">
			<![CDATA[
					AND X.SCENARIO_SEQ IN (#{scenarioSeq})
			]]>
		</if>
		LIMIT 1000001
		) A
	</select>

	<!-- 대화 내용 -->
	<select id="selectRecordCallList" resultType="java.util.HashMap">
		/* MonitoringConsultingMapper.selectRecordCallList */
		SELECT
			T.*
			 , (SELECT COUNT(Z.SEQ) FROM TB_TALK_BOOKMARK Z WHERE Z.TOKEN = T.TOKEN AND Z.TALK_SEQ = T.TALK_SEQ AND Z.LOG_TYPE = T.TYPE AND (Z.MEASURE_TYPE IS NULL OR Z.MEASURE_TYPE = '')) AS COUNT
		    , (SELECT X.SEQ FROM TB_TALK_BOOKMARK X WHERE X.TOKEN = T.TOKEN AND X.TALK_SEQ = T.TALK_SEQ AND X.LOG_TYPE = T.TYPE AND (X.MEASURE_TYPE IS NULL OR X.MEASURE_TYPE = '')) AS SEQ
		FROM (
			SELECT A.BOT_TEXT AS CHAT_TEXT
				, A.TOKEN AS TOKEN
				, A.TALK_SEQ AS TALK_SEQ
				, A.REGIST_DATE AS REGIST_DATE
				, B.REC_FILE_PATH AS recFilePath
				, 'B' AS TYPE
				, 2 AS ORDERING
				, C.OPR_MNG_CODE AS oprMngCode
			FROM TB_CALL_BOT_LOG A
			LEFT JOIN TB_CALL_BOT_REC B ON A.TOKEN = B.TOKEN AND A.TALK_SEQ = B.TALK_SEQ
			LEFT JOIN TB_CALL_INFO C ON A.TOKEN = C.TOKEN
			WHERE A.TOKEN = #{token}
			UNION ALL
			SELECT A.USER_TEXT AS CHAT_TEXT
				, A.TOKEN AS TOKEN
				, A.TALK_SEQ AS TALK_SEQ
				, A.REGIST_DATE AS REGIST_DATE
				, B.REC_FILE_PATH AS recFilePath
				, 'C' AS TYPE
				, 1 AS ORDERING
				, A.OPR_MNG_CODE AS oprMngCode
			FROM TB_CALL_USER_LOG A
			LEFT JOIN TB_CALL_USER_REC B ON A.TOKEN = B.TOKEN AND A.TALK_SEQ = B.TALK_SEQ
			WHERE A.TOKEN = #{token}
			AND A.TALK_SEQ != 1
			) AS T
		ORDER BY T.TALK_SEQ ASC, ORDERING ASC
	</select>

	<select id="selectScenarioSeqAndName" resultType="java.util.HashMap">
		SELECT
		SEQ AS seq
		, SCENARIO_NAME AS scenarioName
		FROM
		TB_SCENARIO_MASTER
		WHERE
		USE_YN = 'Y'
		<if test="oprMngCode != null and oprMngCode != ''">
			<choose>
				<when test="oprMngCode == 'all'">
					<![CDATA[
						AND OPR_MNG_CODE IN
					]]>
					<foreach item="item" index="index" collection="oprMngCodeList" open="(" separator="," close=")">
						#{item}
					</foreach>
				</when>
				<otherwise>
					<![CDATA[
						AND OPR_MNG_CODE = #{oprMngCode}
					]]>
				</otherwise>
			</choose>
		</if>
	</select>

	<select id="selectCommonCodeAndDesc" resultType="java.util.HashMap">
		SELECT
		MAJOR_CODE AS majorCode
		, MINOR_CODE AS minorCode
		, CODE_NAME AS description
		FROM
		TB_BOT_COMMON_CODE
		WHERE 1=1
		<if test="oprMngCode != null and oprMngCode != ''">
			<choose>
				<when test="oprMngCode == 'all'">
					<![CDATA[
						AND CUST_CODE IN
					]]>
					<foreach item="item" index="index" collection="custCodeList" open="(" separator="," close=")">
						#{item}
					</foreach>

					<![CDATA[
						AND OPR_MNG_CODE IN
					]]>
					<foreach item="item" index="index" collection="oprMngCodeUpperList" open="(" separator="," close=")">
						#{item}
					</foreach>
				</when>
				<otherwise>
					<![CDATA[
						AND CUST_CODE = #{custCode}
						AND OPR_MNG_CODE = #{oprMngCodeUpper}
					]]>
				</otherwise>
			</choose>
		</if>
		<if test="majorCode != null and majorCode != ''">
			<![CDATA[
				AND MAJOR_CODE = #{majorCode}
			]]>
		</if>
		GROUP BY MINOR_CODE
	</select>

	<select id="selectUserCallCnt" resultType="int">
		<![CDATA[
		SELECT
		    COUNT(*)
		FROM TB_CALL_INFO
		WHERE IN_CHANNEL_TYPE = 'VOICE'
		]]>
		<if test="telNo != null and telNo != ''">
			<![CDATA[
			AND TEL_NO = #{telNo}
		]]>
		</if>
		<if test="oprMngCode != null and oprMngCode != ''">
			<![CDATA[
			AND OPR_MNG_CODE = #{oprMngCode}
		]]>
		</if>
		<if test="callInitDateFrom != null and callInitDateFrom != '' and callInitDateTo != null and callInitDateTo != ''">
			<![CDATA[
			AND CALL_INIT_DATE > #{callInitDateFrom} AND CALL_INIT_DATE < DATE_ADD(#{callInitDateTo}, INTERVAL 1 DAY)
		]]>
		</if>
	</select>

</mapper>
