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

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

    @Autowired
    private MemoryUsageRateReportService memoryUsageRateReportService;

    /**
     *
     * @param model
     *            模型
     * @return 跳转到指定页面
     */
    @RequestMapping(value = "memoryUsageRate")
    private String memoryUsageRateView(Model model) {
        String group = "1 = 1";
        Integer interval = 24;
        Integer topNum = 5;
        List<MemoryUsageRateReport> multicycleHighMemoryUsageRateReports = selectMulticycleHighMemoryUsageRateReport(
            group, interval, topNum);
        List<MemoryUsageRateReport> multicycleLowMemoryUsageRateReports = selectMulticycleLowMemoryUsageRateReport(
            group, interval, topNum);
        model.addAttribute("multicycleHighMemoryUsageRateReports", multicycleHighMemoryUsageRateReports);
        model.addAttribute("multicycleLowMemoryUsageRateReports", multicycleLowMemoryUsageRateReports);
        model.addAttribute("topNum", topNum);
        return "report/memoryUsageRate";
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum4
     *            top数量
     * @return 高内存使用率数据
     */
    @RequestMapping(value = "selectHighMemoryUsageRateReport")
    @ResponseBody
    public List<MemoryUsageRateReport> selectHighMemoryUsageRateReport(String group, Integer interval, Integer topNum) {
        return memoryUsageRateReportService.selectHighMemoryUsageRateReportService(group, interval, topNum);
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 低内存使用率数据
     */
    @RequestMapping(value = "selectLowMemoryUsageRateReport")
    @ResponseBody
    public List<MemoryUsageRateReport> selectLowMemoryUsageRateReport(String group, Integer interval, Integer topNum) {
        return memoryUsageRateReportService.selectLowMemoryUsageRateReportService(group, interval, topNum);
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 本周期，上周期高内存使用率数据
     */
    @RequestMapping(value = "selectMulticycleHighMemoryUsageRateReport")
    @ResponseBody
    public List<MemoryUsageRateReport> selectMulticycleHighMemoryUsageRateReport(String group, Integer interval,
        Integer topNum) {
        return memoryUsageRateReportService.selectMulticycleHighMemoryUsageRateReportService(group, interval, topNum);
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 本周期，上周期低内存使用率数据
     */
    @RequestMapping(value = "selectMulticycleLowMemoryUsageRateReport")
    @ResponseBody
    public List<MemoryUsageRateReport> selectMulticycleLowMemoryUsageRateReport(String group, Integer interval,
        Integer topNum) {
        return memoryUsageRateReportService.selectMulticycleLowMemoryUsageRateReportService(group, interval, topNum);
    }

    /**
     * 导出pdf文件
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     */
    @RequestMapping("memoryUsageRateExport")
    public void memoryUsageRateExport(HttpServletRequest request, HttpServletResponse response, String group,
        Integer interval, Integer topNum, String filename,  String startDate, String endDate ) {
        List<String> imageList = Util.getImgList(request, response); //获取图片
        List<MemoryUsageRateReport> multicycleHighMemoryUsageRateReports;
        List<MemoryUsageRateReport> multicycleLowMemoryUsageRateReports;
        if(interval != 12){
        	multicycleHighMemoryUsageRateReports = memoryUsageRateReportService
                    .selectMulticycleHighMemoryUsageRateReportService(group, interval, topNum);
            multicycleLowMemoryUsageRateReports = memoryUsageRateReportService
                    .selectMulticycleLowMemoryUsageRateReportService(group, interval, topNum);
        }else {
        	multicycleHighMemoryUsageRateReports = memoryUsageRateReportService
                    .selectMulticycleHighMemoryUsageRateReportServiceByDate(group, interval, topNum,startDate, endDate);
            multicycleLowMemoryUsageRateReports = memoryUsageRateReportService
                    .selectMulticycleLowMemoryUsageRateReportServiceByDate(group, interval, topNum,startDate, endDate);
        }
        
        
        
        PdfPTable[] table = { MemoryUsageRateReportService.createTable(multicycleHighMemoryUsageRateReports),
            MemoryUsageRateReportService.createTable(multicycleLowMemoryUsageRateReports) };
        ServletContext context = request.getSession().getServletContext();
        String exportPdf = context.getRealPath("/exportPdf/");
        //查看是否存在该目录，如不存在则创建
        File file = new File(exportPdf);
        if (!file.exists()) {
            file.mkdir();
        }
        MemoryUsageRateReportService.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 = "selectMulticycleHighMemoryUsageRateReportByDate")
   @ResponseBody
   public List<MemoryUsageRateReport> selectMulticycleHighMemoryUsageRateReportByDate(String group, Integer interval,
       Integer topNum, String startDate, String endDate) {
       return memoryUsageRateReportService.selectMulticycleHighMemoryUsageRateReportServiceByDate(group, interval, topNum, startDate, endDate);
   }
   
   /**
   *
   * @param group
   *            主机组
   * @param interval
   *            时间间隔
   * @param topNum
   *            top数量
   * @return 本周期，上周期低内存使用率数据
   */
  @RequestMapping(value = "selectMulticycleLowMemoryUsageRateReportByDate")
  @ResponseBody
  public List<MemoryUsageRateReport> selectMulticycleLowMemoryUsageRateReportByDate(String group, Integer interval,
      Integer topNum) {
      return memoryUsageRateReportService.selectMulticycleLowMemoryUsageRateReportService(group, interval, topNum);
  }
   
   /**
   *
   * @param group
   *            主机组
   * @param interval
   *            时间间隔
   * @param topNum
   *            top数量
   * @return 高内存使用率数据
   */
  @RequestMapping(value = "selectHighMemoryUsageRateReportByDate")
  @ResponseBody
  public List<MemoryUsageRateReport> selectHighMemoryUsageRateReportByDate(String group, Integer interval, Integer topNum, String startDate, String endDate) {
      return memoryUsageRateReportService.selectHighMemoryUsageRateReportServiceByDate(group, interval, topNum, startDate, endDate);
  }
  
  /**
  *
  * @param group
  *            主机组
  * @param interval
  *            时间间隔
  * @param topNum
  *            top数量
  * @return 低内存使用率数据
  */
 @RequestMapping(value = "selectLowMemoryUsageRateReportByDate")
 @ResponseBody
 public List<MemoryUsageRateReport> selectLowMemoryUsageRateReportByDate(String group, Integer interval, Integer topNum, String startDate, String endDate) {
     return memoryUsageRateReportService.selectLowMemoryUsageRateReportServiceByDate(group, interval, topNum, startDate, endDate);
 }
}
