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

	<select id="selectOutscenarioList" resultType="java.util.HashMap">
	/* MonitoringOutscenarioMapper.selectOutscenarioList */
	<![CDATA[
		SELECT
			CONCAT(Z.SCENARIO_SEQ, '_', Z.NODE_CODE) AS 'groupKey'
			, 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'
			, Z.SCENARIO_SEQ AS 'scenarioSeq'
			, Z.SCENARIO_NAME AS 'scenarioName'
			, Z.NODE_CODE AS 'nodeCode'
			, Z.NODE_NAME AS 'nodeName'
			, Z.NODE_TYPE AS 'nodeType'
			, COUNT(CONCAT(Z.SCENARIO_SEQ, '_', Z.NODE_CODE)) AS 'nodeCnt'
		FROM
			TB_SCENARIO_BREAK_NODE Z
		LEFT JOIN TB_CALL_INFO Y ON Y.TOKEN = Z.TOKEN
		WHERE Z.SCENARIO_RESULT_CODE = 'F'
			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>
		<choose>
			<when test="scenarioIdList.size > 0 or (chkScenarioAll != null and chkScenarioAll != '')">
				AND Z.SCENARIO_SEQ IN
				<choose>
					<when test='chkScenarioAll == "Y"'>
						<foreach item="item" index="index" collection="allScenarioIdList" open="(" separator="," close=")">
							#{item}
						</foreach>
					</when>
					<otherwise>
						<foreach item="item" index="index" collection="scenarioIdList" open="(" separator="," close=")">
							#{item}
						</foreach>
					</otherwise>
				</choose>
			</when>
			<otherwise>
				AND 1=0
			</otherwise>
		</choose>
	<![CDATA[
		GROUP BY groupKey
		ORDER BY scenarioSeq, nodeCnt DESC
	]]>
		<choose>
			<when test="page != null and page != ''">
				<if test="page != -1">
					LIMIT ${limit}, ${offset}
				</if>
			</when>
			<otherwise>
				LIMIT 0, 100
			</otherwise>
		</choose>
	</select>

	<select id="selectOutscenarioListCnt" resultType="int">
		<![CDATA[
		SELECT
		    COUNT(*)
		FROM (
			SELECT
				DISTINCT Z.SEQ
			FROM TB_SCENARIO_BREAK_NODE Z
				LEFT JOIN TB_CALL_INFO Y
					ON Y.TOKEN = Z.TOKEN
			WHERE Z.SCENARIO_RESULT_CODE = 'F'
				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>
		<choose>
			<when test="scenarioIdList.size > 0 or (chkScenarioAll != null and chkScenarioAll != '')">
				AND Z.SCENARIO_SEQ IN
				<choose>
					<when test='chkScenarioAll == "Y"'>
						<foreach item="item" index="index" collection="allScenarioIdList" open="(" separator="," close=")">
							#{item}
						</foreach>
					</when>
					<otherwise>
						<foreach item="item" index="index" collection="scenarioIdList" open="(" separator="," close=")">
							#{item}
						</foreach>
					</otherwise>
				</choose>
			</when>
			<otherwise>
				AND 1=0
			</otherwise>
		</choose>
		LIMIT 1000001
		) A
	</select>

	<select id="selectOutscenarioCnt" resultType="int">
		<![CDATA[
		SELECT COUNT(*)
		FROM TB_SCENARIO_BREAK_NODE sbn LEFT JOIN TB_CALL_INFO ci ON sbn.TOKEN = ci.TOKEN
		WHERE SCENARIO_RESULT_CODE = 'F' AND SCENARIO_SEQ = #{scenarioSeq} AND ci.IN_CHANNEL_TYPE = 'VOICE' AND ci.CALL_INIT_DATE > #{callInitDateFrom} AND ci.CALL_INIT_DATE < DATE_ADD(#{callInitDateTo}, INTERVAL 1 DAY)
		]]>
	</select>

	<select id="selectScenarioNameList" resultType="java.util.HashMap">
		<![CDATA[
		SELECT
		    SEQ AS seq
			, OPR_MNG_CODE AS oprMngCode
			, SCENARIO_NAME AS scenarioName
			, USE_YN AS useYn
		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>
		GROUP BY SEQ
		ORDER BY OPR_MNG_CODE, SCENARIO_NAME ASC
	</select>

	<select id="selectTokenListByScenario" resultType="string">
		<![CDATA[
		SELECT
		    Z.TOKEN
		FROM TB_SCENARIO_BREAK_NODE Z
			LEFT JOIN TB_CALL_INFO Y
			    ON Y.TOKEN = Z.TOKEN
		WHERE Z.SCENARIO_RESULT_CODE = 'F'
			AND Y.IN_CHANNEL_TYPE = 'VOICE'
		]]>
		<if test="oprMngCode != null and oprMngCode != ''">
			<![CDATA[
		  		AND Z.OPR_MNG_CODE = #{oprMngCode}
			]]>
		</if>
		<if test="scenarioSeq != null and scenarioSeq != ''">
			<![CDATA[
		  		AND Z.SCENARIO_SEQ = #{scenarioSeq}
			]]>
		</if>
		<if test="nodeCode != null and nodeCode != ''">
			<![CDATA[
		  		AND Z.NODE_CODE = #{nodeCode}
			]]>
		</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>
		GROUP BY Z.TOKEN
	</select>

	<select id="selectCallListByScenario" resultType="java.util.HashMap">
		SELECT
			*
		FROM (
			 SELECT
				 (SELECT @ROWNUM := @ROWNUM + 1) AS 'rownum'
	    		, A.*
		FROM (
			SELECT
				z.TOKEN AS 'token'
				, z.opr_mng_code AS 'oprMngCode'
				, CONCAT(IFNULL(z.call_time, ''), '초') AS 'callTime'
				, DATE_FORMAT(z.call_init_date, '%Y-%m-%d %H:%i') AS 'callInitDate'
				, z.tel_no AS 'telNo'
				, z.dnis_no AS 'dnisNo'
			FROM TB_CALL_INFO z
			WHERE z.IN_CHANNEL_TYPE = 'VOICE'
			<if test="tokenList.size > 0">
				AND z.TOKEN IN
				<foreach item="item" index="index" collection="tokenList" open="(" separator="," close=")">
					#{item}
				</foreach>
			</if>
			GROUP BY z.TOKEN
			ORDER BY z.call_init_date DESC
			) A, (SELECT @ROWNUM := 0) B
		) C
	</select>

</mapper>
