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

	<select id="selectIntentNameList" resultType="java.util.HashMap">
	/* IntentAnalysisMapper.selectIntentNameList */
	<![CDATA[
		SELECT
		    SEQ AS seq
			, IFNULL(IF_MAPPING_ID, 1) AS ifMappingId
			, INTENT_NAME AS intentName
			, OPR_MNG_CODE AS oprMngCode
			, USE_YN AS useYn
		FROM
		    TB_INTENT_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>
		  <if test="intentGroupCode != null and intentGroupCode != ''">
			  AND CATEGORY = #{intentGroupCode}
		  </if>
		ORDER BY OPR_MNG_CODE, INTENT_NAME ASC
	</select>

	<select id="selectServiceGroupUpper" resultType="java.lang.String">
		SELECT
		    SERVICE_GROUP_UPPER
		FROM
		    TB_SERVICE_GROUP
		WHERE SERVICE_GROUP = #{serviceGroup}
	</select>

	<select id="selectOprByServiceCode" resultType="java.lang.String">
		SELECT
			OPR_MNG_CODE
		FROM
			TB_ICS_OPR_MNG_INFO
		WHERE SERVICE_CODE = #{serviceGroup}
	</select>

	<select id="selectIntentAnalysisList" resultType="java.util.HashMap">
		<![CDATA[
			SELECT
				(SELECT @ROWNUM := @ROWNUM + 1) AS 'rownum'
				, A.*
			FROM (
				SELECT
			  		(SELECT IFNULL(u.SERVICE_GROUP_NAME, '') FROM TB_SERVICE_GROUP u WHERE u.SERVICE_GROUP = SUBSTRING_INDEX(z.OPR_MNG_CODE, '_', -1)) AS 'oprMngCodeName'
					, z.TOKEN AS token
			  		, z.TALK_SEQ AS talkSeq
					, z.USER_TEXT AS userText
					, IFNULL(z.IF_MAPPING_ID, '') AS intentId
					, IFNULL((SELECT v.INTENT_NAME FROM TB_INTENT_MASTER v WHERE v.SEQ = z.INTENT_ID), '-') AS intentName
					, IFNULL(ROUND(z.SCORE, 1), '-') AS score
					, IFNULL(z.INTENT_RESULT_CODE, '') AS intentResultCode
					, IFNULL((SELECT x.CODE_NAME FROM TB_BOT_COMMON_CODE x WHERE x.major_code = 'INTENT_RESULT_CODE' AND x.minor_code = z.INTENT_RESULT_CODE AND x.CUST_CODE = SUBSTRING_INDEX(z.OPR_MNG_CODE, '_', 1)), '') AS intentResultCodeName
					, DATE_FORMAT(y.call_init_date, '%Y-%m-%d %H:%i') AS callInitDate
				FROM
					TB_CALL_USER_LOG z
				LEFT JOIN TB_CALL_INFO y ON z.TOKEN = y.TOKEN AND z.OPR_MNG_CODE = y.OPR_MNG_CODE
				WHERE z.USER_TEXT != ''
					AND z.INTENT_RESULT_CODE IS NOT NULL
					AND y.IN_CHANNEL_TYPE = 'VOICE'
				]]>
				<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="callInitDateFrom != null and callInitDateFrom != '' and callInitDateTo != null and callInitDateTo != ''">
					<![CDATA[
					AND y.call_init_date >= #{callInitDateFrom} AND y.call_init_date <= #{callInitDateTo}
					]]>
				</if>
				<if test="intentResultCode != null and intentResultCode != ''">
					AND z.INTENT_RESULT_CODE = #{intentResultCode}
				</if>
				<if test="scoreFrom != null and scoreFrom != ''">
					<![CDATA[
					AND z.SCORE >= CAST(#{scoreFrom} AS DECIMAL(4,1))
			  		]]>
				</if>
				<if test="scoreTo != null and scoreTo != ''">
					<![CDATA[
					AND z.SCORE <= CAST(#{scoreTo} AS DECIMAL(4,1))
			  		]]>
				</if>
				<choose>
					<when test="intentIdList.size > 0 or (chkIntentAll != null and chkIntentAll != '')">
						AND
						<if test='intentIdList.contains("00000") or chkIntentAll == "Y"'>
							(
						</if>
						z.IF_MAPPING_ID IN
						<choose>
							<when test='chkIntentAll == "Y"'>
								<foreach item="item" index="index" collection="allIntentIdList" open="(" separator="," close=")">
									#{item}
								</foreach>
								OR z.IF_MAPPING_ID IS NULL)
							</when>
							<otherwise>
								<foreach item="item" index="index" collection="intentIdList" open="(" separator="," close=")">
									#{item}
								</foreach>
								<if test="intentIdList.contains('00000')">
									OR z.IF_MAPPING_ID IS NULL)
								</if>
							</otherwise>
						</choose>
					</when>
					<otherwise>
						AND 1=0
					</otherwise>
				</choose>
		<![CDATA[
				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="selectIntentAnalysisListCnt" resultType="int">
		<![CDATA[
		SELECT
		    COUNT(*)
		FROM (
			SELECT
				z.TOKEN
			FROM TB_CALL_USER_LOG z
				LEFT JOIN TB_CALL_INFO y
					ON z.TOKEN = y.TOKEN
						   AND z.OPR_MNG_CODE = y.OPR_MNG_CODE
			WHERE z.USER_TEXT != ''
				AND z.INTENT_RESULT_CODE IS NOT NULL
				AND y.IN_CHANNEL_TYPE = 'VOICE'
			]]>
			<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="callInitDateFrom != null and callInitDateFrom != '' and callInitDateTo != null and callInitDateTo != ''">
				<![CDATA[
				AND y.call_init_date >= #{callInitDateFrom} AND y.call_init_date <= #{callInitDateTo}
				]]>
			</if>
			<if test="intentResultCode != null and intentResultCode != ''">
				AND z.INTENT_RESULT_CODE = #{intentResultCode}
			</if>
			<if test="scoreFrom != null and scoreFrom != ''">
				<![CDATA[
				AND z.SCORE >= CAST(#{scoreFrom} AS DECIMAL(4,1))
				]]>
			</if>
			<if test="scoreTo != null and scoreTo != ''">
				<![CDATA[
				AND z.SCORE <= CAST(#{scoreTo} AS DECIMAL(4,1))
				]]>
			</if>
			<choose>
				<when test="intentIdList.size > 0 or (chkIntentAll != null and chkIntentAll != '')">
					AND
					<if test='intentIdList.contains("00000") or chkIntentAll == "Y"'>
						(
					</if>
					z.IF_MAPPING_ID IN
					<choose>
						<when test='chkIntentAll == "Y"'>
							<foreach item="item" index="index" collection="allIntentIdList" open="(" separator="," close=")">
								#{item}
							</foreach>
							OR z.IF_MAPPING_ID IS NULL)
						</when>
						<otherwise>
							<foreach item="item" index="index" collection="intentIdList" open="(" separator="," close=")">
								#{item}
							</foreach>
							<if test="intentIdList.contains('00000')">
								OR z.IF_MAPPING_ID IS NULL)
							</if>
						</otherwise>
					</choose>
				</when>
				<otherwise>
					AND 1=0
				</otherwise>
			</choose>
			LIMIT 1000001
		) A
	</select>

</mapper>
