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.AbnormalAlarmReport;
import com.cesgroup.framework.base.dao.IBaseDao;

/**
 * 异常告警 dao
 *
 * @author user
 * @version 1.0.0 2017年5月11日
 */
public abstract interface AbnormalAlarmReportMapper extends IBaseDao<AbnormalAlarmReport> {

    /**
     *
     * @param group
     *            主机组
     * @param host
     *            主机
     * @param priority
     *            严重性
     * @param startTime
     *            开始时间戳
     * @param endTime
     *            结束时间戳
     * @param groupBy
     *            group by条件
     * @return 统计异常告警
     */
    @Select({ "select g.name,h.name hname,h.host,t.priority,count(*) countPriority "
        + " from hosts h,groups g,hosts_groups hg,items i,functions f,triggers t,events e "
        + " where h.hostid = hg.hostid and g.groupid = hg.groupid "
        + " and h.hostid = i.hostid and i.itemid = f.itemid and f.triggerid = t.triggerid "
        + " and e.objectid = t.triggerid and ${group} and ${host} and ${priority} "
        + " and e.object = 0 and e.value = 1 and e.source = 0 "
        + " and e.clock >= ${startTime} and e.clock <= ${endTime} group by ${groupBy}" })
    public abstract List<AbnormalAlarmReport> countAbnormalAlarm(@Param("group") String group,
        @Param("host") String host, @Param("priority") String priority, @Param("startTime") Long startTime,
        @Param("endTime") Long endTime, @Param("groupBy") String groupBy);

    /**
     *
     * @param group
     *            主机组
     * @param host
     *            主机
     * @param priority
     *            严重性
     * @param startTime
     *            开始时间戳
     * @param endTime
     *            结束时间戳
     * @return 查询出异常告警
     */
    @Select({ "select g.name,h.name hname,h.host,t.description,t.priority,count(*) countPriority "
        + " from hosts h,groups g,hosts_groups hg,items i,functions f,triggers t,events e "
        + " where h.hostid = hg.hostid and g.groupid = hg.groupid and 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 ${group} and ${host} and ${priority} and e.clock >= #{3} and e. clock <= #{4}"
        + " group by h.host,t.description,t.priority order by countPriority desc" })
    public abstract List<AbnormalAlarmReport> selectTopAbnormalAlarm(@Param("group") String group,
        @Param("host") String host, @Param("priority") String priority, Long startTime, Long endTime);

    @Select({ "select g.name,h.name hname,h.host,t.description,t.priority,count(*) countPriority "
        + "from hosts h,groups g,hosts_groups hg,items i,functions f,triggers t,events e,users_groups ug "
        + ",users u,usrgrp usrg,rights r where h.hostid = hg.hostid and g.groupid = hg.groupid and 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 ${group} and ${host} and ${priority} and e.clock >= #{3} and "
        + "e. clock <= #{4} AND f.itemid=i.itemid AND h.hostid=i.hostid and h.hostid = hg.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,t.description,t.priority order by countPriority desc" })
    public abstract List<AbnormalAlarmReport> selectTopAbnormalAlarmByUser(@Param("group") String group,
        @Param("host") String host, @Param("priority") String priority, Long startTime, Long endTime,
        @Param("userId") Integer userId);

    @Select({ "select g.name,h.name hname,h.host,t.priority,count(*) countPriority "
        + "from hosts h,groups g,hosts_groups hg,items i,functions f,triggers t,events e ,users_groups ug,users u,"
        + "usrgrp usrg, "
        + "rights r where h.hostid = hg.hostid and g.groupid = hg.groupid and h.hostid = i.hostid and "
        + "i.itemid = f.itemid "
        + "and f.triggerid = t.triggerid and e.objectid = t.triggerid and ${group} and ${host} and ${priority}"
        + " and e.object = 0 "
        + "and e.value = 1 and e.source = 0 and e.clock >= #{3} and e.clock <= #{4} AND f.itemid=i.itemid "
        + "and h.hostid = hg.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 ${groupBy}" })
    public abstract List<AbnormalAlarmReport> countAbnormalAlarmByUser(@Param("group") String group,
        @Param("host") String host, @Param("priority") String priority, Long startTime, Long endTime,
        @Param("userId") Integer userId, @Param("groupBy") String groupBy);
}