<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap namespace="EAILog">

	<resultMap id="portalStatisticsResult" class="com.eactive.eai.rms.data.entity.onl.apim.portalstatistics.PortalStatisticsVO">
        <result property="clientId" column="CLIENT_ID"/>
        <result property="orgId" column="ORG_ID"/>
        <result property="orgName" column="ORG_NAME"/>
		<result property="appId" column="APP_ID"/>
        <result property="appName" column="APP_NAME"/>
        <result property="apiId" column="API_ID"/>
        <result property="apiName" column="API_NAME"/>
        <result property="requestCount" column="REQUEST_COUNT"/>
        <result property="failureCount" column="FAILURE_COUNT"/>
        <result property="statisticsTime" column="STATISTICS_TIME"/>
        <result property="routingUri" column="ROUTING_URI"/>
    </resultMap>

	<statement id="selectLogList" parameterClass="java.util.HashMap" resultMap="portalStatisticsResult">
		SELECT
				A.CLIENT_ID,
				A.ORG_ID,
				MAX(A.ORG_NAME) 					  AS ORG_NAME,
				A.API_ID,
				MAX(B.eaisvcdesc)                     AS API_NAME,
				MAX(C.clientname)                     AS APP_ID,
				MAX(C.clientname)                     AS APP_NAME,
				A.ROUTING_URI,
				SUM(A.END_COUNT) 				  	  AS REQUEST_COUNT,
				SUM(A.START_COUNT) - SUM(A.END_COUNT) AS FAILURE_COUNT,
				A.STATISTICS_TIME
		FROM (
			    SELECT
		            CLIENTID  		AS  CLIENT_ID,
		            random() 			AS REQUEST_URI,
		            '$startTime$'		AS STATISTICS_TIME,
		            EAISVCNAME		AS API_ID,
		            NULL 				AS API_NAME,
		            NULL 				AS ROUTING_URI,
		            NULL 				AS APP_ID,
		            NULL 				AS APP_NAME,
		            ORGID 				AS ORG_ID,
		            ORGNAME 			AS ORG_NAME,
					0 						AS END_COUNT,
			        1 						AS START_COUNT
			    FROM $schemaId$.$tableName$
			    WHERE MSGDPSTYMS BETWEEN #startTime#  AND #endTime#
			    	AND LOGPRCSSSERNO = '100'
			    UNION ALL
			    SELECT
			            CLIENTID 		 	AS CLIENT_ID,
			            random() 		 	AS REQUEST_URI,
						'$startTime$'	 	AS STATISTICS_TIME,
			            EAISVCNAME		AS API_ID,
			            NULL 			 	AS API_NAME,
			            NULL				 	AS ROUTING_URI,
			            NULL 				AS APP_ID,
			            NULL 				AS APP_NAME,
			            ORGID 				AS ORG_ID,
			            ORGNAME 			AS ORG_NAME,
				        CASE
				        		WHEN SUBSTRING(RSPNSERRCDNAME, 1, 2) = 'RE' THEN 0
				                WHEN SUBSTRING(RSPNSERRCDNAME, 1, 2) = 'FE' THEN 0
				                ELSE 1
				                END 			AS END_COUNT,
				        0 						AS START_COUNT
			      FROM $schemaId$.$tableName$
				 WHERE
				 	MSGDPSTYMS BETWEEN #startTime#  AND #endTime#
			       AND LOGPRCSSSERNO = '400'
			    ) A
				 LEFT JOIN $schemaId$.tseaihe01 B ON A.API_ID = B.eaisvcname
				 LEFT JOIN $schemaId$.tseaiau01 C ON A.CLIENT_ID = C.clientid
			GROUP BY
				A.ORG_ID,
				A.CLIENT_ID,
				A.API_ID,
				A.STATISTICS_TIME,
				A.ROUTING_URI
	</statement>
	
	
</sqlMap>
