package com.izouma.zhirongip;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.izouma.zhirongip.dto.gen.GenCode;
import com.izouma.zhirongip.utils.FileUtils;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.ResourceUtils;

import java.io.*;
import java.nio.file.Paths;
import java.util.*;

@RunWith(SpringRunner.class)
@SpringBootTest
public class GenCodeTest {
    @Autowired
    private Configuration cfg;

    @Test
    public void getGenCodeList() {

        try {
            BufferedReader reader = null;

            List<GenCode> genCodeList = new ArrayList<>();
            File file = ResourceUtils.getFile("classpath:genjson");
            File[] fs = file.listFiles();    //遍历path下的文件和目录，放在File数组中
            for (File f : fs) {                    //遍历File[]数组
                if (!f.isDirectory()) {//若非目录(即文件)，则打印
                    System.out.println(f);
                    reader = new BufferedReader(new FileReader(f));
                    Gson gson = new GsonBuilder().create();

                    GenCode genCode = gson.fromJson(reader, GenCode.class);
                    genCodeList.add(genCode);

                }
            }

            System.out.println(genCodeList);

        } catch (Exception e) {
            System.err.println("获取文件列表失败！");
            e.printStackTrace();
        }

    }


    @Test
    public void getGenCodeOne() {

        try {
            BufferedReader reader = null;
            String genCodeName = "test.json";
            GenCode genCode = new GenCode();
            File file = ResourceUtils.getFile("classpath:genjson/" + genCodeName);
            if (!file.isDirectory()) {//若非目录(即文件)，则打印
                System.out.println(file);
                reader = new BufferedReader(new FileReader(file));
                Gson gson = new GsonBuilder().create();

                genCode = gson.fromJson(reader, GenCode.class);

            }

            System.out.println(genCode);

        } catch (Exception e) {
            System.err.println("获取文件列表失败！");
            e.printStackTrace();
        }

    }

