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.MemoryUsageRateReport;
import com.cesgroup.cesmonitor.common.EhcacheUtil;
import com.cesgroup.cesmonitor.common.Util;
import com.cesgroup.cesmonitor.dao.MemoryUsageRateReportMapper;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

/**
 * 内存使用率报表service
 *
 * @author user
 * @version 1.0.0 2017年5月11日
 */
@Service("memoryUsageRateReportService")
public class MemoryUsageRateReportService {

    @Autowired
    private MemoryUsageRateReportMapper memoryUsageRateReportMapper;

    private Long timeFormat = 0L;

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 高内存使用率
     */
    public List<MemoryUsageRateReport> selectHighMemoryUsageRateReportService(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<MemoryUsageRateReport> highMemoryUsageRateReports = new ArrayList<>();
        List<MemoryUsageRateReport> highMemoryUsageRateReports2 = new ArrayList<>();

        if (isSuperAdmin) {
            highMemoryUsageRateReports = memoryUsageRateReportMapper.selectHighMemoryUsageReport(group,
                deadLine - interval * timeFormat, deadLine);
        } else {
            highMemoryUsageRateReports = memoryUsageRateReportMapper.selectHighMemoryUsageReportByUser(group,
                deadLine - interval * timeFormat, deadLine, userId);
        }

        highMemoryUsageRateReports2.addAll(highMemoryUsageRateReports);
        String ehcacheKey2 = "myhighMemoryUsageRateReports" + deadLine.toString() + group + interval + topNum;

        EhcacheUtil.put(ehcacheKey2, highMemoryUsageRateReports2);

        Iterator<MemoryUsageRateReport> it = highMemoryUsageRateReports.iterator();
        while (it.hasNext()) {
            MemoryUsageRateReport memoryUsageRateReport = it.next();
            if (memoryUsageRateReport.getValueAvg() <= 0D) {
                it.remove();
            }

            memoryUsageRateReport.setValueMin(Util.round(memoryUsageRateReport.getValueMin(), 2));
            memoryUsageRateReport.setValueAvg(Util.round(memoryUsageRateReport.getValueAvg(), 2));
            memoryUsageRateReport.setValueMax(Util.round(memoryUsageRateReport.getValueMax(), 2));
        }

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

    /**
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 低内存使用率
     */
    @SuppressWarnings("unchecked")
    public List<MemoryUsageRateReport> selectLowMemoryUsageRateReportService(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<MemoryUsageRateReport> lowMemoryUsageRateReports = new ArrayList<>();

        if (isSuperAdmin) {

            lowMemoryUsageRateReports = (List<MemoryUsageRateReport>) EhcacheUtil
                .get("myhighMemoryUsageRateReports" + deadLine.toString() + group + interval + topNum);
            if (null == lowMemoryUsageRateReports) {

                lowMemoryUsageRateReports = memoryUsageRateReportMapper.selectLowMemoryUsageReport(group,
                    deadLine - interval * timeFormat, deadLine);
            } else {
                Collections.reverse(lowMemoryUsageRateReports);
            }

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

            /*  lowMemoryUsageRateReports = memoryUsageRateReportMapper.selectLowMemoryUsageReport(group,
                    deadLine - interval * timeFormat, deadLine);*/
        } else {
            lowMemoryUsageRateReports = memoryUsageRateReportMapper.selectLowMemoryUsageReportByUser(group,
                deadLine - interval * timeFormat, deadLine, userId);
        }
        Iterator<MemoryUsageRateReport> it = lowMemoryUsageRateReports.iterator();
        while (it.hasNext()) {
            MemoryUsageRateReport memoryUsageRateReport = it.next();
            if (memoryUsageRateReport.getValueAvg() <= 0D) {
                it.remove();
            }

            memoryUsageRateReport.setValueMin(Util.round(memoryUsageRateReport.getValueMin(), 2));
            memoryUsageRateReport.setValueAvg(Util.round(memoryUsageRateReport.getValueAvg(), 2));
            memoryUsageRateReport.setValueMax(Util.round(memoryUsageRateReport.getValueMax(), 2));
        }
        lowMemoryUsageRateReports = Util.reduceList(lowMemoryUsageRateReports, topNum);
        return lowMemoryUsageRateReports;
    }

    /**
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 低内存使用率
     */
    public List<MemoryUsageRateReport> selectLowMemoryUsageRateReportService2(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<MemoryUsageRateReport> lowMemoryUsageRateReports = new ArrayList<>();

        if (isSuperAdmin) {

            /*lowMemoryUsageRateReports = (List<MemoryUsageRateReport>) EhcacheUtil.get("myhighMemoryUsageRateReports" +
             * deadLine.toString() + group + interval + topNum);
            if(null == lowMemoryUsageRateReports){

             lowMemoryUsageRateReports = memoryUsageRateReportMapper.selectLowMemoryUsageReport(group,
                     deadLine - interval * timeFormat, deadLine);
            }else{
             Collections.reverse(lowMemoryUsageRateReports);
            }

            EhcacheUtil.remove("myhighMemoryUsageRateReports" + deadLine.toString() + group + interval + topNum);*/

            lowMemoryUsageRateReports = memoryUsageRateReportMapper.selectLowMemoryUsageReport(group,
                deadLine - interval * timeFormat, deadLine);
        } else {
            lowMemoryUsageRateReports = memoryUsageRateReportMapper.selectLowMemoryUsageReportByUser(group,
                deadLine - interval * timeFormat, deadLine, userId);
        }
        Iterator<MemoryUsageRateReport> it = lowMemoryUsageRateReports.iterator();
        while (it.hasNext()) {
            MemoryUsageRateReport memoryUsageRateReport = it.next();
            if (memoryUsageRateReport.getValueAvg() <= 0D) {
                it.remove();
            }

            memoryUsageRateReport.setValueMin(Util.round(memoryUsageRateReport.getValueMin(), 2));
            memoryUsageRateReport.setValueAvg(Util.round(memoryUsageRateReport.getValueAvg(), 2));
            memoryUsageRateReport.setValueMax(Util.round(memoryUsageRateReport.getValueMax(), 2));
        }
        lowMemoryUsageRateReports = Util.reduceList(lowMemoryUsageRateReports, topNum);
        return lowMemoryUsageRateReports;
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 本周期，上周期高内存使用率
     */
    public List<MemoryUsageRateReport> selectMulticycleHighMemoryUsageRateReportService(final String group,
        final Integer interval,
        final Integer topNum) {

        Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);
        List<MemoryUsageRateReport> multicycleHighMemoryUsageRateReports = new ArrayList<>();
        List<MemoryUsageRateReport> tempMemoryUsageRateReports = selectHighMemoryUsageRateReportService(group, interval,
            topNum);

        int size = tempMemoryUsageRateReports.size();
        for (int i = 0; i < size; i++) {
            MemoryUsageRateReport previousCycleMemoryUsageRateReport = memoryUsageRateReportMapper
                .selectMemoryUsageReportByHost(tempMemoryUsageRateReports.get(i).getHost(),
                    deadLine - 2 * interval * timeFormat, deadLine - interval * timeFormat);
            if (previousCycleMemoryUsageRateReport == null) {
                previousCycleMemoryUsageRateReport = new MemoryUsageRateReport();
                previousCycleMemoryUsageRateReport.setHost(tempMemoryUsageRateReports.get(i).getHost());
                previousCycleMemoryUsageRateReport.setName(tempMemoryUsageRateReports.get(i).getName());
            } else {
                previousCycleMemoryUsageRateReport
                .setValueMin(Util.round(previousCycleMemoryUsageRateReport.getValueMin(), 2));
                previousCycleMemoryUsageRateReport
                .setValueAvg(Util.round(previousCycleMemoryUsageRateReport.getValueAvg(), 2));
                previousCycleMemoryUsageRateReport
                .setValueMax(Util.round(previousCycleMemoryUsageRateReport.getValueMax(), 2));
            }

            multicycleHighMemoryUsageRateReports.add(tempMemoryUsageRateReports.get(i));
            multicycleHighMemoryUsageRateReports.add(previousCycleMemoryUsageRateReport);
        }
        return multicycleHighMemoryUsageRateReports;
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 本周期，上周期低内存使用率
     */
    public List<MemoryUsageRateReport> selectMulticycleLowMemoryUsageRateReportService(final String group,
        final Integer interval,
        final Integer topNum) {
        Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);
        List<MemoryUsageRateReport> multicycleLowMemoryUsageRateReports = new ArrayList<>();
        List<MemoryUsageRateReport> tempMemoryUsageRateReports = selectLowMemoryUsageRateReportService2(group, interval,
            topNum);
        int size = tempMemoryUsageRateReports.size();
        for (int i = 0; i < size; i++) {
            MemoryUsageRateReport previousCycleMemoryUsageRateReport = memoryUsageRateReportMapper
                .selectMemoryUsageReportByHost(tempMemoryUsageRateReports.get(i).getHost(),
                    deadLine - 2 * interval * timeFormat, deadLine - interval * timeFormat);
            if (previousCycleMemoryUsageRateReport == null) {
                previousCycleMemoryUsageRateReport = new MemoryUsageRateReport();
                previousCycleMemoryUsageRateReport.setHost(tempMemoryUsageRateReports.get(i).getHost());
                previousCycleMemoryUsageRateReport.setName(tempMemoryUsageRateReports.get(i).getName());
            } else {
                previousCycleMemoryUsageRateReport
                .setValueMin(Util.round(previousCycleMemoryUsageRateReport.getValueMin(), 2));
                previousCycleMemoryUsageRateReport
                .setValueAvg(Util.round(previousCycleMemoryUsageRateReport.getValueAvg(), 2));
                previousCycleMemoryUsageRateReport
                .setValueMax(Util.round(previousCycleMemoryUsageRateReport.getValueMax(), 2));
            }

            multicycleLowMemoryUsageRateReports.add(tempMemoryUsageRateReports.get(i));
            multicycleLowMemoryUsageRateReports.add(previousCycleMemoryUsageRateReport);
        }
        return multicycleLowMemoryUsageRateReports;
    }

    /**
     * 创建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 memoryUsageRateReports
     *            内存使用率数据
     * @return 表格对象
     */
    public static PdfPTable createTable(final List<MemoryUsageRateReport> memoryUsageRateReports) {
        PdfPTable table = null;
        try {
            table = new PdfPTable(8);
            table.setTotalWidth(800);
            table.setWidths(new int[] { 8, 8, 5, 5, 5, 5, 5, 5 });
            // 居中
            //            table.setHorizontalAlignment(Element.ALIGN_CENTER);
            //            table.setHorizontalAlignment(Element.ALIGN_LEFT);
            BaseFont baseFont = BaseFont.createFont(Util.getFontPath(), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            Font font = new Font(baseFont);
            table.addCell(new Paragraph("主机名", font));
            table.addCell(new Paragraph("主机组", font));
            table.addCell(new Paragraph("本周期平均", font));
            table.addCell(new Paragraph("上周期平均", font));
            table.addCell(new Paragraph("本周期峰值", font));
            table.addCell(new Paragraph("上周期峰值", font));
            table.addCell(new Paragraph("本周期最低", font));
            table.addCell(new Paragraph("上周期最低", font));

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

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

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

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 高内存使用率
     */
    public List<MemoryUsageRateReport> selectHighMemoryUsageRateReportServiceByDate(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;

        Date startDate1;
        Date endDate1;

        try {
            startDate1 = df.parse(startDate);
            myStartDate = startDate1.getTime() / 1000;
            endDate1 = df.parse(endDate);
            myEndDate = endDate1.getTime() / 1000;

        } catch (ParseException e) {
            e.printStackTrace();
        }

        timeFormat = Util.getTimeFormat(interval);
        List<MemoryUsageRateReport> highMemoryUsageRateReports = new ArrayList<>();

        if (isSuperAdmin) {
            highMemoryUsageRateReports = memoryUsageRateReportMapper.selectHighMemoryUsageReport(group,
                myStartDate, myEndDate);
        } else {
            highMemoryUsageRateReports = memoryUsageRateReportMapper.selectHighMemoryUsageReportByUser(group,
                myStartDate, myEndDate, userId);
        }

        Iterator<MemoryUsageRateReport> it = highMemoryUsageRateReports.iterator();
        while (it.hasNext()) {
            MemoryUsageRateReport memoryUsageRateReport = it.next();
            if (memoryUsageRateReport.getValueAvg() == 0D) {
                it.remove();
            }

            memoryUsageRateReport.setValueMin(Util.round(memoryUsageRateReport.getValueMin(), 2));
            memoryUsageRateReport.setValueAvg(Util.round(memoryUsageRateReport.getValueAvg(), 2));
            memoryUsageRateReport.setValueMax(Util.round(memoryUsageRateReport.getValueMax(), 2));
        }

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

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 本周期，上周期高内存使用率
     */
    public List<MemoryUsageRateReport> selectMulticycleHighMemoryUsageRateReportServiceByDate(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<MemoryUsageRateReport> multicycleHighMemoryUsageRateReports = new ArrayList<>();

        List<MemoryUsageRateReport> tempMemoryUsageRateReports = selectHighMemoryUsageRateReportServicebyDate(group,
            interval,
            topNum, myStartDate, myEndDate);

        int size = tempMemoryUsageRateReports.size();
        for (int i = 0; i < size; i++) {
            MemoryUsageRateReport previousCycleMemoryUsageRateReport = memoryUsageRateReportMapper
                .selectMemoryUsageReportByHost(tempMemoryUsageRateReports.get(i).getHost(),
                    myStartDate - 2 * cycle, myStartDate - cycle);
            if (previousCycleMemoryUsageRateReport == null) {
                previousCycleMemoryUsageRateReport = new MemoryUsageRateReport();
                previousCycleMemoryUsageRateReport.setHost(tempMemoryUsageRateReports.get(i).getHost());
                previousCycleMemoryUsageRateReport.setName(tempMemoryUsageRateReports.get(i).getName());
            } else {
                previousCycleMemoryUsageRateReport
                .setValueMin(Util.round(previousCycleMemoryUsageRateReport.getValueMin(), 2));
                previousCycleMemoryUsageRateReport
                .setValueAvg(Util.round(previousCycleMemoryUsageRateReport.getValueAvg(), 2));
                previousCycleMemoryUsageRateReport
                .setValueMax(Util.round(previousCycleMemoryUsageRateReport.getValueMax(), 2));
            }

            multicycleHighMemoryUsageRateReports.add(tempMemoryUsageRateReports.get(i));
            multicycleHighMemoryUsageRateReports.add(previousCycleMemoryUsageRateReport);
        }
        return multicycleHighMemoryUsageRateReports;
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 本周期，上周期高内存使用率
     */
    public List<MemoryUsageRateReport> selectMulticycleLowMemoryUsageRateReportServiceByDate(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<MemoryUsageRateReport> multicycleHighMemoryUsageRateReports = new ArrayList<>();

        List<MemoryUsageRateReport> tempMemoryUsageRateReports = selectLowMemoryUsageRateReportServicebyDate(group,
            interval,
            topNum, myStartDate, myEndDate);

        int size = tempMemoryUsageRateReports.size();
        for (int i = 0; i < size; i++) {
            MemoryUsageRateReport previousCycleMemoryUsageRateReport = memoryUsageRateReportMapper
                .selectMemoryUsageReportByHost(tempMemoryUsageRateReports.get(i).getHost(),
                    myStartDate - 2 * cycle, myStartDate - cycle);
            if (previousCycleMemoryUsageRateReport == null) {
                previousCycleMemoryUsageRateReport = new MemoryUsageRateReport();
                previousCycleMemoryUsageRateReport.setHost(tempMemoryUsageRateReports.get(i).getHost());
                previousCycleMemoryUsageRateReport.setName(tempMemoryUsageRateReports.get(i).getName());
            } else {
                previousCycleMemoryUsageRateReport
                .setValueMin(Util.round(previousCycleMemoryUsageRateReport.getValueMin(), 2));
                previousCycleMemoryUsageRateReport
                .setValueAvg(Util.round(previousCycleMemoryUsageRateReport.getValueAvg(), 2));
                previousCycleMemoryUsageRateReport
                .setValueMax(Util.round(previousCycleMemoryUsageRateReport.getValueMax(), 2));
            }

            multicycleHighMemoryUsageRateReports.add(tempMemoryUsageRateReports.get(i));
            multicycleHighMemoryUsageRateReports.add(previousCycleMemoryUsageRateReport);
        }
        return multicycleHighMemoryUsageRateReports;
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 高内存使用率
     */
    public List<MemoryUsageRateReport> selectHighMemoryUsageRateReportServicebyDate(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<MemoryUsageRateReport> highMemoryUsageRateReports = new ArrayList<>();

        if (isSuperAdmin) {
            highMemoryUsageRateReports = memoryUsageRateReportMapper.selectHighMemoryUsageReport(group,
                startDate, endDate);
        } else {
            highMemoryUsageRateReports = memoryUsageRateReportMapper.selectHighMemoryUsageReportByUser(group,
                startDate, endDate, userId);
        }

        Iterator<MemoryUsageRateReport> it = highMemoryUsageRateReports.iterator();
        while (it.hasNext()) {
            MemoryUsageRateReport memoryUsageRateReport = it.next();
            if (memoryUsageRateReport.getValueAvg() == 0D) {
                it.remove();
            }

            memoryUsageRateReport.setValueMin(Util.round(memoryUsageRateReport.getValueMin(), 2));
            memoryUsageRateReport.setValueAvg(Util.round(memoryUsageRateReport.getValueAvg(), 2));
            memoryUsageRateReport.setValueMax(Util.round(memoryUsageRateReport.getValueMax(), 2));
        }

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

    /**
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 低内存使用率
     */
    public List<MemoryUsageRateReport> selectLowMemoryUsageRateReportServiceByDate(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;
        Date startDate1;
        Date endDate1;

        try {
            startDate1 = df.parse(startDate);
            myStartDate = startDate1.getTime() / 1000;
            endDate1 = df.parse(endDate);
            myEndDate = endDate1.getTime() / 1000;

        } catch (ParseException e) {
            e.printStackTrace();
        }

        timeFormat = Util.getTimeFormat(interval);
        List<MemoryUsageRateReport> lowMemoryUsageRateReports = new ArrayList<>();

        if (isSuperAdmin) {
            lowMemoryUsageRateReports = memoryUsageRateReportMapper.selectLowMemoryUsageReport(group,
                myStartDate, myEndDate);
        } else {
            lowMemoryUsageRateReports = memoryUsageRateReportMapper.selectLowMemoryUsageReportByUser(group,
                myStartDate, myEndDate, userId);
        }

        Iterator<MemoryUsageRateReport> it = lowMemoryUsageRateReports.iterator();
        while (it.hasNext()) {
            MemoryUsageRateReport memoryUsageRateReport = it.next();
            if (memoryUsageRateReport.getValueAvg() <= 0D) {
                it.remove();
            }
            memoryUsageRateReport.setValueMin(Util.round(memoryUsageRateReport.getValueMin(), 2));
            memoryUsageRateReport.setValueAvg(Util.round(memoryUsageRateReport.getValueAvg(), 2));
            memoryUsageRateReport.setValueMax(Util.round(memoryUsageRateReport.getValueMax(), 2));
        }
        lowMemoryUsageRateReports = Util.reduceList(lowMemoryUsageRateReports, topNum);
        return lowMemoryUsageRateReports;
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 高内存使用率
     */
    public List<MemoryUsageRateReport> selectLowMemoryUsageRateReportServicebyDate(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<MemoryUsageRateReport> highMemoryUsageRateReports = new ArrayList<>();

        if (isSuperAdmin) {

            highMemoryUsageRateReports = memoryUsageRateReportMapper.selectHighMemoryUsageReport(group,
                startDate, endDate);
        } else {
            highMemoryUsageRateReports = memoryUsageRateReportMapper.selectHighMemoryUsageReportByUser(group,
                startDate, endDate, userId);
        }

        Iterator<MemoryUsageRateReport> it = highMemoryUsageRateReports.iterator();
        while (it.hasNext()) {
            MemoryUsageRateReport memoryUsageRateReport = it.next();
            if (memoryUsageRateReport.getValueAvg() == 0D) {
                it.remove();
            }

            memoryUsageRateReport.setValueMin(Util.round(memoryUsageRateReport.getValueMin(), 2));
            memoryUsageRateReport.setValueAvg(Util.round(memoryUsageRateReport.getValueAvg(), 2));
            memoryUsageRateReport.setValueMax(Util.round(memoryUsageRateReport.getValueMax(), 2));
        }

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

}