/**
 * <p>Copyright:Copyright(c) 2017</p>
 * <p>Company:上海中信信息发展股份有限公司</p>
 * <p>包名:com.cesgroup.cesmonitor.web</p>
 * <p>文件名:ScreenController.java</p>
 * <p>创建时间:2017年6月9日 上午11:18:46</p>
 * <p>作者:Administrator</p>
 */

package com.cesgroup.cesmonitor.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cesgroup.cesmonitor.common.Constant;
import com.cesgroup.cesmonitor.common.Util;
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.NetWorkTraffic;
import com.cesgroup.cesmonitor.screenbean.NumberCount;
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.cesmonitor.service.screen.ScreenService;
import com.zabbix4j.host.HostObject;

/**
 *
 * @author cloud
 * @version 1.0.0 2017年6月9日
 */
@Controller
@RequestMapping(value = "/screen")
public class ScreenController {

    @Autowired
    ScreenService screenService;

    /**
     *
     * @param model
     *
     * @return 返回到大屏界面
     */
    @RequestMapping(value = "index1")
    public String index(Model model) {
        NumberCount nc = screenService.findNumber();

        String host = "无相关交换机";
        if (selectChangeHost().size() != 0) {
            host = selectChangeHost().get(0).getHost();
        }
        String interfaceName = "";
        if (selectInterfaceByHost(host).size() != 0) {
            interfaceName = selectInterfaceByHost(host).get(0).getItemName();
        } else {
            interfaceName = "无相关接口";
        }

        model.addAttribute("host", host);
        model.addAttribute("interfaceName", interfaceName);
        model.addAttribute("MonitorNodeNum", nc.getMonitorNodeNum());
        model.addAttribute("NormalMonitorNodeNum", nc.getNormalMonitorNodeNum());
        model.addAttribute("MonitorAlarmNum", nc.getMonitorAlarmNum());
        model.addAttribute("MonitorAlarmList", screenService.findMonitorAlarmList());
        return "screen/index1";
    }

    /**
     *
     * @return 返回拓扑图信息（sysmapid,name）
     */
    @RequestMapping(value = "topologicalScreen")
    @ResponseBody
    public List<TopologicalScreen> listTopologicalInfo() {
        return screenService.listTopologicalService();
    }

    /**
     *
     * @return 返回可用性列表
     */
    @RequestMapping(value = "useAbilty")
    @ResponseBody
    public List<Usability> findUseAbilty(Integer priority) {
        return screenService.findUseAbilty(priority);
    }

    /**
     *
     * @param model
     *
     * @return 返回到大屏界面
     */
    @RequestMapping(value = "index2")
    public String index2(Model model) {
        return "screen/index2";
    }

    /**
     *
     * @return 返回页面2的数量值
     */
    @RequestMapping(value = "alarmNum")
    public NumberCount findMonitorAlarmNum() {
        NumberCount nc = screenService.findNumber();
        return nc;
    }

    /**
     *
     * @return 返回异常分组信息
     */
    @RequestMapping(value = "countGroupPriority")
    @ResponseBody
    public List<PriorityScreen> findCountGroupByPriority() {
        return screenService.findCountGroupByPriority();
    }

    /**
     *
     * @param limit
     *            取多少条（20或50）
     * @return 返回异常信息
     */
    @RequestMapping(value = "priorityDetailScreen")
    @ResponseBody
    public List<PriorityDetailScreen> findPriorityDetailScreen(Integer priority, Integer limit) {
        List<PriorityDetailScreen> priorityDetailMap = screenService.findPriorityDetailScreen(priority, limit);
        return priorityDetailMap;
    }

