package com.jiayue.passback.util;

import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.util.ResourceUtils;

import java.io.*;
import java.net.URLDecoder;


public class FreemarkerUtil {

   public static void genFile(Object data,String outPath,String fileName) throws Exception {
       //1.创建freemarker的配置对象
       Configuration configuration = new Configuration(Configuration.getVersion());
       //2.设置模板文件所在目录
       String templateFilePath = getResourceBasePath() +File.separator+ "template";

       configuration.setDirectoryForTemplateLoading(new File(templateFilePath));
       //3.设置字符集
       configuration.setDefaultEncoding("utf-8");
       //4数字格式去除逗号
       configuration.setNumberFormat("#");
       //5.加载模板
       Template template = configuration.getTemplate("template.ftl");
       //6.准备模板文件中所需的数据，一般通过map构造
//       Map<String,String> map = new HashMap<>();
//       map.put("name","panghl");
//       map.put("message","你好啊！！！freemarker");
       //7.创建Writer对象,用于输出静态文件
       Writer out = new FileWriter(new File(outPath+File.separator+fileName));
       //8.输出文件
       template.process(data,out);
       //9.关闭流
       out.close();
   }

    public static void genLlkyFile(Object data,String outPath,String fileName) throws Exception {
        //1.创建freemarker的配置对象
        Configuration configuration = new Configuration(Configuration.getVersion());
        //2.设置模板文件所在目录
        String templateFilePath = getResourceBasePath() +File.separator+ "template";

        configuration.setDirectoryForTemplateLoading(new File(templateFilePath));
        //3.设置字符集
        configuration.setDefaultEncoding("utf-8");
        //4数字格式去除逗号
        configuration.setNumberFormat("#");
        //5.加载模板
        Template template = configuration.getTemplate("templateLlKy.ftl");
        //6.准备模板文件中所需的数据，一般通过map构造
//       Map<String,String> map = new HashMap<>();
//       map.put("name","panghl");
//       map.put("message","你好啊！！！freemarker");
        //7.创建Writer对象,用于输出静态文件
        Writer out = new FileWriter(new File(outPath+File.separator+fileName));
        //8.输出文件
        template.process(data,out);
        //9.关闭流
        out.close();
    }


    /**
     * 获取项目根路径
     *
     * @return
     */
    public static String getResourceBasePath() {
        // 获取跟目录
        File path = null;
        try {
            path = new File(ResourceUtils.getURL("classpath:").getPath());

        } catch (FileNotFoundException e) {
            // nothing to do
        }
        if (path == null || !path.exists()) {
            path = new File("");
        }

        String pathStr = path.getAbsolutePath();
        try {
            pathStr = URLDecoder.decode(pathStr, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        // 如果是在eclipse中运行，则和target同级目录,如果是jar部署到服务器，则默认和jar包同级
//    pathStr = pathStr.replace("\\target\\classes", "");

        return pathStr;
    }

}
