package com.cesgroup.cesmonitor.service;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cesgroup.cesmonitor.bean.InterfaceIncomingReport;
import com.cesgroup.cesmonitor.bean.ResponseTimeReport;
import com.cesgroup.cesmonitor.common.EhcacheUtil;
import com.cesgroup.cesmonitor.common.Util;
import com.cesgroup.cesmonitor.dao.ResponseTimeReportMapper;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

/**
 * 响应时间报表service
 *
 * @author user
 * @version 1.0.0 2017年5月11日
 */
@Service("responseTimeReportService")
public class ResponseTimeReportService {

    @Autowired
    private ResponseTimeReportMapper responseTimeReportMapper;

    private Long timeFormat = 0L;

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            间隔时间
     * @param topNum
     *            top数量
     * @return 慢响应时间
     */
    @SuppressWarnings("unchecked")
    public List<ResponseTimeReport> selectSlowResponseTimeService(final String group, final Integer interval,
        final Integer topNum) {
        LoginUser user = Util.getLoginUser();
        Integer userId = user.getUserid();
        boolean isSuperAdmin = user.isSuperAdmin();

        List responseTimeReports = new ArrayList();
        List<?> responseTimeReports2 = new ArrayList();
        Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);

        if (isSuperAdmin) {
            responseTimeReports = responseTimeReportMapper.selectSlowResponseTime(group,
                deadLine - interval * timeFormat,
                deadLine);
        } else {
            responseTimeReports = responseTimeReportMapper.selectSlowResponseTimeByUser(group,
                deadLine - interval * timeFormat,
                deadLine, userId);
        }

        String ehcacheKey = "responseTimeReports" + deadLine.toString() + group + interval + topNum;
        responseTimeReports2.addAll(responseTimeReports);
        String ehcacheKey2 = "myresponseTimeReports" + deadLine.toString() + group + interval + topNum;

        EhcacheUtil.put(ehcacheKey, responseTimeReports);
        EhcacheUtil.put(ehcacheKey2, responseTimeReports2);

        Iterator<?> it = responseTimeReports.iterator();
        while (it.hasNext()) {
            ResponseTimeReport responseTimeReport = (ResponseTimeReport) it.next();
            if (responseTimeReport.getValueAvg() == 0D) {
                it.remove();
            }

            //设置保留位数
            responseTimeReport.setValueMin(Util.round(responseTimeReport.getValueMin() * 1000, 3));
            responseTimeReport.setValueAvg(Util.round(responseTimeReport.getValueAvg() * 1000, 3));
            responseTimeReport.setValueMax(Util.round(responseTimeReport.getValueMax() * 1000, 3));

        }
        responseTimeReports = Util.reduceList(responseTimeReports, topNum.intValue());
        return responseTimeReports;
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            间隔时间
     * @param topNum
     *            top数量
     * @return 快响应时间
     */
    @SuppressWarnings("unchecked")
    public List<ResponseTimeReport> selectFastResponseTimeService(final String group, final Integer interval,
        final Integer topNum) {
        LoginUser user = Util.getLoginUser();
        Integer userId = user.getUserid();
        boolean isSuperAdmin = user.isSuperAdmin();
        List responseTimeReports = new ArrayList();
        Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);