    /**
     *
     * @param model
     *
     * @return 返回到大屏界面
     */
    @RequestMapping(value = "index3")
    public String index3(Model model) {
        //被关注主机
        String host = null;
        if (screenService.selectSingleHostByHostGroupNameService(Constant.PC) != null) {
            host = screenService.selectSingleHostByHostGroupNameService(Constant.PC).getHost();
        }
        //被关注主机的相关数据
        AttentionedHost attentionedHost = selectAttentionedHostInfo(host);
        //磁盘利用率数据
        List<AttentionedHostItem> diskUsageRates = selectDiskUsageRate(host);
        //接口状态数据
        List<AttentionedHostItem> interfaceStatusItems = selectInterfaceStatus(host);

        //被关注的网络设备
        String secondHost = null;
        if (screenService.selectSingleHostByHostGroupNameService(Constant.SWITCH_BOARD) != null) {
            secondHost = screenService.selectSingleHostByHostGroupNameService(Constant.SWITCH_BOARD).getHost();
        }
        AttentionedHost secondAttentionedHost = selectAttentionedHostInfo(secondHost);
        List<AttentionedHostItem> interfaceIncomings = selectInterfaceIncoming(secondHost);
        List<AttentionedHostItem> interfaceOutgoings = selectInterfaceOutgoing(secondHost);

        AttentionedHostItem firstInterfaceIncoming = interfaceIncomings.get(0);
        if (firstInterfaceIncoming.getValue() == null) {
            firstInterfaceIncoming.setValue(0.0);
        } else {
            firstInterfaceIncoming.setValue(Util.round(firstInterfaceIncoming.getValue() / 1024, 2));
        }
        AttentionedHostItem firstInterfaceOutgoing = selectInterfaceOutgoingByInterfaceName(
            firstInterfaceIncoming.getHost(), firstInterfaceIncoming.getItemName());
        if (firstInterfaceOutgoing.getValue() == null) {
            firstInterfaceOutgoing.setValue(0.0);
        } else {
            firstInterfaceOutgoing.setValue(Util.round(firstInterfaceOutgoing.getValue() / 1024, 2));
        }
        AttentionedHostItem icmppingLossItem = selectIcmppingLoss(secondHost);

        //被关注主机
        model.addAttribute("attentionedHost", attentionedHost);
        model.addAttribute("diskUsageRates", diskUsageRates);
        model.addAttribute("interfaceStatusItems", interfaceStatusItems);

        //网络设备
        model.addAttribute("secondAttentionedHost", secondAttentionedHost);
        model.addAttribute("interfaceIncomings", interfaceIncomings);
        model.addAttribute("interfaceOutgoings", interfaceOutgoings);
        model.addAttribute("firstInterfaceIncoming", firstInterfaceIncoming);
        model.addAttribute("firstInterfaceOutgoing", firstInterfaceOutgoing);
        model.addAttribute("icmppingLossItem", icmppingLossItem);
        return "screen/index3";
    }

    /**
     *
     * @param limit
     *            取多少条
     * @return CPU利用率
     */
    @RequestMapping(value = "hostsCpu")
    @ResponseBody
    public List<CpuScreen> allHostsCpu(Integer threshold, Integer limit) {
        return screenService.allHostsCpu(threshold, limit);
    }

    /**
     *
     * @param name
     *            组名
     * @return 根据组名返回异常分布信息
     */
    @RequestMapping(value = "countByName")
    @ResponseBody
    public List<PriorityCountScreen> findCountByName(String name) {
        return screenService.findCountByName(name);
    }

    /**
     * 统计时间段内的网络流量
     *
     * @param host
     *            主机
     * @return 网络流量
     */
    @RequestMapping(value = "sumNetWorkTraffic")
    @ResponseBody
    public List<NetWorkTraffic> sumNetWorkTraffic(String host) {
        return screenService.sumNetWorkTrafficService(host);
    }

    /**
     * 统计时间段内的异常告警数量
     *
     * @param interval
     *            时间间隔
     * @return 时间段内的异常告警数量
     */
    @RequestMapping(value = "countAbnormalAlarmScreen")
    @ResponseBody
    public List<AbnormalAlarmScreen> countAbnormalAlarmScreen(Integer interval) {
        return screenService.countAbnormalAlarmScreenService(interval);
    }

    /**
     * 被关注主机的一些信息
     *
     * @param host
     *            主机
     * @return 主机信息
     */
    @RequestMapping(value = "selectAttentionedHostInfo")
    @ResponseBody
    public AttentionedHost selectAttentionedHostInfo(String host) {
        return screenService.selectAttentionedHostInfoService(host);
    }

    /**
     * CPU使用率
     *
     * @param host
     *            主机
     * @return cpu数据
     */
    @RequestMapping(value = "selectCpuUsageRate")
    @ResponseBody
    public AttentionedHostItem selectCpuUsageRate(String host) {
        return screenService.selectCpuUsageRateService(host);
    }

    /**
     * 内存使用率
     *
     * @param host
     *            主机
     * @return 内存数据
     */
    @RequestMapping(value = "selectMemoryUsageRate")
    @ResponseBody
    public AttentionedHostItem selectMemoryUsageRate(String host) {
        return screenService.selectMemoryUsageRateService(host);
    }

    /**
     * 磁盘使用率
     *
     * @param host
     *            主机
     * @return 磁盘数据
     */
    @RequestMapping(value = "selectDiskUsageRate")
    @ResponseBody
    public List<AttentionedHostItem> selectDiskUsageRate(String host) {
        return screenService.selectDiskUsageRateService(host);
    }

    /**
     * 接口状态
     *
     * @param host
     *            主机
     * @return 接口数据
     */
    @RequestMapping(value = "selectInterfaceStatus")
    @ResponseBody
    public List<AttentionedHostItem> selectInterfaceStatus(String host) {
        return screenService.selectInterfaceStatusService(host);
    }

    /**
     * 接口流入流量数据
     *
     * @param host
     *            主机
     * @return 流入流量数据
     */
    @RequestMapping(value = "selectInterfaceIncoming")
    @ResponseBody
    public List<AttentionedHostItem> selectInterfaceIncoming(String host) {
        return screenService.selectInterfaceIncomingService(host);
    }

