/**
 * <p>Copyright:Copyright(c) 2017</p>
 * <p>Company:上海中信信息发展股份有限公司</p>
 * <p>包名:com.cesgroup.cesmonitor.dao</p>
 * <p>文件名:ScreenMapper.java</p>
 * <p>创建时间:2017年6月9日 下午1:17:58</p>
 * <p>作者:Administrator</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.common.Constant;
import com.cesgroup.cesmonitor.screenbean.AbnormalAlarmScreen;
import com.cesgroup.cesmonitor.screenbean.AttentionedHost;
import com.cesgroup.cesmonitor.screenbean.AttentionedHostItem;
import com.cesgroup.cesmonitor.screenbean.CpuScreen;
import com.cesgroup.cesmonitor.screenbean.MonitorAlarm;
import com.cesgroup.cesmonitor.screenbean.NetWorkTraffic;
import com.cesgroup.cesmonitor.screenbean.PriorityCountScreen;
import com.cesgroup.cesmonitor.screenbean.PriorityDetailScreen;
import com.cesgroup.cesmonitor.screenbean.PriorityScreen;
import com.cesgroup.cesmonitor.screenbean.TopologicalScreen;
import com.cesgroup.cesmonitor.screenbean.Usability;
import com.cesgroup.framework.base.dao.IBaseDao;
import com.zabbix4j.host.HostObject;

/**
 * 大屏展示功能DAO
 *
 * @author user
 * @version 1.0.0 2017年6月9日
 */
public interface ScreenMapper extends IBaseDao<Object> {

    /**
     * 统计时间段内的流量
     *
     * @param host
     *            主机
     * @param startTime
     *            开始时间戳
     * @param endTime
     *            截止时间戳
     * @return 返回时间段内的流量
     */
    @Select("select h.host,sum(hu.value) value from hosts h,items i, history_uint hu"
        + " where h.hostid=i.hostid and i.itemid=hu.itemid and h.host=#{0}"
        + " and i.name like '%口流量%' and hu.clock>=${startTime} and hu.clock<=${endTime}")
    public NetWorkTraffic sumNetWorkTrafficMapper(String host, @Param("startTime") Long startTime,
        @Param("endTime") Long endTime);

    /**
     * 统计时间段内的异常告警数量
     *
     * @param startTime
     *            开始时间戳
     * @param endTime
     *            截止时间戳
     * @return 时间段内的异常告警数量
     */
    @Select("select count(*) num from hosts h,items i,functions f,triggers t,events e"
        + " where h.hostid = i.hostid and i.itemid = f.itemid and f.triggerid = t.triggerid"
        + " and e.objectid = t.triggerid and e.object = 0 and e.value = 1 and e.source = 0 "
        + " and e.clock >=${startTime} and e.clock<=${endTime}")
    public AbnormalAlarmScreen countAbnormalAlarmScreenMapper(@Param("startTime") Long startTime,
        @Param("endTime") Long endTime);

    /**
     * 查询出主机状态，ip地址
     *
     * @param host
     *            主机
     * @return 返回主机相关信息
     */
    @Select("select h.status,inf.ip,h.host" + " from hosts h,items i,interface inf" + " where h.hostid = inf.hostid"
        + " and h.host = #{0} limit 1")
    public AttentionedHost selectAttentionedHostInfoMapper(String host);

    /**
     * 查询出主机的操作系统
     *
     * @param host
     *            主机
     * @return 操作系统信息
     */
    @Select("select hs.value from hosts h,items i, history_str hs "
        + " where h.hostid=i.hostid and i.itemid=hs.itemid and h.host=#{0}" + " and i.name='" + Constant.ITEMS_OS + "'"
        + " and hs.clock> UNIX_TIMESTAMP(now()) - i.lifetime * 2 limit 1")
    public String selectAttentionedHostOSMapper(String host);

    /**
     * 查询出主机的CPU使用率监控项
     *
     * @param host
     *            主机
     * @return CPU数据
     */
    @Select("select h.host,i.name itemName,hi.value from hosts h,items i,history hi"
        + " where h.hostid = i.hostid and i.itemid = hi.itemid and h.host = #{0}" + " and i.name='"
        + Constant.CPU_USE_PERCENT + "'" + " and hi.clock> UNIX_TIMESTAMP(now()) - i.lifetime * 2 limit 1")
    public AttentionedHostItem selectCpuUsageRateMapper(String host);