        if (isSuperAdmin) {

            responseTimeReports = (List<InterfaceIncomingReport>) EhcacheUtil
                .get("myresponseTimeReports" + deadLine.toString() + group + interval + topNum);
            /*lowInterfaceIncomingReports = interfaceIncomingReportMapper.selectLowInterfaceIncomingReport(group,
                    deadLine - interval * timeFormat, deadLine);*/
            if (null == responseTimeReports) {

                responseTimeReports = responseTimeReportMapper.selectFastResponseTime(group,
                    deadLine - interval * timeFormat,
                    deadLine);
            } else {
                Collections.reverse(responseTimeReports);
            }

            EhcacheUtil.remove("myresponseTimeReports" + deadLine.toString() + group + interval + topNum);
            /*  lowInterfaceIncomingReports = interfaceIncomingReportMapper.selectLowInterfaceIncomingReport(group,
                       deadLine - interval * timeFormat, deadLine);*/

        } else {
            responseTimeReports = responseTimeReportMapper.selectFastResponseTimeByUser(group,
                deadLine - interval * timeFormat,
                deadLine, userId);

            Iterator<?> it = responseTimeReports.iterator();
            while (it.hasNext()) {
                ResponseTimeReport responseTimeReport = (ResponseTimeReport) it.next();
                if (responseTimeReport.getValueAvg() == 0D) {
                    it.remove();
                }

                //设置保留位数
                responseTimeReport.setValueMin(Util.round(responseTimeReport.getValueMin() * 1000, 3));
                responseTimeReport.setValueAvg(Util.round(responseTimeReport.getValueAvg() * 1000, 3));
                responseTimeReport.setValueMax(Util.round(responseTimeReport.getValueMax() * 1000, 3));
            }

        }

        responseTimeReports = Util.reduceList(responseTimeReports, topNum.intValue());
        return responseTimeReports;
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            间隔时间
     * @param topNum
     *            top数量
     * @return 快响应时间
     */
    @SuppressWarnings("unchecked")
    public List<ResponseTimeReport> selectFastResponseTimeService2(final String group, final Integer interval,
        final Integer topNum) {
        LoginUser user = Util.getLoginUser();
        Integer userId = user.getUserid();
        boolean isSuperAdmin = user.isSuperAdmin();
        List responseTimeReports = new ArrayList();
        Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);

        if (isSuperAdmin) {

            responseTimeReports = responseTimeReportMapper.selectFastResponseTime(group,
                deadLine - interval * timeFormat,
                deadLine);

            //responseTimeReports = (List<InterfaceIncomingReport>) EhcacheUtil.get("myresponseTimeReports" + deadLine.toString() + group + interval + topNum);
            /*lowInterfaceIncomingReports = interfaceIncomingReportMapper.selectLowInterfaceIncomingReport(group,
                    deadLine - interval * timeFormat, deadLine);*/
            /*        	 if(null == responseTimeReports){

                responseTimeReports = responseTimeReportMapper.selectFastResponseTime(group, deadLine - interval * timeFormat,
                        deadLine);
                    	 }else{
                    		 Collections.reverse(responseTimeReports);
                    	 }

                    	EhcacheUtil.remove("myresponseTimeReports" + deadLine.toString() + group + interval + topNum);
              lowInterfaceIncomingReports = interfaceIncomingReportMapper.selectLowInterfaceIncomingReport(group,
                       deadLine - interval * timeFormat, deadLine);*/

        } else {
            responseTimeReports = responseTimeReportMapper.selectFastResponseTimeByUser(group,
                deadLine - interval * timeFormat,
                deadLine, userId);
        }

        Iterator<?> it = responseTimeReports.iterator();
        while (it.hasNext()) {
            ResponseTimeReport responseTimeReport = (ResponseTimeReport) it.next();
            if (responseTimeReport.getValueAvg() == 0D) {
                it.remove();
            }

            //设置保留位数
            responseTimeReport.setValueMin(Util.round(responseTimeReport.getValueMin() * 1000, 3));
            responseTimeReport.setValueAvg(Util.round(responseTimeReport.getValueAvg() * 1000, 3));
            responseTimeReport.setValueMax(Util.round(responseTimeReport.getValueMax() * 1000, 3));

        }

