package com.cesgroup.cesmonitor.service;

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

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

import com.cesgroup.cesmonitor.bean.CPUUsageRateReport;
import com.cesgroup.cesmonitor.common.Constant;
import com.cesgroup.cesmonitor.common.EhcacheUtil;
import com.cesgroup.cesmonitor.common.Util;
import com.cesgroup.cesmonitor.dao.CPUUsageRateReportMapper;
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;

/**
 * CPU使用率service
 *
 * @author user
 * @version 1.0.0 2017年5月11日
 */
@Service("cpuUsageRateReportService")
public class CPUUsageRateReportService {

    @Autowired
    private CPUUsageRateReportMapper cpuUsageRateReportMapper;

    private Long timeFormat = 0L;

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 查询出CPU使用率数据
     */
    public List<CPUUsageRateReport> selectHighCPUUsageRateReportService(final String group, final Integer interval,
        final Integer topNum) {

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

        List<CPUUsageRateReport> cpuUsageRateReports = new ArrayList<CPUUsageRateReport>();
        List<CPUUsageRateReport> cpuUsageRateReports2 = new ArrayList<CPUUsageRateReport>();
        Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);

        if (isSuperAdmin) {
            cpuUsageRateReports = cpuUsageRateReportMapper.selectHighCPUUsageRateReport(group,
                deadLine - interval * timeFormat, deadLine, Constant.CPU_USE_PERCENT_FLAG);
        } else {
            cpuUsageRateReports = cpuUsageRateReportMapper.selectHighCPUUsageRateReportUserId(group,
                deadLine - interval * timeFormat, deadLine, userId, Constant.CPU_USE_PERCENT);
        }

        // String ehcacheKey = "cpuUsageRateReports" + deadLine.toString() + group + interval + topNum;
        cpuUsageRateReports2.addAll(cpuUsageRateReports);
        String ehcacheKey2 = "mycpuUsageRateReports" + deadLine.toString() + group + interval + topNum;

        //EhcacheUtil.put(ehcacheKey, cpuUsageRateReports);
        EhcacheUtil.put(ehcacheKey2, cpuUsageRateReports2);

        Iterator<CPUUsageRateReport> it = cpuUsageRateReports.iterator();
        while (it.hasNext()) {
            CPUUsageRateReport cpuUsageRateReport = (CPUUsageRateReport) it.next();

            if (cpuUsageRateReport.getValueAvg() == 0.0) {
                it.remove();
            }

            //设置数据小数点保留位数,考虑到cpu使用率可能非常小
            cpuUsageRateReport.setValueMin(Util.round(cpuUsageRateReport.getValueMin(), 3));
            cpuUsageRateReport.setValueAvg(Util.round(cpuUsageRateReport.getValueAvg(), 3));
            cpuUsageRateReport.setValueMax(Util.round(cpuUsageRateReport.getValueMax(), 3));

        }
        cpuUsageRateReports = Util.reduceList(cpuUsageRateReports, topNum);
        return cpuUsageRateReports;
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 查询出低CPU使用率数据
     */
    public List<CPUUsageRateReport> selectLowCPUUsageRateReportService(final String group, final Integer interval,
        final Integer topNum) {
        List<CPUUsageRateReport> cpuUsageRateReports = new ArrayList<CPUUsageRateReport>();
        LoginUser user = Util.getLoginUser();
        Integer userId = user.getUserid();
        boolean isSuperAdmin = user.isSuperAdmin();

        Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);

