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

/**
 * 响应时间报表controller
 *
 * @author user
 * @version 1.0.0 2017年5月11日
 */
@Controller
@RequestMapping("report")
public class ResponseTimeReportController {

    @Autowired
    private ResponseTimeReportService responseTimeReportService;

    /**
     *
     * @param model
     *            模型
     * @return 跳转到指定页面
     */
    @RequestMapping("responseTime")
    public String responseTimeView(final Model model) {
        String group = "1 = 1";
        Integer interval = 24;
        Integer topNum = 5;
        List<ResponseTimeReport> multicycleSlowResponseTimeReports = selectMulticycleSlowResponseTime(group, interval,
            topNum);
        List<ResponseTimeReport> multicycleFastResponseTimeReports = selectMulticycleFastResponseTime(group, interval,
            topNum);
        model.addAttribute("multicycleSlowResponseTimeReports", multicycleSlowResponseTimeReports);
        model.addAttribute("multicycleFastResponseTimeReports", multicycleFastResponseTimeReports);
        model.addAttribute("topNum", topNum);
        return "report/responseTime";
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 慢的响应时间
     */
    @RequestMapping("selectSlowResponseTime")
    @ResponseBody
    public List<ResponseTimeReport> selectSlowResponseTime(final String group, final Integer interval,
        final Integer topNum) {
        return responseTimeReportService.selectSlowResponseTimeService(group, interval, topNum);
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 快的响应时间
     */
    @RequestMapping("selectFastResponseTime")
    @ResponseBody
    public List<ResponseTimeReport> selectFastResponseTime(final String group, final Integer interval,
        final Integer topNum) {
        return responseTimeReportService.selectFastResponseTimeService(group, interval, topNum);
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 本周期，上周期的慢的响应时间
     */
    @RequestMapping("selectMulticycleSlowResponseTime")
    @ResponseBody
    public List<ResponseTimeReport> selectMulticycleSlowResponseTime(final String group, final Integer interval,
        final Integer topNum) {
        return responseTimeReportService.selectMulticycleSlowResponseTimeService(group, interval, topNum);
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 本周期，上周期的快的响应时间
     */
    @RequestMapping("selectMulticycleFastResponseTime")
    @ResponseBody
    public List<ResponseTimeReport> selectMulticycleFastResponseTime(final String group, final Integer interval,
        final Integer topNum) {
        return responseTimeReportService.selectMulticycleFastResponseTimeService(group, interval, topNum);
    }

    /**
     * 导出相关pdf文档
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     */
    @RequestMapping("responseTimeExport")
    public void responseTimeExport(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<ResponseTimeReport> multicycleSlowResponseTimes;
        List<ResponseTimeReport> multicycleFastResponseTimes;
        if (interval == 12) {
            multicycleSlowResponseTimes = responseTimeReportService
                .selectMulticycleSlowResponseTimeServiceByDate(group, interval, topNum, startDate, endDate);
            multicycleFastResponseTimes = responseTimeReportService
                .selectMulticycleFastResponseTimeServiceByDate(group, interval, topNum, startDate, endDate);
        } else {
            multicycleSlowResponseTimes = responseTimeReportService
                .selectMulticycleSlowResponseTimeService(group, interval, topNum);
            multicycleFastResponseTimes = responseTimeReportService
                .selectMulticycleFastResponseTimeService(group, interval, topNum);

        }

        PdfPTable[] table = { ResponseTimeReportService.createTable(multicycleSlowResponseTimes),
            ResponseTimeReportService.createTable(multicycleFastResponseTimes) };
        ServletContext context = request.getSession().getServletContext();
        String exportPdf = context.getRealPath("/exportPdf/");
        //查看是否存在该目录，如不存在则创建
        File file = new File(exportPdf);
        if (!file.exists()) {
            file.mkdir();
        }
        ResponseTimeReportService.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("selectSlowResponseTimeByDate")
    @ResponseBody
    public List<ResponseTimeReport> selectSlowResponseTimeByDate(final String group, final Integer interval,
        final Integer topNum, final String startDate, final String endDate) {
        return responseTimeReportService.selectSlowResponseTimeServiceByDate(group, interval, topNum, startDate,
            endDate);
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 快的响应时间
     */
    @RequestMapping("selectFastResponseTimeByDate")
    @ResponseBody
    public List<ResponseTimeReport> selectFastResponseTimeByDate(final String group, final Integer interval,
        final Integer topNum, final String startDate, final String endDate) {
        return responseTimeReportService.selectFastResponseTimeServiceByDate(group, interval, topNum, startDate,
            endDate);
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 本周期，上周期的慢的响应时间
     */
    @RequestMapping("selectMulticycleSlowResponseTimeByDate")
    @ResponseBody
    public List<ResponseTimeReport> selectMulticycleSlowResponseTimeByDate(final String group, final Integer interval,
        final Integer topNum, final String startDate, final String endDate) {
        return responseTimeReportService.selectMulticycleSlowResponseTimeServiceByDate(group, interval, topNum,
            startDate, endDate);
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 本周期，上周期的快的响应时间
     */
    @RequestMapping("selectMulticycleFastResponseTimeByDate")
    @ResponseBody
    public List<ResponseTimeReport> selectMulticycleFastResponseTimeByDate(final String group, final Integer interval,
        final Integer topNum, final String startDate, final String endDate) {
        return responseTimeReportService.selectMulticycleFastResponseTimeServiceByDate(group, interval, topNum,
            startDate, endDate);
    }
}