    /**
     *
     * @param host
     *            主机
     * @return snmp监控模式下的cpu使用率
     */
    @Select("select avg(h.value) from history h,hosts ho,items i where i.name LIKE '" + Constant.CPU_USE_PERCENT_FLAG
        + "' and i.itemid=h.itemid and ho.host=#{0} AND ho.hostid = i.hostid "
        + "AND ho.`status` = 0 AND i.`status` = 0 and "
        + " h.clock> UNIX_TIMESTAMP(now()) - i.lifetime * 2 limit 1")
    public Double findSnmpCpuRate(String host);

    /**
     * 查询出主机的内存使用率监控项
     *
     * @param host
     *            主机
     * @return 内存数据
     */
    @Select("select h.host,i.name itemName,hi.value from hosts h,items i,history hi"
        + " where h.hostid = i.hostid and i.itemid = hi.itemid and h.host = #{0}" + " and i.name='"
        + Constant.USE_MEMORY_RATE + "'" + " and hi.clock> UNIX_TIMESTAMP(now()) - i.lifetime * 2 limit 1")
    public AttentionedHostItem selectMemoryUsageRateMapper(String host);

    /**
     * 查询出磁盘使用率的监控项
     *
     * @param host
     *            主机
     * @return 磁盘数据
     */
    @Select("select h.host,i.name itemName,i.key_ itemKey,100 - hi.value value,hi.clock from hosts h,items i,history hi"
        + " where h.hostid = i.hostid and i.itemid = hi.itemid and h.host = #{0}" + " and i.name like'"
        + Constant.DISK_UNUSE_PERCENT + "'" + " and hi.clock> UNIX_TIMESTAMP(now()) - i.lifetime * 2")
    public List<AttentionedHostItem> selectDiskUsageRateMapper(String host);

    /**
     * 查询出相关接口的状态
     *
     * @param host
     *            主机
     * @return 返回接口状态
     */
    @Select("select h.host,i.name itemName,i.key_ itemKey,t.value status from hosts h,items i,functions f,triggers t"
        + " where h.hostid = i.hostid and i.itemid = f.itemid and f.triggerid = t.triggerid"
        + " and i.key_ like 'ifOperStatus%' and h.host = #{0}")
    public List<AttentionedHostItem> selectInterfaceStatusMapper(String host);

    /**
     * 查询出相关接口的流入流量
     *
     * @param host
     *            主机
     * @return 返回接口流入流量数据
     */
    @Select("select h.host,i.name itemName,i.key_ itemKey,hi.value value,hi.clock from hosts h,items i,history_uint hi"
        + " where h.hostid = i.hostid and i.itemid = hi.itemid and h.host = #{0}" + " and i.key_ like 'ifHCInOctets%'"
        + " and hi.clock> UNIX_TIMESTAMP(now()) - i.lifetime * 2")
    public List<AttentionedHostItem> selectInterfaceIncomingMapper(String host);

    /**
     * 查询出特定接口的触发器的严重等级
     *
     * @param host
     *            主机
     * @param interfaceName
     *            接口名
     * @return 特定接口的触发器的严重等级
     */
    @Select("select t.priority from hosts h,items i,functions f,triggers t"
        + " where h.hostid = i.hostid and i.itemid = f.itemid and f.triggerid = t.triggerid" + " and h.host = #{0}"
        + " and i.key_ = 'ifOperStatus[${interfaceName}]'")
    public Integer selectPriorityByInterfaceName(String host, @Param("interfaceName") String interfaceName);

    /**
     *
     * @param host
     *            主机
     * @param interfaceName
     *            接口名
     * @return 特定接口的触发器最高等级
     */
    @Select("select max(t.priority) from hosts h,items i,functions f,triggers t"
        + " where h.hostid = i.hostid and i.itemid = f.itemid" + " and f.triggerid = t.triggerid  and h.host = #{0}"
        + " and t.value = 1 and i.key_ like '%${interfaceName}%'")
    public Integer selectMaxPriorityByInterfaceName(String host, @Param("interfaceName") String interfaceName);

    /**
     * 查询出特定接口的流入流量数据
     *
     * @param host
     *            主机
     * @param interfaceName
     *            接口名
     * @return 特定接口的流出流量数据
     */
    @Select("select h.host,i.name itemName,i.key_ itemKey,hi.value value,hi.clock,i.units "
        + " from hosts h,items i,history_uint hi"
        + " where h.hostid = i.hostid and i.itemid = hi.itemid and h.host = #{0}"
        + " and i.key_ = 'ifHCInOctets[${interfaceName}]' and hi.clock > UNIX_TIMESTAMP(now()) - i.lifetime * 2")
    public AttentionedHostItem selectInterfaceIncomingByInterfaceNameMapper(String host,
        @Param("interfaceName") String interfaceName);

