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

	<select id="selectStatIntentDay" resultType="java.util.HashMap">
	/* StatisticsIntentMapper.selectStatIntentDay */
	<![CDATA[
	SELECT
		Z.OPR_MNG_CODE AS oprMngCode
		, Z.SCORE AS score
		, Z.INTENT_RESULT_CODE AS intentResultCode
		, (SELECT IFNULL(u.service_group_name, '') FROM TB_SERVICE_GROUP u WHERE u.service_group = SUBSTRING_INDEX(Z.OPR_MNG_CODE, '_', -1)) AS oprMngCodeName
	   , DATE_FORMAT(Y.CALL_INIT_DATE, '%Y-%m-%d') AS callInitDate
	FROM TB_CALL_USER_LOG Z
	INNER JOIN TB_CALL_INFO Y
		ON Y.TOKEN = Z.TOKEN
	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 < DATE_ADD(#{callInitDateTo}, INTERVAL 1 DAY)
	]]>
	</if>
		ORDER BY callInitDate DESC
	</select>

	<select id="selectStatIntentItem" resultType="java.util.HashMap">
	/* StatisticsIntentMapper.selectStatIntentItem */
	<![CDATA[
	SELECT
		Z.OPR_MNG_CODE AS oprMngCode
		, IFNULL(Z.IF_MAPPING_ID, '00000') AS intentId
		, IFNULL((SELECT v.INTENT_NAME FROM TB_INTENT_MASTER v WHERE v.SEQ = Z.INTENT_ID), '인텐트 분류 실패') AS intentName
		, IFNULL((SELECT DISTINCT O.CODE_NAME FROM TB_BOT_COMMON_CODE O, TB_INTENT_MASTER P WHERE P.SEQ = Z.INTENT_ID AND P.CATEGORY = O.MINOR_CODE), '-') AS intentGroup
		, Z.SCORE AS score
		, Z.INTENT_RESULT_CODE AS intentResultCode
		, (SELECT IFNULL(u.service_group_name, '') FROM TB_SERVICE_GROUP u WHERE u.service_group = SUBSTRING_INDEX(Z.OPR_MNG_CODE, '_', -1)) AS oprMngCodeName
	   , DATE_FORMAT(Y.CALL_INIT_DATE, '%Y-%m-%d') AS callInitDate
	FROM TB_CALL_USER_LOG Z
	INNER JOIN TB_CALL_INFO Y
		ON Y.TOKEN = Z.TOKEN
	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>
	<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>
	<if test="callInitDateFrom != null and callInitDateFrom != '' and callInitDateTo != null and callInitDateTo != ''">
		<![CDATA[
			AND Y.CALL_INIT_DATE > #{callInitDateFrom} AND Y.CALL_INIT_DATE < DATE_ADD(#{callInitDateTo}, INTERVAL 1 DAY)
		]]>
	</if>
		ORDER BY oprMngCode DESC
	</select>


</mapper>
