package com.cesgroup.cesmonitor.web;

import java.io.File;
import java.util.List;
import java.util.Map;

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.AbnormalAlarmReport;
import com.cesgroup.cesmonitor.common.Util;
import com.cesgroup.cesmonitor.service.AbnormalAlarmReportService;
import com.cesgroup.cesmonitor.service.ReportCommonService;

/**
 * 异常告警报表 controller
 *
 * @author user
 * @version 1.0.0 2017年5月11日
 */
@Controller
@RequestMapping("report")
public class AbnormalAlarmReportController {

    @Autowired
    private AbnormalAlarmReportService abnormalAlarmReportService;

    @Autowired
    private ReportCommonService reportCommonService;

    /**
     *
     * @param model
     *            模型
     * @return 指定页面
     */
    @RequestMapping("abnormalAlarm")
    public String abnormalAlarm(final Model model) {
        String group = "1 = 1";
        String host = "1 = 1";
        String priority = "1 = 1";
        Integer interval = 7;
        List<AbnormalAlarmReport> topAbnormalAlarmReports = selectTopAbnormalAlarm(group, host, priority, interval);
        model.addAttribute("topAbnormalAlarmReports", topAbnormalAlarmReports);
        return "report/abnormalAlarm";
    }

    /**
     *
     * @param group
     *            主机组
     * @param host
     *            主机
     * @param priority
     *            严重性
     * @param interval
     *            时间间隔
     * @return 统计异常报表
     */
    @RequestMapping("countAbnormalAlarm")
    @ResponseBody
    public Map countAbnormalAlarm(final String group, final String host, final String priority,
        final Integer interval) {
        return abnormalAlarmReportService.countAbnormalAlarmService(group, host, priority, interval);
    }

    /**
     *
     * @param interval
     *            时间间隔
     * @return 异常告警
     */
    @RequestMapping("selectTopAbnormalAlarm")
    @ResponseBody
    public List<AbnormalAlarmReport> selectTopAbnormalAlarm(final String group, final String host,
        final String priority,
        final Integer interval) {
        return abnormalAlarmReportService.selectTopAbnormalAlarmService(group, host, priority, interval);
    }

    /**
     *
     * @param group
     *            主机组
     * @param host
     *            主机
     * @param priority
     *            严重性
     * @param interval
     *            时间间隔
     * @return 统计异常报表
     */
    @RequestMapping("countAbnormalAlarmByDate")
    @ResponseBody
    public Map countAbnormalAlarmByDate(final String group, final String host, final String priority,
        final Integer interval, final String startDate,
        final String endDate) {
        return abnormalAlarmReportService.countAbnormalAlarmServiceByDate(group, host, priority, interval, startDate,
            endDate);
    }

    /**
     *
     * @param interval
     *            时间间隔
     * @return 异常告警
     */
    @RequestMapping("selectTopAbnormalAlarmByDate")
    @ResponseBody
    public List<AbnormalAlarmReport> selectTopAbnormalAlarmByDate(final String group, final String host,
        final String priority,
        final Integer interval, final String startDate, final String endDate) {
        return abnormalAlarmReportService.selectTopAbnormalAlarmServiceByDate(group, host, priority, interval,
            startDate, endDate);
    }

    /**
     * pdf文件导出
     *
     * @param interval
     *            时间间隔
     */
    @RequestMapping("abnormalAlarmExport")
    public void abnormalAlarmExport(final HttpServletRequest request, final HttpServletResponse response,
        final String group, final String host,
        final String priority, final Integer interval, final String filename, final String startDate,
        final String endDate) {
        List<String> imageList = Util.getImgList(request, response); //获取图片
        List<AbnormalAlarmReport> abnormalAlarmReports;
        if (interval == 12) {
            abnormalAlarmReports = abnormalAlarmReportService.selectTopAbnormalAlarmServiceByDate(group,
                host, priority, interval, startDate, endDate);
        } else {
            abnormalAlarmReports = abnormalAlarmReportService.selectTopAbnormalAlarmService(group,
                host, priority, interval);
        }

        ServletContext context = request.getSession().getServletContext();
        String exportPdf = context.getRealPath("/exportPdf/");

        //查看是否存在该目录，如不存在则创建
        File file = new File(exportPdf);
        if (!file.exists()) {
            file.mkdir();
        }
        AbnormalAlarmReportService.createPDF(exportPdf + filename, imageList, interval, group, host, priority,
            AbnormalAlarmReportService.createTable(abnormalAlarmReports), startDate, endDate);
        String picPath = context.getRealPath("/upload");
        File f = new File(picPath);
        Util.deleteFile(f);
    }
}