package com.cesgroup.cesmonitor.dao;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.cesgroup.cesmonitor.bean.Alert;
import com.cesgroup.cesmonitor.bean.DiskUsageInfo;
import com.cesgroup.cesmonitor.bean.EventInfo;
import com.cesgroup.cesmonitor.bean.FaultWarningDto;
import com.cesgroup.cesmonitor.bean.InterfaceInfo;
import com.cesgroup.cesmonitor.bean.OneHostDetailInfo;
import com.cesgroup.cesmonitor.bean.OneHostUsedRateInfo;
import com.cesgroup.cesmonitor.bean.ResourceAlarmInfo;
import com.cesgroup.cesmonitor.bean.TopologicalInfo;
import com.cesgroup.cesmonitor.bean.WebMonitorStatus;
import com.cesgroup.framework.base.dao.IBaseDao;

/**
 *
 *
 * @author cloud
 * @version 1.0.0 2017年5月10日
 */
public interface MonitorConfigMapper extends IBaseDao<Object> {

    /**
     * 查询主机详细信息中的主机状态、主机名称、描述信息
     *
     * @param hostId
     *
     * @return 返回指定主机的主机状态、主机名称、描述信息
     */
    @Select("select h.status,h.name,h.description from hosts h where h.hostid=#{param1} and h.status=0")
    OneHostDetailInfo findPartDetailMsgByHostId(Long hostId);

    /**
     *
     * @param hostId
     *
     * @return 返回指定的IP地址信息
     */
    @Select("select distinct i.ip from interface i where i.hostid=#{param1}")
    List<String> findIpByHostId(Long hostId);

    /**
     *
     * @param itemName
     *            监控项名字
     * @param hostId
     *
     * @param time
     *
     * @return 返回具体某一台主机的操作系统
     */
    @Select("select hi.value from items i,hosts h,history_str hi where i.name=#{param1} and"
        + " h.hostid=#{param2} and h.hostid=i.hostid and i.itemid=hi.itemid and hi.clock>=#{param3}"
        + " and h.status=0 ORDER BY hi.clock DESC limit 1")
    String findOsforHostByHostId(String itemName, Long hostId, Long time);

    /**
     *
     * @param itemName
     *            系统时间
     * @param hostId
     *
     * @param time
     *
     * @return 返回具体某一台主机的启动时间
     */
    @Select("select hi.value from items i,hosts h,history_uint hi where i.name=#{param1} "
        + "and h.hostid=#{param2} and h.hostid=i.hostid and i.itemid=hi.itemid and hi.clock>=#{param3} "
        + "and h.status=0 ORDER BY hi.clock DESC limit 1")
    String findStartTimeByHostId(String itemName, Long hostId, Long time);

    /**
     * @param itemName
     *            系统运行时间
     * @param hostId
     *
     * @param time
     *
     * @return 返回具体某一台主机的系统运行时间，即持续时间
     */
    @Select("select hi.value from items i,hosts h,history_uint hi where i.name=#{param1} and h.status=0 "
        + "and h.hostid=#{param2} and h.hostid=i.hostid and i.itemid=hi.itemid and hi.clock>=#{param3} "
        + "ORDER BY hi.clock DESC limit 1")
    String findUpTimeByHostId(String itemName, Long hostId, Long time);

    /**
     *
     * @param itemName
     *
     * @param hostId
     *
     * @param time
     *
     * @return 返回具体某一台主机的CPU使用率
     */
    @Select("select hi.value cpuRate,i.itemid cpuRateItemId from items i,hosts h,history hi where i.name=#{param1} "
        + "and h.hostid=#{param2} and h.hostid=i.hostid and i.itemid=hi.itemid and hi.clock>=#{param3} "
        + "and h.status = 0 "
        + "ORDER BY hi.clock DESC limit 1")
    OneHostUsedRateInfo findCpuRateByHostId(String itemName, Long hostId, Long time);

    /**
     *
     * @param itemName
     *
     * @param hostId
     *
     * @param time
     *
     * @return snmp监控模式下CPU使用率是通过自动扫描获取的，会有多个值
     */
    @Select("select avg(h.value) from history h,hosts ho,items i where h.clock>=#{param3}  "
        + "and i.name LIKE #{param1} and i.itemid=h.itemid and i.hostid=#{param2} AND ho.hostid = i.hostid "
        + " AND ho.`status` = 0 AND i.`status` = 0")
    Double findAvgCpuRateByHostId(String itemName, Long hostId, Long time);