    /**
     * 特定接口的流入流量数据
     *
     * @param host
     *            主机
     * @param interfaceName
     *            接口
     * @return 特定接口的流入流量数据
     */
    @RequestMapping(value = "selectInterfaceIncomingByInterfaceName")
    @ResponseBody
    public AttentionedHostItem selectInterfaceIncomingByInterfaceName(String host, String interfaceName) {
        return screenService.selectInterfaceIncomingByInterfaceNameService(host, interfaceName);
    }

    /**
     * 接口流出流量数据
     *
     * @param host
     *            主机
     * @return 流入流量数据
     */
    @RequestMapping(value = "selectInterfaceOutgoing")
    @ResponseBody
    public List<AttentionedHostItem> selectInterfaceOutgoing(String host) {
        return screenService.selectInterfaceOutgoingService(host);
    }

    /**
     * 特定接口的流出流量数据
     *
     * @param host
     *            主机
     * @param interfaceName
     *            接口
     * @return 特定接口的流出流量数据
     */
    @RequestMapping(value = "selectInterfaceOutgoingByInterfaceName")
    @ResponseBody
    public AttentionedHostItem selectInterfaceOutgoingByInterfaceName(String host, String interfaceName) {
        return screenService.selectInterfaceOutgoingByInterfaceNameService(host, interfaceName);
    }

    /**
     * 丢包率数据
     *
     * @param host
     *            主机
     * @return 丢包率数据
     */
    @RequestMapping(value = "selectIcmppingLoss")
    @ResponseBody
    public AttentionedHostItem selectIcmppingLoss(String host) {
        return screenService.selectIcmppingLossService(host);
    }

    /**
     * 获取特定主机组所属的全部主机
     *
     * @param group
     *            主机组名称
     * @return 特定主机组所属的全部主机
     */
    @RequestMapping(value = "selectHostByHostGroupName")
    @ResponseBody
    public List<HostObject> selectHostByHostGroupName(String group) {
        return screenService.selectHostByHostGroupNameService(group);
    }

    /**
     * 获取特定主机组所属的全部主机，并包括ip
     *
     * @param group
     *            主机组名称
     * @return 特定主机组所属的全部主机
     */
    @RequestMapping(value = "selectInfoByGroupName")
    @ResponseBody
    public List<AttentionedHost> selectInfoByGroupName(String group) {
        return screenService.selectInfoByGroupNameService(group);
    }

    /**
     * 获取主机组单个主机
     *
     * @param group
     *            主机组
     * @return 单个主机
     */
    @RequestMapping(value = "selectSingleHostByHostGroupName")
    @ResponseBody
    public AttentionedHost selectSingleHostByHostGroupName(String group) {
        return screenService.selectSingleHostByHostGroupNameService(group);
    }

    /**
     *
     * @return 交换机组下的主机
     */
    @RequestMapping(value = "selectChangeHost")
    @ResponseBody
    public List<AttentionedHost> selectChangeHost() {
        return screenService.selectChangeHostService();
    }

    /**
     *
     * @param host
     *            主机
     * @return 该主机下被监控的接口
     */
    @RequestMapping(value = "selectInterfaceByHost")
    @ResponseBody
    public List<AttentionedHostItem> selectInterfaceByHost(String name) {
        return screenService.selectInterfaceByHostService(name);
    }

    /**
     *
     * @param host
     *            主机
     * @param interfaceName
     *            接口名称
     * @param interval
     *            时间间隔
     * @return 时间间隔内的流量
     */
    @RequestMapping(value = "sumChangeNetWorkTraffic")
    @ResponseBody
    public List<NetWorkTraffic> sumChangeNetWorkTraffic(String name, String interfaceName, Integer interval) {
        return screenService.sumChangeNetWorkTrafficService(name, interfaceName, interval);
    }

    /**
     *
     * @param host
     *            主机
     * @param interfaceName
     *            接口名称
     * @param interval
     *            时间间隔
     * @return 时间间隔内的流量,单个数据
     */
    @RequestMapping(value = "sumSingleChangeNetWorkTraffic")
    @ResponseBody
    public NetWorkTraffic sumSingleChangeNetWorkTraffic(String name, String interfaceName, Integer interval) {
        return screenService.sumSingleChangeNetWorkTrafficService(name, interfaceName, interval);
    }

    /**
     *
     * @return 返回当前用户刷新时间
     */
    @RequestMapping(value = "getUserRefresh")
    @ResponseBody
    public Integer getUserRefresh(Integer userId) {
        return screenService.findUserRefresh(userId);
    }
    
    /**
     * 特定接口的速率
     *
     * @param host
     *            主机
     * @param interfaceName
     *            接口
     * @return 特定接口的速率
     */
    @RequestMapping(value = "selectInterfaceSpeedByInterfaceName")
    @ResponseBody
    public AttentionedHostItem selectInterfaceSpeedByInterfaceName(String host, String interfaceName) {
        return screenService.selectInterfaceBySpeedInterfaceNameService(host, interfaceName);
    }
    
    
}
