/**
 * <p>Copyright:Copyright(c) 2017</p>
 * <p>Company:上海中信信息发展股份有限公司</p>
 * <p>包名:com.cesgroup.cesmonitor.dao</p>
 * <p>文件名:InterfaceOutgoingReportMapper.java</p>
 * <p>创建时间:2017年5月12日 上午10:39:21</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.InterfaceOutgoingReport;
import com.cesgroup.cesmonitor.common.Constant;
import com.cesgroup.framework.base.dao.IBaseDao;

/**
 *
 * @author user
 * @version 1.0.0 2017年5月12日
 */
public interface InterfaceOutgoingReportMapper extends IBaseDao<InterfaceOutgoingReport> {

    /**
     *
     * @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,i.name itemName,i.key_ itemKey"
        + " from hosts h,groups g,hosts_groups hg,items i,trends_uint t"
        + " where h.hostid = hg.hostid and g.groupid = hg.groupid" + " and h.hostid = i.hostid"
        + " and i.itemid = t.itemid" + " and ${group} and i.name like '" + Constant.INTERFACE_OUTPUT_FLOW + "'"
        + " 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<InterfaceOutgoingReport> selectHighInterfaceOutgoingReport(@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,i.name itemName,i.key_ itemKey"
        + " from hosts h,groups g,hosts_groups hg,items i,trends_uint t"
        + " where h.hostid = hg.hostid and g.groupid = hg.groupid" + " and h.hostid = i.hostid"
        + " and i.itemid = t.itemid" + " and ${group} and i.name like '" + Constant.INTERFACE_OUTPUT_FLOW + "'"
        + " 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<InterfaceOutgoingReport> selectLowInterfaceOutgoingReport(@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(t.value_avg)/count(t.value_avg) valueAvg,"
        + " max(t.value_max) valueMax,min(t.value_min) valueMin,i.name itemName,i.key_ itemKey"
        + " from hosts h,groups g,hosts_groups hg,items i,trends_uint 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 InterfaceOutgoingReport selectInterfaceOutgoingReportByHostAndItemKey(String host, String itemKey,
        @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,i.name itemName,i.key_ itemKey "
        + "from hosts h,groups g,hosts_groups hg,items i,trends_uint 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 ${group} and i.name like '" + Constant.INTERFACE_OUTPUT_FLOW
        + "' and t.clock >=${startTime} and t.clock <=${endTime}  "
        + "and t.value_avg <> 0 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,itemName,itemKey order by valueAvg desc")
    public List<InterfaceOutgoingReport> selectHighInterfaceOutgoingReportByUser(@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,i.name itemName,i.key_ itemKey "
        + "from hosts h,groups g,hosts_groups hg,items i,trends_uint 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 ${group} "
        + "and i.name like '" + Constant.INTERFACE_OUTPUT_FLOW
        + "' 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<InterfaceOutgoingReport> selectLowInterfaceOutgoingReportByUser(@Param("group") String group,
        @Param("startTime") Long startTime, @Param("endTime") Long endTime, @Param("userId") Integer userId);
}
