<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sohu.cache.dao.AppClientExceptionStatDao">
	<sql id="columns">
		app_id,collect_time,client_ip,report_time,create_time,exception_class,exception_count,instance_host,instance_port,instance_id,type
	</sql>
	
	<insert id="save" parameterType="AppClientExceptionStat"
		keyProperty="id" useGeneratedKeys="true">
		insert into app_client_exception_minute_stat(<include refid="columns" />)
		values(#{appId},#{collectTime},#{clientIp},#{reportTime},#{createTime},#{exceptionClass},#{exceptionCount},#{instanceHost},#{instancePort},#{instanceId},#{type})
	</insert>
	
	<insert id="batchSave">
		insert into app_client_exception_minute_stat(<include refid="columns" />)
		values
		<foreach collection="appClientExceptionStatList" item="clientException" separator=",">
			(#{clientException.appId},#{clientException.collectTime},#{clientException.clientIp},#{clientException.reportTime},#{clientException.createTime},
			#{clientException.exceptionClass},#{clientException.exceptionCount},#{clientException.instanceHost},#{clientException.instancePort},#{clientException.instanceId},#{clientException.type})
	    </foreach>
	</insert>
	
	<select id="getAppExceptionList" resultType="AppClientExceptionStat">
    	select id,<include refid="columns" />
    	from app_client_exception_minute_stat
    	where app_id=#{appId} and collect_time between #{startTime} and #{endTime}
    	<choose>
        	<when test="type > 0">
        	 	and type=#{type}
        	</when>
        </choose>
        <choose>
        	<when test="clientIp != '' and clientIp != null">
        	 	and client_ip=#{clientIp}
        	</when>
        </choose>
        order by collect_time desc
        <choose>
        	<when test="page != null">
        		<choose>
		        	<when test="page.totalCount > page.pageSize">
            			limit #{page.start},#{page.pageSize};
		        	</when>
		        	<otherwise>
		        	    limit #{page.totalCount}
		        	</otherwise>
		        </choose>
        	</when>
        </choose>
    </select>
    
    <select id="getAppExceptionCount" resultType="int">
    	select count(*)
    	from app_client_exception_minute_stat
    	where app_id=#{appId} and collect_time between #{startTime} and #{endTime}
    	<choose>
        	<when test="type > 0">
        	 	and type=#{type}
        	</when>
        </choose>
        <choose>
        	<when test="clientIp != '' and clientIp != null">
        	 	and client_ip=#{clientIp}
        	</when>
        </choose>
    </select>
    
    
    <select id="getInstanceExceptionStat" resultType="ClientInstanceException">
    	select app_id,instance_id,instance_host,instance_port,count(1) as exceptionCount from app_client_exception_minute_stat 
    	<where>
	    	<choose>
	        	<when test="collectTime > 0">
	        	 	and collect_time>=#{collectTime}
	        	</when>
	        </choose>
	        <choose>
	        	<when test="ip != '' and ip != null">
	        	 	and instance_host=#{ip}
	        	</when>
	        </choose>
    	</where>
    	group by instance_id order by exceptionCount desc;
    </select>
    
    

</mapper>