/**
 * <p>Copyright:Copyright(c) 2017</p>
 * <p>Company:上海中信信息发展股份有限公司</p>
 * <p>包名:com.cesgroup.cesmonitor.service</p>
 * <p>文件名:InterfaceOutgoingReportService.java</p>
 * <p>创建时间:2017年5月12日 下午1:18:30</p>
 * <p>作者:user</p>
 */

package com.cesgroup.cesmonitor.service;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
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.InterfaceOutgoingReport;
import com.cesgroup.cesmonitor.common.EhcacheUtil;
import com.cesgroup.cesmonitor.common.Util;
import com.cesgroup.cesmonitor.dao.InterfaceOutgoingReportMapper;
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;

/**
 *
 * @author user
 * @version 1.0.0 2017年5月12日
 */
@Service("interfaceOutgoingReportService")
public class InterfaceOutgoingReportService {

    @Autowired
    private InterfaceOutgoingReportMapper interfaceOutgoingReportMapper;

    private Long timeFormat = 0L; //时间格式

    /**
     *
     * @param src
     *            资源
     * @return 信息
     * @throws IOException
     *             异常
     * @throws ClassNotFoundException
     *             异常
     */

    public static <T> List<T> deepCopy(final List<T> src) throws IOException, ClassNotFoundException {
        final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        final ObjectOutputStream out = new ObjectOutputStream(byteOut);
        out.writeObject(src);

        final ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
        final ObjectInputStream in = new ObjectInputStream(byteIn);
        @SuppressWarnings("unchecked")
        final List<T> dest = (List<T>) in.readObject();
        return dest;
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 高接口流入流量
     */
    public List<InterfaceOutgoingReport> selectHighInterfaceOutgoingReportService(final String group,
        final Integer interval,
        final Integer topNum) {

        final long startTime = System.currentTimeMillis(); // 获取开始时间
        final LoginUser user = Util.getLoginUser();
        final Integer userId = user.getUserid();
        final boolean isSuperAdmin = user.isSuperAdmin();

        final Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);
        List<InterfaceOutgoingReport> highInterfaceOutgoingReports = new ArrayList<>();
        final List<InterfaceOutgoingReport> highInterfaceOutgoingReports2 = new ArrayList<>();

        if (isSuperAdmin) {
            highInterfaceOutgoingReports = interfaceOutgoingReportMapper.selectHighInterfaceOutgoingReport(group,
                deadLine - interval * timeFormat, deadLine);
        } else {
            highInterfaceOutgoingReports = interfaceOutgoingReportMapper.selectHighInterfaceOutgoingReportByUser(group,
                deadLine - interval * timeFormat, deadLine, userId);
        }

        highInterfaceOutgoingReports2.addAll(highInterfaceOutgoingReports);
        final String ehcacheKey2 = "myhighInterfaceOutgoingReports" + deadLine.toString() + group + interval + topNum;
        EhcacheUtil.put(ehcacheKey2, highInterfaceOutgoingReports2);

        final Iterator<InterfaceOutgoingReport> it = highInterfaceOutgoingReports.iterator();

        final InterfaceOutgoingReport myInterfaceOutcomingReport = new InterfaceOutgoingReport();
        if (highInterfaceOutgoingReports.size() > 0) {
            if (highInterfaceOutgoingReports.get(0).getValueAvg() / 10000 > 1000000000) {
                myInterfaceOutcomingReport.setAccuracy("Gbps");

            } else if (highInterfaceOutgoingReports.get(0).getValueAvg() / 10000 > 1000000) {
                myInterfaceOutcomingReport.setAccuracy("Mbps");

            } else if (highInterfaceOutgoingReports.get(0).getValueAvg() / 10000 > 1000) {
                myInterfaceOutcomingReport.setAccuracy("Kbps");

            } else if (highInterfaceOutgoingReports.get(0).getValueAvg() / 1000 > 1) {
                myInterfaceOutcomingReport.setAccuracy("bps");
            }

            int i = 0;
            while (it.hasNext() && i < 20) {
                final InterfaceOutgoingReport interfaceOutgoingReport = it.next();
                if (interfaceOutgoingReport.getValueAvg() == 0) {
                    it.remove(); // 移除数据
                }

                if ("Gbps".equals(myInterfaceOutcomingReport.getAccuracy())) {
                    interfaceOutgoingReport.setAccuracy("Gbps");
                    interfaceOutgoingReport.setValueAvg(
                        Util.round(Util.round(interfaceOutgoingReport.getValueAvg() / 1000000000, 2) / 1000, 2));
                } else if ("Mbps".equals(myInterfaceOutcomingReport.getAccuracy())) {
                    interfaceOutgoingReport.setAccuracy("Mbps");
                    interfaceOutgoingReport
                    .setValueAvg(Util.round(interfaceOutgoingReport.getValueAvg() / 1000000000, 2));
                } else if ("Kbps".equals(myInterfaceOutcomingReport.getAccuracy())) {
                    interfaceOutgoingReport.setAccuracy("Kbps");
                    interfaceOutgoingReport.setValueAvg(Util.round(interfaceOutgoingReport.getValueAvg() / 1000000, 2));
                } else if ("bps".equals(myInterfaceOutcomingReport.getAccuracy())) {
                    interfaceOutgoingReport.setAccuracy("bps");
                    interfaceOutgoingReport.setValueAvg(Util.round(interfaceOutgoingReport.getValueAvg() / 1000, 2));
                }

                if ("Kbps".equals(interfaceOutgoingReport.getAccuracy())) {
                    interfaceOutgoingReport.setValueMin(Util.round(interfaceOutgoingReport.getValueMin() / 1000000, 2));
                } else if ("Mbps".equals(interfaceOutgoingReport.getAccuracy())) {
                    interfaceOutgoingReport
                    .setValueMin(Util.round(interfaceOutgoingReport.getValueMin() / 1000000000, 2));
                } else if ("Gbps".equals(interfaceOutgoingReport.getAccuracy())) {
                    interfaceOutgoingReport.setValueMin(
                        Util.round(Util.round(interfaceOutgoingReport.getValueMin() / 1000000000, 2) / 1000, 2));
                } else {

                    interfaceOutgoingReport.setValueMin(Util.round(interfaceOutgoingReport.getValueMin() / 1000, 2));

                }
                if ("Kbps".equals(interfaceOutgoingReport.getAccuracy())) {
                    interfaceOutgoingReport.setValueMax(Util.round(interfaceOutgoingReport.getValueMax() / 1000000, 2));
                } else if ("Mbps".equals(interfaceOutgoingReport.getAccuracy())) {
                    interfaceOutgoingReport
                    .setValueMax(Util.round(interfaceOutgoingReport.getValueMax() / 1000000000, 2));
                } else if ("Gbps".equals(interfaceOutgoingReport.getAccuracy())) {
                    interfaceOutgoingReport.setValueMax(
                        Util.round(Util.round(interfaceOutgoingReport.getValueMax() / 1000000000, 2) / 1000, 2));
                } else {

                    interfaceOutgoingReport.setValueMax(Util.round(interfaceOutgoingReport.getValueMax() / 1000, 2));

                }
                i++;
            }
        }
        final long endTime = System.currentTimeMillis(); // 获取结束时间

        System.out.println("程序运行时间：" + (endTime - startTime) + "ms"); // 输出程序运行时间

        highInterfaceOutgoingReports = Util.reduceList(highInterfaceOutgoingReports, topNum); // 剔除数据
        highInterfaceOutgoingReports = parseSymbol(highInterfaceOutgoingReports);
        return highInterfaceOutgoingReports;
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 低接口流入流量
     */
    @SuppressWarnings("unchecked")
    public List<InterfaceOutgoingReport> selectLowInterfaceOutgoingReportService(final String group,
        final Integer interval,
        final Integer topNum) {

        final LoginUser user = Util.getLoginUser();
        final Integer userId = user.getUserid();
        final boolean isSuperAdmin = user.isSuperAdmin();

        final Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);
        List<InterfaceOutgoingReport> lowInterfaceOutgoingReports2 = new ArrayList<>();
        List<InterfaceOutgoingReport> lowInterfaceOutgoingReports = new ArrayList<>();
        if (isSuperAdmin) {
            lowInterfaceOutgoingReports2 = (List<InterfaceOutgoingReport>) EhcacheUtil
                .get("myhighInterfaceOutgoingReports" + deadLine.toString() + group + interval + topNum);

            try {
                lowInterfaceOutgoingReports = deepCopy(lowInterfaceOutgoingReports2);
            } catch (final ClassNotFoundException e) {
                e.printStackTrace();
            } catch (final IOException e) {
                e.printStackTrace();
            }
            if (null == lowInterfaceOutgoingReports) {

                lowInterfaceOutgoingReports = interfaceOutgoingReportMapper.selectLowInterfaceOutgoingReport(group,
                    deadLine - interval * timeFormat, deadLine);
            } else {
                Collections.reverse(lowInterfaceOutgoingReports);
            }

        } else {
            lowInterfaceOutgoingReports = interfaceOutgoingReportMapper.selectLowInterfaceOutgoingReportByUser(group,
                deadLine - interval * timeFormat, deadLine, userId);
        }

        final InterfaceOutgoingReport myInterfaceOutcomingReport = new InterfaceOutgoingReport();
        if (lowInterfaceOutgoingReports.size() > 0) {
            if (lowInterfaceOutgoingReports.get(0).getValueAvg() / 10000 > 1000000000) {
                myInterfaceOutcomingReport.setAccuracy("Gbps");

            } else if (lowInterfaceOutgoingReports.get(0).getValueAvg() / 10000 > 1000000) {
                myInterfaceOutcomingReport.setAccuracy("Mbps");

            } else if (lowInterfaceOutgoingReports.get(0).getValueAvg() / 10000 > 1000) {
                myInterfaceOutcomingReport.setAccuracy("Kbps");

            } else if (lowInterfaceOutgoingReports.get(0).getValueAvg() / 1000 > 1) {
                myInterfaceOutcomingReport.setAccuracy("bps");
            } else {
                myInterfaceOutcomingReport.setAccuracy("bps");
            }
        }
        int i = 0;
        final Iterator<InterfaceOutgoingReport> it = lowInterfaceOutgoingReports.iterator();
        while (it.hasNext() && i < 20) {
            final InterfaceOutgoingReport interfaceOutgoingReport = it.next();
            if (interfaceOutgoingReport.getValueAvg() == 0) {
                it.remove(); //移除数据
            }
            //设置保留位数，数据太小，则不保留固定的位数
            //if (interfaceOutgoingReport != null) {
            if ("Gbps".equals(myInterfaceOutcomingReport.getAccuracy())) {
                interfaceOutgoingReport.setAccuracy("Gbps");
                interfaceOutgoingReport.setValueAvg(
                    Util.round(Util.round(interfaceOutgoingReport.getValueAvg() / 1000000000, 2) / 1000, 2));
                interfaceOutgoingReport.setValueMin(
                    Util.round(Util.round(interfaceOutgoingReport.getValueMin() / 1000000000, 2) / 1000, 2));
                interfaceOutgoingReport.setValueMax(
                    Util.round(Util.round(interfaceOutgoingReport.getValueMax() / 1000000000, 2) / 1000, 2));

            } else if ("Mbps".equals(myInterfaceOutcomingReport.getAccuracy())) {
                interfaceOutgoingReport.setAccuracy("Mbps");
                interfaceOutgoingReport
                .setValueAvg(Util.round(interfaceOutgoingReport.getValueAvg() / 1000000000, 2));
                interfaceOutgoingReport
                .setValueMin(Util.round(interfaceOutgoingReport.getValueMin() / 1000000000, 2));
                interfaceOutgoingReport
                .setValueMax(Util.round(interfaceOutgoingReport.getValueMax() / 1000000000, 2));

            } else if ("Kbps".equals(myInterfaceOutcomingReport.getAccuracy())) {
                interfaceOutgoingReport.setAccuracy("Kbps");
                interfaceOutgoingReport.setValueAvg(Util.round(interfaceOutgoingReport.getValueAvg() / 1000000, 2));
                interfaceOutgoingReport.setValueMin(Util.round(interfaceOutgoingReport.getValueMin() / 1000000, 2));
                interfaceOutgoingReport.setValueMax(Util.round(interfaceOutgoingReport.getValueMax() / 1000000, 2));

            } else if ("bps".equals(myInterfaceOutcomingReport.getAccuracy())) {
                interfaceOutgoingReport.setAccuracy("bps");
                interfaceOutgoingReport.setValueAvg(Util.round(interfaceOutgoingReport.getValueAvg() / 1000, 2));
                interfaceOutgoingReport.setValueMin(Util.round(interfaceOutgoingReport.getValueMin() / 1000, 2));
                interfaceOutgoingReport.setValueMax(Util.round(interfaceOutgoingReport.getValueMax() / 1000, 2));

            }
            // }
            i++;
        }
        lowInterfaceOutgoingReports = Util.reduceList(lowInterfaceOutgoingReports, topNum);
        lowInterfaceOutgoingReports = parseSymbol(lowInterfaceOutgoingReports);
        return lowInterfaceOutgoingReports;
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 低接口流入流量
     */
    public List<InterfaceOutgoingReport> selectLowInterfaceOutgoingReportService2(final String group,
        final Integer interval,
        final Integer topNum) {

        final LoginUser user = Util.getLoginUser();
        final Integer userId = user.getUserid();
        final boolean isSuperAdmin = user.isSuperAdmin();

        final Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);
        List<InterfaceOutgoingReport> lowInterfaceOutgoingReports = new ArrayList<>();

        if (isSuperAdmin) {

            lowInterfaceOutgoingReports = interfaceOutgoingReportMapper.selectLowInterfaceOutgoingReport(group,
                deadLine - interval * timeFormat, deadLine);
        } else {
            lowInterfaceOutgoingReports = interfaceOutgoingReportMapper.selectLowInterfaceOutgoingReportByUser(group,
                deadLine - interval * timeFormat, deadLine, userId);
        }

        final InterfaceOutgoingReport myInterfaceOutcomingReport = new InterfaceOutgoingReport();
        if (lowInterfaceOutgoingReports.size() > 0) {
            if (lowInterfaceOutgoingReports.get(0).getValueAvg() / 10000 > 1000000000) {
                myInterfaceOutcomingReport.setAccuracy("Gbps");

            } else if (lowInterfaceOutgoingReports.get(0).getValueAvg() / 10000 > 1000000) {
                myInterfaceOutcomingReport.setAccuracy("Mbps");

            } else if (lowInterfaceOutgoingReports.get(0).getValueAvg() / 10000 > 1000) {
                myInterfaceOutcomingReport.setAccuracy("Kbps");

            } else if (lowInterfaceOutgoingReports.get(0).getValueAvg() / 1000 > 1) {
                myInterfaceOutcomingReport.setAccuracy("bps");
            } else {
                myInterfaceOutcomingReport.setAccuracy("bps");
            }
        }
        final Iterator<InterfaceOutgoingReport> it = lowInterfaceOutgoingReports.iterator();

        int i = 0;
        while (it.hasNext() & i < 20) {
            final InterfaceOutgoingReport interfaceOutgoingReport = it.next();
            if (interfaceOutgoingReport.getValueAvg() == 0) {
                it.remove(); //移除数据
            }
            //设置保留位数，数据太小，则不保留固定的位数
            //if (interfaceOutgoingReport != null) {
            if ("Gbps".equals(myInterfaceOutcomingReport.getAccuracy())) {
                interfaceOutgoingReport.setAccuracy("Gbps");
                interfaceOutgoingReport.setValueAvg(
                    Util.round(Util.round(interfaceOutgoingReport.getValueAvg() / 1000000000, 2) / 1000, 2));
                interfaceOutgoingReport.setValueMin(
                    Util.round(Util.round(interfaceOutgoingReport.getValueMin() / 1000000000, 2) / 1000, 2));
                interfaceOutgoingReport.setValueMax(
                    Util.round(Util.round(interfaceOutgoingReport.getValueMax() / 1000000000, 2) / 1000, 2));

            } else if ("Mbps".equals(myInterfaceOutcomingReport.getAccuracy())) {
                interfaceOutgoingReport.setAccuracy("Mbps");
                interfaceOutgoingReport
                .setValueAvg(Util.round(interfaceOutgoingReport.getValueAvg() / 1000000000, 2));
                interfaceOutgoingReport
                .setValueMin(Util.round(interfaceOutgoingReport.getValueMin() / 1000000000, 2));
                interfaceOutgoingReport
                .setValueMax(Util.round(interfaceOutgoingReport.getValueMax() / 1000000000, 2));

            } else if ("Kbps".equals(myInterfaceOutcomingReport.getAccuracy())) {
                interfaceOutgoingReport.setAccuracy("Kbps");
                interfaceOutgoingReport.setValueAvg(Util.round(interfaceOutgoingReport.getValueAvg() / 1000000, 2));
                interfaceOutgoingReport.setValueMin(Util.round(interfaceOutgoingReport.getValueMin() / 1000000, 2));
                interfaceOutgoingReport.setValueMax(Util.round(interfaceOutgoingReport.getValueMax() / 1000000, 2));

            } else if ("bps".equals(myInterfaceOutcomingReport.getAccuracy())) {
                interfaceOutgoingReport.setAccuracy("bps");
                interfaceOutgoingReport.setValueAvg(Util.round(interfaceOutgoingReport.getValueAvg() / 1000, 2));
                interfaceOutgoingReport.setValueMin(Util.round(interfaceOutgoingReport.getValueMin() / 1000, 2));
                interfaceOutgoingReport.setValueMax(Util.round(interfaceOutgoingReport.getValueMax() / 1000, 2));

            }
            //}
        }
        i++;
        lowInterfaceOutgoingReports = Util.reduceList(lowInterfaceOutgoingReports, topNum);
        lowInterfaceOutgoingReports = parseSymbol(lowInterfaceOutgoingReports);
        return lowInterfaceOutgoingReports;
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 本周期，上周期高接口流入流量数据
     */
    public List<InterfaceOutgoingReport> selectMulticycleHighInterfaceOutgoingReportService(final String group,
        final Integer interval, final Integer topNum) {

        final Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);
        final List<InterfaceOutgoingReport> highInterfaceOutgoingReports = new ArrayList<>();
        List<InterfaceOutgoingReport> highInterfaceOutgoingReports2 = new ArrayList<>();
        List<InterfaceOutgoingReport> tempInterfaceOutgoingReports = new ArrayList<>();
        tempInterfaceOutgoingReports = selectHighInterfaceOutgoingReportService(group, interval, topNum);

        try {
            highInterfaceOutgoingReports2 = deepCopy(tempInterfaceOutgoingReports);
        } catch (final ClassNotFoundException e) {
            e.printStackTrace();
        } catch (final IOException e) {
            e.printStackTrace();
        }
        final String ehcacheKey2 = "adminHighInterfaceIncomingReports" + deadLine.toString() + group + interval
            + topNum;

        EhcacheUtil.put(ehcacheKey2, highInterfaceOutgoingReports2);

        final int size = tempInterfaceOutgoingReports.size();
        InterfaceOutgoingReport previousCycleInterfaceOutgoingReport = new InterfaceOutgoingReport();
        final Long start = deadLine - 2 * interval * timeFormat;
        final Long end = deadLine - interval * timeFormat;

        final InterfaceIncomingReport myInterfaceIncomingReport = new InterfaceIncomingReport();
        if (size != 0) {

            if ("Gbps".equals(tempInterfaceOutgoingReports.get(0).getAccuracy())) {
                myInterfaceIncomingReport.setAccuracy("Gbps");

            } else if ("Mbps".equals(tempInterfaceOutgoingReports.get(0).getAccuracy())) {
                myInterfaceIncomingReport.setAccuracy("Mbps");

            } else if ("Kbps".equals(tempInterfaceOutgoingReports.get(0).getAccuracy())) {
                myInterfaceIncomingReport.setAccuracy("Kbps");

            } else if ("bps".equals(tempInterfaceOutgoingReports.get(0).getAccuracy())) {
                myInterfaceIncomingReport.setAccuracy("bps");
            }

            for (int i = 0; i < size; i++) {
                previousCycleInterfaceOutgoingReport = interfaceOutgoingReportMapper
                    .selectInterfaceOutgoingReportByHostAndItemKey(tempInterfaceOutgoingReports.get(i).getHost(),
                        tempInterfaceOutgoingReports.get(i).getItemKey(), start,
                        end);
                //设置保留位数，数据太小，则不保留固定的位数
                if (previousCycleInterfaceOutgoingReport != null) {
                    if ("Gbps".equals(myInterfaceIncomingReport.getAccuracy())) {
                        previousCycleInterfaceOutgoingReport.setAccuracy("Gbps");
                        previousCycleInterfaceOutgoingReport.setValueAvg(Util.round(
                            Util.round(previousCycleInterfaceOutgoingReport.getValueAvg() / 1000000000, 2) / 1000, 2));
                        previousCycleInterfaceOutgoingReport.setValueMin(Util.round(
                            Util.round(previousCycleInterfaceOutgoingReport.getValueMin() / 1000000000, 2) / 1000, 2));
                        previousCycleInterfaceOutgoingReport.setValueMax(Util.round(
                            Util.round(previousCycleInterfaceOutgoingReport.getValueMax() / 1000000000, 2) / 1000, 2));
                    } else if ("Mbps".equals(myInterfaceIncomingReport.getAccuracy())) {
                        previousCycleInterfaceOutgoingReport.setAccuracy("Mbps");
                        previousCycleInterfaceOutgoingReport.setValueAvg(
                            Util.round(previousCycleInterfaceOutgoingReport.getValueAvg() / 1000000000, 2));
                        previousCycleInterfaceOutgoingReport.setValueMin(
                            Util.round(previousCycleInterfaceOutgoingReport.getValueMin() / 1000000000, 2));
                        previousCycleInterfaceOutgoingReport.setValueMax(
                            Util.round(previousCycleInterfaceOutgoingReport.getValueMax() / 1000000000, 2));
                    } else if ("Kbps".equals(myInterfaceIncomingReport.getAccuracy())) {
                        previousCycleInterfaceOutgoingReport.setAccuracy("Kbps");
                        previousCycleInterfaceOutgoingReport
                        .setValueAvg(Util.round(previousCycleInterfaceOutgoingReport.getValueAvg() / 1000000, 2));
                        previousCycleInterfaceOutgoingReport
                        .setValueMin(Util.round(previousCycleInterfaceOutgoingReport.getValueMin() / 1000000, 2));
                        previousCycleInterfaceOutgoingReport
                        .setValueMax(Util.round(previousCycleInterfaceOutgoingReport.getValueMax() / 1000000, 2));
                    } else if ("bps".equals(myInterfaceIncomingReport.getAccuracy())) {
                        previousCycleInterfaceOutgoingReport.setAccuracy("bps");
                        previousCycleInterfaceOutgoingReport
                        .setValueAvg(Util.round(previousCycleInterfaceOutgoingReport.getValueAvg() / 1000, 2));
                        previousCycleInterfaceOutgoingReport
                        .setValueMin(Util.round(previousCycleInterfaceOutgoingReport.getValueMin() / 1000, 2));
                        previousCycleInterfaceOutgoingReport
                        .setValueMax(Util.round(previousCycleInterfaceOutgoingReport.getValueMax() / 1000, 2));
                    }
                }

                highInterfaceOutgoingReports.add(tempInterfaceOutgoingReports.get(i));
                highInterfaceOutgoingReports.add(previousCycleInterfaceOutgoingReport);
            }
        }
        return highInterfaceOutgoingReports;
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 本周期，上周期低接口流入流量
     */
    public List<InterfaceOutgoingReport> selectMulticycleLowInterfaceOutgoingReportService(final String group,
        final Integer interval, final Integer topNum) {
        final Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);
        final List<InterfaceOutgoingReport> lowInterfaceOutgoingReports = new ArrayList<>();
        List<InterfaceOutgoingReport> tempInterfaceOutgoingReports = new ArrayList<>();
        tempInterfaceOutgoingReports = selectLowInterfaceOutgoingReportService(group, interval, topNum);

        final int size = tempInterfaceOutgoingReports.size();

        final InterfaceIncomingReport myInterfaceIncomingReport = new InterfaceIncomingReport();
        if (size != 0) {

            if ("Gbps".equals(tempInterfaceOutgoingReports.get(0).getAccuracy())) {
                myInterfaceIncomingReport.setAccuracy("Gbps");

            } else if ("Mbps".equals(tempInterfaceOutgoingReports.get(0).getAccuracy())) {
                myInterfaceIncomingReport.setAccuracy("Mbps");

            } else if ("Kbps".equals(tempInterfaceOutgoingReports.get(0).getAccuracy())) {
                myInterfaceIncomingReport.setAccuracy("Kbps");

            } else if ("bps".equals(tempInterfaceOutgoingReports.get(0).getAccuracy())) {
                myInterfaceIncomingReport.setAccuracy("bps");
            }

            for (int i = 0; i < size; i++) {
                InterfaceOutgoingReport previousCycleInterfaceOutgoingReport = new InterfaceOutgoingReport();
                previousCycleInterfaceOutgoingReport = interfaceOutgoingReportMapper
                    .selectInterfaceOutgoingReportByHostAndItemKey(tempInterfaceOutgoingReports.get(i).getHost(),
                        tempInterfaceOutgoingReports.get(i).getItemKey(), deadLine - 2 * interval * timeFormat,
                        deadLine - interval * timeFormat);

                //设置保留位数，数据太小，则不保留固定的位数
                if (previousCycleInterfaceOutgoingReport != null) {

                    if ("Gbps".equals(myInterfaceIncomingReport.getAccuracy())) {
                        previousCycleInterfaceOutgoingReport.setAccuracy("Gbps");
                        previousCycleInterfaceOutgoingReport.setValueAvg(Util.round(
                            Util.round(previousCycleInterfaceOutgoingReport.getValueAvg() / 1000000000, 2) / 1000, 2));
                        previousCycleInterfaceOutgoingReport.setValueMin(Util.round(
                            Util.round(previousCycleInterfaceOutgoingReport.getValueMin() / 1000000000, 2) / 1000, 2));
                        previousCycleInterfaceOutgoingReport.setValueMax(Util.round(
                            Util.round(previousCycleInterfaceOutgoingReport.getValueMax() / 1000000000, 2) / 1000, 2));
                    } else if ("Mbps".equals(myInterfaceIncomingReport.getAccuracy())) {
                        previousCycleInterfaceOutgoingReport.setAccuracy("Mbps");
                        previousCycleInterfaceOutgoingReport.setValueAvg(
                            Util.round(previousCycleInterfaceOutgoingReport.getValueAvg() / 1000000000, 2));
                        previousCycleInterfaceOutgoingReport.setValueMin(
                            Util.round(previousCycleInterfaceOutgoingReport.getValueMin() / 1000000000, 2));
                        previousCycleInterfaceOutgoingReport
                        .setValueMax(
                            Util.round(previousCycleInterfaceOutgoingReport.getValueMax() / 1000000000, 2));
                    } else if ("Kbps".equals(myInterfaceIncomingReport.getAccuracy())) {
                        previousCycleInterfaceOutgoingReport.setAccuracy("Kbps");
                        previousCycleInterfaceOutgoingReport
                        .setValueAvg(Util.round(previousCycleInterfaceOutgoingReport.getValueAvg() / 1000000, 2));
                        previousCycleInterfaceOutgoingReport
                        .setValueMin(Util.round(previousCycleInterfaceOutgoingReport.getValueMin() / 1000000, 2));
                        previousCycleInterfaceOutgoingReport
                        .setValueMax(Util.round(previousCycleInterfaceOutgoingReport.getValueMax() / 1000000, 2));
                    } else if ("bps".equals(myInterfaceIncomingReport.getAccuracy())) {
                        previousCycleInterfaceOutgoingReport.setAccuracy("bps");
                        previousCycleInterfaceOutgoingReport
                        .setValueAvg(Util.round(previousCycleInterfaceOutgoingReport.getValueAvg() / 1000, 2));
                        previousCycleInterfaceOutgoingReport
                        .setValueMin(Util.round(previousCycleInterfaceOutgoingReport.getValueMin() / 1000, 2));
                        previousCycleInterfaceOutgoingReport
                        .setValueMax(Util.round(previousCycleInterfaceOutgoingReport.getValueMax() / 1000, 2));
                    }
                }

                lowInterfaceOutgoingReports.add(tempInterfaceOutgoingReports.get(i)); //加入本周期数据
                lowInterfaceOutgoingReports.add(previousCycleInterfaceOutgoingReport); //加入上周期数据
            }
        }
        return lowInterfaceOutgoingReports;
    }