        if (isSuperAdmin) {

            cpuUsageRateReports = (List<CPUUsageRateReport>) EhcacheUtil
                .get("mycpuUsageRateReports" + deadLine.toString() + group + interval + topNum);
            if (null == cpuUsageRateReports) {

                cpuUsageRateReports = cpuUsageRateReportMapper.selectLowCPUUsageRateReport(group,
                    deadLine - interval * timeFormat, deadLine, Constant.CPU_USE_PERCENT_FLAG);
            } else {
                Collections.reverse(cpuUsageRateReports);
            }

            EhcacheUtil.remove("myicmppingLossReports" + deadLine.toString() + group + interval + topNum);

            /* cpuUsageRateReports = cpuUsageRateReportMapper.selectLowCPUUsageRateReport(group,
                    deadLine - interval * timeFormat, deadLine);*/
        } else {
            cpuUsageRateReports = cpuUsageRateReportMapper.selectLowCPUUsageRateReportByUser(group,
                deadLine - interval * timeFormat, deadLine, userId, Constant.CPU_USE_PERCENT);
        }
        Iterator<CPUUsageRateReport> it = cpuUsageRateReports.iterator();
        while (it.hasNext()) {
            CPUUsageRateReport cpuUsageRateReport = (CPUUsageRateReport) it.next();

            if (cpuUsageRateReport.getValueAvg() == 0.0) {
                it.remove();
            }
            //设置数据小数点保留位数,考虑到cpu使用率可能非常小
            cpuUsageRateReport.setValueMin(Util.round(cpuUsageRateReport.getValueMin(), 3));
            cpuUsageRateReport.setValueAvg(Util.round(cpuUsageRateReport.getValueAvg(), 3));
            cpuUsageRateReport.setValueMax(Util.round(cpuUsageRateReport.getValueMax(), 3));
        }
        cpuUsageRateReports = Util.reduceList(cpuUsageRateReports, topNum); //剔除到指定数量
        return cpuUsageRateReports;
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 查询出低CPU使用率数据
     */
    public List<CPUUsageRateReport> selectLowCPUUsageRateReportService2(final String group, final Integer interval,
        final Integer topNum) {
        List<CPUUsageRateReport> cpuUsageRateReports = new ArrayList<CPUUsageRateReport>();
        LoginUser user = Util.getLoginUser();
        Integer userId = user.getUserid();
        boolean isSuperAdmin = user.isSuperAdmin();

        Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);