    /**
     * 查询出相关接口的流出流量
     *
     * @param host
     *            主机
     * @return 返回接口流出流量数据
     */
    @Select("select h.host,i.name itemName,i.key_ itemKey,hi.value value,hi.clock from hosts h,items i,history_uint hi"
        + " where h.hostid = i.hostid and i.itemid = hi.itemid and h.host = #{0}" + " and i.key_ like 'ifHCOutOctets%'"
        + " and hi.clock> UNIX_TIMESTAMP(now()) - i.lifetime * 2")
    public List<AttentionedHostItem> selectInterfaceOutgoingMapper(String host);

    /**
     * 查询出特定接口的流出流量数据
     *
     * @param host
     *            主机
     * @param interfaceName
     *            接口名称
     * @return 特定接口的流出流量数据
     */
    @Select("select h.host,i.name itemName,i.key_ itemKey,hi.value value,hi.clock,i.units "
        + " from hosts h,items i,history_uint hi"
        + " where h.hostid = i.hostid and i.itemid = hi.itemid and h.host = #{0}"
        + " and i.key_ = 'ifHCOutOctets[${interfaceName}]' and hi.clock > UNIX_TIMESTAMP(now()) - i.lifetime * 2")
    public AttentionedHostItem selectInterfaceOutgoingByInterfaceNameMapper(String host,
        @Param("interfaceName") String interfaceName);

    /**
     * 查询出丢包率数据
     *
     * @param host
     *            主机
     * @return 丢包率数据
     */
    @Select("select h.host,i.name itemName,i.key_ itemKey,hi.value value,hi.clock from hosts h,items i,history hi"
        + " where h.hostid = i.hostid and i.itemid = hi.itemid and h.host = #{0}" + " and i.name = '"
        + Constant.ICMP_LOSS + "'" + " and hi.clock > UNIX_TIMESTAMP(now()) - i.lifetime * 2")
    public AttentionedHostItem selectIcmppingLossMapper(String host);

    /**
     * 获取特定主机组所属的单个主机
     *
     * @param group
     *            主机组名称
     * @return 主机
     */
    /* @Select("select h.host from hosts h,hosts_groups hg,groups g where h.hostid = hg.hostid"
        + " and hg.groupid = g.groupid" + " and g.name = #{0} limit 1")*/
    @Select("select h.host from hosts h where h.host_type=#{0} limit 1")
    public AttentionedHost selectSingleHostByHostGroupNameMapper(String group);

    /**
     * 获取特定主机组所属的全部主机
     *
     * @param group
     *            主机组名称
     * @return 特定主机组所属的全部主机
     */
    @Select("select h.host from hosts h,hosts_groups hg,groups g where h.hostid = hg.hostid"
        + " and hg.groupid = g.groupid" + " and g.name = #{0}")
    public List<HostObject> selectHostByHostGroupNameMapper(String group);

    /**
     * 获取特定主机组所属的全部主机，并包括ip
     *
     * @param group
     *            主机组名称
     * @return 特定主机组所属的全部主机
     */
    /*  @Select("select h.host,i.ip from hosts h,interface i,groups g,hosts_groups hg"
        + " where h.hostid = i.hostid and h.hostid = hg.hostid and g.groupid = hg.groupid"
        + " and g.name = #{0} group by h.host")*/
    @Select("select h.host,i.ip from hosts h,interface i where h.hostid = i.hostid and "
        + " h.host_type= #{0} group by h.host")
    public List<AttentionedHost> selectInfoByGroupNameMapper(String group);

    /**
     * @return 返回监控节点数量(主机数量)
     */
    @Select("SELECT COUNT(*) from `hosts` WHERE `status` <>3 and flags <>2 and status=0")
    Integer findMonitorNodeNum();

    /**
     *
     * @return 返回故障监控节点数量
     */
    @Select("SELECT COUNT(DISTINCT a.`host`) FROM(" + "SELECT h.`host`,h.`name`,t.description,t.priority "
        + "FROM `triggers` t, `events` e,functions f, items i, `hosts` h "
        + "WHERE e.object = 0 AND e.source=0 AND h.hostid =i.hostid and t.`status`=0 and h.status=0 "
        + "AND e.objectid = t.triggerid AND f.triggerid = t.triggerid " + "AND f.itemid = i.itemid AND t.`value` = 1 "
        + "GROUP BY h.`host`,t.description,t.priority) a")
    Integer findFaultMonitorNodeNum();

