/**
 * <p>Copyright:Copyright(c) 2017</p>
 * <p>Company:上海中信信息发展股份有限公司</p>
 * <p>包名:com.cesgroup.cesmonitor.web</p>
 * <p>文件名:DiskUsageRateReportController.java</p>
 * <p>创建时间:2017年5月11日 下午8:38:54</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.DiskUsageRateReport;
import com.cesgroup.cesmonitor.common.EhcacheUtil;
import com.cesgroup.cesmonitor.common.Util;
import com.cesgroup.cesmonitor.service.DiskUsageRateReportService;
import com.itextpdf.text.pdf.PdfPTable;

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

    @Autowired
    private DiskUsageRateReportService diskUsageRateReportService;

    /**
     *
     * @param model
     *            模型
     * @return 调整到指定页面
     */
    @RequestMapping(value = "diskUsageRate")
    public String diskUsageRateView(final Model model) {
        String group = "1 = 1";
        Integer interval = 24;
        Integer topNum = 5;

        List<DiskUsageRateReport> multicycleHighDiskUsageRateReports = selectMulticycleHighDiskUsageRateReport(group,
            interval, topNum);
        List<DiskUsageRateReport> multicycleLowDiskUsageRateReports = selectMulticycleLowDiskUsageRateReport(group,
            interval, topNum);

        model.addAttribute("multicycleHighDiskUsageRateReports", multicycleHighDiskUsageRateReports);
        model.addAttribute("multicycleLowDiskUsageRateReports", multicycleLowDiskUsageRateReports);
        model.addAttribute("topNum", topNum);

        return "report/diskUsageRate";
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 高磁盘使用率
     */
    @RequestMapping(value = "selectHighDiskUsageRateReport")
    @ResponseBody
    public List<DiskUsageRateReport> selectHighDiskUsageRateReport(final String group, final Integer interval,
        final Integer topNum) {
        Long deadLine = Util.getDeadLine(interval);
        String ehcacheKey = "HighDiskUsageRate" + deadLine.toString() + group + interval + topNum;
        List<DiskUsageRateReport> highDiskUsageRateReports = (List<DiskUsageRateReport>) EhcacheUtil.get(ehcacheKey);
        if (null == highDiskUsageRateReports) {
            return diskUsageRateReportService.selectHighDiskUsageRateReportService(group, interval, topNum);
        } else {
            EhcacheUtil.remove(ehcacheKey);
            return highDiskUsageRateReports;
        }

    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 低磁盘使用率
     */
    @RequestMapping(value = "selectLowDiskUsageRateReport")
    @ResponseBody
    public List<DiskUsageRateReport> selectLowDiskUsageRateReport(final String group, final Integer interval,
        final Integer topNum) {
        Long deadLine = Util.getDeadLine(interval);
        String ehcacheKey = "LowDiskUsageRate" + deadLine.toString() + group + interval + topNum;
        List<DiskUsageRateReport> lowDiskUsageRateReports = (List<DiskUsageRateReport>) EhcacheUtil.get(ehcacheKey);
        if (null == lowDiskUsageRateReports) {
            return diskUsageRateReportService.selectLowDiskUsageRateReportService(group, interval, topNum);
        } else {
            EhcacheUtil.remove(ehcacheKey);
            return lowDiskUsageRateReports;
        }

    }

    /**
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 本周期，上周期高磁盘使用率数据
     */
    @RequestMapping(value = "selectMulticycleHighDiskUsageRateReport")
    @ResponseBody
    public List<DiskUsageRateReport> selectMulticycleHighDiskUsageRateReport(final String group, final Integer interval,
        final Integer topNum) {
        return diskUsageRateReportService.selectMulticycleHighDiskUsageRateReportService(group, interval, topNum);
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 本周期，上周期低磁盘使用率数据
     */
    @RequestMapping(value = "selectMulticycleLowDiskUsageRateReport")
    @ResponseBody
    public List<DiskUsageRateReport> selectMulticycleLowDiskUsageRateReport(final String group, final Integer interval,
        final Integer topNum) {
        return diskUsageRateReportService.selectMulticycleLowDiskUsageRateReportService(group, interval, topNum);
    }

    /**
     * 导出pdf文件
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     */
    @RequestMapping("diskUsageRateExport")
    public void diskUsageRateExport(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<DiskUsageRateReport> multicycleHighDiskUsageRateReports;
        List<DiskUsageRateReport> multicycleLowDiskUsageRateReports;
        if (interval != 12) {
            multicycleHighDiskUsageRateReports = diskUsageRateReportService
                .selectMulticycleHighDiskUsageRateReportService(group, interval, topNum);
            multicycleLowDiskUsageRateReports = diskUsageRateReportService
                .selectMulticycleLowDiskUsageRateReportService(group, interval, topNum);
        } else {

            multicycleHighDiskUsageRateReports = diskUsageRateReportService
                .selectMulticycleHighDiskUsageRateReportServiceByDate(group, interval, topNum, startDate, endDate);
            multicycleLowDiskUsageRateReports = diskUsageRateReportService
                .selectMulticycleLowDiskUsageRateReportServiceByDate(group, interval, topNum, startDate, endDate);
        }

        PdfPTable[] table = { DiskUsageRateReportService.createTable(multicycleHighDiskUsageRateReports),
            DiskUsageRateReportService.createTable(multicycleLowDiskUsageRateReports) };
        ServletContext context = request.getSession().getServletContext();
        String exportPdf = context.getRealPath("/exportPdf/");
        //查看是否存在该目录，如不存在则创建
        File file = new File(exportPdf);
        if (!file.exists()) {
            file.mkdir();
        }
        DiskUsageRateReportService.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 = "selectHighDiskUsageRateReportByDate")
    @ResponseBody
    public List<DiskUsageRateReport> selectHighDiskUsageRateReportByDate(final String group, final Integer interval,
        final Integer topNum, final String startDate, final String endDate) {
        Long deadLine = Util.getDeadLine(interval);
        return diskUsageRateReportService.selectHighDiskUsageRateReportServiceByDate(group, interval, topNum, startDate,
            endDate);

    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 低磁盘使用率
     */
    @RequestMapping(value = "selectLowDiskUsageRateReportByDate")
    @ResponseBody
    public List<DiskUsageRateReport> selectLowDiskUsageRateReportByDate(final String group, final Integer interval,
        final Integer topNum, final String startDate, final String endDate) {
        Long deadLine = Util.getDeadLine(interval);
        return diskUsageRateReportService.selectLowDiskUsageRateReportServiceByDate(group, interval, topNum, startDate,
            endDate);
    }

    /**
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 本周期，上周期高磁盘使用率数据
     */
    @RequestMapping(value = "selectMulticycleHighDiskUsageRateReportByDate")
    @ResponseBody
    public List<DiskUsageRateReport> selectMulticycleHighDiskUsageRateReportByDate(final String group,
        final Integer interval,
        final Integer topNum, final String startDate, final String endDate) {
        return diskUsageRateReportService.selectMulticycleHighDiskUsageRateReportServiceByDate(group, interval, topNum,
            startDate, endDate);
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 本周期，上周期低磁盘使用率数据
     */
    @RequestMapping(value = "selectMulticycleLowDiskUsageRateReportByDate")
    @ResponseBody
    public List<DiskUsageRateReport> selectMulticycleLowDiskUsageRateReportByDate(final String group,
        final Integer interval,
        final Integer topNum, final String startDate, final String endDate) {
        return diskUsageRateReportService.selectMulticycleLowDiskUsageRateReportServiceByDate(group, interval, topNum,
            startDate, endDate);
    }
}
