package org.wfc.gen.util;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.fill.Column;
import org.wfc.common.mybatis.domain.BaseData;

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

/**
 * @description: 代码生成器工具
 * @author: cyc
 * @since: 2024-12-06
 */
public class GenPlusUtils {

    // 生成代码输出路径
//    public static final String OUTPUT_PATH = "D://gencode/test23";
    public static final String OUTPUT_PATH = "D:\\projects\\pro\\be.wfc\\wfc-modules\\wfc-system\\src\\main\\java";
//    public static final String OUTPUT_PATH_XML = "D://gencode/test23";
    public static final String OUTPUT_PATH_XML = "D:\\projects\\pro\\be.wfc\\wfc-modules\\wfc-system\\src\\main\\resources\\mapper\\user";
    // 模块名：system/user
    public static final String MODULE_NAME = "system";
    // 数据库URL
    public static final String URL = "jdbc:mysql://localhost:3306/wfc_user_db?useUnicode=true&useSSL=false&characterEncoding=utf8";
    // 表名
    public static final String TABLE_NAME = "u_account_package";


    public static void main(String[] args) {
        FastAutoGenerator.create(URL, "root", "123456")
                .globalConfig(builder -> {
                    builder.author("sys") // 设置作者
                            .enableSpringdoc()
                            .dateType(DateType.ONLY_DATE)
                            .outputDir(OUTPUT_PATH); // 指定输出目录
                })
                .dataSourceConfig(builder ->
                        builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                            int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                            if (typeCode == Types.SMALLINT || typeCode == Types.TINYINT) {
                                // 自定义类型转换
                                return DbColumnType.INTEGER;
                            }
                            if (typeCode == Types.CHAR) {
                                // 自定义类型转换
                                return DbColumnType.BOOLEAN;
                            }
                            return typeRegistry.getColumnType(metaInfo);
                        })
                )
                .packageConfig(builder ->
                        builder.parent("org.wfc") // 设置父包名
                                .moduleName(MODULE_NAME) // 设置父包模块名
                                .entity("domain")
                                .pathInfo(Collections.singletonMap(OutputFile.xml, OUTPUT_PATH_XML)) // 设置mapperXml生成路径
                )
                .strategyConfig(builder ->
                        builder.addInclude(TABLE_NAME) // 设置需要生成的表名
                                .addTablePrefix("t_", "c_") // 设置过滤表前缀
                                .controllerBuilder()
                                .enableRestStyle()
                                .template("/templates/controller.java.vm")
                                .enableFileOverride()
                                .entityBuilder()
                                .enableLombok()
                                .enableFileOverride()
                                .superClass(BaseData.class)
                                .addSuperEntityColumns("id", "create_by", "create_time", "update_by", "update_time", "del_flag")
                                .addTableFills(new Column("create_time", FieldFill.INSERT))
                                .addTableFills(new Column("update_time", FieldFill.INSERT_UPDATE))
                                .addTableFills(new Column("create_by", FieldFill.INSERT))
                                .addTableFills(new Column("update_by", FieldFill.INSERT_UPDATE))
                                .serviceBuilder()
                                .enableFileOverride()
                                .serviceImplTemplate("/templates/serviceImpl.java.vm")
                                .serviceTemplate("/templates/service.java.vm")
                                .mapperBuilder()
                                .enableFileOverride()
                )
                .execute();
    }
}