    /**
     *
     * @return 返回监控告警数量（实时触发器坏的）
     */
    @Select("SELECT COUNT(*) FROM (SELECT t.triggerid " + "from `triggers` t,functions f,items i,`hosts` h "
        + "where t.`value`=1 and t.`status`=0 and h.`status`=0 "
        + "and i.`status`=0 AND t.flags <>2 AND t.triggerid=f.triggerid "
        + "and f.itemid=i.itemid and i.hostid=h.hostid GROUP BY t.triggerid) a")
    Integer findMonitorAlarmNum();

    /**
     *
     * @return 返回监控告警列表（最近20条数据，这个是按照级别，时间排序过的）
     */
    @Select("select a.`host`,a.description,a.priority,a.triggerid "
        + "FROM(SELECT h.`host`,t.description,t.priority,e.clock,t.triggerid "
        + "FROM  `hosts` h,items i,functions f,`triggers` t,`events` e "
        + "WHERE i.hostid = h.hostid AND i.itemid = f.itemid AND f.triggerid = t.triggerid "
        + "and (t.priority=5 or t.priority=4) and t.`status`=0 "
        + "AND e.object = 0 AND e.objectid = t.triggerid and h.`status`=0 and t.value=1 "
        + "ORDER BY e.clock DESC) a group by a.description,a.host,a.priority "
        + "ORDER BY a.priority DESC,a.clock DESC limit 20")
    List<MonitorAlarm> findMonitorAlarmList();

    /**
     *
     * @return 返回可用性列表
     */
    @Select("SELECT b.serviceid,b.`name` from " + "(SELECT DISTINCT serviceupid from services_links where serviceupid "
        + "not in(SELECT servicedownid from services_links))a " + "LEFT JOIN (SELECT `name`,serviceid from services) b "
        + "on a.serviceupid=b.serviceid")
    List<Usability> findUseAbilty();

    /**
     *
     * @param limit
     *            取多少条
     * @return 返回当前故障列表
     */
    @Select("SELECT h.`host`,t.description,e.objectid,t.priority "
        + "FROM `triggers` t,`events` e,functions f,items i,`hosts` h,groups g,hosts_groups hg "
        + "WHERE t.priority >=#{param1} AND e.object =0 and e.source=0 AND e.objectid = t.triggerid "
        + "AND f.triggerid = t.triggerid AND f.itemid =i.itemid AND i.hostid = h.hostid "
        + "AND h.hostid=hg.hostid AND g.groupid=hg.groupid AND h.`status` = 0 AND t.`status` = 0 "
        + "AND t.`value` = 1 GROUP BY description,`host`,priority LIMIT #{param2}")
    List<PriorityDetailScreen> findAlarmList(Integer priority, Integer limit);

    /**
     *
     * @param objectid
     *            触发器ID
     * @param time
     *            触发器开始时间
     * @return 找到这个触发器恢复的时间
     */
    @Select("select e.clock start_clock " + "from events e,`triggers` t " + "where e.object=0 and e.objectid=#{param1} "
        + "and e.objectid=t.triggerid  and t.value=1 " + "order by e.clock DESC LIMIT 1")
    List<PriorityDetailScreen> findStartClock(Integer objectid);

    /**
     * 表的信息类型是：浮点数
     *
     * @param itemName
     *            监控项
     * @param time
     *            时间，单位为秒
     * @return 所有主机CPU的TOPN排行
     */
    @Select("select a.host host_name,a.value cpu_rate " + "from ("
        + "select DISTINCT(h.host),hi.value from items i,hosts h,history hi " + "where i.name=#{param1} "
        + "and i.hostid=h.hostid " + "and i.itemid=hi.itemid " + "and hi.clock>=#{param2} and hi.value>=#{param3} "
        + "order by hi.value desc,hi.clock desc)" + " as a "
        + "group by a.host ORDER BY a.`value` desc limit #{param4}")
    List<CpuScreen> findComputerConfigMessageforCpu(String itemName, Long time, Integer threshold, Integer num);

