/**
 * <p>Copyright:Copyright(c) 2017</p>
 * <p>Company:上海中信信息发展股份有限公司</p>
 * <p>包名:com.cesgroup.cesmonitor.web</p>
 * <p>文件名:InterfaceIncomingReportController.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.InterfaceIncomingReport;
import com.cesgroup.cesmonitor.common.Util;
import com.cesgroup.cesmonitor.service.InterfaceIncomingReportService;
import com.itextpdf.text.pdf.PdfPTable;

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

    @Autowired
    private InterfaceIncomingReportService interfaceIncomingReportService;

    /**
     *
     * @param model
     *            模型
     * @return 跳转到指定页面
     */
    @RequestMapping(value = "interfaceIncoming")
    public String interfaceIncomingView(final Model model) {
        long startTime = System.currentTimeMillis(); //获取开始时间

        String group = "1 = 1";
        Integer interval = 24;
        Integer topNum = 5;
        List<InterfaceIncomingReport> multicycleHighInterfaceIncomingReports = selectMulticycleHighInterfaceIncomingReport(
            group, interval, topNum);

        List<InterfaceIncomingReport> multicycleLowInterfaceIncomingReports = selectMulticycleLowInterfaceIncomingReport(
            group, interval, topNum);

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

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

        model.addAttribute("multicycleHighInterfaceIncomingReports", multicycleHighInterfaceIncomingReports);
        model.addAttribute("multicycleLowInterfaceIncomingReports", multicycleLowInterfaceIncomingReports);
        model.addAttribute("topNum", topNum);

        return "report/interfaceIncoming";
    }

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

    }

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

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

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

    /**
     * 导出pdf文件
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     */
    @RequestMapping("interfaceIncomingExport")
    public void interfaceIncomingExport(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<InterfaceIncomingReport> multicycleHighInterfaceIncomingReports;
        List<InterfaceIncomingReport> multicycleLowInterfaceIncomingReports;

        if (interval != 12) {
            multicycleHighInterfaceIncomingReports = interfaceIncomingReportService
                .selectMulticycleHighInterfaceIncomingReportService(group, interval, topNum);
            multicycleLowInterfaceIncomingReports = interfaceIncomingReportService
                .selectMulticycleLowInterfaceIncomingReportService(group, interval, topNum);
        } else {
            multicycleHighInterfaceIncomingReports = interfaceIncomingReportService
                .selectMulticycleHighInterfaceIncomingReportServiceByDate(group, interval, topNum, startDate, endDate);
            multicycleLowInterfaceIncomingReports = interfaceIncomingReportService
                .selectMulticycleLowInterfaceIncomingReportServiceByDate(group, interval, topNum, startDate, endDate);
        }

        PdfPTable[] table = { InterfaceIncomingReportService.createTable(multicycleHighInterfaceIncomingReports),
            InterfaceIncomingReportService.createTable(multicycleLowInterfaceIncomingReports) };
        ServletContext context = request.getSession().getServletContext();
        String exportPdf = context.getRealPath("/exportPdf/");
        //查看是否存在该目录，如不存在则创建
        File file = new File(exportPdf);
        if (!file.exists()) {
            file.mkdir();
        }
        /*        try {
        	filename = new String(filename.getBytes(),"GBK");
        		} catch (UnsupportedEncodingException e) {
        			// TODO Auto-generated catch block
        			e.printStackTrace();
        		}*/

        InterfaceIncomingReportService.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 = "selectHighInterfaceIncomingReportByDate")
    @ResponseBody
    public List<InterfaceIncomingReport> selectHighInterfaceIncomingReportByDate(final String group,
        final Integer interval,
        final Integer topNum, final String startDate, final String endDate) {
        return interfaceIncomingReportService.selectHighInterfaceIncomingReportServiceByDate(group, interval, topNum,
            startDate, endDate);
    }

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

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

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