/**
 * <p>Copyright:Copyright(c) 2017</p>
 * <p>Company:上海中信信息发展股份有限公司</p>
 * <p>包名:com.cesgroup.cesmonitor.service</p>
 * <p>文件名:InterfaceIncomingReportService.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.common.EhcacheUtil;
import com.cesgroup.cesmonitor.common.Util;
import com.cesgroup.cesmonitor.dao.InterfaceIncomingReportMapper;
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("interfaceIncomingReportService")
public class InterfaceIncomingReportService {

    @Autowired
    private InterfaceIncomingReportMapper interfaceIncomingReportMapper;

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

    /**
     *
     * @param src
     *            资源
     * @return 信息
     * @throws IOException
     *             异常
     * @throws ClassNotFoundException
     *             异常
     */
    public static <T> List<T> deepCopy(final List<T> src) throws IOException, ClassNotFoundException {
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(byteOut);
        out.writeObject(src);

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

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

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

        Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);
        List<InterfaceIncomingReport> highInterfaceIncomingReports = new ArrayList<>();
        List<InterfaceIncomingReport> highInterfaceIncomingReports2 = new ArrayList<>();
        List<InterfaceIncomingReport> highInterfaceIncomingReports3 = new ArrayList<>();

        if (isSuperAdmin) {
            highInterfaceIncomingReports = interfaceIncomingReportMapper.selectHighInterfaceIncomingReport(group,
                deadLine - interval * timeFormat, deadLine);
            try {
                highInterfaceIncomingReports2 = deepCopy(highInterfaceIncomingReports);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            String ehcacheKey2 = "adminHighInterfaceIncomingReports" + deadLine.toString() + group + interval + topNum;
            EhcacheUtil.put(ehcacheKey2, highInterfaceIncomingReports2);

        } else {
            highInterfaceIncomingReports = interfaceIncomingReportMapper.selectHighInterfaceIncomingReportByUser(group,
                deadLine - interval * timeFormat, deadLine, userId);

            highInterfaceIncomingReports3.addAll(highInterfaceIncomingReports);
            String ehcacheKey2 = "myhighInterfaceIncomingReports" + deadLine.toString() + group + interval + topNum;

            EhcacheUtil.put(ehcacheKey2, highInterfaceIncomingReports3);

        }
        if (highInterfaceIncomingReports.size() > 0) {
            String ehcacheKey = "highInterfaceIncomingReports" + deadLine.toString() + group + interval + topNum;
            EhcacheUtil.put(ehcacheKey, highInterfaceIncomingReports);

            Iterator<InterfaceIncomingReport> it = highInterfaceIncomingReports.iterator();
            highInterfaceIncomingReports.get(0).getValueAvg();

            InterfaceIncomingReport myInterfaceIncomingReport = new InterfaceIncomingReport();

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

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

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

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

            while (it.hasNext()) {
                InterfaceIncomingReport interfaceIncomingReport = it.next();
                if (interfaceIncomingReport.getValueAvg() == 0) {
                    it.remove();
                }
                if ("Gbps".equals(myInterfaceIncomingReport.getAccuracy())) {
                    interfaceIncomingReport.setAccuracy("Gbps");
                    interfaceIncomingReport.setValueAvg(
                        Util.round(Util.round(interfaceIncomingReport.getValueAvg() / 1000000000, 2) / 1000, 2));
                    interfaceIncomingReport.setValueMin(
                        Util.round(Util.round(interfaceIncomingReport.getValueMin() / 1000000000, 2) / 1000, 2));
                    interfaceIncomingReport.setValueMax(
                        Util.round(Util.round(interfaceIncomingReport.getValueMax() / 1000000000, 2) / 1000, 2));

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

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

                } else if ("bps".equals(myInterfaceIncomingReport.getAccuracy())) {
                    interfaceIncomingReport.setAccuracy("bps");
                    interfaceIncomingReport.setValueAvg(Util.round(interfaceIncomingReport.getValueAvg() / 1000, 2));
                    interfaceIncomingReport.setValueMin(Util.round(interfaceIncomingReport.getValueMin() / 1000, 2));
                    interfaceIncomingReport.setValueMax(Util.round(interfaceIncomingReport.getValueMax() / 1000, 2));
                }
            }
            highInterfaceIncomingReports = Util.reduceList(highInterfaceIncomingReports, topNum); // 剔除数据
            highInterfaceIncomingReports = parseSymbol(highInterfaceIncomingReports);
        }
        return highInterfaceIncomingReports;
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 低接口流入流量
     */

    public List<InterfaceIncomingReport> selectLowInterfaceIncomingReportService(final String group,
        final Integer interval,
        final Integer topNum) {
        //long startTime = System.currentTimeMillis(); // 获取开始时间
        LoginUser user = Util.getLoginUser();
        Integer userId = user.getUserid();
        boolean isSuperAdmin = user.isSuperAdmin();

        Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);
        List<InterfaceIncomingReport> lowInterfaceIncomingReports = new ArrayList<>();
        List<InterfaceIncomingReport> lowInterfaceIncomingReports2 = new ArrayList<>();

        if (isSuperAdmin) {

            String ehcacheKey = "adminHighInterfaceIncomingReports" + deadLine.toString() + group + interval + topNum;
            lowInterfaceIncomingReports2 = (List<InterfaceIncomingReport>) EhcacheUtil.get(ehcacheKey);
            try {
                lowInterfaceIncomingReports = deepCopy(lowInterfaceIncomingReports2);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (null == lowInterfaceIncomingReports) {

                lowInterfaceIncomingReports = interfaceIncomingReportMapper.selectLowInterfaceIncomingReport(group,
                    deadLine - interval * timeFormat, deadLine);

            } else {
                Collections.reverse(lowInterfaceIncomingReports);
            }
        } else {
            lowInterfaceIncomingReports = interfaceIncomingReportMapper.selectLowInterfaceIncomingReportByUser(group,
                deadLine - interval * timeFormat, deadLine, userId);
        }
        InterfaceIncomingReport myInterfaceIncomingReport = new InterfaceIncomingReport();
        //System.err.println("240lowInterfaceIncomingReports" + lowInterfaceIncomingReports);
        //System.err.println("size1()" + lowInterfaceIncomingReports.size());
        if (lowInterfaceIncomingReports.size() > 0) {
            //System.err.println("size()" + lowInterfaceIncomingReports.size());

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

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

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

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

            Iterator<InterfaceIncomingReport> it = lowInterfaceIncomingReports.iterator();
            while (it.hasNext()) {
                InterfaceIncomingReport interfaceIncomingReport = it.next();
                if (interfaceIncomingReport.getValueAvg() == 0) {
                    it.remove(); // 移除数据
                }

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

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

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

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

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

            //  long endTime = System.currentTimeMillis(); // 获取结束时间
        }
        //System.out.println("selectLowInterfaceIncoming程序运行时间：" + (endTime - startTime) + "ms"); // 输出程序运行时间
        // System.err.println("调用查询" + lowInterfaceIncomingReports);
        return lowInterfaceIncomingReports;
    }

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

        LoginUser user = Util.getLoginUser();
        Integer userId = user.getUserid();
        boolean isSuperAdmin = user.isSuperAdmin();
        Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);
        List<InterfaceIncomingReport> lowInterfaceIncomingReports = new ArrayList<>();
        if (isSuperAdmin) {
            String ehcacheKey = "adminHighInterfaceIncomingReports" + deadLine.toString() + group + interval + topNum;
            lowInterfaceIncomingReports = (List<InterfaceIncomingReport>) EhcacheUtil.get(ehcacheKey);
            Collections.reverse(lowInterfaceIncomingReports);
        } else {
            lowInterfaceIncomingReports = interfaceIncomingReportMapper.selectLowInterfaceIncomingReportByUser(group,
                deadLine - interval * timeFormat, deadLine, userId);
        }
        InterfaceIncomingReport myInterfaceIncomingReport = new InterfaceIncomingReport();

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

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

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

        } else if (lowInterfaceIncomingReports.get(0).getValueAvg() / 1000 > 1) {
            myInterfaceIncomingReport.setAccuracy("bps");
        } else {
            myInterfaceIncomingReport.setAccuracy("bps");
        }
        String ehcacheKey = "lowInterfaceIncomingReports" + deadLine.toString() + group + interval + topNum;
        EhcacheUtil.put(ehcacheKey, lowInterfaceIncomingReports);

        Iterator<InterfaceIncomingReport> it = lowInterfaceIncomingReports.iterator();
        while (it.hasNext()) {
            InterfaceIncomingReport interfaceIncomingReport = it.next();
            if (interfaceIncomingReport.getValueAvg() == 0) {
                it.remove(); // 移除数据
            }

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

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

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

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

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

        return lowInterfaceIncomingReports;
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 本周期，上周期高接口流入流量数据
     */
    public List<InterfaceIncomingReport> selectMulticycleHighInterfaceIncomingReportService(final String group,
        final Integer interval, final Integer topNum) {
        // long startTime = System.currentTimeMillis(); // 获取开始时间

        Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);
        List<InterfaceIncomingReport> highInterfaceIncomingReports = new ArrayList<>();
        List<InterfaceIncomingReport> tempInterfaceIncomingReports = new ArrayList<>();
        tempInterfaceIncomingReports = selectHighInterfaceIncomingReportService(group, interval, topNum);

        InterfaceIncomingReport myInterfaceIncomingReport = new InterfaceIncomingReport();

        if (tempInterfaceIncomingReports.size() > 0) {
            if ("Gbps".equals(tempInterfaceIncomingReports.get(0).getAccuracy())) {
                myInterfaceIncomingReport.setAccuracy("Gbps");

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

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

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

            int size = tempInterfaceIncomingReports.size();
            for (int i = 0; i < size; i++) {
                InterfaceIncomingReport previousCycleInterfaceIncomingReport = new InterfaceIncomingReport();
                previousCycleInterfaceIncomingReport = interfaceIncomingReportMapper
                    .selectInterfaceIncomingReportByHostAndItemKey(tempInterfaceIncomingReports.get(i).getHost(),
                        tempInterfaceIncomingReports.get(i).getItemKey(), deadLine - 2 * interval * timeFormat,
                        deadLine - interval * timeFormat);
                if (previousCycleInterfaceIncomingReport != null) {

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

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

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

                    }

                }

                highInterfaceIncomingReports.add(tempInterfaceIncomingReports.get(i));
                highInterfaceIncomingReports.add(previousCycleInterfaceIncomingReport);
            }
        }

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

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

        return highInterfaceIncomingReports;
    }

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

        long startTime = System.currentTimeMillis(); // 获取开始时间

        Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);
        List<InterfaceIncomingReport> lowInterfaceIncomingReports = new ArrayList<>();
        List<InterfaceIncomingReport> tempInterfaceIncomingReports = new ArrayList<>();
        //System.err.println("selectMulticycleLowInterfaceIncomingReportService" + tempInterfaceIncomingReports);
        //indexout 越界 in/out 进行非空判断，并且避免size为0
        tempInterfaceIncomingReports = selectLowInterfaceIncomingReportService(group, interval, topNum);
        //  System.err.println("selectMulticycleLowInterfaceIncomingReportService" + tempInterfaceIncomingReports);
        //if (null != tempInterfaceIncomingReports) {

        int size = tempInterfaceIncomingReports.size();
        //System.err.println("size" + size);
        InterfaceIncomingReport myInterfaceIncomingReport = new InterfaceIncomingReport();

        if (size > 0) {

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

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

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

            } else if ("bps".equals(tempInterfaceIncomingReports.get(0).getAccuracy())) {
                myInterfaceIncomingReport.setAccuracy("bps");
            }
            long endTime = System.currentTimeMillis(); // 获取结束时间

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

            for (int i = 0; i < size; i++) {
                InterfaceIncomingReport previousCycleInterfaceIncomingReport = new InterfaceIncomingReport();
                previousCycleInterfaceIncomingReport = interfaceIncomingReportMapper
                    .selectInterfaceIncomingReportByHostAndItemKey(tempInterfaceIncomingReports.get(i).getHost(),
                        tempInterfaceIncomingReports.get(i).getItemKey(), deadLine - 2 * interval * timeFormat,
                        deadLine - interval * timeFormat);

                if (previousCycleInterfaceIncomingReport != null) {
                    if ("Gbps".equals(myInterfaceIncomingReport.getAccuracy())) {
                        previousCycleInterfaceIncomingReport.setAccuracy("Gbps");
                        previousCycleInterfaceIncomingReport.setValueAvg(Util.round(
                            Util.round(previousCycleInterfaceIncomingReport.getValueAvg() / 1000000000, 2) / 1000,
                            2));
                        previousCycleInterfaceIncomingReport.setValueMin(Util.round(
                            Util.round(previousCycleInterfaceIncomingReport.getValueMin() / 1000000000, 2) / 1000,
                            2));
                        previousCycleInterfaceIncomingReport.setValueMax(Util.round(
                            Util.round(previousCycleInterfaceIncomingReport.getValueMax() / 1000000000, 2) / 1000,
                            2));
                    } else if ("Mbps".equals(myInterfaceIncomingReport.getAccuracy())) {
                        previousCycleInterfaceIncomingReport.setAccuracy("Mbps");
                        previousCycleInterfaceIncomingReport.setValueAvg(
                            Util.round(previousCycleInterfaceIncomingReport.getValueAvg() / 1000000000, 2));
                        previousCycleInterfaceIncomingReport.setValueMin(
                            Util.round(previousCycleInterfaceIncomingReport.getValueMin() / 1000000000, 2));
                        previousCycleInterfaceIncomingReport.setValueMax(
                            Util.round(previousCycleInterfaceIncomingReport.getValueMax() / 1000000000, 2));

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

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

                    }
                }
                lowInterfaceIncomingReports.add(tempInterfaceIncomingReports.get(i)); // 加入本周期数据
                lowInterfaceIncomingReports.add(previousCycleInterfaceIncomingReport); // 加入上周期数据
            }
        }

        //}
        //System.err.println("selectMulticycleLowInterfaceIncomingReportService" + lowInterfaceIncomingReports);
        return lowInterfaceIncomingReports;
    }

    /**
     * 创建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 interfaceIncomingReports
     *            接口流入流量数据
     * @return 表格
     */
    public static PdfPTable createTable(final List<InterfaceIncomingReport> interfaceIncomingReports) {
        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, 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));

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

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

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

    /**
     * 替换$符号
     *
     * @param interfaceIncomingReports
     *            接口流入流量
     * @return 替换$后的接口流入流量
     */
    public List<InterfaceIncomingReport> parseSymbol(final List<InterfaceIncomingReport> interfaceIncomingReports) {
        for (InterfaceIncomingReport interfaceIncomingReport : interfaceIncomingReports) {
            if (interfaceIncomingReport.getItemName().contains("$")) {
                int begin = interfaceIncomingReport.getItemKey().indexOf("[");
                int end = interfaceIncomingReport.getItemKey().indexOf("]");
                String interfaceName = interfaceIncomingReport.getItemKey().substring(begin + 1, end); // 获取接口名称
                interfaceIncomingReport.setItemName(interfaceName);
            }
        }
        return interfaceIncomingReports;
    }

    /**
     *
     * @param group
     *            群组
     * @param interval
     *            时间间隔
     * @param topNum
     *            区间
     * @param startDate
     *            开始时间
     * @param endDate
     *            结束时间
     * @return
     *         根据时间的流量排序
     */
    public List<InterfaceIncomingReport> selectHighInterfaceIncomingReportServiceByDate(final String group,
        final Integer interval,
        final Integer topNum, final String startDate, final String endDate) {

        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();
        }

        Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);
        List<InterfaceIncomingReport> highInterfaceIncomingReports = new ArrayList<>();
        highInterfaceIncomingReports = interfaceIncomingReportMapper.selectHighInterfaceIncomingReport(group,
            myStartDate, myEndDate);

        InterfaceIncomingReport myInterfaceIncomingReport = new InterfaceIncomingReport();

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

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

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

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

        Iterator<InterfaceIncomingReport> it = highInterfaceIncomingReports.iterator();
        while (it.hasNext()) {
            InterfaceIncomingReport interfaceIncomingReport = it.next();
            if (interfaceIncomingReport.getValueAvg() == 0) {
                it.remove();
            }
            if ("Gbps".equals(myInterfaceIncomingReport.getAccuracy())) {
                interfaceIncomingReport.setAccuracy("Gbps");
                interfaceIncomingReport.setValueAvg(
                    Util.round(Util.round(interfaceIncomingReport.getValueAvg() / 1000000000, 2) / 1000, 2));
                interfaceIncomingReport.setValueMin(
                    Util.round(Util.round(interfaceIncomingReport.getValueMin() / 1000000000, 2) / 1000, 2));
                interfaceIncomingReport.setValueMax(
                    Util.round(Util.round(interfaceIncomingReport.getValueMax() / 1000000000, 2) / 1000, 2));

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

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

            } else if ("bps".equals(myInterfaceIncomingReport.getAccuracy())) {
                interfaceIncomingReport.setAccuracy("bps");
                interfaceIncomingReport.setValueAvg(Util.round(interfaceIncomingReport.getValueAvg() / 1000, 2));
                interfaceIncomingReport.setValueMin(Util.round(interfaceIncomingReport.getValueMin() / 1000, 2));
                interfaceIncomingReport.setValueMax(Util.round(interfaceIncomingReport.getValueMax() / 1000, 2));
            }
        }
        highInterfaceIncomingReports = Util.reduceList(highInterfaceIncomingReports, topNum); // 剔除数据
        highInterfaceIncomingReports = parseSymbol(highInterfaceIncomingReports);

        return highInterfaceIncomingReports;
    }

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

        InterfaceIncomingReport myInterfaceIncomingReport = new InterfaceIncomingReport();

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

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

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

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

        while (it.hasNext()) {
            InterfaceIncomingReport interfaceIncomingReport = it.next();
            if (interfaceIncomingReport.getValueAvg() == 0) {
                it.remove();
            }

            if ("Gbps".equals(myInterfaceIncomingReport.getAccuracy())) {
                interfaceIncomingReport.setAccuracy("Gbps");
                interfaceIncomingReport.setValueAvg(
                    Util.round(Util.round(interfaceIncomingReport.getValueAvg() / 1000000000, 2) / 1000, 2));
                interfaceIncomingReport.setValueMin(
                    Util.round(Util.round(interfaceIncomingReport.getValueMin() / 1000000000, 2) / 1000, 2));
                interfaceIncomingReport.setValueMax(
                    Util.round(Util.round(interfaceIncomingReport.getValueMax() / 1000000000, 2) / 1000, 2));
            } else if ("Mbps".equals(myInterfaceIncomingReport.getAccuracy())) {
                interfaceIncomingReport.setAccuracy("Mbps");
                interfaceIncomingReport.setValueAvg(Util.round(interfaceIncomingReport.getValueAvg() / 1000000000, 2));
                interfaceIncomingReport.setValueMin(Util.round(interfaceIncomingReport.getValueMin() / 1000000000, 2));
                interfaceIncomingReport.setValueMax(Util.round(interfaceIncomingReport.getValueMax() / 1000000000, 2));
            } else if ("Kbps".equals(myInterfaceIncomingReport.getAccuracy())) {
                interfaceIncomingReport.setAccuracy("Kbps");
                interfaceIncomingReport.setValueAvg(Util.round(interfaceIncomingReport.getValueAvg() / 1000000, 2));
                interfaceIncomingReport.setValueMin(Util.round(interfaceIncomingReport.getValueMin() / 1000000, 2));
                interfaceIncomingReport.setValueMax(Util.round(interfaceIncomingReport.getValueMax() / 1000000, 2));
            } else if ("bps".equals(myInterfaceIncomingReport.getAccuracy())) {
                interfaceIncomingReport.setAccuracy("bps");
                interfaceIncomingReport.setValueAvg(Util.round(interfaceIncomingReport.getValueAvg() / 1000, 2));
                interfaceIncomingReport.setValueMin(Util.round(interfaceIncomingReport.getValueMin() / 1000, 2));
                interfaceIncomingReport.setValueMax(Util.round(interfaceIncomingReport.getValueMax() / 1000, 2));
            }
        }
        highInterfaceIncomingReports = Util.reduceList(highInterfaceIncomingReports, topNum); //剔除数据
        highInterfaceIncomingReports = parseSymbol(highInterfaceIncomingReports);

        return highInterfaceIncomingReports;
    }

    /**
     *
     * @param group
     *            群组
     * @param interval
     *            时间间隔
     * @param topNum
     *            区间
     * @param startDate
     *            开始时间
     * @param endDate
     *            结束时间
     * @return
     *         根据时间的流量排序
     */
    public List<InterfaceIncomingReport> selectLowInterfaceIncomingReportServiceByDate(final String group,
        final Integer interval,
        final Integer topNum, final String startDate, final String endDate) {

        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();
        }

        Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);
        List<InterfaceIncomingReport> lowInterfaceIncomingReports = new ArrayList<>();
        lowInterfaceIncomingReports = interfaceIncomingReportMapper.selectLowInterfaceIncomingReport(group, myStartDate,
            myEndDate);

        String ehcacheKey = "lowInterfaceIncomingReports" + deadLine.toString() + group + interval + topNum;
        EhcacheUtil.put(ehcacheKey, lowInterfaceIncomingReports);

        InterfaceIncomingReport myInterfaceIncomingReport = new InterfaceIncomingReport();

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

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

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

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

        Iterator<InterfaceIncomingReport> it = lowInterfaceIncomingReports.iterator();
        while (it.hasNext()) {
            InterfaceIncomingReport interfaceIncomingReport = it.next();
            if (interfaceIncomingReport.getValueAvg() == 0) {
                it.remove(); // 移除数据
            }
            // 设置保留位数，数据太小，则不保留固定的位数
            //if (interfaceIncomingReport != null) {
            if ("Gbps".equals(myInterfaceIncomingReport.getAccuracy())) {
                interfaceIncomingReport.setAccuracy("Gbps");
                interfaceIncomingReport.setValueAvg(
                    Util.round(Util.round(interfaceIncomingReport.getValueAvg() / 1000000000, 2) / 1000, 2));
                interfaceIncomingReport.setValueMin(
                    Util.round(Util.round(interfaceIncomingReport.getValueMin() / 1000000000, 2) / 1000, 2));
                interfaceIncomingReport.setValueMax(
                    Util.round(Util.round(interfaceIncomingReport.getValueMax() / 1000000000, 2) / 1000, 2));
            } else if ("Mbps".equals(myInterfaceIncomingReport.getAccuracy())) {
                interfaceIncomingReport.setAccuracy("Mbps");
                interfaceIncomingReport
                .setValueAvg(Util.round(interfaceIncomingReport.getValueAvg() / 1000000000, 2));
                interfaceIncomingReport
                .setValueMin(Util.round(interfaceIncomingReport.getValueMin() / 1000000000, 2));
                interfaceIncomingReport
                .setValueMax(Util.round(interfaceIncomingReport.getValueMax() / 1000000000, 2));
            } else if ("Kbps".equals(myInterfaceIncomingReport.getAccuracy())) {
                interfaceIncomingReport.setAccuracy("Kbps");
                interfaceIncomingReport.setValueAvg(Util.round(interfaceIncomingReport.getValueAvg() / 1000000, 2));
                interfaceIncomingReport.setValueMin(Util.round(interfaceIncomingReport.getValueMin() / 1000000, 2));
                interfaceIncomingReport.setValueMax(Util.round(interfaceIncomingReport.getValueMax() / 1000000, 2));
            } else if ("bps".equals(myInterfaceIncomingReport.getAccuracy())) {
                interfaceIncomingReport.setAccuracy("bps");
                interfaceIncomingReport.setValueAvg(Util.round(interfaceIncomingReport.getValueAvg() / 1000, 2));
                interfaceIncomingReport.setValueMin(Util.round(interfaceIncomingReport.getValueMin() / 1000, 2));
                interfaceIncomingReport.setValueMax(Util.round(interfaceIncomingReport.getValueMax() / 1000, 2));
            }

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

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 低接口流入流量
     */
    public List<InterfaceIncomingReport> selectLowInterfaceIncomingReportServiceByDate(final String group,
        final Integer interval,
        final Integer topNum, final long startDate, final long endDate) {
        Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);
        List<InterfaceIncomingReport> lowInterfaceIncomingReports = new ArrayList<>();
        lowInterfaceIncomingReports = interfaceIncomingReportMapper.selectLowInterfaceIncomingReport(group,
            startDate, endDate);

        Iterator<InterfaceIncomingReport> it = lowInterfaceIncomingReports.iterator();

        InterfaceIncomingReport myInterfaceIncomingReport = new InterfaceIncomingReport();

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

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

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

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

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

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

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

    /**
     *
     * @param group
     *            群组
     * @param interval
     *            时间间隔
     * @param topNum
     *            区间
     * @param startDate
     *            开始时间
     * @param endDate
     *            结束时间
     * @return
     *         根据时间的流量排序
     */
    public List<InterfaceIncomingReport> selectMulticycleHighInterfaceIncomingReportServiceByDate(final String group,
        final Integer interval, final Integer topNum, final String startDate, final String endDate) {

        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();
        }

        Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);
        List<InterfaceIncomingReport> highInterfaceIncomingReports = new ArrayList<>();
        List<InterfaceIncomingReport> tempInterfaceIncomingReports = new ArrayList<>();
        tempInterfaceIncomingReports = selectHighInterfaceIncomingReportServiceByDate(group, interval, topNum,
            myStartDate, myEndDate);

        InterfaceIncomingReport myInterfaceIncomingReport = new InterfaceIncomingReport();

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

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

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

        } else if ("bps".equals(tempInterfaceIncomingReports.get(0).getAccuracy())) {
            myInterfaceIncomingReport.setAccuracy("bps");
        } else {
            myInterfaceIncomingReport.setAccuracy("bps");
        }
        int size = tempInterfaceIncomingReports.size();
        for (int i = 0; i < size; i++) {
            InterfaceIncomingReport previousCycleInterfaceIncomingReport = new InterfaceIncomingReport();
            previousCycleInterfaceIncomingReport = interfaceIncomingReportMapper
                .selectInterfaceIncomingReportByHostAndItemKey(tempInterfaceIncomingReports.get(i).getHost(),
                    tempInterfaceIncomingReports.get(i).getItemKey(), myStartDate - 2 * cycle,
                    myStartDate - cycle);
            if (previousCycleInterfaceIncomingReport != null) {

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

            highInterfaceIncomingReports.add(tempInterfaceIncomingReports.get(i));
            highInterfaceIncomingReports.add(previousCycleInterfaceIncomingReport);
        }
        return highInterfaceIncomingReports;
    }

    /**
     *
     * @param group
     *            群组
     * @param interval
     *            时间间隔
     * @param topNum
     *            区间
     * @param startDate
     *            开始时间
     * @param endDate
     *            结束时间
     * @return
     *         根据时间的流量排序
     */
    public List<InterfaceIncomingReport> selectMulticycleLowInterfaceIncomingReportServiceByDate(final String group,
        final Integer interval, final Integer topNum, final String startDate, final String endDate) {

        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();
        }

        Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);
        List<InterfaceIncomingReport> lowInterfaceIncomingReports = new ArrayList<>();
        List<InterfaceIncomingReport> tempInterfaceIncomingReports = new ArrayList<>();
        tempInterfaceIncomingReports = selectLowInterfaceIncomingReportServiceByDate(group, interval, topNum,
            myStartDate, myEndDate);

        InterfaceIncomingReport myInterfaceIncomingReport = new InterfaceIncomingReport();

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

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

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

        } else if ("bps".equals(tempInterfaceIncomingReports.get(0).getAccuracy())) {
            myInterfaceIncomingReport.setAccuracy("bps");
        }
        int size = tempInterfaceIncomingReports.size();
        for (int i = 0; i < size; i++) {
            InterfaceIncomingReport previousCycleInterfaceIncomingReport = new InterfaceIncomingReport();
            previousCycleInterfaceIncomingReport = interfaceIncomingReportMapper
                .selectInterfaceIncomingReportByHostAndItemKey(tempInterfaceIncomingReports.get(i).getHost(),
                    tempInterfaceIncomingReports.get(i).getItemKey(), myStartDate - 2 * cycle,
                    myStartDate - cycle);

            if (previousCycleInterfaceIncomingReport != null) {
                if ("Gbps".equals(myInterfaceIncomingReport.getAccuracy())) {
                    previousCycleInterfaceIncomingReport.setAccuracy("Gbps");
                    previousCycleInterfaceIncomingReport.setValueAvg(Util.round(
                        Util.round(previousCycleInterfaceIncomingReport.getValueAvg() / 1000000000, 2) / 1000, 2));
                    previousCycleInterfaceIncomingReport.setValueMin(Util.round(
                        Util.round(previousCycleInterfaceIncomingReport.getValueMin() / 1000000000, 2) / 1000, 2));
                    previousCycleInterfaceIncomingReport.setValueMax(Util.round(
                        Util.round(previousCycleInterfaceIncomingReport.getValueMax() / 1000000000, 2) / 1000, 2));

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

            lowInterfaceIncomingReports.add(tempInterfaceIncomingReports.get(i)); // 加入本周期数据
            lowInterfaceIncomingReports.add(previousCycleInterfaceIncomingReport); // 加入上周期数据
        }
        return lowInterfaceIncomingReports;
    }

}
