/**
 * <p>Copyright:Copyright(c) 2017</p>
 * <p>Company:上海中信信息发展股份有限公司</p>
 * <p>包名:com.cesgroup.cesmonitor.dao</p>
 * <p>文件名:DiskUsageRateReportMapper.java</p>
 * <p>创建时间:2017年5月11日 下午5:32:13</p>
 * <p>作者:user</p>
 */

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.DiskUsageRateReport;
import com.cesgroup.cesmonitor.common.Constant;
import com.cesgroup.framework.base.dao.IBaseDao;

/**
 *
 * @author user
 * @version 1.0.0 2017年5月11日
 */
public interface DiskUsageRateReportMapper extends IBaseDao<DiskUsageRateReport> {

    /**
     *
     * @param group
     *            主机组
     * @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,i.name itemName,i.key_ itemKey"
        + " 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 '"
        + Constant.DISK_UNUSE_PERCENT + "'"
        + " and ${group} and t.clock >= ${startTime} and t.clock <= ${endTime} and t.value_avg <> 0"
        + " group by h.host,itemName,itemKey order by valueAvg desc")
    public List<DiskUsageRateReport> selectHighDiskUsageRateReport(@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(100 - t.value_avg)/count(100 - t.value_avg) valueAvg,"
        + "100 - min(t.value_max) valueMax,"
        + " 100 - max(t.value_min) valueMin,i.name itemName,i.key_ itemKey"
        + " 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 '"
        + Constant.DISK_UNUSE_PERCENT + "'"
        + " and ${group} and t.clock >= ${startTime} and t.clock <= ${endTime} and t.value_avg <> 0"
        + " group by h.host,itemName,itemKey order by valueAvg asc")
    public List<DiskUsageRateReport> selectLowDiskUsageRateReport(@Param("group") String group,
        @Param("startTime") Long startTime, @Param("endTime") Long endTime);

    /**
     *
     * @param host
     *            主机名
     * @param itemKey
     *            监控项key
     * @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,i.name itemName" + " 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 h.host = #{0} and i.key_ = #{1} "
        + " and t.clock >= ${startTime} and t.clock <= ${endTime} and t.value_avg <> 0")
    public DiskUsageRateReport selectDiskUsageRateReportByHostAndItemKey(String host, String itemKey,
        @Param("startTime") Long startTime, @Param("endTime") Long endTime);

    @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,i.name itemName,i.key_ itemKey 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 like '" + Constant.DISK_UNUSE_PERCENT
        + "' 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} and t.value_avg <> 0 "
        + " group by h.host,itemName,itemKey order by valueAvg desc ")
    public List<DiskUsageRateReport> selectHighDiskUsageRateReportByUser(@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(100 - t.value_avg)/count(100 - t.value_avg) valueAvg,"
        + "100 - min(t.value_max) valueMax, "
        + "100 - max(t.value_min) valueMin,i.name itemName,i.key_ itemKey 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 like '" + Constant.DISK_UNUSE_PERCENT
        + "' 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}  and t.value_avg <> 0 "
        + "group by h.host,itemName,itemKey order by valueAvg asc ")
    public List<DiskUsageRateReport> selectLowDiskUsageRateReportByUser(@Param("group") String group,
        @Param("startTime") Long startTime, @Param("endTime") Long endTime, @Param("userId") Integer userId);
}
