package com.dayou.report;

import com.dayou.dto.RiskReportDTO;
import com.deepoove.poi.XWPFTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;
import java.io.*;

/**
 * 类说明：
 *
 * @author: wucl
 * @since: 2023/8/9
 * created with IntelliJ IDEA.
 */
@Slf4j
@Component
public class GenerateReportEngine implements ResourceLoaderAware {

    private ResourceLoader resourceLoader;

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    public void loadBaseTemplate(RiskReportDTO riskReportDTO) throws IOException {
        //转成PDF
//        OutputStream outPDF = Files.newOutputStream(Paths.get("C:\\Users\\Administrator\\Desktop\\output\\xinwen.pdf"));
//        PdfOptions pdfOptions = PdfOptions.create();
//        InputStream inputStream1 = Files.newInputStream(Paths.get("C:\\Users\\Administrator\\Desktop\\output\\xinwen.docx"));
//        XWPFDocument document = new XWPFDocument(inputStream1);
//        PdfConverter.getInstance().convert(document,outPDF,pdfOptions);
//        outPDF.close();
//        inputStream1.close();
        Resource resource = resourceLoader.getResource("classpath:report_template/land/risk/base.docx");
        try(InputStream inputStream = resource.getInputStream()){
            XWPFTemplate template = XWPFTemplate.compile(inputStream);
            template.render(riskReportDTO);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            template.writeAndClose(byteArrayOutputStream);
            FileOutputStream out = new FileOutputStream(new File("C:\\Users\\Administrator\\Desktop\\output\\result.docx"));
            byteArrayOutputStream.writeTo(out);
        }
    }
}