    /**
     * @return 按组名进行分组，统计每组异常的数量
     */
    /*  @Select("SELECT a.priority,COUNT(*) count_num " + "FROM (SELECT t.priority,t.description,h.host "
        + "from `triggers` t,`hosts` h,items i,functions f,hosts_groups hg,groups g "
        + "WHERE ${name} and t.triggerid=f.triggerid "
        + "and t.value=1 AND t.status = 0 AND f.itemid=i.itemid AND h.hostid=i.hostid and h.status=0 "
        + "and h.hostid=hg.hostid and hg.groupid=g.groupid GROUP BY t.description,t.priority,h.host)"
        + " a GROUP BY a.priority")*/
    @Select("SELECT a.priority,COUNT(*) count_num FROM (SELECT t.priority,t.description,h.host from "
        + " `triggers` t,`hosts` h,items i,functions f WHERE ${name} and t.triggerid=f.triggerid and "
        + " t.value=1 AND t.status = 0 AND f.itemid=i.itemid AND h.hostid=i.hostid and h.status=0 "
        + " GROUP BY t.description,t.priority,h.host) a GROUP BY a.priority")
    List<PriorityCountScreen> findCountByPriority(@Param("name") String name);

    /**
     *
     * @return 返回异常分组信息
     */
    @Select("SELECT a.priority,COUNT(*) num " + "FROM (SELECT t.priority,t.description,h.host "
        + "from `triggers` t,`hosts` h,items i,functions f " + "WHERE t.triggerid=f.triggerid "
        + "and t.value=1 AND t.status = 0 AND f.itemid=i.itemid AND h.hostid=i.hostid "
        + "and h.status=0 GROUP BY t.description,t.priority,h.host)" + " a GROUP BY a.priority")
    List<PriorityScreen> findCountGroupByPriority();

    /**
     *
     * @return 返回拓扑图的id和名字
     */
    @Select("SELECT sysmapid sysmapId,`name` sysmapName FROM `sysmaps`")
    List<TopologicalScreen> findAllTopologicalInfo();

    /**
     *
     * @return 筛选出交换机组的主机
     */
    /*    @Select("select h.name from hosts h,groups g, hosts_groups hg " + " where h.hostid=hg.hostid "
        + " and g.groupid=hg.groupid and g.name='交换机'")*/
    @Select("select h.name from hosts h where h.host_type=1")
    List<AttentionedHost> selectChangeHostMapper();

    /**
     *
     * @param host
     *            主机
     * @return 主机全部流出流量接口，用于获取接口
     */
    /*    @Select("select h.name,g.name,i.name itemName,i.key_ itemKey" + " from hosts h,groups g,hosts_groups hg,items i "
        + " where h.hostid = hg.hostid and g.groupid = hg.groupid"
        + " and h.hostid = i.hostid and h.name = #{0} and g.name = '交换机' and i.name like '出口流量%'")*/
    @Select("select h.name,i.name itemName,i.key_ itemKey from hosts h,items i where h.hostid = i.hostid "
        + " and h.name = #{0} and h.host_type=1 and i.name like '出口流量%'")
    List<AttentionedHostItem> selectInterfaceByHostMapper(String name);

    /**
     *
     * @param host
     *            主机
     * @param interfaceName
     *            接口名称
     * @param startTime
     *            开始时间戳
     * @param endTime
     *            结束时间戳
     * @return 时间间隔中的流出流量数据
     */
    @Select("select h.name,sum(hu.value) value from hosts h,items i,history_uint hu"
        + " where h.hostid=i.hostid and i.itemid=hu.itemid and h.name=#{0}"
        + " and (i.key_='net.if.out[${interfaceName}]' or i.key_='ifHCOutOctets[${interfaceName}]')"
        + " and hu.clock>=${startTime} and hu.clock<=${endTime}")
    public NetWorkTraffic sumChangeNetWorkTrafficMapper(String name, @Param("interfaceName") String interfaceName,
        @Param("startTime") Long startTime, @Param("endTime") Long endTime);

    /**
     *
     * @param userId
     *            用户name
     * @return 返回用户刷新时间
     */
    @Select("select refresh from users where userid=#{param1}")
    Integer findUserRefreshByUserId(Integer userId);

    /**
     * 查询特定接口的速率
     *
     * @param host
     *            主机
     * @param interfaceName
     *            接口名称
     * @return
     *         特定接口的速率
     */
    @Select("select h.host,i.name itemName,i.key_ itemKey,hi.value value,hi.clock,i.units"
        + " from hosts h,items i,history_uint hi"
        + " where h.hostid = i.hostid and i.itemid = hi.itemid and h.host = #{0}"
        + " and i.key_ = 'ifSpeed[${interfaceName}]' and hi.clock > UNIX_TIMESTAMP(now()) - i.lifetime * 2 "
        + "GROUP BY `value`")
    AttentionedHostItem selectInterfaceSpeedByInterfaceNameMapper(String host,
        @Param("interfaceName") String interfaceName);

}