    /**
     * 这个查询是因为这个历史记录表的信息类型是数字（无符号），表是history_uint
     *
     * @param itemName
     *
     * @param hostId
     *
     * @param time
     *
     * @return 返回具体某一台主机的配置信息（总内存）
     */
    @Select("select hi.value from items i,hosts h,history_uint hi where i.name=#{param1} "
        + "and h.hostid=#{param2} and h.hostid=i.hostid and i.itemid=hi.itemid group by a.`HOST`")
    Long findTotalMemoryByHostId(String itemName, Long hostId);

    /**
     * 这个查询是因为这个历史记录表的信息类型是数字（无符号），表是history_uint
     *
     * @param itemName
     *
     * @param hostId
     *
     * @param time
     *
     * @return 返回具体某一台主机的配置信息（可用内存）
     */
    @Select("select hi.value from items i,hosts h,history_uint hi where i.name=#{param1} "
        + "and h.hostid=#{param2} and h.hostid=i.hostid  and i.itemid=hi.itemid and hi.clock>=#{param3}"
        + " ORDER BY hi.clock DESC LIMIT 1")
    Long findFreeMemoryByHostId(String itemName, Long hostId, Long time);

    /**
     * 这个查询是因为这个历史记录表的信息类型是数字（无符号），表是history
     *
     * @param itemName
     *
     * @param hostId
     *
     * @param time
     *
     * @return 返回具体某一台主机的配置信息（内存使用率）
     */
    @Select("select hi.value memoryRate,i.itemid memoryRateItemId from items i,hosts h,history hi "
        + "where i.name=#{param1} "
        + "and h.hostid=#{param2} and h.hostid=i.hostid and h.status = 0 and i.itemid=hi.itemid and hi.clock>=#{param3}"
        + " ORDER BY hi.clock DESC LIMIT 1")
    OneHostUsedRateInfo findMemoryRateByHostId(String itemName, Long hostId, Long time);

    @Select("select graphid cpuRateGraphId from graphs_items where itemid=#{param1}")
    List<String> findcpuGraphIdByItemId(String itemId);

    @Select("select graphid memoryRateGraphId from graphs_items where itemid=#{param1}")
    List<String> findMemoryGraphIdByItemId(String itemId);

    //snmp查询内存使用率的
    @Select("select DISTINCT graphid memoryRateGraphId from items i,graphs_items g,hosts h where i.name "
        + " LIKE #{param1} and h.hostid=#{param2} and h.hostid=i.hostid and h.status = 0 and "
        + " i.`status` = 0 and i.itemid=g.itemid LIMIT 1")
    String findPhysicalMemoryGraphIdByHostId(String itemName, Long hostId);

    //snmp查询CPU使用率的
    @Select("select DISTINCT graphid cpuRateGraphId from items i,graphs_items g,hosts h where i.name "
        + " LIKE #{param1} and h.hostid=#{param2} and h.hostid=i.hostid and h.status = 0 and "
        + " i.`status` = 0 and i.itemid=g.itemid LIMIT 1")
    String findsnmpCpuGraphIdByHostId(String itemName, Long hostId);

    /**
     *
     * @param itemName
     *
     * @param hostId
     *
     * @param time
     *
     * @return 返回具体某一台主机24小时之内的响应时间
     */
    @Select("select hi.value from items i,hosts h,history hi where i.name=#{param1} "
        + "and h.hostid=#{param2} and h.hostid=i.hostid and h.status = 0 and i.itemid=hi.itemid "
        + "and hi.clock>=#{param3}")
    List<Double> findResponseTimeByHostId(String itemName, Long hostId, Long time);

    /**
     *
     * @param itemName
     *
     * @param hostId
     *
     * @param time
     *
     * @return 返回具体某一台主机的丢包率
     */
    @Select("select hi.value from items i,hosts h,history hi where i.name=#{param1} "
        + "and h.hostid=#{param2} and h.hostid=i.hostid and h.status = 0 "
        + "and i.itemid=hi.itemid and hi.clock>=#{param3} "
        + "ORDER BY hi.clock DESC limit 1")
    Double findPacketLossByHostId(String itemName, Long hostId, Long time);

