package com.willalp.common.file;

import cn.hutool.core.io.resource.ClassPathResource;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.HtmlExporter;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleHtmlExporterOutput;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;


@Service
public class ReportExportFileSer {

    public void reportHtmlForStream(List<Map<String, Object>> list, Map<String, Object> params, String jasperName, HttpServletResponse response) {
        JRDataSource jrBeanCollectionDataSource = new JRBeanCollectionDataSource(list);
        response.setContentType("text/html;charset=UTF-8");

        try {
            JasperReport jasperReport = (JasperReport)JRLoader.loadObject(this.getFileInputStream(jasperName));
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, jrBeanCollectionDataSource);
            HtmlExporter exporter = new HtmlExporter();
            exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
            exporter.setExporterOutput(new SimpleHtmlExporterOutput(response.getOutputStream()));
            exporter.exportReport();
        } catch (IOException | JRException var9) {
        }

    }

    public InputStream getFileInputStream(String fileName) {
        ClassPathResource classPathResource = new ClassPathResource(fileName);
        return classPathResource.getStream();
    }

}
