package com.cesgroup.cesmonitor.dao;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.cesgroup.cesmonitor.bean.ResponseTimeReport;
import com.cesgroup.cesmonitor.common.Constant;
import com.cesgroup.framework.base.dao.IBaseDao;

/**
 * 响应时间
 *
 * @author user
 * @version 1.0.0 2017年5月11日
 */
public abstract interface ResponseTimeReportMapper extends IBaseDao<ResponseTimeReport> {

    /**
     *
     * @param group
     *            主机组
     * @param startTime
     *            开始时间戳
     * @param endTime
     *            结束时间戳
     * @return 慢的响应时间数据
     */
    @Select({ "select h.host,h.name hname,g.name,sum(t.value_avg)/count(t.value_avg) valueAvg,"
        + " max(t.value_max) valueMax,min(t.value_min) valueMin "
        + " from hosts h,groups g,hosts_groups hg,items i,trends t "
        + " where h.hostid = hg.hostid and g.groupid = hg.groupid and h.hostid = i.hostid "
        + " and i.itemid = t.itemid and i.name = '" + Constant.ICMP_RESPONSE + "'"
        + " and ${group} and t.clock >= ${startTime} and t.value_avg <> 0 "
        + " and t.clock <= ${endTime} group by h.host order by valueAvg desc" })
    public abstract List<ResponseTimeReport> selectSlowResponseTime(@Param("group") String group,
        @Param("startTime") Long startTime, @Param("endTime") Long endTime);

    /**
     *
     * @param group
     *            主机组
     * @param startTime
     *            开始时间戳
     * @param endTime
     *            结束时间戳
     * @return 快的响应时间数据
     */
    @Select({ "select h.host,h.name hname,g.name,sum(t.value_avg)/count(t.value_avg) valueAvg,"
        + " max(t.value_max) valueMax,min(t.value_min) valueMin "
        + " from hosts h,groups g,hosts_groups hg,items i,trends t "
        + " where h.hostid = hg.hostid and g.groupid = hg.groupid and h.hostid = i.hostid "
        + " and i.itemid = t.itemid and i.name = '" + Constant.ICMP_RESPONSE + "'"
        + " and ${group} and t.clock >= ${startTime} and t.value_avg <> 0 "
        + " and t.clock <= ${endTime} group by h.host order by valueAvg asc" })
    public abstract List<ResponseTimeReport> selectFastResponseTime(@Param("group") String group,
        @Param("startTime") Long startTime, @Param("endTime") Long endTime);

    /**
     *
     * @param host
     *            主机名
     * @param startTime
     *            开始时间戳
     * @param endTime
     *            结束时间戳
     * @return 通过主机获取响应时间数据
     */
    @Select({ "select h.host,h.name hname,g.name,sum(t.value_avg)/count(t.value_avg) valueAvg,"
        + " max(t.value_max) valueMax,min(t.value_min) valueMin"
        + " from hosts h,groups g,hosts_groups hg,items i,trends t "
        + " where h.hostid = hg.hostid and g.groupid = hg.groupid and h.hostid = i.hostid "
        + " and i.itemid = t.itemid and i.name = '" + Constant.ICMP_RESPONSE + "'"
        + " and h.host = '${host}' and t.clock >= ${startTime} and t.clock <= ${endTime} and t.value_avg <> 0" })
    public abstract ResponseTimeReport selectResponseTimeByHost(@Param("host") String host,
        @Param("startTime") Long startTime, @Param("endTime") Long endTime);

    @Select({ "select h.host,h.name hname,g.name,sum(t.value_avg)/count(t.value_avg) valueAvg, "
        + " max(t.value_max) valueMax,min(t.value_min) valueMin from hosts h,groups g, "
        + " hosts_groups hg,items i,trends t,users_groups ug,users u,usrgrp usrg,rights r "
        + " where h.hostid = hg.hostid and g.groupid = hg.groupid and h.hostid = i.hostid "
        + " and i.itemid = t.itemid and i.name = '" + Constant.ICMP_RESPONSE
        + "' and ${group}  and t.clock  >= ${startTime} "
        + " and t.value_avg <> 0 and t.clock <= ${endTime}  AND h.hostid=i.hostid and h.hostid = hg.hostid "
        + " and r.id = hg.groupid and r.groupid = ug.usrgrpid and ug.userid = u.userid and ug.usrgrpid = usrg.usrgrpid "
        + " and u.userid = ${userId} group by h.host order by valueAvg desc" })
    public abstract List<ResponseTimeReport> selectSlowResponseTimeByUser(@Param("group") String group,
        @Param("startTime") Long startTime, @Param("endTime") Long endTime, @Param("userId") Integer userId);

    /**
     *
     * @param group
     *            主机组
     * @param startTime
     *            开始时间戳
     * @param endTime
     *            结束时间戳
     * @return 快的响应时间数据
     */
    @Select({ "select h.host,h.name hname,g.name,sum(t.value_avg)/count(t.value_avg) valueAvg, "
        + "max(t.value_max) valueMax,min(t.value_min) valueMin from hosts h,groups g,hosts_groups hg, "
        + "items i,trends t,users u,users_groups ug,usrgrp usrg,rights r where h.hostid = hg.hostid "
        + "and g.groupid = hg.groupid and h.hostid = i.hostid and i.itemid = t.itemid and i.name = '响应时间' "
        + "and ${group} and t.clock  >= ${startTime} and t.value_avg <> 0 "
        + "and t.clock  <= ${endTime} and h.hostid = hg.hostid "
        + "AND h.hostid = i.hostid and r.id = hg.groupid and r.groupid = ug.usrgrpid and ug.userid = u.userid "
        + "and ug.usrgrpid = usrg.usrgrpid and u.userid = ${userId} group by h.host order by valueAvg asc" })
    public abstract List<ResponseTimeReport> selectFastResponseTimeByUser(@Param("group") String group,
        @Param("startTime") Long startTime, @Param("endTime") Long endTime, @Param("userId") Integer userId);
}