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

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.DiskUsageRateReport;
import com.cesgroup.cesmonitor.common.EhcacheUtil;
import com.cesgroup.cesmonitor.common.Util;
import com.cesgroup.cesmonitor.dao.DiskUsageRateReportMapper;
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月11日
 */
@Service("diskUsageRateReportService")
public class DiskUsageRateReportService {

    @Autowired
    private DiskUsageRateReportMapper diskUsageRateReportMapper;

    private Long timeFormat = 0L;

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 高磁盘使用
     */
    public List<DiskUsageRateReport> selectHighDiskUsageRateReportService(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<DiskUsageRateReport> highDiskUsageRateReports = new ArrayList<>();
        List<DiskUsageRateReport> highDiskUsageRateReports2 = new ArrayList<>();
        if (isSuperAdmin) {

            highDiskUsageRateReports = diskUsageRateReportMapper.selectHighDiskUsageRateReport(group,
                deadLine - interval * timeFormat, deadLine);
        } else {
            highDiskUsageRateReports = diskUsageRateReportMapper.selectHighDiskUsageRateReportByUser(group,
                deadLine - interval * timeFormat, deadLine, userId);
        }

        highDiskUsageRateReports2.addAll(highDiskUsageRateReports);
        String ehcacheKey2 = "myhighDiskUsageRateReports" + deadLine.toString() + group + interval + topNum;

        EhcacheUtil.put(ehcacheKey2, highDiskUsageRateReports2);

        Iterator<DiskUsageRateReport> it = highDiskUsageRateReports.iterator();
        while (it.hasNext()) {
            DiskUsageRateReport diskUsageRateReport = it.next();

            if (diskUsageRateReport.getValueAvg() == 0.0) {
                it.remove();
            } else {
                //设置保留位数
                diskUsageRateReport.setValueMin(Util.round(diskUsageRateReport.getValueMin(), 2));
                diskUsageRateReport.setValueAvg(Util.round(diskUsageRateReport.getValueAvg(), 2));
                diskUsageRateReport.setValueMax(Util.round(diskUsageRateReport.getValueMax(), 2));
            }
        }
        ArrayList<Integer> removelist = new ArrayList<Integer>();
        for (int i = 0; i < highDiskUsageRateReports.size(); i++) {

            if (highDiskUsageRateReports.get(i).getItemKey().contains("\\")) {
                int begin = highDiskUsageRateReports.get(i).getItemKey().indexOf("[");
                int end = highDiskUsageRateReports.get(i).getItemKey().indexOf("\\");
                String drive = highDiskUsageRateReports.get(i).getItemKey().substring(begin + 1, end); //获取盘符,如D:
                if (drive.contains(",pfree")) {
                    int myend = drive.indexOf(",pfree");
                    drive = drive.substring(0, myend); //获取盘符,如D:
                }
                highDiskUsageRateReports.get(i).setItemName(drive);
            } else if (highDiskUsageRateReports.get(i).getItemKey().contains("Physical Memory")) {
                removelist.add(i);
            } else if (highDiskUsageRateReports.get(i).getItemKey().contains("Virtual Memory")) {
                removelist.add(i);
            } else if (highDiskUsageRateReports.get(i).getItemName().contains("空闲率")) {
                int begin = highDiskUsageRateReports.get(i).getItemKey().indexOf("[");
                int end = highDiskUsageRateReports.get(i).getItemKey().indexOf("]");
                String drive = highDiskUsageRateReports.get(i).getItemKey().substring(begin + 1, end); //获取盘符,如D:
                if (drive.contains(",pfree")) {
                    int myend = drive.indexOf(",pfree");
                    drive = drive.substring(0, myend); //获取盘符,如D:
                }
                highDiskUsageRateReports.get(i).setItemName(drive);
            }

        }

        for (int j = 0; j < highDiskUsageRateReports.size(); j++) {
            if (!removelist.contains(j)) {
                highDiskUsageRateReports2.add(highDiskUsageRateReports.get(j));
            }
        }

        highDiskUsageRateReports2 = Util.reduceList(highDiskUsageRateReports2, topNum);
        highDiskUsageRateReports2 = parseSymbol(highDiskUsageRateReports2); //解析$符号
        return highDiskUsageRateReports2;
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 低磁盘使用
     */
    public List<DiskUsageRateReport> selectLowDiskUsageRateReportService(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<DiskUsageRateReport> lowDiskUsageRateReports = new ArrayList<>();
        List<DiskUsageRateReport> lowDiskUsageRateReports2 = new ArrayList<>();

        if (isSuperAdmin) {

            lowDiskUsageRateReports = (List<DiskUsageRateReport>) EhcacheUtil
                .get("myhighMemoryUsageRateReports" + deadLine.toString() + group + interval + topNum);
            if (null == lowDiskUsageRateReports) {
                lowDiskUsageRateReports = diskUsageRateReportMapper.selectLowDiskUsageRateReport(group,
                    deadLine - interval * timeFormat, deadLine);
            } else {
                Collections.reverse(lowDiskUsageRateReports);
            }

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

            /*lowDiskUsageRateReports = diskUsageRateReportMapper.selectLowDiskUsageRateReport(group,
             deadLine - interval * timeFormat, deadLine);*/
        } else {
            lowDiskUsageRateReports = diskUsageRateReportMapper.selectLowDiskUsageRateReportByUser(group,
                deadLine - interval * timeFormat, deadLine, userId);
        }

        Iterator<DiskUsageRateReport> it = lowDiskUsageRateReports.iterator();
        while (it.hasNext()) {
            DiskUsageRateReport diskUsageRateReport = it.next();

            if (diskUsageRateReport.getValueAvg() == 0.0) {
                it.remove();
            } else {
                //设置保留位数
                diskUsageRateReport.setValueMin(Util.round(diskUsageRateReport.getValueMin(), 2));
                if (diskUsageRateReport.getValueAvg() > 0.01) {
                    diskUsageRateReport.setValueAvg(Util.round(diskUsageRateReport.getValueAvg(), 2));
                }
                diskUsageRateReport.setValueMax(Util.round(diskUsageRateReport.getValueMax(), 2));
            }

        }

        ArrayList<Integer> removelist = new ArrayList<Integer>();
        for (int i = 0; i < lowDiskUsageRateReports.size(); i++) {

            if (lowDiskUsageRateReports.get(i).getItemKey().contains("\\")) {
                int begin = lowDiskUsageRateReports.get(i).getItemKey().indexOf("[");
                int end = lowDiskUsageRateReports.get(i).getItemKey().indexOf("\\");
                String drive = lowDiskUsageRateReports.get(i).getItemKey().substring(begin + 1, end); //获取盘符,如D:
                if (drive.contains(",pfree")) {
                    int myend = drive.indexOf(",pfree");
                    drive = drive.substring(0, myend); //获取盘符,如D:
                }
                lowDiskUsageRateReports.get(i).setItemName(drive);
            } else if (lowDiskUsageRateReports.get(i).getItemKey().contains("Physical Memory")) {
                removelist.add(i);
            } else if (lowDiskUsageRateReports.get(i).getItemKey().contains("Virtual Memory")) {
                removelist.add(i);
            } else if (lowDiskUsageRateReports.get(i).getItemName().contains("空闲率")) {
                int begin = lowDiskUsageRateReports.get(i).getItemKey().indexOf("[");
                int end = lowDiskUsageRateReports.get(i).getItemKey().indexOf("]");
                String drive = lowDiskUsageRateReports.get(i).getItemKey().substring(begin + 1, end); //获取盘符,如D:
                if (drive.contains(",pfree")) {
                    int myend = drive.indexOf(",pfree");
                    drive = drive.substring(0, myend); //获取盘符,如D:
                }
                lowDiskUsageRateReports.get(i).setItemName(drive);
            }

        }

        for (int j = 0; j < lowDiskUsageRateReports.size(); j++) {
            if (!removelist.contains(j)) {
                lowDiskUsageRateReports2.add(lowDiskUsageRateReports.get(j));
            }
        }
        //移除list大小至指定大小
        lowDiskUsageRateReports2 = Util.reduceList(lowDiskUsageRateReports2, topNum);
        lowDiskUsageRateReports2 = parseSymbol(lowDiskUsageRateReports2);
        return lowDiskUsageRateReports2;
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 低磁盘使用
     */
    public List<DiskUsageRateReport> selectLowDiskUsageRateReportService2(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<DiskUsageRateReport> lowDiskUsageRateReports = new ArrayList<>();
        List<DiskUsageRateReport> lowDiskUsageRateReports2 = new ArrayList<>();

        if (isSuperAdmin) {

            lowDiskUsageRateReports = diskUsageRateReportMapper.selectLowDiskUsageRateReport(group,
                deadLine - interval * timeFormat, deadLine);
        } else {
            lowDiskUsageRateReports = diskUsageRateReportMapper.selectLowDiskUsageRateReportByUser(group,
                deadLine - interval * timeFormat, deadLine, userId);
        }

        Iterator<DiskUsageRateReport> it = lowDiskUsageRateReports.iterator();
        while (it.hasNext()) {
            DiskUsageRateReport diskUsageRateReport = it.next();

            if (diskUsageRateReport.getValueAvg() == 0.0) {
                it.remove();
            } else {
                //设置保留位数
                diskUsageRateReport.setValueMin(Util.round(diskUsageRateReport.getValueMin(), 2));
                if (diskUsageRateReport.getValueAvg() > 0.01) {
                    diskUsageRateReport.setValueAvg(Util.round(diskUsageRateReport.getValueAvg(), 2));
                }
                diskUsageRateReport.setValueMax(Util.round(diskUsageRateReport.getValueMax(), 2));
            }

        }

        ArrayList<Integer> removelist = new ArrayList<Integer>();
        for (int i = 0; i < lowDiskUsageRateReports.size(); i++) {

            if (lowDiskUsageRateReports.get(i).getItemKey().contains("\\")) {
                int begin = lowDiskUsageRateReports.get(i).getItemKey().indexOf("[");
                int end = lowDiskUsageRateReports.get(i).getItemKey().indexOf("\\");
                String drive = lowDiskUsageRateReports.get(i).getItemKey().substring(begin + 1, end); //获取盘符,如D:
                if (drive.contains(",pfree")) {
                    int myend = drive.indexOf(",pfree");
                    drive = drive.substring(0, myend); //获取盘符,如D:
                }
                lowDiskUsageRateReports.get(i).setItemName(drive);
            } else if (lowDiskUsageRateReports.get(i).getItemKey().contains("Physical Memory")) {
                removelist.add(i);
            } else if (lowDiskUsageRateReports.get(i).getItemKey().contains("Virtual Memory")) {
                removelist.add(i);
            } else if (lowDiskUsageRateReports.get(i).getItemName().contains("空闲率")) {
                int begin = lowDiskUsageRateReports.get(i).getItemKey().indexOf("[");
                int end = lowDiskUsageRateReports.get(i).getItemKey().indexOf("]");
                String drive = lowDiskUsageRateReports.get(i).getItemKey().substring(begin + 1, end); //获取盘符,如D:
                if (drive.contains(",pfree")) {
                    int myend = drive.indexOf(",pfree");
                    drive = drive.substring(0, myend); //获取盘符,如D:
                }
                lowDiskUsageRateReports.get(i).setItemName(drive);
            }

        }

        for (int j = 0; j < lowDiskUsageRateReports.size(); j++) {
            if (!removelist.contains(j)) {
                lowDiskUsageRateReports2.add(lowDiskUsageRateReports.get(j));
            }
        }

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

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 本周期，上周期高磁盘使用率
     */
    public List<DiskUsageRateReport> selectMulticycleHighDiskUsageRateReportService(final String group,
        final Integer interval,
        final Integer topNum) {
        Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);
        List<DiskUsageRateReport> multicycleHighDiskUsageRateReports = new ArrayList<>();
        List<DiskUsageRateReport> tempDiskUsageRateReports = selectHighDiskUsageRateReportService(group, interval,
            topNum);
        String ehcacheKey = "HighDiskUsageRate" + deadLine.toString() + group + interval + topNum;
        EhcacheUtil.put(ehcacheKey, tempDiskUsageRateReports);
        int size = tempDiskUsageRateReports.size();
        DiskUsageRateReport previousCycleHighDiskUsageRateReport = new DiskUsageRateReport();
        for (int i = 0; i < size; i++) {
            previousCycleHighDiskUsageRateReport = diskUsageRateReportMapper.selectDiskUsageRateReportByHostAndItemKey(
                tempDiskUsageRateReports.get(i).getHost(), tempDiskUsageRateReports.get(i).getItemKey(),
                deadLine - 2 * interval * timeFormat, deadLine - interval * timeFormat);
            if (previousCycleHighDiskUsageRateReport != null) {
                //设置保留位数
                previousCycleHighDiskUsageRateReport
                .setValueMin(Util.round(previousCycleHighDiskUsageRateReport.getValueMin(), 2));
                previousCycleHighDiskUsageRateReport
                .setValueAvg(Util.round(previousCycleHighDiskUsageRateReport.getValueAvg(), 2));
                previousCycleHighDiskUsageRateReport
                .setValueMax(Util.round(previousCycleHighDiskUsageRateReport.getValueMax(), 2));
            }

            multicycleHighDiskUsageRateReports.add(tempDiskUsageRateReports.get(i));
            multicycleHighDiskUsageRateReports.add(previousCycleHighDiskUsageRateReport);
        }
        return multicycleHighDiskUsageRateReports;
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 本周期，上周期低磁盘使用率
     */
    public List<DiskUsageRateReport> selectMulticycleLowDiskUsageRateReportService(final String group,
        final Integer interval,
        final Integer topNum) {
        Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);
        List<DiskUsageRateReport> multicycleLowDiskUsageRateReports = new ArrayList<>();
        List<DiskUsageRateReport> tempDiskUsageRateReports = selectLowDiskUsageRateReportService2(group, interval,
            topNum);
        String ehcacheKey = "LowDiskUsageRate" + deadLine.toString() + group + interval + topNum;
        EhcacheUtil.put(ehcacheKey, tempDiskUsageRateReports);

        int size = tempDiskUsageRateReports.size();
        DiskUsageRateReport previousCycleLowDiskUsageRateReport = new DiskUsageRateReport();
        Long start = deadLine - 2 * interval * timeFormat;
        Long end = deadLine - interval * timeFormat;
        String host;
        String itemKey;
        for (int i = 0; i < size; i++) {
            host = tempDiskUsageRateReports.get(i).getHost();
            itemKey = tempDiskUsageRateReports.get(i).getItemKey();
            previousCycleLowDiskUsageRateReport = diskUsageRateReportMapper.selectDiskUsageRateReportByHostAndItemKey(
                host, itemKey, start, end);
            if (previousCycleLowDiskUsageRateReport != null) {
                //设置保留位数
                previousCycleLowDiskUsageRateReport
                .setValueMin(Util.round(previousCycleLowDiskUsageRateReport.getValueMin(), 2));
                previousCycleLowDiskUsageRateReport
                .setValueAvg(Util.round(previousCycleLowDiskUsageRateReport.getValueAvg(), 2));
                previousCycleLowDiskUsageRateReport
                .setValueMax(Util.round(previousCycleLowDiskUsageRateReport.getValueMax(), 2));
            }

            multicycleLowDiskUsageRateReports.add(tempDiskUsageRateReports.get(i));
            multicycleLowDiskUsageRateReports.add(previousCycleLowDiskUsageRateReport);
        }
        return multicycleLowDiskUsageRateReports;
    }

    /**
     * 创建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 diskUsageRateReports
     *            磁盘使用率数据
     * @return 表格
     */
    public static PdfPTable createTable(final List<DiskUsageRateReport> diskUsageRateReports) {
        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 = diskUsageRateReports.size();
            for (int i = 0; i < size; i = i + 2) {
                //需对ItemName进行截取，获取盘符
                table.addCell(new Paragraph(
                    diskUsageRateReports.get(i).getHost() + diskUsageRateReports.get(i).getItemName(), font));
                table.addCell(new Paragraph(diskUsageRateReports.get(i).getName(), font));
                table.addCell(String.valueOf(diskUsageRateReports.get(i).getValueAvg()));
                if (diskUsageRateReports.get(i + 1) == null) {
                    table.addCell(new Paragraph("-  -  -", font));
                } else {
                    table.addCell(String.valueOf(diskUsageRateReports.get(i + 1).getValueAvg()));
                }

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

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

    /**
     * 对itemName中的$符号进行解析
     *
     * @param diskUsageRateReports
     *            磁盘使用率报表
     * @return itemName解析后的磁盘使用率报表
     */
    public List<DiskUsageRateReport> parseSymbol(final List<DiskUsageRateReport> diskUsageRateReports) {
        for (DiskUsageRateReport diskUsageRateReport : diskUsageRateReports) {
            if (diskUsageRateReport.getItemName().contains("$")) {
                int begin = diskUsageRateReport.getItemKey().indexOf("["); //key_类似vfs.fs.size[C:,pfree]
                int end = diskUsageRateReport.getItemKey().indexOf(",");
                String drive = diskUsageRateReport.getItemKey().substring(begin + 1, end); //获取盘符,如D:
                //                diskUsageRateReport.setItemName(diskUsageRateReport.getItemName().replaceAll("\\$\\d", drive));
                diskUsageRateReport.setItemName(drive);
            }
        }
        return diskUsageRateReports;
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 高磁盘使用
     */
    public List<DiskUsageRateReport> selectHighDiskUsageRateReportServiceByDate(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();
        }
        Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);
        List<DiskUsageRateReport> highDiskUsageRateReports = new ArrayList<>();
        List<DiskUsageRateReport> highDiskUsageRateReports2 = new ArrayList<>();
        if (isSuperAdmin) {
            highDiskUsageRateReports = diskUsageRateReportMapper.selectHighDiskUsageRateReport(group,
                myStartDate, myEndDate);
        } else {
            highDiskUsageRateReports = diskUsageRateReportMapper.selectHighDiskUsageRateReportByUser(group,
                myStartDate, myEndDate, userId);
        }
        Iterator<DiskUsageRateReport> it = highDiskUsageRateReports.iterator();
        while (it.hasNext()) {
            DiskUsageRateReport diskUsageRateReport = it.next();
            if (diskUsageRateReport.getValueAvg() == 0.0) {
                it.remove();
            } else {
                //设置保留位数
                diskUsageRateReport.setValueMin(Util.round(diskUsageRateReport.getValueMin(), 2));
                diskUsageRateReport.setValueAvg(Util.round(diskUsageRateReport.getValueAvg(), 2));
                diskUsageRateReport.setValueMax(Util.round(diskUsageRateReport.getValueMax(), 2));
            }
        }

        ArrayList<Integer> removelist = new ArrayList<Integer>();
        for (int i = 0; i < highDiskUsageRateReports.size(); i++) {

            if (highDiskUsageRateReports.get(i).getItemKey().contains("\\")) {
                int begin = highDiskUsageRateReports.get(i).getItemKey().indexOf("[");
                int end = highDiskUsageRateReports.get(i).getItemKey().indexOf("\\");
                String drive = highDiskUsageRateReports.get(i).getItemKey().substring(begin + 1, end); //获取盘符,如D:
                if (drive.contains(",pfree")) {
                    int myend = drive.indexOf(",pfree");
                    drive = drive.substring(0, myend); //获取盘符,如D:
                }
                highDiskUsageRateReports.get(i).setItemName(drive);
            } else if (highDiskUsageRateReports.get(i).getItemKey().contains("Physical Memory")) {
                removelist.add(i);
            } else if (highDiskUsageRateReports.get(i).getItemKey().contains("Virtual Memory")) {
                removelist.add(i);
            } else if (highDiskUsageRateReports.get(i).getItemName().contains("空闲率")) {
                int begin = highDiskUsageRateReports.get(i).getItemKey().indexOf("[");
                int end = highDiskUsageRateReports.get(i).getItemKey().indexOf("]");
                String drive = highDiskUsageRateReports.get(i).getItemKey().substring(begin + 1, end); //获取盘符,如D:
                if (drive.contains(",pfree")) {
                    int myend = drive.indexOf(",pfree");
                    drive = drive.substring(0, myend); //获取盘符,如D:
                }
                highDiskUsageRateReports.get(i).setItemName(drive);
            }

        }

        for (int j = 0; j < highDiskUsageRateReports.size(); j++) {
            if (!removelist.contains(j)) {
                highDiskUsageRateReports2.add(highDiskUsageRateReports.get(j));
            }
        }

        highDiskUsageRateReports2 = Util.reduceList(highDiskUsageRateReports2, topNum);
        highDiskUsageRateReports2 = parseSymbol(highDiskUsageRateReports2); //解析$符号
        return highDiskUsageRateReports2;
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 低磁盘使用
     */
    public List<DiskUsageRateReport> selectLowDiskUsageRateReportServiceByDate(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();
        }

        Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);
        List<DiskUsageRateReport> lowDiskUsageRateReports = new ArrayList<>();
        List<DiskUsageRateReport> lowDiskUsageRateReports2 = new ArrayList<>();
        if (isSuperAdmin) {
            lowDiskUsageRateReports = diskUsageRateReportMapper.selectLowDiskUsageRateReport(group,
                myStartDate, myEndDate);
        } else {
            lowDiskUsageRateReports = diskUsageRateReportMapper.selectLowDiskUsageRateReportByUser(group,
                myStartDate, myEndDate, userId);
        }
        Iterator<DiskUsageRateReport> it = lowDiskUsageRateReports.iterator();
        while (it.hasNext()) {
            DiskUsageRateReport diskUsageRateReport = it.next();

            if (diskUsageRateReport.getValueAvg() == 0.0) {
                it.remove();
            } else {
                //设置保留位数
                diskUsageRateReport.setValueMin(Util.round(diskUsageRateReport.getValueMin(), 2));
                if (diskUsageRateReport.getValueAvg() > 0.01) {
                    diskUsageRateReport.setValueAvg(Util.round(diskUsageRateReport.getValueAvg(), 2));
                }
                diskUsageRateReport.setValueMax(Util.round(diskUsageRateReport.getValueMax(), 2));
            }

        }

        ArrayList<Integer> removelist = new ArrayList<Integer>();
        for (int i = 0; i < lowDiskUsageRateReports.size(); i++) {

            if (lowDiskUsageRateReports.get(i).getItemKey().contains("\\")) {
                int begin = lowDiskUsageRateReports.get(i).getItemKey().indexOf("[");
                int end = lowDiskUsageRateReports.get(i).getItemKey().indexOf("\\");
                String drive = lowDiskUsageRateReports.get(i).getItemKey().substring(begin + 1, end); //获取盘符,如D:
                if (drive.contains(",pfree")) {
                    int myend = drive.indexOf(",pfree");
                    drive = drive.substring(0, myend); //获取盘符,如D:
                }
                lowDiskUsageRateReports.get(i).setItemName(drive);
            } else if (lowDiskUsageRateReports.get(i).getItemKey().contains("Physical Memory")) {
                removelist.add(i);
            } else if (lowDiskUsageRateReports.get(i).getItemKey().contains("Virtual Memory")) {
                removelist.add(i);
            } else if (lowDiskUsageRateReports.get(i).getItemName().contains("空闲率")) {
                int begin = lowDiskUsageRateReports.get(i).getItemKey().indexOf("[");
                int end = lowDiskUsageRateReports.get(i).getItemKey().indexOf("]");
                String drive = lowDiskUsageRateReports.get(i).getItemKey().substring(begin + 1, end); //获取盘符,如D:
                if (drive.contains(",pfree")) {
                    int myend = drive.indexOf(",pfree");
                    drive = drive.substring(0, myend); //获取盘符,如D:
                }
                lowDiskUsageRateReports.get(i).setItemName(drive);
            }

        }

        for (int j = 0; j < lowDiskUsageRateReports.size(); j++) {
            if (!removelist.contains(j)) {
                lowDiskUsageRateReports2.add(lowDiskUsageRateReports.get(j));
            }
        }

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

    public List<DiskUsageRateReport> selectMulticycleHighDiskUsageRateReportServiceByDate(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<DiskUsageRateReport> multicycleHighDiskUsageRateReports = new ArrayList<>();

        List<DiskUsageRateReport> tempDiskUsageRateReports = selectHighDiskUsageRateReportServiceByDate(group, interval,
            topNum, myStartDate, myEndDate);
        int size = tempDiskUsageRateReports.size();
        DiskUsageRateReport previousCycleHighDiskUsageRateReport = new DiskUsageRateReport();
        for (int i = 0; i < size; i++) {
            previousCycleHighDiskUsageRateReport = diskUsageRateReportMapper.selectDiskUsageRateReportByHostAndItemKey(
                tempDiskUsageRateReports.get(i).getHost(), tempDiskUsageRateReports.get(i).getItemKey(),
                myStartDate - 2 * cycle, myStartDate - cycle);
            if (previousCycleHighDiskUsageRateReport != null) {
                //设置保留位数
                previousCycleHighDiskUsageRateReport
                .setValueMin(Util.round(previousCycleHighDiskUsageRateReport.getValueMin(), 2));
                previousCycleHighDiskUsageRateReport
                .setValueAvg(Util.round(previousCycleHighDiskUsageRateReport.getValueAvg(), 2));
                previousCycleHighDiskUsageRateReport
                .setValueMax(Util.round(previousCycleHighDiskUsageRateReport.getValueMax(), 2));
            }

            multicycleHighDiskUsageRateReports.add(tempDiskUsageRateReports.get(i));
            multicycleHighDiskUsageRateReports.add(previousCycleHighDiskUsageRateReport);
        }

        return multicycleHighDiskUsageRateReports;
    }

    public List<DiskUsageRateReport> selectMulticycleLowDiskUsageRateReportServiceByDate(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<DiskUsageRateReport> multicycleLowDiskUsageRateReports = new ArrayList<>();
        List<DiskUsageRateReport> tempDiskUsageRateReports = selectLowDiskUsageRateReportServiceByDate(group, interval,
            topNum, myStartDate, myEndDate);
        int size = tempDiskUsageRateReports.size();
        DiskUsageRateReport previousCycleLowDiskUsageRateReport = new DiskUsageRateReport();
        String host;
        String itemKey;
        for (int i = 0; i < size; i++) {
            host = tempDiskUsageRateReports.get(i).getHost();
            itemKey = tempDiskUsageRateReports.get(i).getItemKey();
            previousCycleLowDiskUsageRateReport = diskUsageRateReportMapper.selectDiskUsageRateReportByHostAndItemKey(
                host, itemKey, myStartDate - 2 * cycle, myStartDate - cycle);
            if (previousCycleLowDiskUsageRateReport != null) {
                //设置保留位数
                previousCycleLowDiskUsageRateReport
                .setValueMin(Util.round(previousCycleLowDiskUsageRateReport.getValueMin(), 2));
                previousCycleLowDiskUsageRateReport
                .setValueAvg(Util.round(previousCycleLowDiskUsageRateReport.getValueAvg(), 2));
                previousCycleLowDiskUsageRateReport
                .setValueMax(Util.round(previousCycleLowDiskUsageRateReport.getValueMax(), 2));
            }

            multicycleLowDiskUsageRateReports.add(tempDiskUsageRateReports.get(i));
            multicycleLowDiskUsageRateReports.add(previousCycleLowDiskUsageRateReport);
        }
        return multicycleLowDiskUsageRateReports;
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 高磁盘使用
     */
    public List<DiskUsageRateReport> selectHighDiskUsageRateReportServiceByDate(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();

        Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);
        List<DiskUsageRateReport> highDiskUsageRateReports = new ArrayList<>();
        List<DiskUsageRateReport> highDiskUsageRateReports2 = new ArrayList<>();
        if (isSuperAdmin) {
            highDiskUsageRateReports = diskUsageRateReportMapper.selectHighDiskUsageRateReport(group,
                startDate, endDate);
        } else {
            highDiskUsageRateReports = diskUsageRateReportMapper.selectHighDiskUsageRateReportByUser(group,
                startDate, endDate, userId);
        }
        Iterator<DiskUsageRateReport> it = highDiskUsageRateReports.iterator();
        while (it.hasNext()) {
            DiskUsageRateReport diskUsageRateReport = it.next();

            if (diskUsageRateReport.getValueAvg() == 0.0) {
                it.remove();
            } else {
                //设置保留位数
                diskUsageRateReport.setValueMin(Util.round(diskUsageRateReport.getValueMin(), 2));
                diskUsageRateReport.setValueAvg(Util.round(diskUsageRateReport.getValueAvg(), 2));
                diskUsageRateReport.setValueMax(Util.round(diskUsageRateReport.getValueMax(), 2));
            }
        }

        ArrayList<Integer> removelist = new ArrayList<Integer>();
        for (int i = 0; i < highDiskUsageRateReports.size(); i++) {

            if (highDiskUsageRateReports.get(i).getItemKey().contains("\\")) {
                int begin = highDiskUsageRateReports.get(i).getItemKey().indexOf("[");
                int end = highDiskUsageRateReports.get(i).getItemKey().indexOf("\\");
                String drive = highDiskUsageRateReports.get(i).getItemKey().substring(begin + 1, end); //获取盘符,如D:
                if (drive.contains(",pfree")) {
                    int myend = drive.indexOf(",pfree");
                    drive = drive.substring(0, myend); //获取盘符,如D:
                }
                highDiskUsageRateReports.get(i).setItemName(drive);
            } else if (highDiskUsageRateReports.get(i).getItemKey().contains("Physical Memory")) {
                removelist.add(i);
            } else if (highDiskUsageRateReports.get(i).getItemKey().contains("Virtual Memory")) {
                removelist.add(i);
            } else if (highDiskUsageRateReports.get(i).getItemName().contains("空闲率")) {
                int begin = highDiskUsageRateReports.get(i).getItemKey().indexOf("[");
                int end = highDiskUsageRateReports.get(i).getItemKey().indexOf("]");
                String drive = highDiskUsageRateReports.get(i).getItemKey().substring(begin + 1, end); //获取盘符,如D:
                if (drive.contains(",pfree")) {
                    int myend = drive.indexOf(",pfree");
                    drive = drive.substring(0, myend); //获取盘符,如D:
                }
                highDiskUsageRateReports.get(i).setItemName(drive);
            }

        }

        for (int j = 0; j < highDiskUsageRateReports.size(); j++) {
            if (!removelist.contains(j)) {
                highDiskUsageRateReports2.add(highDiskUsageRateReports.get(j));
            }
        }

        highDiskUsageRateReports2 = Util.reduceList(highDiskUsageRateReports2, topNum);
        highDiskUsageRateReports2 = parseSymbol(highDiskUsageRateReports2); //解析$符号
        return highDiskUsageRateReports2;
    }

    /**
     *
     * @param group
     *            主机组
     * @param interval
     *            时间间隔
     * @param topNum
     *            top数量
     * @return 低磁盘使用
     */
    public List<DiskUsageRateReport> selectLowDiskUsageRateReportServiceByDate(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();
        Long deadLine = Util.getDeadLine(interval);
        timeFormat = Util.getTimeFormat(interval);
        List<DiskUsageRateReport> lowDiskUsageRateReports = new ArrayList<>();
        List<DiskUsageRateReport> lowDiskUsageRateReports2 = new ArrayList<>();

        if (isSuperAdmin) {
            lowDiskUsageRateReports = diskUsageRateReportMapper.selectLowDiskUsageRateReport(group,
                startDate, endDate);
        } else {
            lowDiskUsageRateReports = diskUsageRateReportMapper.selectLowDiskUsageRateReportByUser(group,
                startDate, endDate, userId);
        }
        Iterator<DiskUsageRateReport> it = lowDiskUsageRateReports.iterator();
        while (it.hasNext()) {
            DiskUsageRateReport diskUsageRateReport = it.next();

            if (diskUsageRateReport.getValueAvg() == 0.0) {
                it.remove();
            } else {
                //设置保留位数
                diskUsageRateReport.setValueMin(Util.round(diskUsageRateReport.getValueMin(), 2));
                if (diskUsageRateReport.getValueAvg() > 0.01) {
                    diskUsageRateReport.setValueAvg(Util.round(diskUsageRateReport.getValueAvg(), 2));
                }
                diskUsageRateReport.setValueMax(Util.round(diskUsageRateReport.getValueMax(), 2));
            }

        }

        ArrayList<Integer> removelist = new ArrayList<Integer>();
        for (int i = 0; i < lowDiskUsageRateReports.size(); i++) {

            if (lowDiskUsageRateReports.get(i).getItemKey().contains("\\")) {
                int begin = lowDiskUsageRateReports.get(i).getItemKey().indexOf("[");
                int end = lowDiskUsageRateReports.get(i).getItemKey().indexOf("\\");
                String drive = lowDiskUsageRateReports.get(i).getItemKey().substring(begin + 1, end); //获取盘符,如D:
                if (drive.contains(",pfree")) {
                    int myend = drive.indexOf(",pfree");
                    drive = drive.substring(0, myend); //获取盘符,如D:
                }
                lowDiskUsageRateReports.get(i).setItemName(drive);
            } else if (lowDiskUsageRateReports.get(i).getItemKey().contains("Physical Memory")) {
                removelist.add(i);
            } else if (lowDiskUsageRateReports.get(i).getItemKey().contains("Virtual Memory")) {
                removelist.add(i);
            } else if (lowDiskUsageRateReports.get(i).getItemName().contains("空闲率")) {
                int begin = lowDiskUsageRateReports.get(i).getItemKey().indexOf("[");
                int end = lowDiskUsageRateReports.get(i).getItemKey().indexOf("]");
                String drive = lowDiskUsageRateReports.get(i).getItemKey().substring(begin + 1, end); //获取盘符,如D:
                if (drive.contains(",pfree")) {
                    int myend = drive.indexOf(",pfree");
                    drive = drive.substring(0, myend); //获取盘符,如D:
                }
                lowDiskUsageRateReports.get(i).setItemName(drive);
            }

        }

        for (int j = 0; j < lowDiskUsageRateReports.size(); j++) {
            if (!removelist.contains(j)) {
                lowDiskUsageRateReports2.add(lowDiskUsageRateReports.get(j));
            }
        }

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