package io.gisbi.commons.utils;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.builder.GeneratorBuilder;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.Scanner;

// 演示例子，执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
public class CodeGenerator {

    /**
     * 自定义模板,模板引擎是 freemarker
     */
    private static final String ENTITY_TEMPLATE_PATH = "/templates/entity.java.ftl";
    private static final String XML_TEMPLATE_PATH = "/templates/mapper.xml.ftl";
    private static final String MAPPER_TEMPLATE_PATH = "/templates/mapper.java.ftl";
    private static final String CONTROLLER_TEMPLATE_PATH = "/templates/controller.java.ftl";
    private static final String SERVICE_IMPL_TEMPLATE_PATH = "/templates/serviceImpl.java.ftl";
    private static final String SERVICE_TEMPLATE_PATH = "/templates/service.java.ftl";

    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotBlank(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    public static void main(String[] args) {
        // 获取项目路径
        String projectPath = System.getProperty("user.dir");

        // 数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig.Builder(
                "jdbc:mysql://121.37.111.42:3306/dataease?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true",
                "root",
                "mysql_F8ysiK@2024")
                .build();

        AutoGenerator mpg = new AutoGenerator(dataSourceConfig);
        // 全局配置
        GlobalConfig globalConfig = new GlobalConfig.Builder()
                .outputDir(projectPath + "/core/core-backend/src/main/java") // 修正路径
                .author("zhengsl")
                .disableOpenDir()
                .build();

        mpg.global(globalConfig);

        // 包配置
        String moduleName = scanner("模块名称");
        PackageConfig packageConfig = GeneratorBuilder.packageConfigBuilder()
                .parent("io.gisbi.application") // 修正包名
                .moduleName(moduleName)
                .entity("domain")
                .build();
        mpg.packageInfo(packageConfig);

        // 配置模板
        TemplateConfig templateConfig = GeneratorBuilder.templateConfigBuilder()
                .entity(ENTITY_TEMPLATE_PATH) // 使用定义的常量
                .service(SERVICE_TEMPLATE_PATH)
                .serviceImpl(SERVICE_IMPL_TEMPLATE_PATH)
                .controller(CONTROLLER_TEMPLATE_PATH)
                .mapper(MAPPER_TEMPLATE_PATH)
                .xml(XML_TEMPLATE_PATH)
                .build();
        mpg.template(templateConfig);

        // 策略配置
        StrategyConfig strategyConfig = GeneratorBuilder.strategyConfigBuilder()
                .addInclude(scanner("表名，多个英文逗号分割").split(","))
                .addTablePrefix("app_")
                .entityBuilder()
                .enableLombok()
                .naming(NamingStrategy.underline_to_camel)
                .columnNaming(NamingStrategy.underline_to_camel)
                .controllerBuilder()
                .enableRestStyle()
                .enableHyphenStyle()
                .build();
        mpg.strategy(strategyConfig);
        // 执行代码生成
        mpg.execute();
    }
}
