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.MemoryUsageRateReport;
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 MemoryUsageRateReportMapper extends IBaseDao<MemoryUsageRateReport> {

    /**
     *
     * @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.USE_MEMORY_RATE + "' or i.key_ like '%Physical Memory%') "
            + " 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<MemoryUsageRateReport> selectHighMemoryUsageReport(@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.USE_MEMORY_RATE + "' or i.key_ like '%Physical Memory%') "
            + " 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<MemoryUsageRateReport> selectLowMemoryUsageReport(@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(100 - t.value_avg)/count(100 - t.value_avg) valueAvg,"
            + "100 - min(t.value_max) valueMax,"
            + " 100 - max(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.UNUSE_MOMORY_RATE + "'" + " and h.host = #{0} "
            + " and t.clock >= ${startTime} and t.clock <= ${endTime} and t.value_avg <> 0" })
    public abstract MemoryUsageRateReport selectMemoryUsageReportByHost(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 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 = '" + Constant.UNUSE_MOMORY_RATE + "' 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 desc" })
    public abstract List<MemoryUsageRateReport> selectHighMemoryUsageReportByUser(@Param("group") String group,
        @Param("startTime") Long startTime, @Param("endTime") Long endTime,@Param("userId") Integer userId);



    @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 = '" + Constant.UNUSE_MOMORY_RATE + "' 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<MemoryUsageRateReport> selectLowMemoryUsageReportByUser(@Param("group") String group,
        @Param("startTime") Long startTime, @Param("endTime") Long endTime,@Param("userId") Integer userId);
}