        if (isSuperAdmin) {

            cpuUsageRateReports = cpuUsageRateReportMapper.selectLowCPUUsageRateReport(group,
                deadLine - interval * timeFormat, deadLine, Constant.CPU_USE_PERCENT_FLAG);
        } else {
            cpuUsageRateReports = cpuUsageRateReportMapper.selectLowCPUUsageRateReportByUser(group,
                deadLine - interval * timeFormat, deadLine, userId, Constant.CPU_USE_PERCENT);
        }
        Iterator<CPUUsageRateReport> it = cpuUsageRateReports.iterator();
        while (it.hasNext()) {
            CPUUsageRateReport cpuUsageRateReport = (CPUUsageRateReport) it.next();

            if (cpuUsageRateReport.getValueAvg() == 0.0) {
                it.remove();
            }
            //设置数据小数点保留位数,考虑到cpu使用率可能非常小
            cpuUsageRateReport.setValueMin(Util.round(cpuUsageRateReport.getValueMin(), 3));
            cpuUsageRateReport.setValueAvg(Util.round(cpuUsageRateReport.getValueAvg(), 3));
            cpuUsageRateReport.setValueMax(Util.round(cpuUsageRateReport.getValueMax(), 3));
        }
        cpuUsageRateReports = Util.reduceList(cpuUsageRateReports, topNum); //剔除到指定数量
        return cpuUsageRateReports;
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 本周期和上周期高CPU使用率数据
     */
    public List<CPUUsageRateReport> selectMulticycleHighCPUUsageRateReportService(final String group,
        final Integer interval,
        final Integer topNum) {
        Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);
        List<CPUUsageRateReport> cpuUsageRateReports = new ArrayList<CPUUsageRateReport>();
        List<CPUUsageRateReport> tempCPUUsageRateReports = new ArrayList<CPUUsageRateReport>();
        CPUUsageRateReport cpuUsageRateReport = new CPUUsageRateReport();
        CPUUsageRateReport previousCycleCPUUsageRateReport = new CPUUsageRateReport();
        tempCPUUsageRateReports = selectHighCPUUsageRateReportService(group, interval, topNum);
        int size = tempCPUUsageRateReports.size();
        for (int i = 0; i < size; i++) {
            cpuUsageRateReport = (CPUUsageRateReport) tempCPUUsageRateReports.get(i);
            previousCycleCPUUsageRateReport = this.cpuUsageRateReportMapper.selectCPUUsageRateReportByHost(
                cpuUsageRateReport.getHost(), deadLine - 2 * interval * timeFormat, deadLine - interval * timeFormat,
                Constant.CPU_USE_PERCENT_FLAG);

            if (previousCycleCPUUsageRateReport != null) {
                //设置上周期的数据的保留位数
                previousCycleCPUUsageRateReport
                .setValueMin(Util.round(previousCycleCPUUsageRateReport.getValueMin(), 3));
                previousCycleCPUUsageRateReport
                .setValueAvg(Util.round(previousCycleCPUUsageRateReport.getValueAvg(), 3));
                previousCycleCPUUsageRateReport
                .setValueMax(Util.round(previousCycleCPUUsageRateReport.getValueMax(), 3));
            }

            cpuUsageRateReports.add(cpuUsageRateReport);
            cpuUsageRateReports.add(previousCycleCPUUsageRateReport);
        }
        return cpuUsageRateReports;
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 本周期和上周期低CPU使用率数据
     */
    public List<CPUUsageRateReport> selectMulticycleLowCPUUsageRateReportService(final String group,
        final Integer interval,
        final Integer topNum) {
        List<CPUUsageRateReport> cpuUsageRateReports = new ArrayList<CPUUsageRateReport>();
        List<CPUUsageRateReport> tempCPUUsageRateReports = new ArrayList<CPUUsageRateReport>();
        CPUUsageRateReport cpuUsageRateReport = new CPUUsageRateReport();
        CPUUsageRateReport previousCycleCPUUsageRateReport = new CPUUsageRateReport();
        Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);
        tempCPUUsageRateReports = selectLowCPUUsageRateReportService2(group, interval, topNum);
        int size = tempCPUUsageRateReports.size();
        for (int i = 0; i < size; i++) {
            cpuUsageRateReport = (CPUUsageRateReport) tempCPUUsageRateReports.get(i);
            previousCycleCPUUsageRateReport = cpuUsageRateReportMapper.selectCPUUsageRateReportByHost(
                cpuUsageRateReport.getHost(), deadLine - 2 * interval * timeFormat, deadLine - interval * timeFormat,
                Constant.CPU_USE_PERCENT_FLAG);
            if (previousCycleCPUUsageRateReport != null) {
                //设置上周期的数据的保留位数
                previousCycleCPUUsageRateReport
                .setValueMin(Util.round(previousCycleCPUUsageRateReport.getValueMin(), 3));
                previousCycleCPUUsageRateReport
                .setValueAvg(Util.round(previousCycleCPUUsageRateReport.getValueAvg(), 3));
                previousCycleCPUUsageRateReport
                .setValueMax(Util.round(previousCycleCPUUsageRateReport.getValueMax(), 3));
            } else {
                cpuUsageRateReports.add(cpuUsageRateReport);
            }
            cpuUsageRateReports.add(cpuUsageRateReport);
            cpuUsageRateReports.add(previousCycleCPUUsageRateReport);
        }
        return cpuUsageRateReports;
    }

    /**
     * 创建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("CPU使用率报表", 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("最高CPU使用率Top" + topNum + "数据汇总", font));
                document.add(new Chunk(""));
                document.add(table[0]);
                document.add(new Paragraph("最低CPU使用率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 cpuUsageRateReports
     *            cpu使用率数据
     * @return 表格
     */
    public static PdfPTable createTable(final List<CPUUsageRateReport> cpuUsageRateReports) {
        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 = cpuUsageRateReports.size();
            for (int i = 0; i < size; i = i + 2) {
                table.addCell(new Paragraph(cpuUsageRateReports.get(i).getHname(), font));
                table.addCell(new Paragraph(cpuUsageRateReports.get(i).getName(), font));
                table.addCell(String.valueOf(cpuUsageRateReports.get(i).getValueAvg()));
                if (cpuUsageRateReports.get(i + 1) == null) {
                    table.addCell(new Paragraph("-  -  -", font));
                } else {
                    table.addCell(String.valueOf(cpuUsageRateReports.get(i + 1).getValueAvg()));
                }

                table.addCell(String.valueOf(cpuUsageRateReports.get(i).getValueMax()));

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

                table.addCell(String.valueOf(cpuUsageRateReports.get(i).getValueMin()));

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

    public List<CPUUsageRateReport> selectHighCPUUsageRateReportServiceByDate(final String group,
        final Integer interval,
        final Integer topNum, final String startDate, final String endDate) {
        LoginUser user = Util.getLoginUser();
        Integer userId = user.getUserid();
        boolean isSuperAdmin = user.isSuperAdmin();

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

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

        List<CPUUsageRateReport> cpuUsageRateReports = new ArrayList<CPUUsageRateReport>();
        Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);
        if (isSuperAdmin) {
            cpuUsageRateReports = cpuUsageRateReportMapper.selectHighCPUUsageRateReport(group,
                myStartDate, myEndDate, Constant.CPU_USE_PERCENT_FLAG);
        } else {
            cpuUsageRateReports = cpuUsageRateReportMapper.selectHighCPUUsageRateReportByUser(group,
                myStartDate, myEndDate, userId, Constant.CPU_USE_PERCENT);
        }
        Iterator<CPUUsageRateReport> it = cpuUsageRateReports.iterator();
        while (it.hasNext()) {
            CPUUsageRateReport cpuUsageRateReport = (CPUUsageRateReport) it.next();
            if (cpuUsageRateReport.getValueAvg() == 0.0) {
                it.remove();
            }
            //设置数据小数点保留位数,考虑到cpu使用率可能非常小
            cpuUsageRateReport.setValueMin(Util.round(cpuUsageRateReport.getValueMin(), 3));
            cpuUsageRateReport.setValueAvg(Util.round(cpuUsageRateReport.getValueAvg(), 3));
            cpuUsageRateReport.setValueMax(Util.round(cpuUsageRateReport.getValueMax(), 3));

        }
        cpuUsageRateReports = Util.reduceList(cpuUsageRateReports, topNum);
        return cpuUsageRateReports;
    }

    public List<CPUUsageRateReport> selectLowCPUUsageRateReportServiceByDate(final String group, final Integer interval,
        final Integer topNum, final String startDate, final String endDate) {
        LoginUser user = Util.getLoginUser();
        Integer userId = user.getUserid();
        boolean isSuperAdmin = user.isSuperAdmin();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        long myStartDate = 0;
        long myEndDate = 0;
        long cycle = 0;
        Date startDate1;
        Date endDate1;

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

        List<CPUUsageRateReport> cpuUsageRateReports = new ArrayList<CPUUsageRateReport>();
        timeFormat = Util.getTimeFormat(interval);
        if (isSuperAdmin) {
            cpuUsageRateReports = cpuUsageRateReportMapper.selectLowCPUUsageRateReport(group,
                myStartDate, myEndDate, Constant.CPU_USE_PERCENT_FLAG);
        } else {
            cpuUsageRateReports = cpuUsageRateReportMapper.selectLowCPUUsageRateReportByUser(group,
                myStartDate, myEndDate, userId, Constant.CPU_USE_PERCENT);
        }

        Iterator<CPUUsageRateReport> it = cpuUsageRateReports.iterator();
        while (it.hasNext()) {
            CPUUsageRateReport cpuUsageRateReport = (CPUUsageRateReport) it.next();

            if (cpuUsageRateReport.getValueAvg() == 0.0) {
                it.remove();
            }

            //设置数据小数点保留位数,考虑到cpu使用率可能非常小
            cpuUsageRateReport.setValueMin(Util.round(cpuUsageRateReport.getValueMin(), 3));
            cpuUsageRateReport.setValueAvg(Util.round(cpuUsageRateReport.getValueAvg(), 3));
            cpuUsageRateReport.setValueMax(Util.round(cpuUsageRateReport.getValueMax(), 3));
        }
        cpuUsageRateReports = Util.reduceList(cpuUsageRateReports, topNum); //剔除到指定数量
        return cpuUsageRateReports;
    }

    public List<CPUUsageRateReport> selectMulticycleHighCPUUsageRateReportServiceByDate(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();
        }

        timeFormat = Util.getTimeFormat(interval);
        List<CPUUsageRateReport> cpuUsageRateReports = new ArrayList<CPUUsageRateReport>();
        List<CPUUsageRateReport> tempCPUUsageRateReports = new ArrayList<CPUUsageRateReport>();
        CPUUsageRateReport cpuUsageRateReport = new CPUUsageRateReport();
        CPUUsageRateReport previousCycleCPUUsageRateReport = new CPUUsageRateReport();
        tempCPUUsageRateReports = selectHighCPUUsageRateReportServiceByDate(group, interval, topNum, myStartDate,
            myEndDate);
        int size = tempCPUUsageRateReports.size();
        for (int i = 0; i < size; i++) {
            cpuUsageRateReport = (CPUUsageRateReport) tempCPUUsageRateReports.get(i);
            previousCycleCPUUsageRateReport = this.cpuUsageRateReportMapper.selectCPUUsageRateReportByHost(
                cpuUsageRateReport.getHost(), myStartDate - 2 * cycle, myStartDate - cycle,
                Constant.CPU_USE_PERCENT_FLAG);

            if (previousCycleCPUUsageRateReport != null) {
                //设置上周期的数据的保留位数
                previousCycleCPUUsageRateReport
                .setValueMin(Util.round(previousCycleCPUUsageRateReport.getValueMin(), 3));
                previousCycleCPUUsageRateReport
                .setValueAvg(Util.round(previousCycleCPUUsageRateReport.getValueAvg(), 3));
                previousCycleCPUUsageRateReport
                .setValueMax(Util.round(previousCycleCPUUsageRateReport.getValueMax(), 3));
            }

            cpuUsageRateReports.add(cpuUsageRateReport);
            cpuUsageRateReports.add(previousCycleCPUUsageRateReport);
        }
        return cpuUsageRateReports;
    }

    public List<CPUUsageRateReport> selectMulticycleLowCPUUsageRateReportServiceByDate(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();
        }

        List<CPUUsageRateReport> cpuUsageRateReports = new ArrayList<CPUUsageRateReport>();
        List<CPUUsageRateReport> tempCPUUsageRateReports = new ArrayList<CPUUsageRateReport>();
        CPUUsageRateReport cpuUsageRateReport = new CPUUsageRateReport();
        CPUUsageRateReport previousCycleCPUUsageRateReport = new CPUUsageRateReport();
        timeFormat = Util.getTimeFormat(interval);
        tempCPUUsageRateReports = selectLowCPUUsageRateReportServiceByDate(group, interval, topNum, myStartDate,
            myEndDate);
        int size = tempCPUUsageRateReports.size();
        for (int i = 0; i < size; i++) {
            cpuUsageRateReport = (CPUUsageRateReport) tempCPUUsageRateReports.get(i);
            previousCycleCPUUsageRateReport = cpuUsageRateReportMapper.selectCPUUsageRateReportByHost(
                cpuUsageRateReport.getHost(), myStartDate - 2 * cycle, myStartDate - cycle,
                Constant.CPU_USE_PERCENT_FLAG);

            if (previousCycleCPUUsageRateReport != null) {
                //设置上周期的数据的保留位数
                previousCycleCPUUsageRateReport
                .setValueMin(Util.round(previousCycleCPUUsageRateReport.getValueMin(), 3));
                previousCycleCPUUsageRateReport
                .setValueAvg(Util.round(previousCycleCPUUsageRateReport.getValueAvg(), 3));
                previousCycleCPUUsageRateReport
                .setValueMax(Util.round(previousCycleCPUUsageRateReport.getValueMax(), 3));
            }

            cpuUsageRateReports.add(cpuUsageRateReport);
            cpuUsageRateReports.add(previousCycleCPUUsageRateReport);
        }
        return cpuUsageRateReports;
    }

    public List<CPUUsageRateReport> selectHighCPUUsageRateReportServiceByDate(final String group,
        final Integer interval,
        final Integer topNum, final long startDate, final long endDate) {
        LoginUser user = Util.getLoginUser();
        Integer userId = user.getUserid();
        boolean isSuperAdmin = user.isSuperAdmin();

        List<CPUUsageRateReport> cpuUsageRateReports = new ArrayList<CPUUsageRateReport>();
        timeFormat = Util.getTimeFormat(interval);

        if (isSuperAdmin) {
            cpuUsageRateReports = cpuUsageRateReportMapper.selectHighCPUUsageRateReport(group,
                startDate, endDate, Constant.CPU_USE_PERCENT_FLAG);
        } else {
            cpuUsageRateReports = cpuUsageRateReportMapper.selectHighCPUUsageRateReportByUser(group,
                startDate, endDate, userId, Constant.CPU_USE_PERCENT);
        }

        Iterator<CPUUsageRateReport> it = cpuUsageRateReports.iterator();
        while (it.hasNext()) {
            CPUUsageRateReport cpuUsageRateReport = (CPUUsageRateReport) it.next();

            if (cpuUsageRateReport.getValueAvg() == 0.0) {
                it.remove();
            }

            //设置数据小数点保留位数,考虑到cpu使用率可能非常小
            cpuUsageRateReport.setValueMin(Util.round(cpuUsageRateReport.getValueMin(), 3));
            cpuUsageRateReport.setValueAvg(Util.round(cpuUsageRateReport.getValueAvg(), 3));
            cpuUsageRateReport.setValueMax(Util.round(cpuUsageRateReport.getValueMax(), 3));

        }
        cpuUsageRateReports = Util.reduceList(cpuUsageRateReports, topNum);
        return cpuUsageRateReports;
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 查询出低CPU使用率数据
     */
    public List<CPUUsageRateReport> selectLowCPUUsageRateReportServiceByDate(final String group, final Integer interval,
        final Integer topNum, final long startDate, final long endDate) {
        LoginUser user = Util.getLoginUser();
        Integer userId = user.getUserid();
        boolean isSuperAdmin = user.isSuperAdmin();
        List<CPUUsageRateReport> cpuUsageRateReports = new ArrayList<CPUUsageRateReport>();
        timeFormat = Util.getTimeFormat(interval);

        if (isSuperAdmin) {
            cpuUsageRateReports = cpuUsageRateReportMapper.selectLowCPUUsageRateReport(group,
                startDate, endDate, Constant.CPU_USE_PERCENT_FLAG);
        } else {
            cpuUsageRateReports = cpuUsageRateReportMapper.selectLowCPUUsageRateReportByUser(group,
                startDate, endDate, userId, Constant.CPU_USE_PERCENT);
        }

        Iterator<CPUUsageRateReport> it = cpuUsageRateReports.iterator();
        while (it.hasNext()) {
            CPUUsageRateReport cpuUsageRateReport = (CPUUsageRateReport) it.next();
            if (cpuUsageRateReport.getValueAvg() == 0.0) {
                it.remove();
            }
            //设置数据小数点保留位数,考虑到cpu使用率可能非常小
            cpuUsageRateReport.setValueMin(Util.round(cpuUsageRateReport.getValueMin(), 3));
            cpuUsageRateReport.setValueAvg(Util.round(cpuUsageRateReport.getValueAvg(), 3));
            cpuUsageRateReport.setValueMax(Util.round(cpuUsageRateReport.getValueMax(), 3));
        }
        cpuUsageRateReports = Util.reduceList(cpuUsageRateReports, topNum); //剔除到指定数量
        return cpuUsageRateReports;
    }
}