package xyz.wbsite.dbtool.javafx.manger.callable;

import xyz.wbsite.dbtool.javafx.manger.FreeMarkerManager;
import xyz.wbsite.dbtool.javafx.manger.ManagerFactory;
import xyz.wbsite.dbtool.javafx.manger.XmlManager;
import xyz.wbsite.dbtool.javafx.po.AbstractDBmapper;
import xyz.wbsite.dbtool.javafx.po.DataBase;
import xyz.wbsite.dbtool.javafx.po.Module;
import xyz.wbsite.dbtool.javafx.po.MySQLDBmapper;
import xyz.wbsite.dbtool.javafx.po.OracleDBmapper;
import xyz.wbsite.dbtool.javafx.po.Project;
import xyz.wbsite.dbtool.javafx.po.SQLiteDBmapper;
import xyz.wbsite.dbtool.javafx.po.Table;
import xyz.wbsite.dbtool.javafx.tool.Tool;
import xyz.wbsite.dbtool.web.frame.utils.FileUtil;
import xyz.wbsite.dbtool.web.frame.utils.ResourceUtil;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.concurrent.Callable;

public class SpringBootServiceCallable implements Callable {

    private File root;
    private Project project;
    private AbstractDBmapper dBmapper;
    private FreeMarkerManager freeMarkerManager;

    public SpringBootServiceCallable(Project project, File root) {
        this.root = root;
        this.project = project;
        this.freeMarkerManager = ManagerFactory.getFreeMarkerManager();
        // 根据配置获取数据库处理器
        if (project.getDatabase().name().equals(DataBase.MySQL.name())) {
            this.dBmapper = new MySQLDBmapper();
        } else if (project.getDatabase().name().equals(DataBase.Oracle.name())) {
            this.dBmapper = new OracleDBmapper();
        } else if (project.getDatabase().name().equals(DataBase.SQLite.name())) {
            this.dBmapper = new SQLiteDBmapper();
        } else {
            this.dBmapper = new MySQLDBmapper();
        }
    }

    public Boolean call() throws Exception {
        // 创建项目文件夹
        File projectDir = Tool.createPath(root, project.getProjectName());
        // 清空内容
        Tool.clear(projectDir);
        // 生成java目录
        File javaDir = Tool.createPath(projectDir.getAbsolutePath(), "src", "main", "java");
        // 生成资源目录
        File resourcesDir = Tool.createPath(projectDir.getAbsolutePath(), "src", "main", "resources");
        // 生成域名目录
        File domainDir = javaDir;
        for (String s : project.getProjectBasePackage().split("\\.")) {
            domainDir = Tool.createPath(domainDir.getAbsolutePath(), s);
        }
        // 生成单元测试java目录
        File testJavaDir = Tool.createPath(projectDir.getAbsolutePath(), "src", "test", "java");
        File testDomainDir = testJavaDir;
        for (String s : project.getProjectBasePackage().split("\\.")) {
            testDomainDir = Tool.createPath(testDomainDir.getAbsolutePath(), s);
        }
        // 生成单元测试resources目录
        File testResourcesDir = Tool.createPath(projectDir.getAbsolutePath(), "src", "test", "resources");

        // 生成POM
        generatePom(projectDir, project);

        {//生成java文件
            // 生成 Config
            System.out.println("生成模块:config");
            generateConfig(Tool.createPath(domainDir.getAbsolutePath(), "config"), project);


            {// 模块
                for (Module module : project.getModules()) {// 业务模块
                    module.setProjectAuthor(project.getProjectAuthor());
                    module.setProjectBasePackage(project.getProjectBasePackage());
                    module.setProjectName(project.getProjectName());

                    File moduleDir = Tool.createPath(domainDir.getAbsolutePath(), "module", module.getModuleName());

                    System.out.println("生成模块:Entity");
                    generateEntity(Tool.createPath(moduleDir.getAbsolutePath(), "ent"), module);
                    System.out.println("生成模块:Mapper");
                    generateMapper(Tool.createPath(moduleDir.getAbsolutePath(), "mpr"), module);
                    System.out.println("生成模块:Manager");
                    generateManager(Tool.createPath(moduleDir.getAbsolutePath(), "mgr"), module);
                    System.out.println("生成模块:Request");
                    generateRequest(Tool.createPath(moduleDir.getAbsolutePath(), "req"), module);
                    System.out.println("生成模块:Response");
                    generateResponse(Tool.createPath(moduleDir.getAbsolutePath(), "rsp"), module);
                }
            }

            // frame等内容
            System.out.println("生成模块:frame");
            generateFrame(Tool.createPath(domainDir.getAbsolutePath(), "frame"), project);

            // Task
            System.out.println("生成模块:task");
            generateTask(Tool.createPath(domainDir.getAbsolutePath(), "task"), project);

            // Application
            System.out.println("生成模块:Application");
            generateApplication(Tool.createPath(domainDir.getAbsolutePath()), project);
        }

        {//生成resources文件
            System.out.println("生成模块:Resources");
            generateResources(resourcesDir, project);
            System.out.println("生成模块:Static");
            System.out.println("生成模块:Templates");
        }

        {//生成Test
            System.out.println("生成模块:Test");
            generateTest(testDomainDir, project);
        }

        {//生成Test Resources
            System.out.println("生成模块:Test Resources");
            generateTestResources(testResourcesDir, project);
        }
        System.out.println("finish");
        return true;
    }


