package com.demo.util;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.BeetlTemplateEngine;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.demo.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CodeGenerator {

    private final static Logger logger = LoggerFactory.getLogger(CodeGenerator.class);

    private final static String[] TABLE_ARRAY = {"tb_pluss_merc_member_set"};

    private final static String MODULE_NAME = "code-generator";

    /**
     * 表名前缀
     */
    private final static String TABLE_PREFIX = "tb_pluss";

    private final static String AUTHOR = "Djh";

    private final static String PACKAGE_PREFIX = "cn.pluss.platform";

    /**
     * 模板路径
     */
    private final static String ENTITY = "templates/entity.java";
    private final static String MAPPER = "templates/mapper.java";
//    private final static String SERVICE = "templates/service.java";
//    private final static String SERVICE_IMPL = "templates/serviceImpl.java";
    private final static String CONTROLLER = "templates/controller.java";

    public static void main(String[] args) throws FileNotFoundException {
        YamlUtils yamlUtils = new YamlUtils();
        DataSource dataSource = yamlUtils.readDefaultYaml();

        String projectPath = System.getProperty("user.dir") + "/" + MODULE_NAME;// 获取项目根目录

        BeetlTemplateEngine beetlTemplateEngine = new BeetlTemplateEngine();

        FastAutoGenerator.create(dataSource.getUrl(), dataSource.getUsername(), dataSource.getPassword())
                .templateEngine(beetlTemplateEngine)
                .globalConfig(builder -> {
                    builder.author(AUTHOR) // 设置作者
                            .outputDir(projectPath + "/src/main/java"); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent(PACKAGE_PREFIX) // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.xml, projectPath + "/src/main/resources/mapper")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude(TABLE_ARRAY) // 设置需要生成的表名
                            .addTablePrefix(TABLE_PREFIX) // 设置过滤表前缀
                            .entityBuilder().enableLombok()
                            .serviceBuilder().formatServiceFileName("%sService")
                            .controllerBuilder().enableRestStyle();
                })
                .execute();

        logger.debug("执行生成");
        // 执行生成
    }
}
