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.IcmppingLossReport;
import com.cesgroup.cesmonitor.common.Util;
import com.cesgroup.cesmonitor.service.IcmppingLossReportService;

/**
 * 丢包率报表controller
 *
 * @author user
 * @version 1.0.0 2017年5月11日
 */
@Controller
@RequestMapping("report")
public class IcmppingLossReportController {

    @Autowired
    private IcmppingLossReportService icmppingLossReportService;

    /**
     *
     * @param model
     *            模型
     * @return 跳转到指定页面
     */
    @RequestMapping("icmppingLoss")
    public String icmppingLossView(Model model) {
        String group = "1=1";
        Integer interval = 24;
        Integer topNum = 5;
        List multicycleIcmppingLossReports = icmppingLossReportService.selectMulticycleIcmppingLossService(group,
            interval, topNum);
        model.addAttribute("multicycleIcmppingLossReports", multicycleIcmppingLossReports);
        model.addAttribute("topNum", topNum);
        return "report/icmppingLoss";
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 丢包率
     */
    @RequestMapping("selectIcmppingLoss")
    @ResponseBody
    public List<IcmppingLossReport> selectIcmppingLoss(String group, Integer interval, Integer topNum) {
        return this.icmppingLossReportService.selectIcmppingLossService(group, interval, topNum);
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 本周期，上周期丢包率
     */
    @RequestMapping("selectMulticycleIcmppingLoss")
    @ResponseBody
    public List<IcmppingLossReport> selectMulticycleIcmppingLoss(String group, Integer interval, Integer topNum) {
        return this.icmppingLossReportService.selectMulticycleIcmppingLossService2(group, interval, topNum);
    }

    /**
     * 导出相关pdf文档
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     */
    @RequestMapping(value = "icmppingLossExport")
    public void icmppingLossExport(HttpServletRequest request, HttpServletResponse response, String group,
        Integer interval, Integer topNum, String filename, String startDate, String endDate ) {
        List<String> imageList = Util.getImgList(request, response); //获取图片
        List<IcmppingLossReport> icmppingLossReports;
        if(interval != 12){
            icmppingLossReports = icmppingLossReportService
                    .selectMulticycleIcmppingLossService(group, interval, topNum);
        }else {
            icmppingLossReports = icmppingLossReportService
                    .selectMulticycleIcmppingLossServiceByDate(group, interval, topNum,startDate, endDate);
        }
        

        
        
        ServletContext context = request.getSession().getServletContext();
        String exportPdf = context.getRealPath("/exportPdf/");
        //查看是否存在该目录，如不存在则创建
        File file = new File(exportPdf);
        if (!file.exists()) {
            file.mkdir();
        }
        IcmppingLossReportService.createPDF(exportPdf + filename, imageList, interval, group, topNum,
            IcmppingLossReportService.createTable(icmppingLossReports), startDate,  endDate );
        String picPath = context.getRealPath("/upload");
        File f = new File(picPath);
        Util.deleteFile(f);
    }
    
    /**
    *
    * @param group
    *            主机组
    * @param interval
    *            时间间隔
    * @param topNum
    *            top数量
    * @return 丢包率
    */
   @RequestMapping("selectIcmppingLossByDate")
   @ResponseBody
   public List<IcmppingLossReport> selectIcmppingLossByDate(String group, Integer interval, Integer topNum, String startDate, String endDate) {
       return this.icmppingLossReportService.selectIcmppingLossServiceByDate(group, interval, topNum, startDate, endDate);
   }

   /**
    *
    * @param group
    *            主机组
    * @param interval
    *            时间间隔
    * @param topNum
    *            top数量
    * @return 本周期，上周期丢包率
    */
   @RequestMapping("selectMulticycleIcmppingLossByDate")
   @ResponseBody
   public List<IcmppingLossReport> selectMulticycleIcmppingLossByDate(String group, Integer interval, Integer topNum, String startDate, String endDate) {
       return this.icmppingLossReportService.selectMulticycleIcmppingLossServiceByDate(group, interval, topNum, startDate, endDate);
   }
    
}