    /**
     * 生成POM.xml
     *
     * @param root
     * @param project
     */
    public void generatePom(File root, Project project) {
        HashMap<String, Object> ctx = new HashMap<String, Object>();
        ctx.put("projectName", project.getProjectName());
        ctx.put("basePackage", project.getProjectBasePackage());
        ctx.put("dataBase", project.getDatabase().toString());
        ctx.put("project", project);
        freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "pom.xml"), "SpringBootService/pom.ftl", ctx);
    }

    public void generateConfig(File root, Project project) {
        if (!root.exists()) {
            root.mkdirs();
        } else {
            Tool.clear(root);
        }
        HashMap<String, Object> ctx = new HashMap<String, Object>();
        ctx.put("basePackage", project.getProjectBasePackage());
        ctx.put("projectName", project.getProjectName());
        ctx.put("author", project.getProjectAuthor());
        ctx.put("date", new Date());

        //config
        freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "ScheduleConfig.java"), "SpringBootService/java/config/ScheduleConfig.java", ctx);
        if (project.getDatabase().name().equals(DataBase.SQLite.name())) {
            freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "SQLiteConfig.java"), "SpringBootService/java/config/SQLiteConfig.java", ctx);
        }
        if (project.isNeedMoreDB()) {
            freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "MapperMainConfig.java"), "SpringBootService/java/config/MapperMainConfig.java", ctx);
            freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "MapperTwoConfig.java"), "SpringBootService/java/config/MapperTwoConfig.java", ctx);
        }
    }

    /**
     * 生成entity
     *
     * @param root
     * @param md
     */
    public void generateEntity(File root, Module md) {
        if (!root.exists()) {
            root.mkdirs();
        } else {
            Tool.clear(root);
        }

        for (Table table : md.getTables()) {
            HashMap<String, Object> ctx = new HashMap<String, Object>();
            ctx.put("tool", Tool.class);
            ctx.put("basePackage", md.getProjectBasePackage());
            ctx.put("moduleName", md.getModuleName());
            ctx.put("table", table);
            ctx.put("author", md.getProjectAuthor());
            ctx.put("date", new Date());

            File file = Tool.createFile(root.getAbsolutePath(), Tool.ABB2Abb(table.getTableName()) + ".java");
            freeMarkerManager.outputTemp(file, "SpringBootService/java/module/ent/entity.ftl", ctx);
        }
    }

    /**
     * 生成Mapper
     *
     * @param root
     * @param md
     */
    public void generateMapper(File root, Module md) {
        if (!root.exists()) {
            root.mkdirs();
        } else {
            Tool.clear(root);
        }
        HashMap<String, Object> ctx = new HashMap<String, Object>();

        ctx.put("tool", Tool.class);
        ctx.put("module", md);
        ctx.put("dataBase", project.getDatabase().toString());
        ctx.put("basePackage", md.getProjectBasePackage());
        ctx.put("moduleName", md.getModuleName());
        ctx.put("author", md.getProjectAuthor());
        ctx.put("date", new Date());

        for (Table table : md.getTables()) {
            ctx.put("table", table);
            freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), Tool.ABB2Abb(table.getTableName()) + "Mapper" + ".java"), "SpringBootService/java/module/mpr/mapper.java", ctx);

            if (table.getSys()) {
                freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), Tool.ABB2Abb(table.getTableName()) + "Mapper" + ".xml"), "SpringBootService/java/module/mpr/" + project.getDatabase().name() + "_mapper.xml", ctx);
            } else {
                freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), Tool.ABB2Abb(table.getTableName()) + "Mapper" + ".xml"), "SpringBootService/java/module/mpr/" + project.getDatabase().name() + "_NSYS_mapper.xml", ctx);
            }
        }
    }

    public void generateManager(File root, Module md) {
        if (!root.exists()) {
            root.mkdirs();
        } else {
            Tool.clear(root);
        }
        HashMap<String, Object> ctx = new HashMap<String, Object>();

        ctx.put("tool", Tool.class);
        ctx.put("module", md);
        ctx.put("basePackage", md.getProjectBasePackage());
        ctx.put("moduleName", md.getModuleName());
        ctx.put("author", md.getProjectAuthor());
        ctx.put("date", new Date());

        for (Table table : md.getTables()) {
            ctx.put("table", table);
            freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), Tool.ABB2Abb(table.getTableName()) + "Manager" + ".java"), "SpringBootService/java/module/mgr/manager.ftl", ctx);
            freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), Tool.ABB2Abb(table.getTableName()) + "ManagerImpl" + ".java"), "SpringBootService/java/module/mgr/managerImpl.ftl", ctx);
        }
    }

    /**
     * 生成Request
     *
     * @param root
     * @param md
     */
    public void generateRequest(File root, Module md) {
        if (!root.exists()) {
            root.mkdirs();
        } else {
            Tool.clear(root);
        }

        HashMap<String, Object> ctx = new HashMap<String, Object>();
        ctx.put("tool", Tool.class);
        ctx.put("module", md);
        ctx.put("basePackage", md.getProjectBasePackage());
        ctx.put("moduleName", md.getModuleName());
        ctx.put("author", md.getProjectAuthor());
        ctx.put("date", new Date());

        if (md.getModuleName().equals("api")) {
            freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "ApiExampleRequest" + ".java"), "SpringBootService/java/module/req/ApiExampleRequest.java", ctx);
        }

        for (Table table : md.getTables()) {
            ctx.put("table", table);

            if (table.getCreate()) {
                freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), Tool.ABB2Abb(table.getTableName()) + "CreateRequest" + ".java"), "SpringBootService/java/module/req/createRequestClass.ftl", ctx);
            }

            if (table.getDelete()) {
                freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), Tool.ABB2Abb(table.getTableName()) + "DeleteRequest" + ".java"), "SpringBootService/java/module/req/deleteRequestClass.ftl", ctx);
            }

            if (table.getUpdate()) {
                freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), Tool.ABB2Abb(table.getTableName()) + "UpdateRequest" + ".java"), "SpringBootService/java/module/req/updateRequestClass.ftl", ctx);
            }

            if (table.getFind()) {
                freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), Tool.ABB2Abb(table.getTableName()) + "FindRequest" + ".java"), "SpringBootService/java/module/req/findRequestClass.ftl", ctx);
            }

            if (table.getGet()) {
                freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), Tool.ABB2Abb(table.getTableName()) + "GetRequest" + ".java"), "SpringBootService/java/module/req/getRequestClass.ftl", ctx);
            }

            if (table.getSearch()) {
                freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), Tool.ABB2Abb(table.getTableName()) + "SearchRequest" + ".java"), "SpringBootService/java/module/req/searchRequestClass.ftl", ctx);
            }
        }
    }

    /**
     * 生成一般文件
     *
     * @param root
     * @param md
     */
    public void generateResponse(File root, Module md) {
        if (!root.exists()) {
            root.mkdirs();
        } else {
            Tool.clear(root);
        }

        HashMap<String, Object> ctx = new HashMap<String, Object>();
        ctx.put("tool", Tool.class);
        ctx.put("module", md);
        ctx.put("basePackage", md.getProjectBasePackage());
        ctx.put("moduleName", md.getModuleName());
        ctx.put("author", md.getProjectAuthor());
        ctx.put("date", new Date());

        if (md.getModuleName().equals("api")) {
            freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "ApiExampleResponse" + ".java"), "SpringBootService/java/module/rsp/ApiExampleResponse.java", ctx);
        }

        for (Table table : md.getTables()) {
            ctx.put("table", table);

            if (table.getCreate()) {
                freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), Tool.ABB2Abb(table.getTableName()) + "CreateResponse" + ".java"), "SpringBootService/java/module/rsp/createResponseClass.ftl", ctx);
            }

            if (table.getDelete()) {
                freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), Tool.ABB2Abb(table.getTableName()) + "DeleteResponse" + ".java"), "SpringBootService/java/module/rsp/deleteResponseClass.ftl", ctx);
            }

            if (table.getUpdate()) {
                freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), Tool.ABB2Abb(table.getTableName()) + "UpdateResponse" + ".java"), "SpringBootService/java/module/rsp/updateResponseClass.ftl", ctx);
            }

            if (table.getFind()) {
                freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), Tool.ABB2Abb(table.getTableName()) + "FindResponse" + ".java"), "SpringBootService/java/module/rsp/findResponseClass.ftl", ctx);
            }

            if (table.getGet()) {
                freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), Tool.ABB2Abb(table.getTableName()) + "GetResponse" + ".java"), "SpringBootService/java/module/rsp/getResponseClass.ftl", ctx);
            }

            if (table.getSearch()) {
                freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), Tool.ABB2Abb(table.getTableName()) + "SearchResponse" + ".java"), "SpringBootService/java/module/rsp/searchResponseClass.ftl", ctx);
            }
        }
    }

    /**
     * 生成frame类
     *
     * @param root
     * @param project
     */
    public void generateFrame(File root, Project project) {
        if (!root.exists()) {
            root.mkdirs();
        } else {
            Tool.clear(root);
        }
        HashMap<String, Object> ctx = new HashMap<String, Object>();
        ctx.put("basePackage", project.getProjectBasePackage());
        ctx.put("moduleName", project.getProjectName());
        ctx.put("timestamp", new Date().getTime());

        File auth = Tool.createPath(root.getAbsolutePath(), "auth");
        File base = Tool.createPath(root.getAbsolutePath(), "base");
        File excel = Tool.createPath(root.getAbsolutePath(), "excel");
        File excelannotation = Tool.createPath(excel.getAbsolutePath(), "annotation");
        File excelconverter = Tool.createPath(excel.getAbsolutePath(), "converter");
        File exception = Tool.createPath(excel.getAbsolutePath(), "exception");
        File excelstyle = Tool.createPath(excel.getAbsolutePath(), "style");
        File schedule = Tool.createPath(root.getAbsolutePath(), "schedule");
        File utils = Tool.createPath(root.getAbsolutePath(), "utils");
        File validation = Tool.createPath(root.getAbsolutePath(), "validation");

        //auth
        for (String name : ResourceUtil.getResourceFiles("/modules/SpringBootService/java/frame/auth/")) {
            freeMarkerManager.outputTemp(Tool.createFile(auth.getAbsolutePath(), name), "SpringBootService/java/frame/auth/" + name, ctx);
        }

        //base
        for (String name : ResourceUtil.getResourceFiles("/modules/SpringBootService/java/frame/base/")) {
            freeMarkerManager.outputTemp(Tool.createFile(base.getAbsolutePath(), name), "SpringBootService/java/frame/base/" + name, ctx);
        }

        //excel
        for (String name : ResourceUtil.getResourceFiles("/modules/SpringBootService/java/frame/excel/")) {
            freeMarkerManager.outputTemp(Tool.createFile(excel.getAbsolutePath(), name), "SpringBootService/java/frame/excel/" + name, ctx);
        }
        for (String name : ResourceUtil.getResourceFiles("/modules/SpringBootService/java/frame/excel/annotation/")) {
            freeMarkerManager.outputTemp(Tool.createFile(excelannotation.getAbsolutePath(), name), "SpringBootService/java/frame/excel/annotation/" + name, ctx);
        }
        for (String name : ResourceUtil.getResourceFiles("/modules/SpringBootService/java/frame/excel/converter/")) {
            freeMarkerManager.outputTemp(Tool.createFile(excelconverter.getAbsolutePath(), name), "SpringBootService/java/frame/excel/converter/" + name, ctx);
        }
        for (String name : ResourceUtil.getResourceFiles("/modules/SpringBootService/java/frame/excel/exception/")) {
            freeMarkerManager.outputTemp(Tool.createFile(exception.getAbsolutePath(), name), "SpringBootService/java/frame/excel/exception/" + name, ctx);
        }
        for (String name : ResourceUtil.getResourceFiles("/modules/SpringBootService/java/frame/excel/style/")) {
            freeMarkerManager.outputTemp(Tool.createFile(excelstyle.getAbsolutePath(), name), "SpringBootService/java/frame/excel/style/" + name, ctx);
        }

        //schedule
        for (String name : ResourceUtil.getResourceFiles("/modules/SpringBootService/java/frame/schedule/")) {
            freeMarkerManager.outputTemp(Tool.createFile(schedule.getAbsolutePath(), name), "SpringBootService/java/frame/schedule/" + name, ctx);
        }
        //utils
        for (String name : ResourceUtil.getResourceFiles("/modules/SpringBootService/java/frame/utils/")) {
            ArrayList<String> filters = new ArrayList<>();
            filters.add("MailUtil.java");

            if (filters.contains(name)) {
                if (project.isNeedEMail()) {
                    freeMarkerManager.outputTemp(Tool.createFile(utils.getAbsolutePath(), name), "SpringBootService/java/frame/utils/" + name, ctx);
                }
            } else {
                freeMarkerManager.outputTemp(Tool.createFile(utils.getAbsolutePath(), name), "SpringBootService/java/frame/utils/" + name, ctx);
            }
        }

        //validation
        for (String name : ResourceUtil.getResourceFiles("/modules/SpringBootService/java/frame/validation/")) {
            freeMarkerManager.outputTemp(Tool.createFile(validation.getAbsolutePath(), name), "SpringBootService/java/frame/validation/" + name, ctx);
        }
    }


    /**
     * 生成frame类
     *
     * @param root
     * @param project
     */
    public void generateTask(File root, Project project) {
        if (!root.exists()) {
            root.mkdirs();
        } else {
            Tool.clear(root);
        }
        HashMap<String, Object> ctx = new HashMap<String, Object>();
        ctx.put("basePackage", project.getProjectBasePackage());
        ctx.put("moduleName", project.getProjectName());
        ctx.put("timestamp", new Date().getTime());

        //task
        for (String name : ResourceUtil.getResourceFiles("/modules/SpringBootService/java/task/")) {
            freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), name), "SpringBootService/java/task/" + name, ctx);
        }
    }

    /**
     * 生成base类
     *
     * @param root
     * @param project
     */
    public void generateApplication(File root, Project project) {
        HashMap<String, Object> ctx = new HashMap<String, Object>();
        ctx.put("basePackage", project.getProjectBasePackage());
        ctx.put("moduleName", project.getProjectName());
        ctx.put("project", project);
        ctx.put("timestamp", new Date().getTime());
        freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "Application.java"), "SpringBootService/java/Application.ftl", ctx);
    }

    /**
     * 生成properties
     *
     * @param root
     * @param project
     */
    public void generateResources(File root, Project project) {
        HashMap<String, Object> ctx = new HashMap<String, Object>();

        ctx.put("basePackage", project.getProjectBasePackage());
        ctx.put("tool", Tool.class);
        ctx.put("dataBase", project.getDatabase().toString());
        ctx.put("author", project.getProjectAuthor());
        ctx.put("dBmapper", dBmapper);
        ctx.put("date", new Date());
        ctx.put("author", project.getProjectAuthor());
        ctx.put("projectName", project.getProjectName());
        ctx.put("project", project);
        freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "start-dev.bat"), "SpringBootService/resources/start-dev.bat", ctx);
        freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "start-prod.bat"), "SpringBootService/resources/start-prod.bat", ctx);
        freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "application-dev.properties"), "SpringBootService/resources/application-dev.ftl", ctx);
        freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "application-prod.properties"), "SpringBootService/resources/application-prod.ftl", ctx);
        Tool.outputResource("SpringBootService/resources/logback-spring.xml", Tool.createFile(root.getAbsolutePath(), "logback-spring.xml"));

        File dbtool = Tool.createPath(root.getAbsolutePath(), "dbtool");
        {
            XmlManager xmlManager = ManagerFactory.getXmlManager();
            xmlManager.saveAs(dbtool, project);
        }

        for (Module module : project.getModules()) {
            ctx.put("moduleName", module.getModuleName());
            ctx.put("module", module);
            File tableDir = Tool.createPath(dbtool.getAbsolutePath(), module.getModuleName() + "_table");
            for (Table table : module.getTables()) {
                ctx.put("table", table);
                freeMarkerManager.outputTemp(Tool.createFile(tableDir.getAbsolutePath(), project.getDatabase().name() + "_" + table.getTableName() + ".sql"), "SpringBootService/resources/dbtool/table.ftl", ctx);
            }
            freeMarkerManager.outputTemp(Tool.createFile(tableDir.getAbsolutePath(), project.getDatabase().name() + "_ALL_TABLE" + ".sql"), "SpringBootService/resources/dbtool/tableAll.ftl", ctx);
        }

        if (project.isNeedSys()) {//生成系统模块
            File system = Tool.createPath(dbtool.getAbsolutePath(), "system_table");

            for (String name : ResourceUtil.getResourceFiles("/modules/SpringBootService/resources/dbtool/system/")) {
                if (name.contains(project.getDatabase().name())) {
                    freeMarkerManager.outputTemp(Tool.createFile(system.getAbsolutePath(), name), "SpringBootService/resources/dbtool/system/" + name, ctx);
                }
            }
        }

        if (DataBase.SQLite.name().equals(project.getDatabase().name())) {
            File dbFile = Tool.createFile(root.getAbsolutePath(), project.getProjectName() + ".db");
            Connection connection = null;
            Statement statement = null;
            try {
                Class.forName("org.sqlite.JDBC");
                connection = DriverManager.getConnection("jdbc:sqlite:" + dbFile.getAbsolutePath());

                for (File fileModule : dbtool.listFiles()) {
                    if (!fileModule.isDirectory()) {
                        continue;
                    }
                    for (File file : fileModule.listFiles()) {
                        if (!file.getName().startsWith("SQLite_ALL_") && file.getName().endsWith(".sql")) {
                            statement = connection.createStatement();
                            String sql = FileUtil.readFileToString(file);
                            statement.execute(sql);
                            statement.close();
                        }
                    }
                }
            } catch (ClassNotFoundException | SQLException | IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (statement != null) statement.close();
                    if (connection != null) connection.close();
                } catch (Exception e) {

                }
            }
        }
    }

    public void generateTestResources(File root, Project project) {
        HashMap<String, Object> ctx = new HashMap<String, Object>();

        ctx.put("basePackage", project.getProjectBasePackage());
        ctx.put("projectName", project.getProjectName());
        ctx.put("tool", Tool.class);
        ctx.put("dataBase", project.getDatabase().toString());
        ctx.put("author", project.getProjectAuthor());
        ctx.put("dBmapper", dBmapper);
        ctx.put("date", new Date());
        ctx.put("project", project);
        freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "application.properties"), "SpringBootService/test/application.ftl", ctx);
        Tool.outputResource("SpringBootService/test/logback-spring.xml", Tool.createFile(root.getAbsolutePath(), "logback-spring.xml"));
    }

    public void generateTest(File root, Project project) {

        {
            File config = Tool.createPath(root.getAbsolutePath(), "config");
            config.mkdirs();
            HashMap<String, Object> ctx = new HashMap<String, Object>();
            ctx.put("tool", Tool.class);
            ctx.put("basePackage", project.getProjectBasePackage());
            ctx.put("moduleName", project.getProjectName());
            ctx.put("author", project.getProjectAuthor());
            ctx.put("date", new Date());
            File file = Tool.createFile(config.getAbsolutePath(), "TestConfig" + ".java");
            freeMarkerManager.outputTemp(file, "SpringBootService/test/TestConfig.ftl", ctx);
        }

        {
            HashMap<String, Object> ctx = new HashMap<String, Object>();
            ctx.put("tool", Tool.class);
            ctx.put("basePackage", project.getProjectBasePackage());
            ctx.put("moduleName", project.getProjectName());
            ctx.put("author", project.getProjectAuthor());
            ctx.put("date", new Date());
            File file = Tool.createFile(root.getAbsolutePath(), "UtilTest" + ".java");
            freeMarkerManager.outputTemp(file, "SpringBootService/test/UtilTest.java", ctx);
        }

        for (Module md : project.getModules()) {
            File module = Tool.createPath(root.getAbsolutePath(), md.getModuleName());

            for (Table table : md.getTables()) {
                HashMap<String, Object> ctx = new HashMap<String, Object>();

                ctx.put("tool", Tool.class);
                ctx.put("module", md);
                ctx.put("basePackage", md.getProjectBasePackage());
                ctx.put("moduleName", md.getModuleName());
                ctx.put("table", table);
                ctx.put("author", md.getProjectAuthor());
                ctx.put("date", new Date());

                File file = Tool.createFile(module.getAbsolutePath(), Tool.ABB2Abb(table.getTableName()) + "Test" + ".java");
                freeMarkerManager.outputTemp(file, "SpringBootService/test/test.ftl", ctx);
            }
        }
    }
}