    /**
     *
     * @param hostId
     *
     * @return 返回web监控状态的信息
     */
    /* @Select("SELECT a.`NAME` name, b.stepNo step_no, a.nextcheck recent_time, a.`STATUS` status,"
        + "a.httptestid httptestid FROM "
        + "( SELECT t.NAME, t.nextcheck, t.STATUS, t.httptestid FROM httptest t WHERE t.hostid = ${hostid} ) a "
        + "LEFT JOIN ( SELECT count(httpstepid) stepNo, httptestid FROM httpstep GROUP BY httptestid ) b "
        + "ON a.httptestid = b.httptestid")*/
    @Select("SELECT DISTINCT a.`name` name,b.stepNo step_no,a.nextcheck recent_time,a.`STATUS` STATUS, "
        + "a.httptestid httptestid,c.VALUE info,b.stepName stepName "
        + "FROM(SELECT t. NAME,t.nextcheck,t. STATUS,t.httptestid FROM httptest t WHERE t.hostid = ${hostid}) a "
        + "left JOIN(SELECT count(httpstepid) stepNo,name stepName,httpstepid,httptestid FROM httpstep "
        + "GROUP BY httptestid) b ON a.httptestid = b.httptestid "
        + "LEFT JOIN(SELECT hti.httptestid,hti.type,i.itemid,i.key_,his.`value`,his.clock "
        + "FROM httptestitem hti,items i,httptest ht,history_str his "
        + "WHERE hti.httptestid = ht.httptestid AND hti.itemid = i.itemid "
        + "AND hti.itemid = his.itemid "
        + "ORDER BY his.clock desc limit 30) c ON  c.httptestid = b.httptestid group BY a.`NAME` order by a.httptestid")
    List<WebMonitorStatus> findWebMonitorStatusByHostId(@Param("hostid") Long hostId);

    /**
     *
     * @param hostId
     *
     * @return 返回资源报警信息
     */
    @Select("SELECT h.name host , t.description ,t.priority,"
        + " t.comments messages, t.state action, t.status, e.objectid FROM `triggers` t, `events` e, "
        + " functions f, items i, `hosts` h WHERE e.object = 0 AND e.source= 0 AND h.hostid = #{param1} AND "
        + " h.hostid =i.hostid AND e.objectid = t.triggerid AND f.triggerid = t.triggerid "
        + "AND f.itemid = i.itemid AND h.status = 0 "
        + " AND t.`value` = 1 AND t.status = 0 GROUP BY h.host,t.description ORDER BY e.clock DESC LIMIT 20")
    List<ResourceAlarmInfo> findResouceAlamInfoByHostId(Long hostId);

    /**
     * @return 返回所有资源报警信息
     */
    @Select("SELECT h.hostid hostId, h.`name` hostName, t.description warningMessage,t.priority warningLevel,"
        + "e.value faultStatus,t.triggerid triggerId,e.objectid 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 e.objectid = t.triggerid "
        + "AND f.triggerid = t.triggerid AND f.itemid = i.itemid AND t.`value` = 1 And e.clock>=#{param1} AND "
        + "e.clock<#{param2} GROUP BY h.host,t.description ORDER BY e.clock")
    List<FaultWarningDto> listsAlamInfo(Long startTime, Long endTime);

    /**
     *
     * @param eventId
     *            事件id
     * @return 返回报警
     */
    @Select("select FROM_UNIXTIME(a.clock) time,u.alias,u.name,u.surname,mt.description,a.status,a.error "
        + " from alerts a,users u,media_type mt " + " where a.eventid = #{0} and a.userid = u.userid "
        + " and a.mediatypeid = mt.mediatypeid order by a.clock desc")
    List<Alert> findResourceAlertByEventId(Integer eventId);

    /**
     *
     * @param eventid
     *            event id
     * @return 返回eventid报警的次数
     */
    @Select("select count(*) from alerts a where a.eventid = #{0}")
    Integer countAlertTime(Integer eventid);

    /**
     *
     * @param objectId
     *            触发器ID
     * @return 事件是否确认
     */
    @Select("SELECT e.acknowledged from `events` e where e.objectid=#{param1} "
        + "and e.source=0 and e.object=0 AND e.`value`=1 ORDER BY"
        + " e.clock desc limit 1")
    //修改了
    String findAcknowledgeByObjectId(Integer objectId);

    /**
     *
     * @param objectid
     *            触发器id
     * @return 根据objectid查询出eventids
     */
    @Select("SELECT eventid from `events` where objectid= #{param1} "
        + "and object=0 and source=0 and value=1 ORDER BY clock desc")
    List<String> findEventidsByObjectid(Integer objectid);

    /**
     *
     * @param objectid
     *            触发器id
     * @return 根据objectid查询出活动信息
     */
    @Select("SELECT clock startClock ,value status,acknowledged from events e WHERE objectid=#{param1} and object=0 "
        + "and source=0 ORDER BY clock DESC LIMIT 20")
    List<EventInfo> findEventInfoByObjectid(Integer objectid);

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

    /**
     *
     * @param objectid
     *
     * @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")
    Date findWarningTime(Integer objectid);

    /**
     *
     * @param itemName
     *            空闲率%
     * @param hostId
     *
     * @param time
     *
     * @return 返回磁盘空闲率
     */
    @Select("SELECT a.key_ drive_letter,a.value free_rate from(select i.key_,h.value from `hosts` ho,items i,history h "
        + "WHERE i.name LIKE #{param1} AND h.clock>=#{param3} AND ho.hostid =#{param2} AND ho.hostid=i.hostid and "
        + "i.itemid = h.itemid and ho.`status`=0 and i.`status`=0 ORDER BY h.clock DESC)as a GROUP BY a.key_")
    List<DiskUsageInfo> findFreeRateByHostId(String itemName, Long hostId, Long time);

