package com.sunxung.factoring.component.util;

import com.sunxung.factoring.dict.impl.FileModuleDict;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.core.io.ClassPathResource;

import java.io.*;
import java.util.Map;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * @className:XMlToDoc
 * @description:基于freemarker ，xdocreport生成word。解决freemarkder 处理 docx后，生成的是xml问题
 * @date:2018年8月6日 上午9:43:55
 */
public class XMlToDoc {

    private static Logger logger = LogManager.getLogger(XMlToDoc.class);

    @SuppressWarnings("deprecation")
    public File makePurchContract(Map<String, Object> dataMap, String contractName, String path, String zipName)
            throws Exception {
        String dataPath = FileUtil.getAbsolutePath("data", ".xml", FileModuleDict.ChildEnum.PURCHASE_CONTRACT_FILE);
        String fzPath = FileUtil.getAbsolutePath(contractName, ".docx", FileModuleDict.ChildEnum.PURCHASE_CONTRACT_FILE);
        File docxFile = null;
        /** 初始化配置文件 **/
        Configuration configuration = new Configuration();
        configuration.setDefaultEncoding("UTF-8");
        /** 加载文件 **/
        configuration.setClassForTemplateLoading(getClass(), path);
        /** 加载模板 **/
        Template template = configuration.getTemplate("document.xml");
        /** 指定输出word文件的路径 **/
        File docFile = new File(dataPath);
        if (!docFile.getParentFile().exists()) {
            docFile.getParentFile().mkdirs();
        }
        docFile.createNewFile();
        FileOutputStream fos = new FileOutputStream(docFile);
        Writer out = new BufferedWriter(new OutputStreamWriter(fos), 10240);
        template.process(dataMap, out);
        if (out != null) {
            out.close();
        }
        try {
            docxFile = new File(fzPath);
            if (!docxFile.getParentFile().exists()) {
                docxFile.getParentFile().mkdirs();
            }
            docxFile.createNewFile();
            InputStream inputStream = new ClassPathResource(path + zipName).getInputStream();
            ZipInputStream zipInputStream = ZipUtils.wrapZipInputStream(inputStream);
            ZipOutputStream zipOutputStream = ZipUtils.wrapZipOutputStream(new FileOutputStream(docxFile));
            String itemname = "word/document.xml";
            ZipUtils.replaceItem(zipInputStream, zipOutputStream, itemname, new FileInputStream(new File(dataPath)));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("导出word的出错" + e.getMessage());
        }
        return docxFile;
    }

    public File makeAcceptanceSheet(Map<String, Object> dataMap, String acceptanceNumber, String path, String zipName) throws Exception {
        String dataPath = FileUtil.getAbsolutePath("data", ".xml", FileModuleDict.ChildEnum.ACCEPTANCE_FILE);
        String docxPath = FileUtil.getAbsolutePath(acceptanceNumber, ".docx", FileModuleDict.ChildEnum.ACCEPTANCE_FILE);
        File docxFile = null;
        /** 初始化配置文件 **/
        Configuration configuration = new Configuration();
        configuration.setDefaultEncoding("UTF-8");
        /** 加载文件 **/
        configuration.setClassForTemplateLoading(getClass(), path);
        /** 加载模板 **/
        Template template = configuration.getTemplate("document.xml");
        /** 指定输出word文件的路径 **/
        File docFile = new File(dataPath);
        if (!docFile.getParentFile().exists()) {
            docFile.getParentFile().mkdirs();
        }
        docFile.createNewFile();
        FileOutputStream fos = new FileOutputStream(docFile);
        Writer out = new BufferedWriter(new OutputStreamWriter(fos), 10240);
        template.process(dataMap, out);
        if (out != null) {
            out.close();
        }
        try {
            docxFile = new File(docxPath);
            if (!docxFile.getParentFile().exists()) {
                docxFile.getParentFile().mkdirs();
            }
            docxFile.createNewFile();
            // File zipFile = new ClassPathResource(path + zipName).getFile();
            InputStream inputStream = new ClassPathResource(path + zipName).getInputStream();
            ZipInputStream zipInputStream = ZipUtils.wrapZipInputStream(inputStream);
            ZipOutputStream zipOutputStream = ZipUtils.wrapZipOutputStream(new FileOutputStream(docxFile));
            String itemname = "word/document.xml";
            ZipUtils.replaceItem(zipInputStream, zipOutputStream, itemname, new FileInputStream(new File(dataPath)));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("导出word的出错" + e.getMessage());
        }
        return docxFile;
    }
//resources/ftl/bill.export/document.xml
    public File makeBill(Map<String, Object> dataMap, String fileName, String path, String zipName) throws IOException, TemplateException {
        String dataPath = FileUtil.getAbsolutePath("data", ".xml", FileModuleDict.ChildEnum.SETTLEMENT_UPSTREAM_UNSIGNED_FILE);
        String docxPath = FileUtil.getAbsolutePath(fileName, ".docx", FileModuleDict.ChildEnum.SETTLEMENT_UPSTREAM_UNSIGNED_FILE);
        String pdfPath = FileUtil.getAbsolutePath(fileName, ".pdf", FileModuleDict.ChildEnum.SETTLEMENT_UPSTREAM_UNSIGNED_FILE);
        File docxFile = null;
        /** 初始化配置文件 **/
        Configuration configuration = new Configuration();
        configuration.setDefaultEncoding("UTF-8");
        /** 加载文件 **/
        configuration.setClassForTemplateLoading(getClass(), path);
        /** 加载模板 **/
        Template template = configuration.getTemplate("document.xml");
        /** 指定输出word文件的路径 **/
        File docFile = new File(dataPath);
        if (!docFile.getParentFile().exists()) {
            docFile.getParentFile().mkdirs();
        }
        docFile.createNewFile();
        FileOutputStream fos = new FileOutputStream(docFile);
        Writer out = new BufferedWriter(new OutputStreamWriter(fos), 10240);
        template.process(dataMap, out);
        if (out != null) {
            out.close();
        }
        try {
            docxFile = new File(docxPath);
            if (!docxFile.getParentFile().exists()) {
                docxFile.getParentFile().mkdirs();
            }
            docxFile.createNewFile();
            // File zipFile = new ClassPathResource(path + zipName).getFile();
            InputStream inputStream = new ClassPathResource(path + zipName).getInputStream();
            ZipInputStream zipInputStream = ZipUtils.wrapZipInputStream(inputStream);
            ZipOutputStream zipOutputStream = ZipUtils.wrapZipOutputStream(new FileOutputStream(docxFile));
            String itemname = "word/document.xml";
            ZipUtils.replaceItem(zipInputStream, zipOutputStream, itemname, new FileInputStream(new File(dataPath)));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("导出word的出错" + e.getMessage());
        }
        return docxFile;
    }
}
