package com.cpop.generator;

import com.cpop.common.utils.DateUtils;
import com.cpop.common.utils.StringUtils;
import com.cpop.core.base.entity.BaseEntity;
import com.cpop.core.base.entity.BaseInsertListener;
import com.cpop.core.base.entity.BaseUpdateListener;
import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.EntityConfig;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.config.TableConfig;
import com.mybatisflex.core.exception.MybatisFlexException;
import com.zaxxer.hikari.HikariDataSource;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author DB
 * @Date: 2023/08/04/13:48
 * @Description: 代码生成
 */
public class CpopGenerator {

    /**
     * 数据库 URL
     */
   // private static final String URL = "jdbc:mysql://localhost:3306/cpop-dev?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
    private static final String URL = "jdbc:mysql://localhost:3306/cpop_dev?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
    /**
     * 数据库用户名
     */
    private static final String USERNAME = "root";
    /**
     * 数据库密码
     */
    private static final String PASSWORD = "Admin@123";
    //private static final String PASSWORD = "Customer0401";

    /**
     * 输出路径
     */
    private static final String EXPORT_URL = "/Cpop-Jambox";

    /**
     * 模块
     */
    private static final String EXPORT_ITEM = "jambox";

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

    /**
     * 主入口
     * @param args
     */
    public static void main(String[] args) {
        //配置数据源
        try (HikariDataSource dataSource = new HikariDataSource()) {
            dataSource.setJdbcUrl(URL);
            dataSource.setUsername(USERNAME);
            dataSource.setPassword(PASSWORD);
            //创建配置内容，两种风格都可以。
            GlobalConfig globalConfig = createGlobalConfig();
            //GlobalConfig globalConfig = createGlobalConfigUseStyle2();
            //通过 datasource 和 globalConfig 创建代码生成器
            Generator generator = new Generator(dataSource, globalConfig);
            /*GeneratorFactory.registerGenerator("bo", new BoGenerator());
            GeneratorFactory.registerGenerator("vo", new VoGenerator());
            GeneratorFactory.registerGenerator("pageBo", new PageBoGenerator());
            GeneratorFactory.registerGenerator("pageVo", new PageVoGenerator());
            GeneratorFactory.registerGenerator("dto", new DtoGenerator());*/
            //生成代码
            generator.generate();
        }
    }

    /**
     * 命令行输入表名
     * @return
     */
    private static String scanner() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入表名，多个英文逗号分割：");
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotBlank(ipt)) {
                return ipt;
            }
        }
        throw new MybatisFlexException("请输入正确的表名，多个英文逗号分割！");
    }

    /**
     * @Description: 构建代码生成配置
     * @param
     * @return GlobalConfig
     * @author DB
     * @Date: 2023/8/4 0004 14:05
     */
    private static GlobalConfig createGlobalConfig(){
        String[] tables = scanner().split(",");
        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();

        //设置注释配置
        globalConfig.getJavadocConfig()
                .setAuthor("DB")
                .setSince(DateUtils.getDate());
        //设置包配置
        globalConfig.getPackageConfig()
                .setSourceDir(System.getProperty("user.dir") + EXPORT_URL + "/src/main/java")
                .setBasePackage("com.cpop." + EXPORT_ITEM + ".business")
                .setMapperXmlPath(System.getProperty("user.dir") + EXPORT_URL + "/src/main/resources/mapper");
        //设置策略配置
        globalConfig.getStrategyConfig()
                .setTablePrefix(TABLE_PREFIX)
                .setLogicDeleteColumn("is_delete")
                .setGenerateTable(tables);
        //设置模板配置
        globalConfig.getTemplateConfig()
                .setEntity(System.getProperty("user.dir") + "/Cpop-Generator/src/main/resources/template/enjoy/entity.tpl")
                .setMapper(System.getProperty("user.dir") + "/Cpop-Generator/src/main/resources/template/enjoy/mapper.tpl")
                .setService(System.getProperty("user.dir") + "/Cpop-Generator/src/main/resources/template/enjoy/service.tpl")
                .setServiceImpl(System.getProperty("user.dir") + "/Cpop-Generator/src/main/resources/template/enjoy/serviceImpl.tpl")
                .setController(System.getProperty("user.dir") + "/Cpop-Generator/src/main/resources/template/enjoy/controller.tpl")
                .setMapperXml(System.getProperty("user.dir") + "/Cpop-Generator/src/main/resources/template/enjoy/mapperXml.tpl");
        //Entity 生成配置
        globalConfig.getEntityConfig()
                .setSuperClass(BaseEntity.class)
                .setWithLombok(true)
                .setWithSwagger(true)
                .setSwaggerVersion(EntityConfig.SwaggerVersion.FOX);
        // 开启 Entity 的生成
        globalConfig.enableEntity();
        // 开启 Mapper 的生成
        globalConfig.enableMapper();
        // 开启 Service 的生成
        globalConfig.enableService();
        // 开启 ServiceImpl 的生成
        globalConfig.enableServiceImpl();
        // 开启 Controller 的生成
        globalConfig.enableController();
        // 开启 xml 的生成
        globalConfig.enableMapperXml();
        //遍历设置表配置
        Arrays.asList(tables).forEach(item -> {
            TableConfig tableConfig = new TableConfig();
            tableConfig.setInsertListenerClass(BaseInsertListener.class);
            tableConfig.setUpdateListenerClass(BaseUpdateListener.class);
            tableConfig.setTableName(item);
            tableConfig.setMapperGenerateEnable(false);
            globalConfig.setTableConfig(tableConfig);
        });
        return globalConfig;
    }
}
