package com.starry.generator.config;

import lombok.Data;

/**
 * 代码生成器配置类 用于配置数据库连接和生成参数
 *
 * @author admin
 * @since 2024-01-01
 */
@Data
public class GeneratorConfig {

    // 数据库连接配置
    private String url;
    private String driverName;
    private String username;
    private String password;
    private String databaseName;

    // 代码生成配置
    private String author;
    private String packageName;
    private String outputDir;
    private boolean autoRemovePre;
    private String tablePrefix;

    // 模板类型 (crud, tree, sub)
    private String tplCategory = "crud";

    // 功能名称
    private String functionName;

    // 模块名
    private String moduleName;

    // 业务名
    private String businessName;

    public GeneratorConfig() {
        // 设置默认值
        this.author = "admin";
        this.packageName = "com.starry.play";
        this.outputDir = "./generated-code";
        this.autoRemovePre = false;
        this.tablePrefix = "sys_";
        this.tplCategory = "crud";
    }

    /**
     * 从URL中获取数据库名
     */
    public String getDatabaseName() {
        if (databaseName != null && !databaseName.isEmpty()) {
            return databaseName;
        }

        // 如果没有手动设置数据库名，从URL中解析
        if (url != null) {
            String dbName = url.substring(url.lastIndexOf("/") + 1);
            if (dbName.contains("?")) {
                dbName = dbName.substring(0, dbName.indexOf("?"));
            }
            return dbName;
        }
        return "";
    }
}
