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.CPUUsageRateReport;
import com.cesgroup.framework.base.dao.IBaseDao;

/**
 * CPU使用率报表
 *
 * @author user
 * @version 1.0.0 2017年5月11日
 */
public abstract interface CPUUsageRateReportMapper extends IBaseDao<CPUUsageRateReport> {

    /**
     *
     * @param group
     *            主机组
     * @param startTime
     *            开始时间戳
     * @param endTime
     *            结束时间戳
     * @return 高CPU使用率
     */
    @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 LIKE '${cpuUsePercent}' "
        + " and ${group} and t.clock >= ${startTime} and t.clock <= ${endTime} "
        + " group by h.host order by valueAvg desc" })
    public abstract List<CPUUsageRateReport> selectHighCPUUsageRateReport(@Param("group") String group,
        @Param("startTime") Long startTime, @Param("endTime") Long endTime, @Param("cpuUsePercent") String cpuUsePercent);
    //, Param("cpuUsePercent") String cpuUsePercent
    /**
     *
     * @param group
     *            主机组
     * @param startTime
     *            开始时间戳
     * @param endTime
     *            结束时间戳
     * @return 低CPU使用率
     */
    @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 LIKE '${cpuUsePercent}' "
        + " and ${group} and t.clock >= ${startTime} "
        + " and t.clock <= ${endTime} group by h.host order by valueAvg asc" })
    public abstract List<CPUUsageRateReport> selectLowCPUUsageRateReport(@Param("group") String group,
        @Param("startTime") Long startTime, @Param("endTime") Long endTime, @Param("cpuUsePercent") String cpuUsePercent);

    /**
     *
     * @param host
     *            主机名
     * @param startTime
     *            开始时间戳
     * @param endTime
     *            结束时间戳
     * @return 通过主机名查询出CPU使用率
     */
    @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 like '${cpuUsePercent}' "
        + " and h.host = '${host}' and t.clock >= ${startTime} and t.clock <= ${endTime}" })
    public abstract CPUUsageRateReport selectCPUUsageRateReportByHost(@Param("host") String host,
        @Param("startTime") Long startTime, @Param("endTime") Long endTime, @Param("cpuUsePercent") String cpuUsePercent);




    @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 = '${cpuUsage}' and t.clock >= ${startTime} "
        + "and t.clock  <= ${endTime} and h.hostid = hg.hostid AND h.hostid = i.hostid and  ${group} "
        + "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<CPUUsageRateReport> selectHighCPUUsageRateReportUserId(@Param("group") String group,
        @Param("startTime") Long startTime, @Param("endTime") Long endTime, @Param("userId") Integer userId, @Param("cpuUsage") String cpuUsage);


    @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 = '${cpuUsage}' "
        + "and ${group} and t.clock >= ${startTime} 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<CPUUsageRateReport> selectLowCPUUsageRateReportByUser(@Param("group") String group,
        @Param("startTime") Long startTime, @Param("endTime") Long endTime, @Param("userId") Integer userId, @Param("cpuUsage") String cpuUsage);



    @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 = '${cpuUsage}' and ${group} and t.clock  >= ${startTime} 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 desc" })
    public abstract List<CPUUsageRateReport> selectHighCPUUsageRateReportByUser(@Param("group") String group,
        @Param("startTime") Long startTime, @Param("endTime") Long endTime, @Param("userId") Integer userId, @Param("cpuUsage") String cpuUsage);
}