    /**
     * 创建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
        final Document document = new Document(PageSize.A4);
        //step 2
        if (fileName != null && !fileName.equals("") && !fileName.equals("cancel")) {
            try {
                final PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
                writer.setStrictImageSequence(true);
                //step 3
                document.open();
                //step 4
                final BaseFont baseFont = BaseFont.createFont(Util.getFontPath(), BaseFont.IDENTITY_H,
                    BaseFont.NOT_EMBEDDED);
                final Font font = new Font(baseFont);

                final Font f = new Font(baseFont, 30);
                final Paragraph p = new Paragraph("接口的流出流量报表", f);
                p.setAlignment(Element.ALIGN_CENTER);
                document.add(p);

                final 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));
                    final float heigth = img.getHeight();
                    final float width = img.getWidth();
                    final 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 (final FileNotFoundException e) {
                e.printStackTrace();
            } catch (final DocumentException e) {
                e.printStackTrace();
            } catch (final IOException e) {
                e.printStackTrace();
            } finally {
                document.close();
            }
        }
    }

    /**
     * 创建表格
     *
     * @param interfaceOutgoingReports
     *            接口流出流量数据
     * @return 表格
     */
    public static PdfPTable createTable(final List<InterfaceOutgoingReport> interfaceOutgoingReports) {
        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);
            final BaseFont baseFont = BaseFont.createFont(Util.getFontPath(), BaseFont.IDENTITY_H,
                BaseFont.NOT_EMBEDDED);
            final Font font = new Font(baseFont, 10);
            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));

            final int size = interfaceOutgoingReports.size();
            for (int i = 0; i < size; i = i + 2) {
                //需对ItemName进行截取，获取接口
                table.addCell(new Paragraph(
                    interfaceOutgoingReports.get(i).getHost() + ":" + interfaceOutgoingReports.get(i).getItemName(),
                    font));
                table.addCell(new Paragraph(interfaceOutgoingReports.get(i).getName(), font));
                table.addCell(Util.fullyDisplayNum(interfaceOutgoingReports.get(i).getValueAvg()));
                if (interfaceOutgoingReports.get(i + 1) == null) {
                    table.addCell(new Paragraph("-  -  -", font));
                } else {
                    table.addCell(Util.fullyDisplayNum(interfaceOutgoingReports.get(i + 1).getValueAvg()));
                }

                table.addCell(Util.fullyDisplayNum(interfaceOutgoingReports.get(i).getValueMax()));
                if (interfaceOutgoingReports.get(i + 1) == null) {
                    table.addCell(new Paragraph("-  -  -", font));
                } else {
                    table.addCell(Util.fullyDisplayNum(interfaceOutgoingReports.get(i + 1).getValueMax()));
                }

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

    /**
     * 替换$符号
     *
     * @param interfaceOutgoingReports
     *            接口流出流量
     * @return 替换$后的接口流出流量
     */
    public List<InterfaceOutgoingReport> parseSymbol(final List<InterfaceOutgoingReport> interfaceOutgoingReports) {
        for (final InterfaceOutgoingReport interfaceOutgoingReport : interfaceOutgoingReports) {
            if (interfaceOutgoingReport.getItemName().contains("$")) {
                final int begin = interfaceOutgoingReport.getItemKey().indexOf("[");
                final int end = interfaceOutgoingReport.getItemKey().indexOf("]");
                final String interfaceName = interfaceOutgoingReport.getItemKey().substring(begin + 1, end); //获取接口
                //interfaceOutgoingReport
                //  .setItemName(interfaceOutgoingReport.getItemName().replaceAll("\\$\\d", interfaceName));
                interfaceOutgoingReport.setItemName(interfaceName);
            }
        }
        return interfaceOutgoingReports;
    }

    public List<InterfaceOutgoingReport> selectHighInterfaceOutgoingReportServiceByDate(final String group,
        final Integer interval,
        final Integer topNum, final String startDate, final String endDate) {

        final 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 (final ParseException e) {
            e.printStackTrace();
        }
        final Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);
        List<InterfaceOutgoingReport> highInterfaceOutgoingReports = new ArrayList<>();
        highInterfaceOutgoingReports = interfaceOutgoingReportMapper.selectHighInterfaceOutgoingReport(group,
            myStartDate, myEndDate);
        final Iterator<InterfaceOutgoingReport> it = highInterfaceOutgoingReports.iterator();

        final InterfaceOutgoingReport myInterfaceOutcomingReport = new InterfaceOutgoingReport();

        if (highInterfaceOutgoingReports.get(0).getValueAvg() / 10000 > 1000000000) {
            myInterfaceOutcomingReport.setAccuracy("Gbps");

        } else if (highInterfaceOutgoingReports.get(0).getValueAvg() / 10000 > 1000000) {
            myInterfaceOutcomingReport.setAccuracy("Mbps");

        } else if (highInterfaceOutgoingReports.get(0).getValueAvg() / 10000 > 1000) {
            myInterfaceOutcomingReport.setAccuracy("Kbps");

        } else if (highInterfaceOutgoingReports.get(0).getValueAvg() / 1000 > 1) {
            myInterfaceOutcomingReport.setAccuracy("bps");
        }

        while (it.hasNext()) {
            final InterfaceOutgoingReport interfaceOutgoingReport = it.next();
            if (interfaceOutgoingReport.getValueAvg() == 0) {
                it.remove(); //移除数据
            }
            if ("Gbps".equals(myInterfaceOutcomingReport.getAccuracy())) {
                interfaceOutgoingReport.setAccuracy("Gbps");
                interfaceOutgoingReport.setValueAvg(
                    Util.round(Util.round(interfaceOutgoingReport.getValueAvg() / 1000000000, 2) / 1000, 2));
                interfaceOutgoingReport.setValueMin(
                    Util.round(Util.round(interfaceOutgoingReport.getValueMin() / 1000000000, 2) / 1000, 2));
                interfaceOutgoingReport.setValueMax(
                    Util.round(Util.round(interfaceOutgoingReport.getValueMax() / 1000000000, 2) / 1000, 2));
            } else if ("Mbps".equals(myInterfaceOutcomingReport.getAccuracy())) {
                interfaceOutgoingReport.setAccuracy("Mbps");
                interfaceOutgoingReport.setValueAvg(Util.round(interfaceOutgoingReport.getValueAvg() / 1000000000, 2));
                interfaceOutgoingReport.setValueMin(Util.round(interfaceOutgoingReport.getValueMin() / 1000000000, 2));
                interfaceOutgoingReport.setValueMax(Util.round(interfaceOutgoingReport.getValueMax() / 1000000000, 2));
            } else if ("Kbps".equals(myInterfaceOutcomingReport.getAccuracy())) {
                interfaceOutgoingReport.setAccuracy("Kbps");
                interfaceOutgoingReport.setValueAvg(Util.round(interfaceOutgoingReport.getValueAvg() / 1000000, 2));
                interfaceOutgoingReport.setValueMin(Util.round(interfaceOutgoingReport.getValueMin() / 1000000, 2));
                interfaceOutgoingReport.setValueMax(Util.round(interfaceOutgoingReport.getValueMax() / 1000000, 2));
            } else if ("bps".equals(myInterfaceOutcomingReport.getAccuracy())) {
                interfaceOutgoingReport.setAccuracy("bps");
                interfaceOutgoingReport.setValueAvg(Util.round(interfaceOutgoingReport.getValueAvg() / 1000, 2));
                interfaceOutgoingReport.setValueMin(Util.round(interfaceOutgoingReport.getValueMin() / 1000, 2));
                interfaceOutgoingReport.setValueMax(Util.round(interfaceOutgoingReport.getValueMax() / 1000, 2));
            }
        }

        highInterfaceOutgoingReports = Util.reduceList(highInterfaceOutgoingReports, topNum); //剔除数据
        highInterfaceOutgoingReports = parseSymbol(highInterfaceOutgoingReports);
        return highInterfaceOutgoingReports;
    }

    public List<InterfaceOutgoingReport> selectLowInterfaceOutgoingReportServiceByDate(final String group,
        final Integer interval,
        final Integer topNum, final String startDate, final String endDate) {

        final 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 (final ParseException e) {
            e.printStackTrace();
        }

        final Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);
        List<InterfaceOutgoingReport> lowInterfaceOutgoingReports = new ArrayList<>();
        lowInterfaceOutgoingReports = interfaceOutgoingReportMapper.selectLowInterfaceOutgoingReport(group,
            myStartDate, myEndDate);

        final InterfaceOutgoingReport myInterfaceOutcomingReport = new InterfaceOutgoingReport();

        if (lowInterfaceOutgoingReports.get(0).getValueAvg() / 10000 > 1000000000) {
            myInterfaceOutcomingReport.setAccuracy("Gbps");

        } else if (lowInterfaceOutgoingReports.get(0).getValueAvg() / 10000 > 1000000) {
            myInterfaceOutcomingReport.setAccuracy("Mbps");

        } else if (lowInterfaceOutgoingReports.get(0).getValueAvg() / 10000 > 1000) {
            myInterfaceOutcomingReport.setAccuracy("Kbps");

        } else if (lowInterfaceOutgoingReports.get(0).getValueAvg() / 1000 > 1) {
            myInterfaceOutcomingReport.setAccuracy("bps");
        } else {
            myInterfaceOutcomingReport.setAccuracy("bps");
        }

        final Iterator<InterfaceOutgoingReport> it = lowInterfaceOutgoingReports.iterator();
        while (it.hasNext()) {
            final InterfaceOutgoingReport interfaceOutgoingReport = it.next();
            if (interfaceOutgoingReport.getValueAvg() == 0) {
                it.remove(); //移除数据
            }

            //设置保留位数，数据太小，则不保留固定的位数
            //if (interfaceOutgoingReport != null) {

            if ("Gbps".equals(myInterfaceOutcomingReport.getAccuracy())) {
                interfaceOutgoingReport.setAccuracy("Gbps");
                interfaceOutgoingReport.setValueAvg(
                    Util.round(Util.round(interfaceOutgoingReport.getValueAvg() / 1000000000, 2) / 1000, 2));
                interfaceOutgoingReport.setValueMin(
                    Util.round(Util.round(interfaceOutgoingReport.getValueMin() / 1000000000, 2) / 1000, 2));
                interfaceOutgoingReport.setValueMax(
                    Util.round(Util.round(interfaceOutgoingReport.getValueMax() / 1000000000, 2) / 1000, 2));
            } else if ("Mbps".equals(myInterfaceOutcomingReport.getAccuracy())) {
                interfaceOutgoingReport.setAccuracy("Mbps");
                interfaceOutgoingReport
                .setValueAvg(Util.round(interfaceOutgoingReport.getValueAvg() / 1000000000, 2));
                interfaceOutgoingReport
                .setValueMin(Util.round(interfaceOutgoingReport.getValueMin() / 1000000000, 2));
                interfaceOutgoingReport
                .setValueMax(Util.round(interfaceOutgoingReport.getValueMax() / 1000000000, 2));
            } else if ("Kbps".equals(myInterfaceOutcomingReport.getAccuracy())) {
                interfaceOutgoingReport.setAccuracy("Kbps");
                interfaceOutgoingReport.setValueAvg(Util.round(interfaceOutgoingReport.getValueAvg() / 1000000, 2));
                interfaceOutgoingReport.setValueMin(Util.round(interfaceOutgoingReport.getValueMin() / 1000000, 2));
                interfaceOutgoingReport.setValueMax(Util.round(interfaceOutgoingReport.getValueMax() / 1000000, 2));
            } else if ("bps".equals(myInterfaceOutcomingReport.getAccuracy())) {
                interfaceOutgoingReport.setAccuracy("bps");
                interfaceOutgoingReport.setValueAvg(Util.round(interfaceOutgoingReport.getValueAvg() / 1000, 2));
                interfaceOutgoingReport.setValueMin(Util.round(interfaceOutgoingReport.getValueMin() / 1000, 2));
                interfaceOutgoingReport.setValueMax(Util.round(interfaceOutgoingReport.getValueMax() / 1000, 2));
            }
            //}
        }
        lowInterfaceOutgoingReports = Util.reduceList(lowInterfaceOutgoingReports, topNum);
        lowInterfaceOutgoingReports = parseSymbol(lowInterfaceOutgoingReports);
        return lowInterfaceOutgoingReports;
    }

    public List<InterfaceOutgoingReport> selectMulticycleHighInterfaceOutgoingReportServiceByDate(final String group,
        final Integer interval, final Integer topNum, final String startDate, final String endDate) {

        final 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 (final ParseException e) {
            e.printStackTrace();
        }

        final Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);
        final List<InterfaceOutgoingReport> highInterfaceOutgoingReports = new ArrayList<>();
        List<InterfaceOutgoingReport> tempInterfaceOutgoingReports = new ArrayList<>();
        tempInterfaceOutgoingReports = selectHighInterfaceOutgoingReportServiceByDate(group, interval, topNum,
            myStartDate, myEndDate);
        final int size = tempInterfaceOutgoingReports.size();
        InterfaceOutgoingReport previousCycleInterfaceOutgoingReport = new InterfaceOutgoingReport();

        final InterfaceOutgoingReport myInterfaceOutcomingReport = new InterfaceOutgoingReport();

        if (tempInterfaceOutgoingReports.get(0).getValueAvg() / 10000 > 1000000000) {
            myInterfaceOutcomingReport.setAccuracy("Gbps");

        } else if (tempInterfaceOutgoingReports.get(0).getValueAvg() / 10000 > 1000000) {
            myInterfaceOutcomingReport.setAccuracy("Mbps");

        } else if (tempInterfaceOutgoingReports.get(0).getValueAvg() / 10000 > 1000) {
            myInterfaceOutcomingReport.setAccuracy("Kbps");

        } else if (tempInterfaceOutgoingReports.get(0).getValueAvg() / 1000 > 1) {
            myInterfaceOutcomingReport.setAccuracy("bps");
        } else {
            myInterfaceOutcomingReport.setAccuracy("bps");
        }
        for (int i = 0; i < size; i++) {
            previousCycleInterfaceOutgoingReport = interfaceOutgoingReportMapper
                .selectInterfaceOutgoingReportByHostAndItemKey(tempInterfaceOutgoingReports.get(i).getHost(),
                    tempInterfaceOutgoingReports.get(i).getItemKey(), myStartDate - 2 * cycle, myStartDate - cycle);
            //设置保留位数，数据太小，则不保留固定的位数
            if (previousCycleInterfaceOutgoingReport != null) {

                if ("Gbps".equals(myInterfaceOutcomingReport.getAccuracy())) {
                    previousCycleInterfaceOutgoingReport.setAccuracy("Gbps");
                    previousCycleInterfaceOutgoingReport.setValueAvg(Util.round(
                        Util.round(previousCycleInterfaceOutgoingReport.getValueAvg() / 1000000000, 2) / 1000, 2));
                    previousCycleInterfaceOutgoingReport.setValueMin(Util.round(
                        Util.round(previousCycleInterfaceOutgoingReport.getValueMin() / 1000000000, 2) / 1000, 2));
                    previousCycleInterfaceOutgoingReport.setValueMax(Util.round(
                        Util.round(previousCycleInterfaceOutgoingReport.getValueMax() / 1000000000, 2) / 1000, 2));
                } else if ("Mbps".equals(myInterfaceOutcomingReport.getAccuracy())) {
                    previousCycleInterfaceOutgoingReport.setAccuracy("Mbps");
                    previousCycleInterfaceOutgoingReport
                    .setValueAvg(Util.round(previousCycleInterfaceOutgoingReport.getValueAvg() / 1000000000, 2));
                    previousCycleInterfaceOutgoingReport
                    .setValueMin(Util.round(previousCycleInterfaceOutgoingReport.getValueMin() / 1000000000, 2));
                    previousCycleInterfaceOutgoingReport
                    .setValueMax(Util.round(previousCycleInterfaceOutgoingReport.getValueMax() / 1000000000, 2));
                } else if ("Kbps".equals(myInterfaceOutcomingReport.getAccuracy())) {
                    previousCycleInterfaceOutgoingReport.setAccuracy("Kbps");
                    previousCycleInterfaceOutgoingReport
                    .setValueAvg(Util.round(previousCycleInterfaceOutgoingReport.getValueAvg() / 1000000, 2));
                    previousCycleInterfaceOutgoingReport
                    .setValueMin(Util.round(previousCycleInterfaceOutgoingReport.getValueMin() / 1000000, 2));
                    previousCycleInterfaceOutgoingReport
                    .setValueMax(Util.round(previousCycleInterfaceOutgoingReport.getValueMax() / 1000000, 2));

                } else if ("bps".equals(myInterfaceOutcomingReport.getAccuracy())) {
                    previousCycleInterfaceOutgoingReport.setAccuracy("bps");
                    previousCycleInterfaceOutgoingReport
                    .setValueAvg(Util.round(previousCycleInterfaceOutgoingReport.getValueAvg() / 1000, 2));
                    previousCycleInterfaceOutgoingReport
                    .setValueMin(Util.round(previousCycleInterfaceOutgoingReport.getValueMin() / 1000, 2));
                    previousCycleInterfaceOutgoingReport
                    .setValueMax(Util.round(previousCycleInterfaceOutgoingReport.getValueMax() / 1000, 2));
                }
            }

            highInterfaceOutgoingReports.add(tempInterfaceOutgoingReports.get(i));
            highInterfaceOutgoingReports.add(previousCycleInterfaceOutgoingReport);
        }

        return highInterfaceOutgoingReports;
    }

    public List<InterfaceOutgoingReport> selectMulticycleLowInterfaceOutgoingReportServiceByDate(final String group,
        final Integer interval, final Integer topNum, final String startDate, final String endDate) {

        final 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 (final ParseException e) {
            e.printStackTrace();
        }

        final Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);
        final List<InterfaceOutgoingReport> lowInterfaceOutgoingReports = new ArrayList<>();
        List<InterfaceOutgoingReport> tempInterfaceOutgoingReports = new ArrayList<>();
        tempInterfaceOutgoingReports = selectLowInterfaceOutgoingReportServiceByDate(group, interval, topNum,
            myStartDate, myEndDate);

        final InterfaceOutgoingReport myInterfaceOutcomingReport = new InterfaceOutgoingReport();

        if (tempInterfaceOutgoingReports.get(0).getValueAvg() / 10000 > 1000000000) {
            myInterfaceOutcomingReport.setAccuracy("Gbps");

        } else if (tempInterfaceOutgoingReports.get(0).getValueAvg() / 10000 > 1000000) {
            myInterfaceOutcomingReport.setAccuracy("Mbps");

        } else if (tempInterfaceOutgoingReports.get(0).getValueAvg() / 10000 > 1000) {
            myInterfaceOutcomingReport.setAccuracy("Kbps");

        } else if (tempInterfaceOutgoingReports.get(0).getValueAvg() / 1000 > 1) {
            myInterfaceOutcomingReport.setAccuracy("bps");
        } else {
            myInterfaceOutcomingReport.setAccuracy("bps");
        }

        final int size = tempInterfaceOutgoingReports.size();
        for (int i = 0; i < size; i++) {
            InterfaceOutgoingReport previousCycleInterfaceOutgoingReport = new InterfaceOutgoingReport();
            previousCycleInterfaceOutgoingReport = interfaceOutgoingReportMapper
                .selectInterfaceOutgoingReportByHostAndItemKey(tempInterfaceOutgoingReports.get(i).getHost(),
                    tempInterfaceOutgoingReports.get(i).getItemKey(), deadLine - 2 * interval * timeFormat,
                    deadLine - interval * timeFormat);

            //设置保留位数，数据太小，则不保留固定的位数
            if (previousCycleInterfaceOutgoingReport != null) {

                if ("Gbps".equals(myInterfaceOutcomingReport.getAccuracy())) {
                    previousCycleInterfaceOutgoingReport.setAccuracy("Gbps");
                    previousCycleInterfaceOutgoingReport.setValueAvg(Util.round(
                        Util.round(previousCycleInterfaceOutgoingReport.getValueMax() / 1000000000, 2) / 1000, 2));
                } else if ("Mbps".equals(myInterfaceOutcomingReport.getAccuracy())) {
                    previousCycleInterfaceOutgoingReport.setAccuracy("Mbps");
                    previousCycleInterfaceOutgoingReport
                    .setValueAvg(Util.round(previousCycleInterfaceOutgoingReport.getValueAvg() / 1000000000, 2));
                } else if ("Kbps".equals(myInterfaceOutcomingReport.getAccuracy())) {
                    previousCycleInterfaceOutgoingReport.setAccuracy("Kbps");
                    previousCycleInterfaceOutgoingReport
                    .setValueAvg(Util.round(previousCycleInterfaceOutgoingReport.getValueAvg() / 1000000, 2));
                } else if ("bps".equals(myInterfaceOutcomingReport.getAccuracy())) {
                    previousCycleInterfaceOutgoingReport.setAccuracy("bps");
                    previousCycleInterfaceOutgoingReport
                    .setValueAvg(Util.round(previousCycleInterfaceOutgoingReport.getValueAvg() / 1000, 2));
                }

                if (previousCycleInterfaceOutgoingReport.getValueMin() / 1000 < 0.01) {
                    previousCycleInterfaceOutgoingReport
                    .setValueMin(Util.round(previousCycleInterfaceOutgoingReport.getValueMin() / 1000, 4));
                } else {
                    previousCycleInterfaceOutgoingReport
                    .setValueMin(Util.round(previousCycleInterfaceOutgoingReport.getValueMin() / 1000, 2));
                }

                if ("bps".equals(previousCycleInterfaceOutgoingReport.getAccuracy())) {
                    previousCycleInterfaceOutgoingReport
                    .setValueMax(Util.round(previousCycleInterfaceOutgoingReport.getValueMax() / 1000, 4));
                } else if ("Kbps".equals(previousCycleInterfaceOutgoingReport.getAccuracy())) {
                    previousCycleInterfaceOutgoingReport
                    .setValueMax(Util.round(previousCycleInterfaceOutgoingReport.getValueMax() / 1000000, 2));
                } else if ("Mbps".equals(previousCycleInterfaceOutgoingReport.getAccuracy())) {
                    previousCycleInterfaceOutgoingReport
                    .setValueMax(Util.round(previousCycleInterfaceOutgoingReport.getValueMax() / 1000000000, 2));
                } else if ("Gbps".equals(previousCycleInterfaceOutgoingReport.getAccuracy())) {
                    previousCycleInterfaceOutgoingReport
                    .setValueMax(Util.round(previousCycleInterfaceOutgoingReport.getValueMax() / 1000000000, 2));
                }
            }

            lowInterfaceOutgoingReports.add(tempInterfaceOutgoingReports.get(i)); //加入本周期数据
            lowInterfaceOutgoingReports.add(previousCycleInterfaceOutgoingReport); //加入上周期数据
        }
        return lowInterfaceOutgoingReports;
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 高接口流入流量
     */
    public List<InterfaceOutgoingReport> selectHighInterfaceOutgoingReportServiceByDate(final String group,
        final Integer interval,
        final Integer topNum, final long startDate, final long endDate) {
        final Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);
        List<InterfaceOutgoingReport> highInterfaceOutgoingReports = new ArrayList<>();
        highInterfaceOutgoingReports = interfaceOutgoingReportMapper.selectHighInterfaceOutgoingReport(group,
            startDate, endDate);
        final Iterator<InterfaceOutgoingReport> it = highInterfaceOutgoingReports.iterator();

        final InterfaceOutgoingReport myInterfaceOutcomingReport = new InterfaceOutgoingReport();

        if (highInterfaceOutgoingReports.get(0).getValueAvg() / 10000 > 1000000000) {
            myInterfaceOutcomingReport.setAccuracy("Gbps");

        } else if (highInterfaceOutgoingReports.get(0).getValueAvg() / 10000 > 1000000) {
            myInterfaceOutcomingReport.setAccuracy("Mbps");

        } else if (highInterfaceOutgoingReports.get(0).getValueAvg() / 10000 > 1000) {
            myInterfaceOutcomingReport.setAccuracy("Kbps");

        } else if (highInterfaceOutgoingReports.get(0).getValueAvg() / 1000 > 1) {
            myInterfaceOutcomingReport.setAccuracy("bps");
        } else {
            myInterfaceOutcomingReport.setAccuracy("bps");
        }

        while (it.hasNext()) {
            final InterfaceOutgoingReport interfaceOutgoingReport = it.next();
            if (interfaceOutgoingReport.getValueAvg() == 0) {
                it.remove(); //移除数据
            }

            if ("Gbps".equals(myInterfaceOutcomingReport.getAccuracy())) {
                interfaceOutgoingReport.setAccuracy("Gbps");
                interfaceOutgoingReport.setValueAvg(
                    Util.round(Util.round(interfaceOutgoingReport.getValueAvg() / 1000000000, 2) / 1000, 2));
            } else if ("Mbps".equals(myInterfaceOutcomingReport.getAccuracy())) {
                interfaceOutgoingReport.setAccuracy("Mbps");
                interfaceOutgoingReport.setValueAvg(Util.round(interfaceOutgoingReport.getValueAvg() / 1000000000, 2));
            } else if ("Kbps".equals(myInterfaceOutcomingReport.getAccuracy())) {
                interfaceOutgoingReport.setAccuracy("Kbps");
                interfaceOutgoingReport.setValueAvg(Util.round(interfaceOutgoingReport.getValueAvg() / 1000000, 2));
            } else if ("bps".equals(myInterfaceOutcomingReport.getAccuracy())) {
                interfaceOutgoingReport.setAccuracy("bps");
                interfaceOutgoingReport.setValueAvg(Util.round(interfaceOutgoingReport.getValueAvg() / 1000, 2));
            }

            if ("Kbps".equals(interfaceOutgoingReport.getAccuracy())) {
                interfaceOutgoingReport.setValueMin(Util.round(interfaceOutgoingReport.getValueMin() / 1000000, 2));
            } else if ("Mbps".equals(interfaceOutgoingReport.getAccuracy())) {
                interfaceOutgoingReport.setValueMin(Util.round(interfaceOutgoingReport.getValueMin() / 1000000000, 2));
            } else if ("Gbps".equals(interfaceOutgoingReport.getAccuracy())) {
                interfaceOutgoingReport.setValueMin(
                    Util.round(Util.round(interfaceOutgoingReport.getValueMin() / 1000000000, 2) / 1000, 2));
            } else {

                interfaceOutgoingReport.setValueMin(Util.round(interfaceOutgoingReport.getValueMin() / 1000, 2));

            }

            if ("Kbps".equals(interfaceOutgoingReport.getAccuracy())) {
                interfaceOutgoingReport.setValueMax(Util.round(interfaceOutgoingReport.getValueMax() / 1000000, 2));
            } else if ("Mbps".equals(interfaceOutgoingReport.getAccuracy())) {
                interfaceOutgoingReport.setValueMax(Util.round(interfaceOutgoingReport.getValueMax() / 1000000000, 2));
            } else if ("Gbps".equals(interfaceOutgoingReport.getAccuracy())) {
                interfaceOutgoingReport.setValueMax(
                    Util.round(Util.round(interfaceOutgoingReport.getValueMax() / 1000000000, 2) / 1000, 2));
            } else {

                interfaceOutgoingReport.setValueMax(Util.round(interfaceOutgoingReport.getValueMax() / 1000, 2));

            }
        }

        highInterfaceOutgoingReports = Util.reduceList(highInterfaceOutgoingReports, topNum); //剔除数据
        highInterfaceOutgoingReports = parseSymbol(highInterfaceOutgoingReports);
        return highInterfaceOutgoingReports;
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 低接口流入流量
     */
    public List<InterfaceOutgoingReport> selectLowInterfaceOutgoingReportServiceByDate(final String group,
        final Integer interval,
        final Integer topNum, final long startDate, final long endDate) {
        final Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);
        List<InterfaceOutgoingReport> lowInterfaceOutgoingReports = new ArrayList<>();
        lowInterfaceOutgoingReports = interfaceOutgoingReportMapper.selectLowInterfaceOutgoingReport(group,
            startDate, endDate);
        final Iterator<InterfaceOutgoingReport> it = lowInterfaceOutgoingReports.iterator();

        final InterfaceOutgoingReport myInterfaceOutcomingReport = new InterfaceOutgoingReport();

        if (lowInterfaceOutgoingReports.size() > 0) {
            if (lowInterfaceOutgoingReports.get(0).getValueAvg() / 10000 > 1000000000) {
                myInterfaceOutcomingReport.setAccuracy("Gbps");

            } else if (lowInterfaceOutgoingReports.get(0).getValueAvg() / 10000 > 1000000) {
                myInterfaceOutcomingReport.setAccuracy("Mbps");

            } else if (lowInterfaceOutgoingReports.get(0).getValueAvg() / 10000 > 1000) {
                myInterfaceOutcomingReport.setAccuracy("Kbps");

            } else if (lowInterfaceOutgoingReports.get(0).getValueAvg() / 1000 > 1) {
                myInterfaceOutcomingReport.setAccuracy("bps");
            } else {
                myInterfaceOutcomingReport.setAccuracy("bps");
            }
        }

        while (it.hasNext()) {
            final InterfaceOutgoingReport interfaceOutgoingReport = it.next();
            if (interfaceOutgoingReport.getValueAvg() == 0) {
                it.remove(); //移除数据
            }
            //设置保留位数，数据太小，则不保留固定的位数
            // if (interfaceOutgoingReport != null) {

            if ("Gbps".equals(myInterfaceOutcomingReport.getAccuracy())) {
                interfaceOutgoingReport.setAccuracy("Gbps");
                interfaceOutgoingReport.setValueAvg(
                    Util.round(Util.round(interfaceOutgoingReport.getValueAvg() / 1000000000, 2) / 1000, 2));
                interfaceOutgoingReport.setValueMin(
                    Util.round(Util.round(interfaceOutgoingReport.getValueMin() / 1000000000, 2) / 1000, 2));
                interfaceOutgoingReport.setValueMax(
                    Util.round(Util.round(interfaceOutgoingReport.getValueMax() / 1000000000, 2) / 1000, 2));

            } else if ("Mbps".equals(myInterfaceOutcomingReport.getAccuracy())) {
                interfaceOutgoingReport.setAccuracy("Mbps");
                interfaceOutgoingReport
                .setValueAvg(Util.round(interfaceOutgoingReport.getValueAvg() / 1000000000, 2));
                interfaceOutgoingReport
                .setValueMin(Util.round(interfaceOutgoingReport.getValueMin() / 1000000000, 2));
                interfaceOutgoingReport
                .setValueMax(Util.round(interfaceOutgoingReport.getValueMax() / 1000000000, 2));

            } else if ("Kbps".equals(myInterfaceOutcomingReport.getAccuracy())) {
                interfaceOutgoingReport.setAccuracy("Kbps");
                interfaceOutgoingReport.setValueAvg(Util.round(interfaceOutgoingReport.getValueAvg() / 1000000, 2));
                interfaceOutgoingReport.setValueMin(Util.round(interfaceOutgoingReport.getValueMin() / 1000000, 2));
                interfaceOutgoingReport.setValueMax(Util.round(interfaceOutgoingReport.getValueMax() / 1000000, 2));

            } else if ("bps".equals(myInterfaceOutcomingReport.getAccuracy())) {
                interfaceOutgoingReport.setAccuracy("bps");
                interfaceOutgoingReport.setValueAvg(Util.round(interfaceOutgoingReport.getValueAvg() / 1000, 2));
                interfaceOutgoingReport.setValueMin(Util.round(interfaceOutgoingReport.getValueMin() / 1000, 2));
                interfaceOutgoingReport.setValueMax(Util.round(interfaceOutgoingReport.getValueMax() / 1000, 2));

            }

            // }
        }
        lowInterfaceOutgoingReports = Util.reduceList(lowInterfaceOutgoingReports, topNum);
        lowInterfaceOutgoingReports = parseSymbol(lowInterfaceOutgoingReports);
        return lowInterfaceOutgoingReports;
    }

}
