package cn.iocoder.hake.module.op.utils;


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

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

/**
 * @author chenjr
 * @since 2025-07-31
 */
public class MyBatisPlusAuto {

    public static void main(String[] args) {
        FastAutoGenerator.create("jdbc:mysql://127.0.0.1:3306/hake", "", "")
                .globalConfig(builder -> {
                    builder.author("chenjr") // 设置作者
                            .outputDir(System.getProperty("user.dir") + "/src/main/java/"); // 输出路径
                })
                .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("cn.iocoder.hake")
                            .pathInfo(Collections.singletonMap(OutputFile.xml, System.getProperty("user.dir") + "/src/main/resources/mapper"));
                })
                .strategyConfig(builder -> {
                    builder.entityBuilder()
                            .naming(NamingStrategy.underline_to_camel)
                            .columnNaming(NamingStrategy.underline_to_camel)
                            .enableLombok()
                            .logicDeleteColumnName("deleted");

                    builder.mapperBuilder()
                            .enableFileOverride() // 覆盖已有文件（慎用）
                            .enableBaseResultMap()
                            .enableBaseColumnList();

                    builder.serviceBuilder()
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl");

                    builder.controllerBuilder()
                            .enableFileOverride();

                    // 设置需要生成的表名
                    builder.addInclude("");
                })
                // 使用 Freemarker 模板引擎（默认，可省略）
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}
