/**
 * <p>Copyright:Copyright(c) 2017</p>
 * <p>Company:上海中信信息发展股份有限公司</p>
 * <p>包名:com.cesgroup.cesmonitor.service</p>
 * <p>文件名:ReportCommonService.java</p>
 * <p>创建时间:2017年5月24日 上午10:00:02</p>
 * <p>作者:sl</p>
 */

package com.cesgroup.cesmonitor.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

import com.cesgroup.cesmonitor.common.Util;
import com.cesgroup.cesmonitor.dao.ReportCommonMapper;
import com.zabbix4j.host.HostObject;
import com.zabbix4j.hostgroup.HostgroupObject;

/**
 *
 * @author sl
 * @version 1.0.0 2017年5月24日
 */
@Service("reportCommonService")
public class ReportCommonService {

    @Autowired
    private ReportCommonMapper reportCommonMapper;

    /**
     * 查询所有主机组
     *
     * @return 所有主机组
     */
    public List<HostgroupObject> selectAllHostGroupsService() {
        LoginUser user = Util.getLoginUser();
        Integer userId = user.getUserid();
        List<HostgroupObject> hostGroupObjects = new ArrayList<>();
        if (user.isSuperAdmin()) {
            hostGroupObjects = reportCommonMapper.selectAllHostGroups();
        } else {
            hostGroupObjects = reportCommonMapper.selectAllHostGroupsByUser(userId);
        }

        return hostGroupObjects;
    }

    /**
     * 查询所有的主机
     *
     * @return 所有主机
     */
    public List<HostObject> selectAllHostsService() {
        List<HostObject> hosts = new ArrayList<>();
        hosts = reportCommonMapper.selectAllHosts();
        return hosts;
    }

    /**
     * 查询出特定主机组所属的所有主机
     *
     * @param group
     *            主机组名称
     * @return 特点主机组所属的所有主机
     */
    public List<HostObject> selectHostByHostGroupNameService(final String group) {
        List<HostObject> hosts = new ArrayList<>();
        hosts = reportCommonMapper.selectHostByHostGroupName(group);
        return hosts;
    }

    /**
     * 下载功能
     *
     * @param filename
     *            文件名称
     * @param request
     *            请求
     * @param response
     *            响应
     */
    public void download(final String filename, final HttpServletRequest request, final HttpServletResponse response) {
        try {
            ServletContext context = request.getSession().getServletContext();
            //设置文件MIME类型
            response.setContentType(context.getMimeType(filename));
            response.setContentType("application/pdf;charset=utf-8");
            //设置Content-Disposition
            response.setHeader("Content-Disposition",
                "attachment;filename=" + new String(filename.getBytes("UTF-8"), "ISO-8859-1"));
            //读取目标文件，通过response将目标文件写到客户端
            String realPath = context.getRealPath("/exportPdf/");
            File file = new File(realPath);
            if (!file.exists()) {
                file.mkdir();
            }
            //获取目标文件的绝对路径
            String fullFileName = context.getRealPath("/exportPdf/" + filename);
            //读取文件
            InputStream in = new FileInputStream(fullFileName);
            OutputStream out = response.getOutputStream();
            //写文件
            int b;
            while ((b = in.read()) != -1) {
                out.write(b);
            }
            out.close();
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public List<String> gettimeslot(final Integer interval) {

        SimpleDateFormat myFmt = new SimpleDateFormat("yyyy-MM-dd");

        Date deadLine = Calendar.getInstance().getTime();
        String deadData = myFmt.format(deadLine);

        String startTime = Util.getStartTime(interval);
        List<String> list = new ArrayList<String>();
        list.add(startTime);
        list.add(deadData);

        return list;
    }
}
