<?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.jay.monitor.data.server.daos.JayMonitorUrlMapperExt">

    <insert id="batchPartitionInsert" keyColumn="list.id" keyProperty="list.id" useGeneratedKeys="true">
        insert into jay_monitor_url_${date}
        (application_name, application_ip, url,
        user_id, client_ip, data_group_name,
        data_group_key, data_id, `status`,
        log_id, request_time, created_time )
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.applicationName,jdbcType=VARCHAR}, #{item.applicationIp,jdbcType=VARCHAR},
            #{item.url,jdbcType=VARCHAR},
            #{item.userId,jdbcType=VARCHAR}, #{item.clientIp,jdbcType=VARCHAR}, #{item.dataGroupName,jdbcType=VARCHAR},
            #{item.dataGroupKey,jdbcType=VARCHAR}, #{item.dataId,jdbcType=VARCHAR}, #{item.status,jdbcType=INTEGER},
            #{item.logId,jdbcType=VARCHAR}, #{item.requestTime,jdbcType=INTEGER}, #{item.createdTime,jdbcType=TIMESTAMP}
            )
        </foreach>
    </insert>

    <sql id="Base_Column_List">
    id, application_name, application_ip, url, user_id, client_ip, data_group_name, data_group_key,
    data_id, `status`, log_id, request_time, created_time
  </sql>

    <select id="selectPartitionById" resultType="com.jay.monitor.data.server.models.entity.JayMonitorUrl">
        select
        <include refid="Base_Column_List"/>
        from jay_monitor_url_${date}
        where id = #{id,jdbcType=INTEGER}
    </select>

    <select id="selectByUrlRequestTimeGroupList"
            resultType="com.jay.monitor.data.server.models.dto.report.SlowGroupModel">
        select a.url as `key`,count(1) as `countValue`,max(a.request_time) as `maxValue`,avg(a.request_time) as `avgValue` from jay_monitor_url_${date} a
        where a.request_time >= #{minRequestTime,jdbcType=INTEGER}
        group by a.url having countValue > #{minRequestCount,jdbcType=INTEGER} order by `maxValue` desc limit 10
    </select>

    <select id="selectByUrlCountGroupList" resultType="com.jay.monitor.data.server.models.dto.report.SlowGroupModel">
        select a.url as `key`,count(1) as `countValue`,max(request_time) as `maxValue`,avg(a.request_time) as `avgValue` from jay_monitor_url_${date} a
        group by a.url order by `countValue` desc limit 10
    </select>

    <select id="selectByUrlAvgGroupList" resultType="com.jay.monitor.data.server.models.dto.report.SlowGroupModel">
         select a.url as `key`,count(1) as `countValue`,max(request_time) as `maxValue`,avg(a.request_time) as `avgValue` from jay_monitor_url_${date} a
         where a.request_time >= #{minRequestTime,jdbcType=INTEGER}
         group by a.url having countValue > #{minRequestCount,jdbcType=INTEGER} order by `avgValue` desc limit 10
    </select>

    <select id="selectAllCount" resultType="java.lang.Long">
         select max(id) from jay_monitor_url_${date}
    </select>


    <select id="selectPartitionByList" resultType="com.jay.monitor.data.server.models.entity.JayMonitorUrl">
        select
        <include refid="Base_Column_List"/>
        from jay_monitor_url_${date}
        <include refid="whereCondition"/>
    </select>

    <sql id="whereCondition">
        <where>
            1=1
            <if test="record.id != null and record.id != ''">
                AND id = #{record.id,jdbcType=INTEGER}
            </if>
            <if test="record.applicationName != null and record.applicationName != '' ">
                AND application_name = #{record.applicationName,jdbcType=VARCHAR}
            </if>
            <if test="record.applicationIp != null and record.applicationIp != '' ">
                AND application_ip = #{record.applicationIp,jdbcType=VARCHAR}
            </if>
            <if test="record.url != null and record.url != ''">
                AND url = #{record.url,jdbcType=VARCHAR}
            </if>
            <if test="record.userId != null and record.userId != ''">
                AND user_id = #{record.userId,jdbcType=VARCHAR}
            </if>
            <if test="record.clientIp != null and record.clientIp != ''">
                AND client_ip = #{record.clientIp,jdbcType=VARCHAR}
            </if>
            <if test="record.dataGroupName != null and record.dataGroupName != ''">
                AND data_group_name = #{record.dataGroupName,jdbcType=VARCHAR}
            </if>
            <if test="record.dataGroupKey != null and record.dataGroupKey != ''">
                AND data_group_key = #{record.dataGroupKey,jdbcType=VARCHAR}
            </if>
            <if test="record.dataId != null and record.dataId != ''">
                AND data_id = #{record.dataId,jdbcType=VARCHAR}
            </if>
            <if test="record.status != null and record.status != ''">
                AND `status` = #{record.status,jdbcType=INTEGER}
            </if>
            <if test="record.logId != null and record.logId != ''">
                AND log_id = #{record.logId,jdbcType=VARCHAR}
            </if>
            <if test="record.requestTime != null and record.requestTime != ''">
                AND request_time >= #{record.requestTime,jdbcType=INTEGER}
            </if>
            <if test="record.createdTime != null and record.createdTime != ''">
                AND created_time = #{record.createdTime,jdbcType=TIMESTAMP}
            </if>
        </where>
        order by id desc limit ${pageSize}
    </sql>
</mapper>