package com.liquidnet.service.reconciliation;

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;

/**
 * @author AnJiabin <jiabin.an@lightnet.io>
 * @version V1.0
 * @Description: MAPPER代码生成类
 * @class: ReconciliationMybatisPlusCodeGenerator
 * @Package com.liquidnet.service.reconciliation
 * @Copyright: LightNet @ Copyright (c) 2020
 * @date 2020/10/21 14:42
 */
public class ReconciliationMybatisPlusCodeGenerator {
    /**
     *
     * @param moduleRootPath 项目模块根路径 到 /src 之前 ex：E:\projects\liquidnet-bus-v1\liquidnet-bus-service\liquidnet-common\liquidnet-mybatis
     * @param dsc 数据源
     * @param parentName 相当于业务模块名 com.liquidnet.service.adam 全限定类名
     * @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/reconciliation/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.setUrl("jdbc:mysql://127.0.0.1:3306/liquidnet_reconciliation?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=UTC");
//        dsc.setUrl("jdbc:mysql://192.168.31.229:3306/liquidnet_bank?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=UTC");
        // dsc.setSchemaName("public");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("root");

        String resourcePath = "/Users/anjiabin/myproject/gitlab_lightnet/liquidnet-bus-v1/liquidnet-bus-service/liquidnet-service-reconciliation/liquidnet-service-reconciliation-do";
        String directory = "com.liquidnet.service.reconciliation";
//        String[] dbTableArray = new String[]{"lr_account_check_batch","lr_account_check_mistake","lr_account_check_mistake_scratch_pool"};
//        String[] dbTableArray = new String[]{"lr_fundflow_currency_cloud","lr_fundflow_liquidnet_wallet"};
//        String[] dbTableArray = new String[]{"lr_infoflow_liquidnet_adam"};
//        String[] dbTableArray = new String[]{"lr_account_check_batch_sub"};
        String[] dbTableArray = new String[]{"lr_fundflow_liquidnet_wallet"};
        doGenerator(resourcePath,dsc, directory,dbTableArray);

    }
}
