package org.example.mybatisplus.tools;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class FreemarkerUtil {

    public static void main(String[] args) {
        try {
            // 准备数据模型
            Map<String, Object> data = new HashMap<>();

            Map<String, String> item = new HashMap<>();
            item.put("entName", "项目 66");
            item.put("value", "值 66" );
            data.put("data", item);
            item.put("name", "项目33");
            item.put("value", "值44");
            data.put("item", item);
            // 图片转Base64
            String imagePath = "G:\\Code\\new\\fxy-tool\\src\\main\\resources\\pic1.png";
            File imageFile = new File(imagePath);
            byte[] imageBytes = Files.readAllBytes(imageFile.toPath());
            String imageBase64 = Base64.getEncoder().encodeToString(imageBytes);
            Map<String, String> image = new HashMap<>();
            image.put("stglqk", imageBase64);
            data.put("image", image);
            // 配置FreeMarker
            Configuration cfg = new Configuration(Configuration.VERSION_2_3_34);
            cfg.setClassForTemplateLoading(FreemarkerUtil.class, "/");
            cfg.setDefaultEncoding("UTF-8");

            // 处理模板
            Template template = cfg.getTemplate("/jijinhui.xml");
            StringWriter writer = new StringWriter();
            template.process(data, writer);

            // 输出到文件
            File output = new File("output.docx");
            Files.write(output.toPath(), writer.toString().getBytes("UTF-8"));

            System.out.println("导出成功！");
        } catch (IOException | TemplateException e) {
            e.printStackTrace();
        }
    }
}