/**
 * <p>Copyright:Copyright(c) 2017</p>
 * <p>Company:上海中信信息发展股份有限公司</p>
 * <p>包名:com.cesgroup.cesmonitor.web</p>
 * <p>文件名:InterfaceOutgoingReportController.java</p>
 * <p>创建时间:2017年5月12日 下午7:05:07</p>
 * <p>作者:user</p>
 */

package com.cesgroup.cesmonitor.web;

import java.io.File;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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.bean.InterfaceOutgoingReport;
import com.cesgroup.cesmonitor.common.Util;
import com.cesgroup.cesmonitor.service.InterfaceOutgoingReportService;
import com.itextpdf.text.pdf.PdfPTable;

/**
 *
 * @author user
 * @version 1.0.0 2017年5月12日
 */
@Controller
@RequestMapping(value = "report")
public class InterfaceOutgoingReportController {

    @Autowired
    private InterfaceOutgoingReportService interfaceOutgoingReportService;

    /**
     *
     * @param model
     *            模型
     * @return 跳转到指定页面
     */
    @RequestMapping(value = "interfaceOutgoing")
    public String interfaceOutgoingView(final Model model) {
        String group = "1 = 1";
        Integer interval = 24;
        Integer topNum = 5;
        long startTime2 = System.currentTimeMillis(); //获取开始时间
        List<InterfaceOutgoingReport> multicycleHighInterfaceOutgoingReports =
            selectMulticycleHighInterfaceOutgoingReport(
                group, interval, topNum);

        long endTime2 = System.currentTimeMillis(); //获取结束时间

        System.out.println("MulticycleHigh程序运行时间：" + (endTime2 - startTime2) + "ms"); //输出程序运行时间

        long startTime = System.currentTimeMillis(); //获取开始时间
        List<InterfaceOutgoingReport> multicycleLowInterfaceOutgoingReports =
            selectMulticycleLowInterfaceOutgoingReport(
                group, interval, topNum);
        long endTime = System.currentTimeMillis(); //获取结束时间

        System.out.println("MulticycleLow程序运行时间：" + (endTime - startTime) + "ms"); //输出程序运行时间

        model.addAttribute("multicycleHighInterfaceOutgoingReports", multicycleHighInterfaceOutgoingReports);
        model.addAttribute("multicycleLowInterfaceOutgoingReports", multicycleLowInterfaceOutgoingReports);
        model.addAttribute("topNum", topNum);

        return "report/interfaceOutgoing";
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 高接口流入流量
     */
    @RequestMapping(value = "selectHighInterfaceOutgoingReport")
    @ResponseBody
    public List<InterfaceOutgoingReport> selectHighInterfaceOutgoingReport(final String group, final Integer interval,
        final Integer topNum) {
        /*    	Long deadLine = Util.getDeadLine(interval);
            	String ehcacheKey = "selectHighInterfaceOutgoingReport" + deadLine.toString() + group + interval + topNum;
            	List<InterfaceOutgoingReport> highInterfaceOutgoingReports = (List<InterfaceOutgoingReport>) EhcacheUtil.get(ehcacheKey);
            	if(null == highInterfaceOutgoingReports ){*/
        return interfaceOutgoingReportService.selectHighInterfaceOutgoingReportService(group, interval, topNum);
        /*    	}else{
            		EhcacheUtil.remove(ehcacheKey);
            		return highInterfaceOutgoingReports;
            	}*/
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 低接口流入流量
     */
    @RequestMapping(value = "selectLowInterfaceOutgoingReport")
    @ResponseBody
    public List<InterfaceOutgoingReport> selectLowInterfaceOutgoingReport(final String group, final Integer interval,
        final Integer topNum) {
        /*    	Long deadLine = Util.getDeadLine(interval);
            	String ehcacheKey = "selectLowInterfaceOutgoingReport" + deadLine.toString() + group + interval + topNum;
            	List<InterfaceOutgoingReport> lowInterfaceOutgoingReports = (List<InterfaceOutgoingReport>) EhcacheUtil.get(ehcacheKey);
            	if(null == lowInterfaceOutgoingReports ){*/
        return interfaceOutgoingReportService.selectLowInterfaceOutgoingReportService(group, interval, topNum);
        /*    	}else{
            		EhcacheUtil.remove(ehcacheKey);
            		return lowInterfaceOutgoingReports;
            	}*/
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 本周期，上周期高接口流入流量数据
     */
    @RequestMapping(value = "selectMulticycleHighInterfaceOutgoingReport")
    @ResponseBody
    public List<InterfaceOutgoingReport> selectMulticycleHighInterfaceOutgoingReport(final String group,
        final Integer interval,
        final Integer topNum) {
        return interfaceOutgoingReportService.selectMulticycleHighInterfaceOutgoingReportService(group, interval,
            topNum);
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 本周期，上周期低接口流入流量数据
     */
    @RequestMapping(value = "selectMulticycleLowInterfaceOutgoingReport")
    @ResponseBody
    public List<InterfaceOutgoingReport> selectMulticycleLowInterfaceOutgoingReport(final String group,
        final Integer interval,
        final Integer topNum) {
        return interfaceOutgoingReportService.selectMulticycleLowInterfaceOutgoingReportService(group, interval,
            topNum);
    }

    /**
     * 导出pdf文件
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     */
    @RequestMapping("interfaceOutgoingExport")
    public void interfaceOutgoingExport(final HttpServletRequest request, final HttpServletResponse response,
        final String group,
        final Integer interval, final Integer topNum, final String filename, final String startDate,
        final String endDate) {
        List<String> imageList = Util.getImgList(request, response); //获取图片
        List<InterfaceOutgoingReport> multicycleHighInterfaceOutgoingReports;
        List<InterfaceOutgoingReport> multicycleLowInterfaceOutgoingReports;

        if (interval != 12) {
            multicycleHighInterfaceOutgoingReports = interfaceOutgoingReportService
                .selectMulticycleHighInterfaceOutgoingReportService(group, interval, topNum);
            multicycleLowInterfaceOutgoingReports = interfaceOutgoingReportService
                .selectMulticycleLowInterfaceOutgoingReportService(group, interval, topNum);
        } else {
            multicycleHighInterfaceOutgoingReports = interfaceOutgoingReportService
                .selectMulticycleHighInterfaceOutgoingReportServiceByDate(group, interval, topNum, startDate, endDate);
            multicycleLowInterfaceOutgoingReports = interfaceOutgoingReportService
                .selectMulticycleLowInterfaceOutgoingReportServiceByDate(group, interval, topNum, startDate, endDate);
        }

        PdfPTable[] table = { InterfaceOutgoingReportService.createTable(multicycleHighInterfaceOutgoingReports),
            InterfaceOutgoingReportService.createTable(multicycleLowInterfaceOutgoingReports) };
        ServletContext context = request.getSession().getServletContext();
        String exportPdf = context.getRealPath("/exportPdf/");
        //查看是否存在该目录，如不存在则创建
        File file = new File(exportPdf);
        if (!file.exists()) {
            file.mkdir();
        }
        InterfaceOutgoingReportService.createPDF(exportPdf + filename, imageList, interval, group, topNum, table,
            startDate, endDate);

        String picPath = context.getRealPath("/upload");
        File f = new File(picPath);
        Util.deleteFile(f);
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 高接口流入流量
     */
    @RequestMapping(value = "selectHighInterfaceOutgoingReportByDate")
    @ResponseBody
    public List<InterfaceOutgoingReport> selectHighInterfaceOutgoingReportByDate(final String group,
        final Integer interval,
        final Integer topNum, final String startDate, final String endDate) {
        return interfaceOutgoingReportService.selectHighInterfaceOutgoingReportServiceByDate(group, interval, topNum,
            startDate, endDate);
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 低接口流入流量
     */
    @RequestMapping(value = "selectLowInterfaceOutgoingReportByDate")
    @ResponseBody
    public List<InterfaceOutgoingReport> selectLowInterfaceOutgoingReportByDate(final String group,
        final Integer interval,
        final Integer topNum, final String startDate, final String endDate) {
        return interfaceOutgoingReportService.selectLowInterfaceOutgoingReportServiceByDate(group, interval, topNum,
            startDate, endDate);
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 本周期，上周期高接口流入流量数据
     */
    @RequestMapping(value = "selectMulticycleHighInterfaceOutgoingReportByDate")
    @ResponseBody
    public List<InterfaceOutgoingReport> selectMulticycleHighInterfaceOutgoingReportByDate(final String group,
        final Integer interval,
        final Integer topNum, final String startDate, final String endDate) {
        return interfaceOutgoingReportService.selectMulticycleHighInterfaceOutgoingReportServiceByDate(group, interval,
            topNum, startDate, endDate);
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 本周期，上周期低接口流入流量数据
     */
    @RequestMapping(value = "selectMulticycleLowInterfaceOutgoingReportByDate")
    @ResponseBody
    public List<InterfaceOutgoingReport> selectMulticycleLowInterfaceOutgoingReportByDate(final String group,
        final Integer interval,
        final Integer topNum, final String startDate, final String endDate) {
        return interfaceOutgoingReportService.selectMulticycleLowInterfaceOutgoingReportServiceByDate(group, interval,
            topNum, startDate, endDate);
    }

}
