package com.liquidnet.service;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.List;

public class MybatisPlusCodeGenerator {
    /**
     * @param moduleRootPath 项目模块根路径 到 /src 之前 ex：E:\projects\trlabs-bus-v1\trlabs-bus-service\trlabs-common\trlabs-mybatis
     * @param dsc            数据源
     * @param parentName     相当于业务模块名 com.liquidnet.service.slime 全限定类名
     * @param tableNames     表名
     */
    public static void doGenerator(String moduleRootPath,
                                   DataSourceConfig dsc,
                                   String parentName,
                                   String[] tableNames) {
        AutoGenerator mpg = new AutoGenerator();
        // 全局配置BankMybatisPlusCodeGenerator
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(moduleRootPath + "/src/main/java");
        gc.setAuthor("liquidnet");
        gc.setOpen(false);
        // gc.setSwagger2(true); 实体属性 Swagger2 注解
        mpg.setGlobalConfig(gc);

        // 数据源配置
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(parentName);
        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };

        // 如果模板引擎是 freemarker
        String templatePath = "/templates/mapper.xml.ftl";
        // 自定义输出配置
        List<FileOutConfig> focList = new ArrayList<>();
        // 自定义配置会被优先输出
        focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输出文件名 ， 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化！！
                return moduleRootPath + "/src/main/resources/com/liquidnet/service/slime/mapper/" + pc.getModuleName()
                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);

        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();

        templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        // strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        strategy.setInclude(tableNames);
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix(pc.getModuleName() + "_");
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }

    public static void main(String[] args) {

        DataSourceConfig dsc = new DataSourceConfig();
//        dsc.setSchemaName("public");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUrl("jdbc:mysql://39.106.122.201:3308/dev_ln_scene?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=CST");
        dsc.setUsername("testmall");
        dsc.setPassword("zhengzai!mYT");

        String resourcePath = "/Users/Tice/Downloads/tmp";
        String directory = "com.liquidnet.service.slime";
        String[] dbTableArray = new String[]{
                "slime_fields",
                "slime_field_checkers",
                "slime_field_applies",
                "slime_companies",
                "slime_sponsors",
                "slime_sponsor_applies",
                "slime_authorization_records",
                "slime_permissions",
                "slime_authorization_permissions"
        };

        doGenerator(resourcePath, dsc, directory, dbTableArray);
    }
}