        responseTimeReports = Util.reduceList(responseTimeReports, topNum.intValue());
        return responseTimeReports;
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 本周期和上周期慢的响应时间
     */
    @SuppressWarnings("unused")
    public List<?> selectMulticycleSlowResponseTimeService(final String group, final Integer interval,
        final Integer topNum) {
        LoginUser user = Util.getLoginUser();
        Integer userId = user.getUserid();
        boolean isSuperAdmin = user.isSuperAdmin();

        List<ResponseTimeReport> responseTimeReports = new ArrayList();
        List<?> tempResponseTimeReports = new ArrayList();
        ResponseTimeReport responseTimeReport = new ResponseTimeReport();
        ResponseTimeReport previousCycleResponseTimeReport = new ResponseTimeReport();
        Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);
        tempResponseTimeReports = selectSlowResponseTimeService(group, interval, topNum);
        int size = tempResponseTimeReports.size();
        for (int i = 0; i < size; i++) {
            responseTimeReport = (ResponseTimeReport) tempResponseTimeReports.get(i);

            previousCycleResponseTimeReport = responseTimeReportMapper.selectResponseTimeByHost(
                responseTimeReport.getHost(), deadLine - 2 * interval * timeFormat, deadLine - interval * timeFormat);

            if (previousCycleResponseTimeReport != null) {
                //设置保留位数
                previousCycleResponseTimeReport
                .setValueMin(Util.round(previousCycleResponseTimeReport.getValueMin() * 1000, 3));
                previousCycleResponseTimeReport
                .setValueAvg(Util.round(previousCycleResponseTimeReport.getValueAvg() * 1000, 3));
                previousCycleResponseTimeReport
                .setValueMax(Util.round(previousCycleResponseTimeReport.getValueMax() * 1000, 3));
            }

            responseTimeReports.add(responseTimeReport);
            responseTimeReports.add(previousCycleResponseTimeReport);
        }
        return responseTimeReports;
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 本周期和上周期快的响应时间
     */
    public List<ResponseTimeReport> selectMulticycleFastResponseTimeService(final String group, final Integer interval,
        final Integer topNum) {
        List<ResponseTimeReport> responseTimeReports = new ArrayList<ResponseTimeReport>();
        List<?> tempResponseTimeReports = new ArrayList();
        ResponseTimeReport responseTimeReport = new ResponseTimeReport();
        ResponseTimeReport previousCycleResponseTimeReport = new ResponseTimeReport();
        Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);
        tempResponseTimeReports = selectFastResponseTimeService2(group, interval, topNum);
        int size = tempResponseTimeReports.size();
        for (int i = 0; i < size; i++) {
            responseTimeReport = (ResponseTimeReport) tempResponseTimeReports.get(i);
            previousCycleResponseTimeReport = responseTimeReportMapper.selectResponseTimeByHost(
                responseTimeReport.getHost(), deadLine - 2 * interval * timeFormat, deadLine - interval * timeFormat);

            if (previousCycleResponseTimeReport != null) {
                //设置保留位数
                previousCycleResponseTimeReport
                .setValueMin(Util.round(previousCycleResponseTimeReport.getValueMin() * 1000, 3));
                previousCycleResponseTimeReport
                .setValueAvg(Util.round(previousCycleResponseTimeReport.getValueAvg() * 1000, 3));
                previousCycleResponseTimeReport
                .setValueMax(Util.round(previousCycleResponseTimeReport.getValueMax() * 1000, 3));
            }

            responseTimeReports.add(responseTimeReport);
            responseTimeReports.add(previousCycleResponseTimeReport);
        }
        return responseTimeReports;
    }

    /**
     * 创建pdf文件
     *
     * @param fileName
     *            文件名称
     * @param imgList
     *            图片集合
     * @param interval
     *            时间间隔
     * @param group
     *            主机组
     * @param topNum
     *            top数量
     * @param table
     *            表格
     */
    public static void createPDF(final String fileName, final List<String> imgList, final Integer interval,
        final String group, final Integer topNum,
        final PdfPTable[] table, final String startDate, final String endDate) {
        //step 1
        Document document = new Document(PageSize.A4);
        //step 2
        if (fileName != null && !fileName.equals("") && !fileName.equals("cancel")) {
            try {
                PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
                writer.setStrictImageSequence(true);

                //step 3
                document.open();
                //step 4
                BaseFont baseFont = BaseFont.createFont(Util.getFontPath(), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                Font font = new Font(baseFont);

                Font f = new Font(baseFont, 30);
                Paragraph p = new Paragraph("响应时间报表", f);
                p.setAlignment(Element.ALIGN_CENTER);
                document.add(p);

                String condition = Util.getCondition(interval, group, topNum, startDate, endDate);
                document.add(new Paragraph("统计条件:" + condition, font));
                document.add(new Paragraph("图表生成时间:" + Util.getCurrentTime(), font));

                Image img = null;
                for (int j = 0; j < imgList.size(); j++) {
                    // 图片
                    img = Image.getInstance(imgList.get(j));
                    float heigth = img.getHeight();
                    float width = img.getWidth();
                    int percent = Util.getPercent(heigth, width);
                    img.setAlignment(Image.MIDDLE);
                    img.scalePercent(percent + 3);// 表示是原来图像的比例;
                    document.add(img);
                }
                document.add(new Paragraph("最慢响应时间Top" + topNum + "数据汇总", font));
                document.add(new Chunk(""));
                document.add(table[0]);
                document.add(new Paragraph("最快响应时间Top" + topNum + "数据汇总", font));
                document.add(new Chunk(""));
                document.add(table[1]);
                System.out.println("create pdf success!");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (DocumentException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                document.close();
            }
        }
    }

    /**
     * 创建表格
     *
     * @param responseTimeReports
     *            响应时间数据
     * @return 表格对象
     */
    public static PdfPTable createTable(final List<ResponseTimeReport> responseTimeReports) {
        PdfPTable table = null;
        try {
            table = new PdfPTable(8);
            table.setTotalWidth(800);
            table.setWidths(new int[] { 8, 8, 5, 5, 5, 5, 5, 5 });
            // 居中
            //            table.setHorizontalAlignment(Element.ALIGN_CENTER);
            //            table.setHorizontalAlignment(Element.ALIGN_LEFT);
            BaseFont baseFont = BaseFont.createFont(Util.getFontPath(), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            Font font = new Font(baseFont);
            table.addCell(new Paragraph("主机名", font));
            table.addCell(new Paragraph("主机组", font));
            table.addCell(new Paragraph("本周期平均", font));
            table.addCell(new Paragraph("上周期平均", font));
            table.addCell(new Paragraph("本周期峰值", font));
            table.addCell(new Paragraph("上周期峰值", font));
            table.addCell(new Paragraph("本周期最低", font));
            table.addCell(new Paragraph("上周期最低", font));

            int size = responseTimeReports.size();
            for (int i = 0; i < size; i = i + 2) {
                table.addCell(new Paragraph(responseTimeReports.get(i).getHname(), font));
                table.addCell(new Paragraph(responseTimeReports.get(i).getName(), font));
                table.addCell(String.valueOf(responseTimeReports.get(i).getValueAvg()));
                if (responseTimeReports.get(i + 1) == null) {
                    table.addCell(new Paragraph("-  -  -", font));
                } else {
                    table.addCell(String.valueOf(responseTimeReports.get(i + 1).getValueAvg()));
                }

                table.addCell(String.valueOf(responseTimeReports.get(i).getValueMax()));
                if (responseTimeReports.get(i + 1) == null) {
                    table.addCell(new Paragraph("-  -  -", font));
                } else {
                    table.addCell(String.valueOf(responseTimeReports.get(i + 1).getValueMax()));
                }

                table.addCell(String.valueOf(responseTimeReports.get(i).getValueMin()));
                if (responseTimeReports.get(i + 1) == null) {
                    table.addCell(new Paragraph("-  -  -", font));
                } else {
                    table.addCell(String.valueOf(responseTimeReports.get(i + 1).getValueMin()));
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return table;
    }

    /**
     *
     * @param group
     *            群组
     * @param interval
     *            区间
     * @param topNum
     *            top
     * @param startDate
     *            开始
     * @param endDate
     *            结束
     * @return
     *         相应时间
     */
    @SuppressWarnings({ "unused", "unchecked" })
    public List<ResponseTimeReport> selectSlowResponseTimeServiceByDate(final String group, final Integer interval,
        final Integer topNum, final String startDate, final String endDate) {
        LoginUser user = Util.getLoginUser();
        Integer userId = user.getUserid();
        boolean isSuperAdmin = user.isSuperAdmin();

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        long myStartDate = 0;
        long myEndDate = 0;
        long cycle = 0;
        Date startDate1;
        Date endDate1;

        try {
            startDate1 = df.parse(startDate);
            myStartDate = startDate1.getTime() / 1000;
            endDate1 = df.parse(endDate);
            myEndDate = endDate1.getTime() / 1000;
            cycle = myEndDate - myStartDate;
        } catch (ParseException e) {
            e.printStackTrace();
        }

        List responseTimeReports = new ArrayList();
        Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);

        if (isSuperAdmin) {
            responseTimeReports = responseTimeReportMapper.selectSlowResponseTime(group, myStartDate,
                myEndDate);
        } else {
            responseTimeReports = responseTimeReportMapper.selectSlowResponseTimeByUser(group, myStartDate,
                myEndDate, userId);

        }
        Iterator<?> it = responseTimeReports.iterator();
        while (it.hasNext()) {
            ResponseTimeReport responseTimeReport = (ResponseTimeReport) it.next();
            if (responseTimeReport.getValueAvg() == 0D) {
                it.remove();
            }

            //设置保留位数
            responseTimeReport.setValueMin(Util.round(responseTimeReport.getValueMin() * 1000, 3));
            responseTimeReport.setValueAvg(Util.round(responseTimeReport.getValueAvg() * 1000, 3));
            responseTimeReport.setValueMax(Util.round(responseTimeReport.getValueMax() * 1000, 3));

        }
        responseTimeReports = Util.reduceList(responseTimeReports, topNum.intValue());
        return responseTimeReports;
    }

    /**
     *
     * @param group
     *            群组
     * @param interval
     *            区间
     * @param topNum
     *            top
     * @param startDate
     *            开始
     * @param endDate
     *            结束
     * @return
     *         相应时间
     */
    @SuppressWarnings("unchecked")
    public List<ResponseTimeReport> selectSlowResponseTimeServiceByDate(final String group, final Integer interval,
        final Integer topNum,
        final long startDate, final long endDate) {
        LoginUser user = Util.getLoginUser();
        Integer userId = user.getUserid();
        boolean isSuperAdmin = user.isSuperAdmin();
        List responseTimeReports = new ArrayList();
        Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);

        if (isSuperAdmin) {
            responseTimeReports = responseTimeReportMapper.selectSlowResponseTime(group, startDate,
                endDate);
        } else {
            responseTimeReports = responseTimeReportMapper.selectSlowResponseTimeByUser(group, startDate,
                endDate, userId);
        }

        Iterator it = responseTimeReports.iterator();
        while (it.hasNext()) {
            ResponseTimeReport responseTimeReport = (ResponseTimeReport) it.next();
            if (responseTimeReport.getValueAvg() == 0D) {
                it.remove();
            }

            //设置保留位数
            responseTimeReport.setValueMin(Util.round(responseTimeReport.getValueMin() * 1000, 3));
            responseTimeReport.setValueAvg(Util.round(responseTimeReport.getValueAvg() * 1000, 3));
            responseTimeReport.setValueMax(Util.round(responseTimeReport.getValueMax() * 1000, 3));

        }
        responseTimeReports = Util.reduceList(responseTimeReports, topNum.intValue());
        return responseTimeReports;
    }

    /**
     *
     * @param group
     *            群组
     * @param interval
     *            区间
     * @param topNum
     *            top
     * @param startDate
     *            开始
     * @param endDate
     *            结束
     * @return
     *         相应时间
     */
    @SuppressWarnings("unchecked")
    public List<ResponseTimeReport> selectFastResponseTimeServiceByDate(final String group, final Integer interval,
        final Integer topNum, final String startDate, final String endDate) {
        LoginUser user = Util.getLoginUser();
        Integer userId = user.getUserid();
        boolean isSuperAdmin = user.isSuperAdmin();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        long myStartDate = 0;
        long myEndDate = 0;
        long cycle = 0;
        Date startDate1;
        Date endDate1;

        try {
            startDate1 = df.parse(startDate);
            myStartDate = startDate1.getTime() / 1000;
            endDate1 = df.parse(endDate);
            myEndDate = endDate1.getTime() / 1000;
            cycle = myEndDate - myStartDate;
        } catch (ParseException e) {
            e.printStackTrace();
        }

        List responseTimeReports = new ArrayList();
        Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);

        if (isSuperAdmin) {
            responseTimeReports = responseTimeReportMapper.selectFastResponseTime(group, myStartDate,
                myEndDate);
        } else {
            responseTimeReports = responseTimeReportMapper.selectFastResponseTimeByUser(group, myStartDate,
                myEndDate, userId);
        }

        Iterator<?> it = responseTimeReports.iterator();
        while (it.hasNext()) {
            ResponseTimeReport responseTimeReport = (ResponseTimeReport) it.next();
            if (responseTimeReport.getValueAvg() == 0D) {
                it.remove();
            }

            //设置保留位数
            responseTimeReport.setValueMin(Util.round(responseTimeReport.getValueMin() * 1000, 3));
            responseTimeReport.setValueAvg(Util.round(responseTimeReport.getValueAvg() * 1000, 3));
            responseTimeReport.setValueMax(Util.round(responseTimeReport.getValueMax() * 1000, 3));
        }
        responseTimeReports = Util.reduceList(responseTimeReports, topNum.intValue());
        return responseTimeReports;
    }

    /**
     *
     * @param group
     *            群组
     * @param interval
     *            区间
     * @param topNum
     *            top
     * @param startDate
     *            开始
     * @param endDate
     *            结束
     * @return
     *         相应时间
     */
    @SuppressWarnings({ "unused", "unchecked" })
    public List<ResponseTimeReport> selectFastResponseTimeServiceByDate(final String group, final Integer interval,
        final Integer topNum,
        final long startDate, final long endDate) {
        LoginUser user = Util.getLoginUser();
        Integer userId = user.getUserid();
        boolean isSuperAdmin = user.isSuperAdmin();
        List responseTimeReports = new ArrayList();
        Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);

        if (isSuperAdmin) {
            responseTimeReports = responseTimeReportMapper.selectFastResponseTime(group, startDate,
                endDate);
        } else {
            responseTimeReports = responseTimeReportMapper.selectSlowResponseTimeByUser(group, startDate,
                endDate, userId);
        }

        Iterator<?> it = responseTimeReports.iterator();
        while (it.hasNext()) {
            ResponseTimeReport responseTimeReport = (ResponseTimeReport) it.next();
            if (responseTimeReport.getValueAvg() == 0D) {
                it.remove();
            }

            //设置保留位数
            responseTimeReport.setValueMin(Util.round(responseTimeReport.getValueMin() * 1000, 3));
            responseTimeReport.setValueAvg(Util.round(responseTimeReport.getValueAvg() * 1000, 3));
            responseTimeReport.setValueMax(Util.round(responseTimeReport.getValueMax() * 1000, 3));
        }
        responseTimeReports = Util.reduceList(responseTimeReports, topNum.intValue());
        return responseTimeReports;
    }

    /**
     *
     * @param group
     *            群组
     * @param interval
     *            区间
     * @param topNum
     *            top
     * @param startDate
     *            开始
     * @param endDate
     *            结束
     * @return
     *         相应时间
     */
    @SuppressWarnings({ "unused", "unchecked" })
    public List<ResponseTimeReport> selectMulticycleSlowResponseTimeServiceByDate(final String group,
        final Integer interval,
        final Integer topNum, final String startDate, final String endDate) {
        LoginUser user = Util.getLoginUser();
        Integer userId = user.getUserid();
        boolean isSuperAdmin = user.isSuperAdmin();

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        long myStartDate = 0;
        long myEndDate = 0;
        long cycle = 0;
        Date startDate1;
        Date endDate1;

        try {
            startDate1 = df.parse(startDate);
            myStartDate = startDate1.getTime() / 1000;
            endDate1 = df.parse(endDate);
            myEndDate = endDate1.getTime() / 1000;
            cycle = myEndDate - myStartDate;
        } catch (ParseException e) {
            e.printStackTrace();
        }

        List responseTimeReports = new ArrayList();
        List tempResponseTimeReports = new ArrayList();
        ResponseTimeReport responseTimeReport = new ResponseTimeReport();
        ResponseTimeReport previousCycleResponseTimeReport = new ResponseTimeReport();
        Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);
        tempResponseTimeReports = selectSlowResponseTimeServiceByDate(group, interval, topNum, myStartDate, myEndDate);
        int size = tempResponseTimeReports.size();
        for (int i = 0; i < size; i++) {
            responseTimeReport = (ResponseTimeReport) tempResponseTimeReports.get(i);
            previousCycleResponseTimeReport = responseTimeReportMapper.selectResponseTimeByHost(
                responseTimeReport.getHost(), myStartDate - 2 * cycle, myStartDate - cycle);

            if (previousCycleResponseTimeReport != null) {
                //设置保留位数
                previousCycleResponseTimeReport
                .setValueMin(Util.round(previousCycleResponseTimeReport.getValueMin() * 1000, 3));
                previousCycleResponseTimeReport
                .setValueAvg(Util.round(previousCycleResponseTimeReport.getValueAvg() * 1000, 3));
                previousCycleResponseTimeReport
                .setValueMax(Util.round(previousCycleResponseTimeReport.getValueMax() * 1000, 3));
            }

            responseTimeReports.add(responseTimeReport);
            responseTimeReports.add(previousCycleResponseTimeReport);
        }
        return responseTimeReports;
    }

    /**
     *
     * @param group
     *            群组
     * @param interval
     *            区间
     * @param topNum
     *            top
     * @param startDate
     *            开始
     * @param endDate
     *            结束
     * @return
     *         相应时间
     */
    @SuppressWarnings({ "unchecked", "unused" })
    public List<ResponseTimeReport> selectMulticycleFastResponseTimeServiceByDate(final String group,
        final Integer interval,
        final Integer topNum, final String startDate, final String endDate) {
        LoginUser user = Util.getLoginUser();
        Integer userId = user.getUserid();
        boolean isSuperAdmin = user.isSuperAdmin();

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        long myStartDate = 0;
        long myEndDate = 0;
        long cycle = 0;
        Date startDate1;
        Date endDate1;

        try {
            startDate1 = df.parse(startDate);
            myStartDate = startDate1.getTime() / 1000;
            endDate1 = df.parse(endDate);
            myEndDate = endDate1.getTime() / 1000;
            cycle = myEndDate - myStartDate;
        } catch (ParseException e) {
            e.printStackTrace();
        }

        List responseTimeReports = new ArrayList();
        List tempResponseTimeReports = new ArrayList();
        ResponseTimeReport responseTimeReport = new ResponseTimeReport();
        ResponseTimeReport previousCycleResponseTimeReport = new ResponseTimeReport();
        Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);
        tempResponseTimeReports = selectFastResponseTimeServiceByDate(group, interval, topNum, myStartDate, myEndDate);
        int size = tempResponseTimeReports.size();
        for (int i = 0; i < size; i++) {
            responseTimeReport = (ResponseTimeReport) tempResponseTimeReports.get(i);
            previousCycleResponseTimeReport = responseTimeReportMapper.selectResponseTimeByHost(
                responseTimeReport.getHost(), myStartDate - 2 * cycle, myStartDate - cycle);

            if (previousCycleResponseTimeReport != null) {
                //设置保留位数
                previousCycleResponseTimeReport
                .setValueMin(Util.round(previousCycleResponseTimeReport.getValueMin() * 1000, 3));
                previousCycleResponseTimeReport
                .setValueAvg(Util.round(previousCycleResponseTimeReport.getValueAvg() * 1000, 3));
                previousCycleResponseTimeReport
                .setValueMax(Util.round(previousCycleResponseTimeReport.getValueMax() * 1000, 3));
            }

            responseTimeReports.add(responseTimeReport);
            responseTimeReports.add(previousCycleResponseTimeReport);
        }
        return responseTimeReports;
    }

}