package com.om.utils;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;

import java.sql.Types;
import java.util.Collections;

public class CodeGenerator {
  private final static String url = "jdbc:mysql://47.113.197.148:33306/operation_system?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true";

  public static void main(String[] args) {
    String projectPath = System.getProperty("user.dir");
    String outputDir = projectPath + "/src/main/java";

    String mapperPath = projectPath + "/src/main/resources/mapper";

    FastAutoGenerator.create(url, "root", "om1020")
        .globalConfig(builder -> {
          builder.author("henry-ong") // 设置作者
              .enableSwagger() // 开启 swagger 模式
              .disableOpenDir()
              .outputDir(outputDir); // 指定输出目录
        })
        .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
          int typeCode = metaInfo.getJdbcType().TYPE_CODE;
          if (typeCode == Types.SMALLINT) {
            // 自定义类型转换
            return DbColumnType.INTEGER;
          }
          return typeRegistry.getColumnType(metaInfo);

        }))
        .packageConfig(builder -> {
          builder.parent("com.om") // 设置父包名
              .moduleName(null) // 设置父包模块名
              .pathInfo(Collections.singletonMap(OutputFile.xml, mapperPath)); // 设置mapperXml生成路径
        })
        .strategyConfig(builder -> {
          builder.entityBuilder().enableLombok();  //lombok
          builder.controllerBuilder().enableHyphenStyle().enableRestStyle();
          builder.addInclude("manual") // 设置需要生成的表名
              .addTablePrefix("t_", "sys_"); // 设置过滤表前缀
        })
//            .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
        .execute();
  }
}
