package com.generator;


import com.generator.jsp.JspGenerator;
import com.generator.util.DbUtil;
import com.generator.util.Field;
import com.generator.util.FreemarkerUtil;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.util.*;

public class ServerGenerator {

    static String MODULE = "src\\com\\";
    public static String DB_TABLE="yingjiziyuan";
    //static String toControllerPath = MODULE+"\\src\\main\\java\\com\\course\\"+MODULE+"\\controller\\admin\\";
    static String generatorConfigPath = "src\\com\\generator\\generatorConfig.xml";

    static String toJspPath = "WebRoot\\admin\\views\\";
    static String toJspWebPath = "WebRoot\\";

    /*dao所在目录*/
    public static String toDaoPath = MODULE + "dao\\";
    /*dto所在目录*/
    public static String toDtoPath = MODULE + "dto\\";
    /*service所在目录*/
    public static String toServerPath = MODULE + "service\\";
    /*service\impl所在目录*/
    public static String toServerImplPath = MODULE + "service\\impl\\";
    /*controller\web所在目录*/
    public static String toControllerPath = MODULE + "controller\\admin\\";
    public static String toControllerWebPath = MODULE + "controller\\web\\";



    static String[] tableNames = {"apply","bbs","early_warning","emergency_resources","resources_type"};
    static String[] Domains = {"Apply","Bbs","EarlyWarning","EmergencyResources","ResourcesType"};
    public static void main(String[] args) throws Exception {
        //1.生成entity，mapper,service,service.impl
        generatorEntity();
        //2.重新生成控制层
        for (int i = 0; i < Domains.length; i++) {
            generatorController(tableNames[i],Domains[i]);
            System.out.println("生成控制层表名称==" + Domains[i]);
        }

        //3.生成jsp
        for (int i = 0; i < Domains.length; i++) {
            generatorJsp(tableNames[i],Domains[i]);
            System.out.println("生成jsp表名称==" + Domains[i]);
        }

    }



    /*第一步生成实体类*/
    static void generatorEntity(){
        List<String> war = new ArrayList<>();
        Boolean ovr=true;
        /* D:\workspace\yingjiziyuan\src\com\generator\generatorConfig.xml*/
        File file = new File("src\\com\\generator\\generatorConfig.xml");
        ConfigurationParser cp=new ConfigurationParser(war);
        try {
            Configuration config=cp.parseConfiguration(file);
            DefaultShellCallback back=new DefaultShellCallback(ovr);
            MyBatisGenerator my=new MyBatisGenerator(config, back, war);
            my.generate(null);
            for(String s:war) {
                System.out.println(s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //2.重新生成控制层
    static  void generatorController(String tableName,String Domain) throws Exception {
        //String tableName = tableName;
        String domain = Domain.substring(0, 1).toLowerCase() + Domain.substring(1);
        String tableNameCn = DbUtil.getTableComment(tableName);
        List<Field> fieldList = DbUtil.getColumnByTableName(tableName);

        Set<String> typeSet = getJavaTypes(fieldList);
        Map<String,Object> map = new HashMap<>();
        map.put("Domain",Domain);
        map.put("domain",domain);
        map.put("tableNameCn",tableNameCn);
        map.put("tableName",tableName);
        map.put("module", ServerGenerator.MODULE);
        map.put("fieldList",fieldList);
        map.put("typeSet",typeSet);

        //如果文件夹不存在则创建

        /*生成dao*/
        FreemarkerUtil.initConfig("dao.ftl");
        FreemarkerUtil.generator(toDaoPath+Domain+"Mapper.java",map);



        /*生成server*/
        FreemarkerUtil.initConfig("service.ftl");
        FreemarkerUtil.generator(toServerPath+Domain+"Service.java",map);

        /*生成serverImpl*/
        FreemarkerUtil.initConfig("serviceImpl.ftl");
        FreemarkerUtil.generator(toServerImplPath+Domain+"ServiceImpl.java",map);

        //生成controller
        FreemarkerUtil.initConfig("controller.ftl");
        FreemarkerUtil.generator(toControllerPath+Domain+"Controller.java",map);

        /*生成controller*/
       /* FreemarkerUtil.initConfig("controller_web.ftl");
        FreemarkerUtil.generator(toControllerWebPath+Domain+"ForeController.java",map);*/
        /*生成controller_img*/
        /*FreemarkerUtil.initConfig("controller_img.ftl");
        FreemarkerUtil.generator(toControllerPath+Domain+"Controller.java",map);*/


    }




    //3.生成jsp
    static  void generatorJsp(String tableName,String Domain) throws Exception {
        String domain = Domain.substring(0, 1).toLowerCase() + Domain.substring(1);
        //String tableName = domain;
        String tableNameCn = DbUtil.getTableComment(tableName);
        List<Field> fieldList = DbUtil.getColumnByTableName(tableName);
        String toJspPath = ServerGenerator.toJspPath;
        String toJspWebPath = ServerGenerator.toJspWebPath;

        System.out.println("是否存在-toJspPath" + toJspPath);
        System.out.println("是否存在-tableName" + tableName);
        //如果文件夹不存在则创建
        File targetFile = new File(toJspPath, tableName);
        System.out.println("是否存在-" + targetFile);
        if (!targetFile.exists()) {
            targetFile.mkdir();
        }
        toJspPath = toJspPath + tableName + "\\";
        System.out.println("toJspPath=" + toJspPath);
        System.out.println("targetFile=" + targetFile);

        Set<String> typeSet = getJavaTypes(fieldList);
        Map<String, Object> map = new HashMap<>();
        map.put("Domain", Domain);
        map.put("domain", domain);
        map.put("tableName",tableName);
        map.put("tableNameCn", tableNameCn);
        map.put("module", MODULE);
        map.put("fieldList", fieldList);
        map.put("typeSet", typeSet);

        //生成jsp_add
        FreemarkerUtil.initConfig("bang/jsp_add.ftl");
        FreemarkerUtil.generator(toJspPath + domain + "_add.jsp", map);

        //生成jsp_list
        FreemarkerUtil.initConfig("bang/jsp_list.ftl");
        FreemarkerUtil.generator(toJspPath + domain + "_list.jsp", map);

        //生成jsp_update
        FreemarkerUtil.initConfig("bang/jsp_update.ftl");
        FreemarkerUtil.generator(toJspPath + domain + "_update.jsp", map);
        //生成jsp_look
        FreemarkerUtil.initConfig("bang/jsp_look.ftl");
        FreemarkerUtil.generator(toJspPath + domain + "_look.jsp", map);
    }


    /**
     * 获取所有的Java类型，使用Set去重
     */
    private static Set<String> getJavaTypes(List<Field> fieldList) {
        Set<String> set = new HashSet<>();
        for (int i = 0; i < fieldList.size(); i++) {
            Field field = fieldList.get(i);
            set.add(field.getJavaType());
        }
        return set;
    }
}