    /**
     *
     * @param itemName
     *            总大小%
     * @param hostId
     *
     * @param time
     *
     * @return 返回主机的磁盘总容量
     */
    @Select("SELECT a.itemid total_itemid, a.key_ drive_letter,a.value total_capacity from(SELECT i.itemid,i.key_ ,"
        + "h.VALUE FROM `hosts` ho,items i,history_uint h WHERE i. NAME LIKE #{param1} AND h.clock >=#{param3} "
        + "AND ho.hostid = #{param2} AND ho.hostid = i.hostid AND i.itemid = h.itemid AND ho.`status` = 0 AND "
        + "i.`status` = 0 ORDER BY h.clock DESC)as a GROUP BY a.key_ ")
    List<DiskUsageInfo> findTotalCapacityByHostId(String itemName, Long hostId, Long time);

    /**
     *
     * @param itemName
     *            已使用%
     * @param hostId
     *
     * @param time
     *
     * @return 返回主机磁盘的使用量
     */
    @Select("SELECT a.itemid usage_itemid, a.key_ drive_letter,a.value used_capacity from(select i.itemid,i.key_ ,"
        + "h.value from `hosts` ho,items i,history_uint h WHERE i.name LIKE #{param1} AND h.clock>=#{param3} "
        + "AND ho.hostid = #{param2} AND ho.hostid=i.hostid and i.itemid = h.itemid and ho.`status`=0 and i.`status`=0 "
        + "ORDER BY h.clock DESC)as a GROUP BY a.key_")
    List<DiskUsageInfo> findUsedCapacityByHostId(String itemName, Long hostId, Long time);

    /**
     *
     * @param totalItemid
     *            磁盘总容量监控项itemId
     * @param usageItemid
     *            磁盘使用量监控项itemId
     * @return 返回磁盘图形graphId
     */
    @Select("select g1.graphid from graphs_items g1,graphs_items g2 where g1.itemid =#{param1} and "
        + "g2.itemid =#{param2} and g1.graphid = g2.graphid")
    List<String> findGraphidByItemids(String totalItemid, String usageItemid);

    /**
     *
     * @param itemName
     *            进口流量%
     * @param hostId
     *
     * @param time
     *
     * @return 返回主机的接口信息（接口状态、接口名称、进流量、状态更改时间）
     */
    @Select("select a.key_ interface_name,a.value input_flow,a.clock status_changed_time,a.status,a.inputItemid from "
        + "(select i.key_,i.status,h.value,h.clock,i.itemid inputItemid from items i,history_uint h,hosts ho"
        + " WHERE i.name LIKE #{param1} and i.hostid=#{param2} and (i.flags=4 or i.flags=0) and i.itemid=h.itemid"
        + " AND ho.hostid=i.hostid and ho.`status`=0 and h.clock>=#{param3} "
        + "ORDER BY h.clock DESC)as a GROUP BY a.key_")
    List<InterfaceInfo> findInputFlowInfoByHostId(String itemName, Long hostId, Long time);

    /**
     *
     * @param itemName
     *            出口流量%
     * @param hostId
     *
     * @param time
     *
     * @return 返回主机的接口信息（接口名称、出流量）
     */
    @Select("select a.key_ interface_name,a.value output_flow,a.outputItemid "
        + "from (select i.key_,h.value,i.itemid outputItemid from items i,history_uint h ,hosts ho "
        + "WHERE i.name LIKE #{param1} AND i.hostid=#{param2} and (i.flags=4 or i.flags=0) "
        + "and i.itemid=h.itemid AND ho.hostid=i.hostid and ho.`status`=0 "
        + "and h.clock>=#{param3} ORDER BY h.clock DESC)as a GROUP BY a.key_")
    List<InterfaceInfo> findOutputFlowInfoByHostId(String itemName, Long hostId, Long time);

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

    /**
     *
     * @param sysmapName
     *            拓扑图的名字
     * @return 返回拓扑图的id和名字
     */
    @Select("select sysmapid sysmapId,name sysmapName from sysmaps where name = #{0}")
    TopologicalInfo findToplogicalInfoByName(String sysmapName);

    /**
     *
     * @param serviceId
     *            ITservice id
     * @return 返回拓扑图的id和名字
     */
    @Select("select sys.sysmapid sysmapId,sys.name from services s,sysmaps sys"
        + " where s.serviceid=#{0} and s.name=sys.name")
    TopologicalInfo findToplogicalInfoByServiceId(Integer serviceId);
}
