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.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

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

import com.cesgroup.cesmonitor.bean.AbnormalAlarmReport;
import com.cesgroup.cesmonitor.common.Util;
import com.cesgroup.cesmonitor.dao.AbnormalAlarmReportMapper;
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("abnormalAlarmReportService")
public class AbnormalAlarmReportService {

    @Autowired
    private AbnormalAlarmReportMapper abnormalAlarmReportMapper;

    /**
     *
     * @param group
     *            主机组
     * @param host
     *            主机名
     * @param priority
     *            严重性
     * @param interval
     *            时间间隔
     * @return 统计异常告警
     */
    public Map<String, List<AbnormalAlarmReport>> countAbnormalAlarmService(final String group, final String host,
        final String priority,
        final Integer interval) {
        LoginUser user = Util.getLoginUser();
        Integer userId = user.getUserid();
        boolean isSuperAdmin = user.isSuperAdmin();

        Map<String, List<AbnormalAlarmReport>> data = new LinkedHashMap<String, List<AbnormalAlarmReport>>();
        String groupBy = "";
        Long deadLine = Util.getDeadLine(interval);
        long timeFormat = 0L;
        timeFormat = Util.getTimeFormat(interval);

        if ((!group.contains("g.name")) && (!host.contains("h.host"))) {
            groupBy = "t.priority";
        } else if ((group.contains("g.name")) && (!host.contains("h.host"))) {
            groupBy = "g.name,t.priority";
        } else if ((group.contains("g.name")) && (host.contains("h.host"))) {
            groupBy = "g.name,h.host,t.priority";
        } else {
            groupBy = "h.host,t.priority";
        }

        List<AbnormalAlarmReport> abnormalAlarmReports = new ArrayList<>();
        SimpleDateFormat sdf = null;
        Long startTime = 0L;
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        if (interval == 7) {
            timeFormat = 60 * 60 * 24L;
            sdf = new SimpleDateFormat("MM-dd");
            calendar.add(Calendar.DAY_OF_YEAR, -7);
            startTime = calendar.getTime().getTime() / 1000;
            for (int i = 0; i < interval; i++) {
                Date date = calendar.getTime();
                String s = sdf.format(date);
                calendar.add(Calendar.DAY_OF_YEAR, 1);
                if (isSuperAdmin) {
                    abnormalAlarmReports = abnormalAlarmReportMapper.countAbnormalAlarm(group, host, priority,
                        startTime + i * timeFormat, startTime + (i + 1) * timeFormat, groupBy);
                } else {
                    abnormalAlarmReports = abnormalAlarmReportMapper.countAbnormalAlarmByUser(group, host, priority,
                        startTime + i * timeFormat, startTime + (i + 1) * timeFormat, userId, groupBy);
                }

                data.put(s, abnormalAlarmReports);
            }
        } else if (interval == 30) {
            timeFormat = 60 * 60 * 24L;
            sdf = new SimpleDateFormat("MM-dd");
            calendar.add(Calendar.DAY_OF_YEAR, -30);
            startTime = calendar.getTime().getTime() / 1000;

            Date date = calendar.getTime();
            String s = sdf.format(date);
            int currentDay = calendar.get(Calendar.DAY_OF_WEEK);

            Date date2 = null;
            String ss = "";

            if (currentDay == 1) {
                calendar.add(Calendar.DAY_OF_YEAR, 1);
                date2 = calendar.getTime();
                ss = sdf.format(date2);
                if (isSuperAdmin) {
                    abnormalAlarmReports = abnormalAlarmReportMapper.countAbnormalAlarm(group, host, priority,
                        startTime,
                        startTime + timeFormat, groupBy);
                } else {
                    abnormalAlarmReports = abnormalAlarmReportMapper.countAbnormalAlarmByUser(group, host, priority,
                        startTime,
                        startTime + timeFormat, userId, groupBy);
                }
                startTime = startTime + timeFormat;
            } else {
                calendar.add(Calendar.DAY_OF_YEAR, 9 - currentDay);
                date2 = calendar.getTime();
                ss = sdf.format(date2);

                if (isSuperAdmin) {
                    abnormalAlarmReports = abnormalAlarmReportMapper.countAbnormalAlarm(group, host, priority,
                        startTime,
                        startTime + (9 - currentDay) * timeFormat, groupBy);
                } else {
                    abnormalAlarmReports = abnormalAlarmReportMapper.countAbnormalAlarmByUser(group, host, priority,
                        startTime,
                        startTime + (9 - currentDay) * timeFormat, userId, groupBy);
                }
                startTime = startTime + (9 - currentDay) * timeFormat;
            }
            data.put(s + "至" + ss, abnormalAlarmReports);
            for (int i = 0; i < 3; i++) {
                date = calendar.getTime();
                s = sdf.format(date);
                calendar.add(Calendar.DAY_OF_YEAR, 7);
                date2 = calendar.getTime();
                ss = sdf.format(date2);
                if (isSuperAdmin) {
                    abnormalAlarmReports = abnormalAlarmReportMapper.countAbnormalAlarm(group, host, priority,
                        startTime + i * 7 * timeFormat, startTime + (i + 1) * 7 * timeFormat, groupBy);
                } else {
                    abnormalAlarmReports = abnormalAlarmReportMapper.countAbnormalAlarmByUser(group, host, priority,
                        startTime + i * 7 * timeFormat, startTime + (i + 1) * 7 * timeFormat, userId, groupBy);
                }

                data.put(s + "至" + ss, abnormalAlarmReports);
            }
            date = calendar.getTime();
            s = sdf.format(date);

            if (isSuperAdmin) {
                abnormalAlarmReports = abnormalAlarmReportMapper.countAbnormalAlarm(group, host, priority,
                    startTime + 3 * 7 * timeFormat, deadLine, groupBy);
            } else {
                abnormalAlarmReports = abnormalAlarmReportMapper.countAbnormalAlarmByUser(group, host, priority,
                    startTime + 3 * 7 * timeFormat, deadLine, userId, groupBy);
            }
            String yesterday = Util.getDate(Util.getYesterdayLastTimeTime());
            ss = yesterday.substring(yesterday.indexOf("-") + 1, 10);
            data.put(s + "至" + ss, abnormalAlarmReports);
        } else if (interval == 6 || interval == 12) {
            sdf = new SimpleDateFormat("yyyy-MM");
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            calendar.add(Calendar.MONTH, -interval + 1);
            startTime = calendar.getTime().getTime() / 1000;
            Integer days = 0;
            timeFormat = 60 * 60 * 24L;
            for (int i = 0; i < interval; i++) {
                Date date = calendar.getTime();
                String s = sdf.format(date);
                Integer day = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); //获取每个月的天数
                calendar.add(Calendar.MONTH, 1);
                if (isSuperAdmin) {
                    abnormalAlarmReports = abnormalAlarmReportMapper.countAbnormalAlarm(group, host, priority,
                        startTime + days * timeFormat, startTime + (days + day) * timeFormat, groupBy);
                } else {
                    abnormalAlarmReports = abnormalAlarmReportMapper.countAbnormalAlarmByUser(group, host, priority,
                        startTime + days * timeFormat, startTime + (days + day) * timeFormat, userId, groupBy);
                }
                days = days + day; //天数
                data.put(s, abnormalAlarmReports);
            }
        } else if (interval == 24) {
            calendar = Calendar.getInstance();
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            sdf = new SimpleDateFormat("HH:00");
            calendar.set(Calendar.HOUR_OF_DAY, hour + 1);
            calendar.add(Calendar.HOUR_OF_DAY, -interval);
            startTime = calendar.getTime().getTime() / 1000;
            Integer hours = 0;
            timeFormat = 60 * 60L;
            for (int i = 0; i < interval; i++) {
                Date date = calendar.getTime();
                String s = sdf.format(date);

                //Integer HOURS = calendar.getActualMaximum(Calendar.HOUR_OF_DAY); //获取每天小时数

                calendar.add(Calendar.HOUR_OF_DAY, 1);
                if (isSuperAdmin) {
                    abnormalAlarmReports = abnormalAlarmReportMapper.countAbnormalAlarm(group, host, priority,
                        startTime + hours * timeFormat, startTime + (hours + 1) * timeFormat, groupBy);
                } else {
                    abnormalAlarmReports = abnormalAlarmReportMapper.countAbnormalAlarmByUser(group, host, priority,
                        startTime + hours * timeFormat, startTime + (hours + 1) * timeFormat, userId, groupBy);
                }
                hours = hours + 1;
                data.put(s, abnormalAlarmReports);
            }
        } else if (interval == 3) {
            sdf = new SimpleDateFormat("yyyy-MM");
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            calendar.add(Calendar.MONTH, -interval + 1);
            startTime = calendar.getTime().getTime() / 1000;
            Integer days = 0;
            timeFormat = 60 * 60 * 24L;
            for (int i = 0; i < interval; i++) {
                Date date = calendar.getTime();
                String s = sdf.format(date);
                Integer day = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); //获取每个月的天数
                calendar.add(Calendar.MONTH, 1);

                if (isSuperAdmin) {
                    abnormalAlarmReports = abnormalAlarmReportMapper.countAbnormalAlarm(group, host, priority,
                        startTime + days * timeFormat, startTime + (days + day) * timeFormat, groupBy);
                } else {
                    abnormalAlarmReports = abnormalAlarmReportMapper.countAbnormalAlarmByUser(group, host, priority,
                        startTime + days * timeFormat, startTime + (days + day) * timeFormat, userId, groupBy);
                }

                days = days + day; //天数
                data.put(s, abnormalAlarmReports);
            }
        }
        return data;
    }

    /**
     *
     * @param group
     *            主机组
     * @param host
     *            主机
     * @param priority
     *            严重等级
     * @param interval
     *            时间间隔
     * @return 查询出异常告警
     */
    public List<AbnormalAlarmReport> selectTopAbnormalAlarmService(final String group, final String host,
        final String priority,
        final Integer interval) {
        LoginUser user = Util.getLoginUser();
        Integer userId = user.getUserid();
        boolean isSuperAdmin = user.isSuperAdmin();
        List<AbnormalAlarmReport> abnormalAlarmReports = new ArrayList<AbnormalAlarmReport>();
        Long deadLine = Util.getDeadLine(interval);

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        Long startTime = 0L;
        if (interval == 7) {
            calendar.add(Calendar.DAY_OF_YEAR, -7);
            startTime = calendar.getTime().getTime() / 1000;
            if (isSuperAdmin) {
                abnormalAlarmReports = abnormalAlarmReportMapper.selectTopAbnormalAlarm(group, host, priority,
                    startTime,
                    deadLine);
            } else {
                abnormalAlarmReports = abnormalAlarmReportMapper.selectTopAbnormalAlarmByUser(group, host, priority,
                    startTime,
                    deadLine, userId);
            }

        } else if (interval == 24) {
            calendar.add(Calendar.HOUR_OF_DAY, -24);
            startTime = calendar.getTime().getTime() / 1000;
            if (isSuperAdmin) {
                abnormalAlarmReports = abnormalAlarmReportMapper.selectTopAbnormalAlarm(group, host, priority,
                    startTime,
                    deadLine);
            } else {
                abnormalAlarmReports = abnormalAlarmReportMapper.selectTopAbnormalAlarmByUser(group, host, priority,
                    startTime,
                    deadLine, userId);
            }
        } else if (interval == 30) {
            calendar.add(Calendar.DAY_OF_YEAR, -30);
            startTime = calendar.getTime().getTime() / 1000;
            if (isSuperAdmin) {
                abnormalAlarmReports = abnormalAlarmReportMapper.selectTopAbnormalAlarm(group, host, priority,
                    startTime,
                    deadLine);
            } else {
                abnormalAlarmReports = abnormalAlarmReportMapper.selectTopAbnormalAlarmByUser(group, host, priority,
                    startTime,
                    deadLine, userId);
            }
        } else if (interval == 6 || interval == 12) {
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            //Long endTime = calendar.getTime().getTime() / 1000 - 1;
            calendar.add(Calendar.MONTH, -interval);
            startTime = calendar.getTime().getTime() / 1000;
            if (isSuperAdmin) {
                abnormalAlarmReports = abnormalAlarmReportMapper.selectTopAbnormalAlarm(group, host, priority,
                    startTime,
                    deadLine);
            } else {
                abnormalAlarmReports = abnormalAlarmReportMapper.selectTopAbnormalAlarmByUser(group, host, priority,
                    startTime,
                    deadLine, userId);
            }
        } else if (interval == 3) {
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            //Long endTime = calendar.getTime().getTime() / 1000 - 1;
            calendar.add(Calendar.MONTH, -interval);
            startTime = calendar.getTime().getTime() / 1000;
            if (isSuperAdmin) {
                abnormalAlarmReports = abnormalAlarmReportMapper.selectTopAbnormalAlarm(group, host, priority,
                    startTime,
                    deadLine);
            } else {
                abnormalAlarmReports = abnormalAlarmReportMapper.selectTopAbnormalAlarmByUser(group, host, priority,
                    startTime,
                    deadLine, userId);
            }
        }
        int size = abnormalAlarmReports.size();
        if (size > 10) {
            size = 10;
        }
        abnormalAlarmReports = Util.reduceList(abnormalAlarmReports, size);
        //剔除掉"描述"字段中的{HOST.NAME}字符串
        for (int i = 0; i < size; i++) {
            if (abnormalAlarmReports.get(i).getDescription().contains("{HOST.NAME}")) {
                String description = abnormalAlarmReports.get(i).getDescription().replace("{HOST.NAME}",
                    abnormalAlarmReports.get(i).getHname());
                abnormalAlarmReports.get(i).setDescription(description);
            }
        }
        return abnormalAlarmReports;
    }

    /**
     * 创建pdf文件
     *
     * @param fileName
     *            文件名称
     * @param imgList
     *            图片集合
     * @param interval
     *            时间间隔
     * @param group
     *            主机组
     * @param host
     *            主机
     * @param priority
     *            严重等级
     * @param table
     *            表格
     */
    public static void createPDF(final String fileName, final List<String> imgList, final Integer interval,
        final String group, final String host,
        final String priority, 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);

                StringBuffer condition = new StringBuffer();
                if (interval == 7) {
                    condition.append("最近7天,");
                } else if (interval == 30) {
                    condition.append("最近30天,");
                } else if (interval == 6) {
                    condition.append("最近半年,");
                } else if (interval == 12) {
                    condition.append(startDate + "至" + endDate + " ");
                }

                if (group.equals("1 = 1")) {
                    condition.append("所有主机组,");
                } else { //类似 g.name = 'Linux Server'
                    condition.append(group.subSequence(group.indexOf("'") + 1, group.lastIndexOf("'")) + "组,");
                }

                if (host.equals("1 = 1")) {
                    condition.append("所有主机,");
                } else { //类似 h.host = 'sl'
                    condition.append(host.substring(host.indexOf("'") + 1, host.lastIndexOf("'")) + ",");
                }

                if (priority.equals("1 = 1")) {
                    condition.append("所有异常告警");
                } else {
                    String temp = priority.substring(priority.indexOf("=") + 2);
                    if (temp.equals("0")) {
                        condition.append("未分类");
                    } else if (temp.equals("1")) {
                        condition.append("信息");
                    } else if (temp.equals("2")) {
                        condition.append("警告");
                    } else if (temp.equals("3")) {
                        condition.append("一般严重");
                    } else if (temp.equals("4")) {
                        condition.append("严重");
                    } else {
                        condition.append("灾难");
                    }
                }

                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("经常出现的Top10个问题", font));
                document.add(new Chunk(""));
                document.add(table);
                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 abnormalAlarmReports
     *            异常警告报表
     * @return 表格
     */
    public static PdfPTable createTable(final List<AbnormalAlarmReport> abnormalAlarmReports) {
        PdfPTable table = null;
        try {
            table = new PdfPTable(4);
            table.setTotalWidth(850);
            table.setWidths(new int[] { 13, 18, 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));

            int size = abnormalAlarmReports.size();
            for (int i = 0; i < size; i++) {
                table.addCell(new Paragraph(abnormalAlarmReports.get(i).getHname(), font));
                table.addCell(new Paragraph(abnormalAlarmReports.get(i).getDescription(), font));
                if (abnormalAlarmReports.get(i).getPriority() == 0) {
                    table.addCell(new Paragraph("未分类", font));
                } else if (abnormalAlarmReports.get(i).getPriority() == 1) {
                    table.addCell(new Paragraph("信息", font));
                } else if (abnormalAlarmReports.get(i).getPriority() == 2) {
                    table.addCell(new Paragraph("警告", font));
                } else if (abnormalAlarmReports.get(i).getPriority() == 3) {
                    table.addCell(new Paragraph("一般严重", font));
                } else if (abnormalAlarmReports.get(i).getPriority() == 4) {
                    table.addCell(new Paragraph("严重", font));
                } else if (abnormalAlarmReports.get(i).getPriority() == 5) {
                    table.addCell(new Paragraph("灾难", font));
                }
                table.addCell(String.valueOf(abnormalAlarmReports.get(i).getCountPriority()));
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return table;
    }

    public List<AbnormalAlarmReport> selectTopAbnormalAlarmServiceByDate(final String group, final String host,
        final String priority,
        final Integer interval, 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<AbnormalAlarmReport> abnormalAlarmReports = new ArrayList<AbnormalAlarmReport>();
        Long deadLine = Util.getDeadLine(interval);

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        Long startTime = 0L;

        if (isSuperAdmin) {
            abnormalAlarmReports = abnormalAlarmReportMapper.selectTopAbnormalAlarm(group, host, priority, myStartDate,
                myEndDate);
        } else {
            abnormalAlarmReports = abnormalAlarmReportMapper.selectTopAbnormalAlarmByUser(group, host, priority,
                myStartDate,
                myEndDate, userId);
        }

        int size = abnormalAlarmReports.size();
        if (size > 10) {
            size = 10;
        }
        abnormalAlarmReports = Util.reduceList(abnormalAlarmReports, size);
        //剔除掉"描述"字段中的{HOST.NAME}字符串
        for (int i = 0; i < size; i++) {
            if (abnormalAlarmReports.get(i).getDescription().contains("{HOST.NAME}")) {
                String description = abnormalAlarmReports.get(i).getDescription().replace("{HOST.NAME}",
                    abnormalAlarmReports.get(i).getHname());
                abnormalAlarmReports.get(i).setDescription(description);
            }
        }
        return abnormalAlarmReports;
    }

    public Map countAbnormalAlarmServiceByDate(final String group, final String host, final String priority,
        final Integer interval, 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 = null;
        Date endDate1 = null;*/
        Date startDate1 = new Date();
        Date endDate1 = new Date();

        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();
        }
        // System.err.println("异常开始时间" + startDate1);
        //System.err.println("异常结束时间" + endDate1);
        final int a = (int) ((endDate1.getTime() - startDate1.getTime()) / (1000 * 3600 * 24) + 1);

        Map<String, List<AbnormalAlarmReport>> data = new LinkedHashMap<String, List<AbnormalAlarmReport>>();
        String groupBy = "";
        Long deadLine = Util.getDeadLine(interval);
        long timeFormat = 0L;
        timeFormat = Util.getTimeFormat(interval);

        if ((!group.contains("g.name")) && (!host.contains("h.host"))) {
            groupBy = "t.priority";
        } else if ((group.contains("g.name")) && (!host.contains("h.host"))) {
            groupBy = "g.name,t.priority";
        } else if ((group.contains("g.name")) && (host.contains("h.host"))) {
            groupBy = "g.name,h.host,t.priority";
        } else {
            groupBy = "h.host,t.priority";
        }
        List<AbnormalAlarmReport> abnormalAlarmReports = new ArrayList<>();
        SimpleDateFormat sdf = null;
        Long startTime = 0L;
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        long s1 = startDate1.getTime();
        long s2 = System.currentTimeMillis();
        int day = (int) ((s2 - s1) / 1000 / 60 / 60 / 24) + 1;

        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(startDate1);
        int startMonth = calendar2.get(Calendar.MONTH);

        Calendar calendar3 = Calendar.getInstance();
        calendar3.setTime(endDate1);
        int endMonth = calendar3.get(Calendar.MONTH);
        int totalmonth = endMonth - startMonth + 1;

        if (a <= 14) {
            timeFormat = 60 * 60 * 24L;
            sdf = new SimpleDateFormat("MM-dd");
            calendar.add(Calendar.DAY_OF_YEAR, -(day - 1));
            startTime = myStartDate;
            for (int i = 0; i < a; i++) {
                Date date = calendar.getTime();
                String s = sdf.format(date);
                calendar.add(Calendar.DAY_OF_YEAR, 1);

                if (isSuperAdmin) {
                    abnormalAlarmReports = abnormalAlarmReportMapper.countAbnormalAlarm(group, host, priority,
                        startTime + i * timeFormat, startTime + (i + 1) * timeFormat, groupBy);
                } else {
                    abnormalAlarmReports = abnormalAlarmReportMapper.countAbnormalAlarmByUser(group, host, priority,
                        startTime + i * timeFormat, startTime + (i + 1) * timeFormat, userId, groupBy);
                }
                data.put(s, abnormalAlarmReports);
            }
        } else if (15 <= a && a <= 60) {

            timeFormat = 60 * 60 * 24L;
            sdf = new SimpleDateFormat("MM-dd");
            calendar.add(Calendar.DAY_OF_YEAR, -(day - 1));
            //startTime = calendar.getTime().getTime() / 1000;
            startTime = myStartDate;
            Date date = calendar.getTime();
            String s = sdf.format(date);
            int currentDay = calendar.get(Calendar.DAY_OF_WEEK);

            Date date2 = null;
            String ss = "";

            if (currentDay == 1) {
                calendar.add(Calendar.DAY_OF_YEAR, 2);
                date2 = calendar.getTime();
                ss = sdf.format(date2);
                if (isSuperAdmin) {
                    abnormalAlarmReports = abnormalAlarmReportMapper.countAbnormalAlarm(group, host, priority,
                        startTime,
                        startTime + timeFormat, groupBy);
                } else {
                    abnormalAlarmReports = abnormalAlarmReportMapper.countAbnormalAlarmByUser(group, host, priority,
                        startTime,
                        startTime + timeFormat, userId, groupBy);
                }
                startTime = startTime + timeFormat;
            } else {
                calendar.add(Calendar.DAY_OF_YEAR, 9 - currentDay);

                date2 = calendar.getTime();

                ss = sdf.format(date2);
                if (isSuperAdmin) {
                    abnormalAlarmReports = abnormalAlarmReportMapper.countAbnormalAlarm(group, host, priority,
                        startTime,
                        startTime + (9 - currentDay) * timeFormat, groupBy);
                } else {
                    abnormalAlarmReports = abnormalAlarmReportMapper.countAbnormalAlarmByUser(group, host, priority,
                        startTime,
                        startTime + (9 - currentDay) * timeFormat, userId, groupBy);
                }
                startTime = startTime + (9 - currentDay) * timeFormat;
            }
            data.put(s + "至" + ss, abnormalAlarmReports);
            int j = a / 7;
            for (int i = 0; i < j - 1; i++) {
                calendar.add(Calendar.DAY_OF_YEAR, 1);
                date = calendar.getTime();
                s = sdf.format(date);

                calendar.add(Calendar.DAY_OF_YEAR, 6);
                // calendar.add(Calendar.DAY_OF_YEAR,1);

                date2 = calendar.getTime();

                ss = sdf.format(date2);
                if (date2.after(endDate1)) {
                    ss = sdf.format(endDate1);
                }
                if (isSuperAdmin) {
                    abnormalAlarmReports = abnormalAlarmReportMapper.countAbnormalAlarm(group, host, priority,
                        startTime + i * 7 * timeFormat, startTime + (i + 1) * 7 * timeFormat, groupBy);
                } else {
                    abnormalAlarmReports = abnormalAlarmReportMapper.countAbnormalAlarmByUser(group, host, priority,
                        startTime + i * 7 * timeFormat, startTime + (i + 1) * 7 * timeFormat, userId, groupBy);
                }
                data.put(s + "至" + ss, abnormalAlarmReports);
            }

            String theEndDate = sdf.format(endDate1);

            if (!ss.equals(theEndDate)) {
                calendar.add(Calendar.DAY_OF_YEAR, 1);
                date = calendar.getTime();

                s = sdf.format(date);

                if (isSuperAdmin) {
                    abnormalAlarmReports = abnormalAlarmReportMapper.countAbnormalAlarm(group, host, priority,
                        startTime + (j - 1) * 7 * timeFormat, myEndDate, groupBy);
                } else {
                    abnormalAlarmReports = abnormalAlarmReportMapper.countAbnormalAlarmByUser(group, host, priority,
                        startTime + (j - 1) * 7 * timeFormat, myEndDate, userId, groupBy);
                }

                String yesterday = Util.getDate(myEndDate);
                ss = yesterday.substring(yesterday.indexOf("-") + 1, 10);
                data.put(s + "至" + ss, abnormalAlarmReports);
            }

        } else if (60 < a) {
            timeFormat = 60 * 60 * 24L;
            sdf = new SimpleDateFormat("MM-dd");
            calendar.add(Calendar.DAY_OF_YEAR, -(day - 1));
            //startTime = calendar.getTime().getTime() / 1000;
            startTime = myStartDate;
            Date date = calendar.getTime();
            String s = sdf.format(date);
            int currentDay = calendar.get(Calendar.DAY_OF_MONTH);

            Date date2 = null;
            String ss = "";
            int flag = 0;
            if (currentDay == 1) {
                flag = 1;
            } else {
                //取得当月天数
                Calendar c = Calendar.getInstance();
                c.setTime(startDate1);

                int maxDate = c.getActualMaximum(Calendar.DAY_OF_MONTH);

                calendar.add(Calendar.DAY_OF_YEAR, maxDate - currentDay);
                date2 = calendar.getTime();
                ss = sdf.format(date2);
                if (maxDate - currentDay == 0) {
                    maxDate = maxDate + 1;
                }
                if (isSuperAdmin) {
                    abnormalAlarmReports = abnormalAlarmReportMapper.countAbnormalAlarm(group, host, priority,
                        startTime,
                        startTime + (maxDate - currentDay) * timeFormat, groupBy);
                } else {
                    abnormalAlarmReports = abnormalAlarmReportMapper.countAbnormalAlarmByUser(group, host, priority,
                        startTime,
                        startTime + (maxDate - currentDay) * timeFormat, userId, groupBy);
                }
                startTime = startTime + (maxDate - currentDay) * timeFormat;
                data.put(s + "至" + ss, abnormalAlarmReports);
            }
            int j = a / 30;
            if (flag == 0) {
                totalmonth = totalmonth;
            } else {
                totalmonth = totalmonth + 1;

            }
            for (int i = 0; i < totalmonth - 2; i++) {
                calendar.add(Calendar.DATE, 1);
                int currentYear = calendar.get(Calendar.YEAR);
                int currentMonth = calendar.get(Calendar.MONTH);
                int days = Util.getMonthLastDay(currentYear, currentMonth + 1);
                date = calendar.getTime();
                startTime = date.getTime() / 1000;
                s = sdf.format(date);
                calendar.add(Calendar.DATE, days - 1);
                currentMonth = calendar.get(Calendar.MONTH);

                date2 = calendar.getTime();
                ss = sdf.format(date2);

                if (isSuperAdmin) {
                    abnormalAlarmReports = abnormalAlarmReportMapper.countAbnormalAlarm(group, host, priority,
                        startTime, startTime + 1 * (days - 1) * timeFormat, groupBy);
                } else {
                    abnormalAlarmReports = abnormalAlarmReportMapper.countAbnormalAlarmByUser(group, host, priority,
                        startTime, startTime + 1 * (days - 1) * timeFormat, userId, groupBy);
                }

                data.put(currentMonth + 1 + "月", abnormalAlarmReports);
            }
            calendar.add(Calendar.DATE, 1);
            date = calendar.getTime();
            long myStartDate2 = date.getTime() / 1000;
            int month = calendar.get(Calendar.MONTH) + 1;
            s = month + "月1日";
            if (isSuperAdmin) {
                abnormalAlarmReports = abnormalAlarmReportMapper.countAbnormalAlarm(group, host, priority,
                    myStartDate2, myEndDate, groupBy);
            } else {
                abnormalAlarmReports = abnormalAlarmReportMapper.countAbnormalAlarmByUser(group, host, priority,
                    myStartDate2, myEndDate, userId, groupBy);
            }
            String yesterday = Util.getDate(myEndDate);
            ss = yesterday.substring(yesterday.indexOf("-") + 1, 10);
            data.put(s + "至" + ss, abnormalAlarmReports);

        }
        return data;
    }
}