package com.czg;

import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.*;
import com.mybatisflex.core.service.IService;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.zaxxer.hikari.HikariDataSource;

/**
 * @author ww
 */
public class Main {
    private final static String BASE_URL = "rm-bp1kn7h89nz62cno1ro.mysql.rds.aliyuncs.com";
    private final static String PORT = "3306";
    private final static String USERNAME = "cashier";
    private final static String PASSWORD = "Cashier@1@";
    private final static String DATABASE = "czg_cashier";
    static String currentWorkingDirectory = System.getProperty("user.dir");
    static String basePackage = "com.czg.";

    public static void main(String[] args) {

        //  packageName 指定生成代码项目
        //  tableName   指定需要生成的表

//        String packageName = "system";
        String packageName = "account";
//        String packageName = "product";
//        String packageName = "order";

        String tableName = "tb_order_info";
        String author = "zs";
        //是否生成DTO实体 默认生成
        boolean isGenerateDto = true;

        //配置数据源
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://" + BASE_URL + ":" + PORT + "/" + DATABASE);
        dataSource.setUsername(USERNAME);
        dataSource.setPassword(PASSWORD);

        String projectPath = currentWorkingDirectory + "/cash-service/" + packageName + "-service";

        // 生成 mapper 和 service实现
        GlobalConfig globalConfig = createGlobalConfigUseStyle(projectPath, packageName, "tb", tableName);
        globalConfig.setAuthor(author);
        Generator generator = new Generator(dataSource, globalConfig);
        generator.generate();

        // 生成 entity 和 service
        GlobalConfig serviceConfig = createServiceGlobalConfigUseStyle(currentWorkingDirectory + "/cash-common/cash-common-service",
                basePackage + packageName, "tb", tableName);
        Generator serviceGenerator = new Generator(dataSource, serviceConfig);
        serviceConfig.setAuthor(author);
        serviceGenerator.generate();

        //默认生成全参数DTO
        if(isGenerateDto){
            GlobalConfig dtoConfig = createDtoGlobalConfig(currentWorkingDirectory + "/cash-common/cash-common-service",
                    basePackage + packageName, "tb", tableName);
            Generator dtoGenerator = new Generator(dataSource, dtoConfig);
            dtoConfig.setAuthor(author);
            dtoGenerator.generate();
        }
    }

    public static GlobalConfig createGlobalConfigUseStyle(String projectPath, String packageName, String tablePrefix, String... tableNames) {
        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();

        //设置根包
        globalConfig.getPackageConfig()
                .setSourceDir(projectPath + "/src/main/java")

                .setEntityPackage(basePackage + packageName + ".entity")
                .setServicePackage(basePackage + packageName + ".service")

                .setServiceImplPackage(basePackage + "service." + packageName + ".service.impl")
                .setMapperPackage(basePackage + "service." + packageName + ".mapper")

                .setBasePackage(basePackage + "service." + packageName);

        globalConfig.getServiceConfig()
                .setClassSuffix("Service");

        globalConfig.getServiceImplConfig()
                .setClassSuffix("ServiceImpl")
                .setSuperClass(ServiceImpl.class);
        globalConfig.enableServiceImpl();
        globalConfig.setServiceImplTemplatePath("templates/serviceImpl.tpl");

        globalConfig.getMapperConfig()
                .setClassSuffix("Mapper");
        globalConfig.enableMapper();

        globalConfig.setMapperXmlPath(projectPath + "/src/main/resources/mapper");
        globalConfig.enableMapperXml();

        //设置表前缀和只生成哪些表，setGenerateTable 未配置时，生成所有表
        globalConfig.getStrategyConfig()
                .setTablePrefix(tablePrefix)
                .setGenerateTable(tableNames);

        //设置生成 mapper
        globalConfig.enableMapper();

        return globalConfig;
    }

    public static GlobalConfig createServiceGlobalConfigUseStyle(String projectPath, String packageName, String tablePrefix, String... tableNames) {
        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();

        //设置根包
        globalConfig.getPackageConfig()
                .setSourceDir(projectPath + "/src/main/java")
                .setBasePackage(packageName);

        globalConfig.getServiceConfig()
                .setClassSuffix("Service")
                .setSuperClass(IService.class);
        globalConfig.enableService();

        //设置表前缀和只生成哪些表，setGenerateTable 未配置时，生成所有表
        globalConfig.getStrategyConfig()
                .setTablePrefix(tablePrefix)
                .setGenerateTable(tableNames);

        //设置生成 entity 并启用 Lombok
        globalConfig.enableEntity()
                .setWithLombok(true)
                .setJdkVersion(21);

        //可以单独配置某个列
        ColumnConfig createTime = new ColumnConfig();
        createTime.setColumnName("create_time");
        createTime.setOnInsertValue("now()");
        globalConfig.getStrategyConfig()
                .setColumnConfig(createTime);

        ColumnConfig updateTime = new ColumnConfig();
        updateTime.setColumnName("update_time");
        updateTime.setOnUpdateValue("now()");
        updateTime.setOnInsertValue("now()");
        globalConfig.getStrategyConfig()
                .setColumnConfig(updateTime);

//        ColumnConfig deleted = new ColumnConfig();
//        deleted.setColumnName("is_del");
//        deleted.setLogicDelete(true);
//        globalConfig.getStrategyConfig()
//                .setColumnConfig(deleted);

//        ColumnConfig version = new ColumnConfig();
//        version.setColumnName("version");
//        version.setVersion(true);
//        globalConfig.getStrategyConfig()
//                .setColumnConfig(version);

        return globalConfig;
    }

    public static GlobalConfig createDtoGlobalConfig(String projectPath, String packageName, String tablePrefix, String... tableNames) {
        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();

        //设置根包
        globalConfig.getPackageConfig()
                .setEntityPackage(packageName + ".dto")
                .setSourceDir(projectPath + "/src/main/java")
                .setBasePackage(packageName);

        //设置表前缀和只生成哪些表，setGenerateTable 未配置时，生成所有表
        globalConfig.getStrategyConfig()
                .setTablePrefix(tablePrefix)
                .setGenerateTable(tableNames);
        globalConfig.setEntityTemplatePath("templates/entityDto.tpl");

        globalConfig.getEntityConfig()
                .setClassSuffix("DTO")
        ;

        //设置生成 entity 并启用 Lombok
        globalConfig.enableEntity()
                .setWithLombok(true)
                .setJdkVersion(21);

        return globalConfig;
    }
}