    @Test
    public void genCodeSave() {

        try {
            String genCodeStr = "{\"tableName\":\"app_version1\",\"className\":\"app_version1\",\"remark\":\"app版本\",\"genTable\":true,\"genClass\":true,\"genList\":true,\"genForm\":true,\"genRouter\":true,\"javaPath\":\"D:\\\\projects\\\\shouyoudianjing-b\\\\/src/main/java/com\\\\izouma\\\\awesomeadmin\",\"viewPath\":\"D:\\\\projects\\\\shouyoudianjing-b\\\\/src/main/vue/src/pages\",\"routerPath\":\"D:\\\\projects\\\\shouyoudianjing-b\\\\/src/main/vue/src/router\",\"dataBaseType\":\"Mysql\",\"fields\":[{\"name\":\"id\",\"remark\":\"Id\",\"jdbcType\":\"int\",\"notNull\":true,\"primaryKey\":true,\"autoIncrease\":true,\"showInList\":false,\"showInForm\":false,\"searchable\":false,\"sortable\":true},{\"name\":\"del_flag\",\"remark\":\"删除标识\",\"jdbcType\":\"char\",\"length\":1,\"defaultValue\":\"N\",\"notNull\":true,\"primaryKey\":false,\"autoIncrease\":false,\"showInList\":false,\"showInForm\":false,\"searchable\":false,\"sortable\":false},{\"name\":\"update_time\",\"remark\":\"更新时间\",\"jdbcType\":\"timestamp\",\"length\":0,\"defaultValue\":\"CURRENT_TIMESTAMP\",\"notNull\":true,\"primaryKey\":false,\"autoIncrease\":false,\"showInList\":false,\"showInForm\":false,\"searchable\":false,\"sortable\":false},{\"name\":\"update_user\",\"remark\":\"更新人\",\"jdbcType\":\"varchar\",\"length\":45,\"defaultValue\":\"\",\"notNull\":false,\"primaryKey\":false,\"autoIncrease\":false,\"showInList\":false,\"showInForm\":false,\"searchable\":false,\"sortable\":false},{\"name\":\"create_time\",\"remark\":\"创建时间\",\"jdbcType\":\"timestamp\",\"length\":0,\"defaultValue\":\"CURRENT_TIMESTAMP\",\"notNull\":true,\"primaryKey\":false,\"autoIncrease\":false,\"showInList\":false,\"showInForm\":false,\"searchable\":false,\"sortable\":false},{\"name\":\"create_user\",\"remark\":\"创建人\",\"jdbcType\":\"varchar\",\"length\":45,\"defaultValue\":\"\",\"notNull\":false,\"primaryKey\":false,\"autoIncrease\":false,\"showInList\":false,\"showInForm\":false,\"searchable\":false,\"sortable\":false},{\"name\":\"android_version\",\"remark\":\"安卓版本\",\"jdbcType\":\"varchar\",\"notNull\":false,\"primaryKey\":false,\"autoIncrease\":false,\"showInList\":true,\"showInForm\":true,\"searchable\":true,\"sortable\":true,\"formType\":\"singleLineText\"},{\"name\":\"android_url\",\"remark\":\"安卓地址\",\"jdbcType\":\"varchar\",\"notNull\":false,\"primaryKey\":false,\"autoIncrease\":false,\"showInList\":true,\"showInForm\":true,\"searchable\":true,\"sortable\":true,\"formType\":\"singleLineText\"},{\"name\":\"android_size\",\"remark\":\"安卓大小\",\"jdbcType\":\"varchar\",\"notNull\":false,\"primaryKey\":false,\"autoIncrease\":false,\"showInList\":true,\"showInForm\":true,\"searchable\":true,\"sortable\":true,\"formType\":\"singleLineText\"},{\"name\":\"ios_version\",\"remark\":\"ios版本\",\"jdbcType\":\"varchar\",\"notNull\":false,\"primaryKey\":false,\"autoIncrease\":false,\"showInList\":true,\"showInForm\":true,\"searchable\":true,\"sortable\":true,\"formType\":\"singleLineText\"},{\"name\":\"ios_url\",\"remark\":\"ios地址\",\"jdbcType\":\"varchar\",\"notNull\":false,\"primaryKey\":false,\"autoIncrease\":false,\"showInList\":true,\"showInForm\":true,\"searchable\":true,\"sortable\":true,\"formType\":\"singleLineText\"},{\"name\":\"ios_size\",\"remark\":\"ios大小\",\"jdbcType\":\"varchar\",\"notNull\":false,\"primaryKey\":false,\"autoIncrease\":false,\"showInList\":true,\"showInForm\":true,\"searchable\":true,\"sortable\":true,\"formType\":\"singleLineText\"}],\"readTable\":false,\"dataSourceCode\":\"dataSource\",\"genJson\":\"\",\"typeFlag\":\"电竞互动\",\"subtables\":[],\"update\":false}";
            String rootPath = "D:/projects/zhumj/src/main/resources/genjson/";
            String genCodeName = "test3.json";
            File file = new File(rootPath + genCodeName);

            file.createNewFile();

            BufferedWriter output = new BufferedWriter(new FileWriter(file));
            output.write(genCodeStr);
            output.close();

        } catch (Exception e) {
            System.err.println("获取文件列表失败！");
            e.printStackTrace();
        }

    }


    @Test
    public void testFreemarker() throws IOException, TemplateException {
        Template temp = cfg.getTemplate("ControllerTemplate.ftl");
        Map data = new HashMap();
        String json = FileUtils.readFile(Paths.get(System.getProperty("user.dir"), "src", "main", "resources", "genjson", "TestGenCode.json").toString());
        Gson gson = new Gson();
        GenCode model = gson.fromJson(json, GenCode.class);
        data.put("model", model);
        data.put("imports", new ArrayList<>());
        Writer out = new OutputStreamWriter(System.out);
        temp.process(data, out);

    }
}