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

/**
 * CPU使用率报表controller
 *
 * @author user
 * @version 1.0.0 2017年5月11日
 */
@Controller
@RequestMapping("report")
public class CPUUsageRateReportController {

    @Autowired
    private CPUUsageRateReportService cpuUsageRateReportService;

    /**
     *
     * @param model
     *            模型
     * @return 指定页面
     */
    @RequestMapping("cpuUsageRate")
    public String responseTimeView(final Model model) {
        try {
            String group = "1 = 1";
            Integer interval = 24;
            Integer topNum = 5;
            List<CPUUsageRateReport> multicycleHighCPUUsageRateReports = cpuUsageRateReportService
                .selectMulticycleHighCPUUsageRateReportService(group, interval, topNum);
            List<CPUUsageRateReport> multicycleLowCPUUsageRateReports = cpuUsageRateReportService
                .selectMulticycleLowCPUUsageRateReportService(group, interval, topNum);
            model.addAttribute("multicycleHighCPUUsageRateReports", multicycleHighCPUUsageRateReports);
            model.addAttribute("multicycleLowCPUUsageRateReports", multicycleLowCPUUsageRateReports);
            model.addAttribute("topNum", topNum);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "report/cpuUsageRate";
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 高CPU使用率
     */
    @RequestMapping("selectHighCPUUsageRateReport")
    @ResponseBody
    public List<CPUUsageRateReport> selectHighCPUUsageRateReport(final String group, final Integer interval,
        final Integer topNum) {
        List<CPUUsageRateReport> cpuUsageRateReport = null;
        try {
            cpuUsageRateReport = cpuUsageRateReportService.selectHighCPUUsageRateReportService(group, interval, topNum);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cpuUsageRateReport;
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 低CPU使用率
     */
    @RequestMapping("selectLowCPUUsageRateReport")
    @ResponseBody
    public List<CPUUsageRateReport> selectFastResponseTime(final String group, final Integer interval,
        final Integer topNum) {
        return cpuUsageRateReportService.selectLowCPUUsageRateReportService(group, interval, topNum);
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 本周期，上周期高CPU使用率
     */
    @RequestMapping("selectMulticycleHighCPUUsageRateReport")
    @ResponseBody
    public List<CPUUsageRateReport> selectMulticycleHighCPUUsageRateReport(final String group, final Integer interval,
        final Integer topNum) {
        return cpuUsageRateReportService.selectMulticycleHighCPUUsageRateReportService(group, interval, topNum);
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 本周期，上周期低CPU使用率
     */
    @RequestMapping("selectMulticycleLowCPUUsageRateReport")
    @ResponseBody
    public List<CPUUsageRateReport> selectMulticycleLowCPUUsageRateReport(final String group, final Integer interval,
        final Integer topNum) {
        return cpuUsageRateReportService.selectMulticycleLowCPUUsageRateReportService(group, interval, topNum);
    }

    /**
     * 导出pdf文件
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     */
    @RequestMapping("cpuUsageRateExport")
    public void cpuUsageRateExport(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<CPUUsageRateReport> multicycleHighCPUUsageRateReports;
        List<CPUUsageRateReport> multicycleLowCPUUsageRateReports;
        if (interval != 12) {
            multicycleHighCPUUsageRateReports = cpuUsageRateReportService
                .selectMulticycleHighCPUUsageRateReportService(group, interval, topNum);
            multicycleLowCPUUsageRateReports = cpuUsageRateReportService
                .selectMulticycleLowCPUUsageRateReportService(group, interval, topNum);
        } else {
            multicycleHighCPUUsageRateReports = cpuUsageRateReportService
                .selectMulticycleHighCPUUsageRateReportServiceByDate(group, interval, topNum, startDate, endDate);
            multicycleLowCPUUsageRateReports = cpuUsageRateReportService
                .selectMulticycleLowCPUUsageRateReportServiceByDate(group, interval, topNum, startDate, endDate);
        }

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

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

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 高CPU使用率
     */
    @RequestMapping("selectHighCPUUsageRateReportByDate")
    @ResponseBody
    public List<CPUUsageRateReport> selectHighCPUUsageRateReportByDate(final String group, final Integer interval,
        final Integer topNum, final String startDate, final String endDate) {
        return cpuUsageRateReportService.selectHighCPUUsageRateReportServiceByDate(group, interval, topNum, startDate,
            endDate);
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 低CPU使用率
     */
    @RequestMapping("selectLowCPUUsageRateReportByDate")
    @ResponseBody
    public List<CPUUsageRateReport> selectFastResponseTimeByDate(final String group, final Integer interval,
        final Integer topNum, final String startDate, final String endDate) {
        return cpuUsageRateReportService.selectLowCPUUsageRateReportServiceByDate(group, interval, topNum, startDate,
            endDate);
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 本周期，上周期高CPU使用率
     */
    @RequestMapping("selectMulticycleHighCPUUsageRateReportByDate")
    @ResponseBody
    public List<CPUUsageRateReport> selectMulticycleHighCPUUsageRateReportByDate(final String group,
        final Integer interval,
        final Integer topNum, final String startDate, final String endDate) {
        return cpuUsageRateReportService.selectMulticycleHighCPUUsageRateReportServiceByDate(group, interval, topNum,
            startDate, endDate);
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 本周期，上周期低CPU使用率
     */
    @RequestMapping("selectMulticycleLowCPUUsageRateReportByDate")
    @ResponseBody
    public List<CPUUsageRateReport> selectMulticycleLowCPUUsageRateReportByDate(final String group,
        final Integer interval,
        final Integer topNum, final String startDate, final String endDate) {
        return cpuUsageRateReportService.selectMulticycleLowCPUUsageRateReportServiceByDate(group, interval, topNum,
            startDate, endDate);
    }

}