package net.tccn.base.dbq.jdbc.api;

import lombok.Getter;
import lombok.Setter;
import org.redkale.annotation.Comment;
import org.redkale.convert.ConvertColumn;
import org.redkale.persistence.Column;
import org.redkale.persistence.Entity;
import org.redkale.persistence.Id;

import java.util.List;

@Getter
@Setter
@Entity(comment = "[数据库平台表]")
public class DbAccount {
    @Id
    @Comment("[数据库ID]")
    private int dbid;

    @Column(length = 64, comment = "[名称]")
    private String dbname;

    @Comment("[类型（mysql|ArangoDb）]")
    @Column(length = 32, comment = "[类型]")
    private String cate;

    @Comment("[备注]")
    @Column(comment = "[备注]")
    private String remark;

    @Column(length = 128, comment = "[数据库连接地址]")
    private String url;

    @Column(length = 32, comment = "[账号]")
    private String user;

    @Column(length = 64, comment = "[密码]")
    private String pwd;

    // 将catalogs作为JSON字符串存储，但此处为了方便解析，将其转换为List<String>
    @Column(length = 255, comment = "[库]（JSON格式存储多个库名）")
    private List<String> catalogs;

    @Column(comment = "[状态] 1启用，0未启用")
    private short status;

    @Column(updatable = false, comment = "[创建时间]")
    private long createtime;

    public String accountKey() {
        int start = url.indexOf("//") + 2;
        int end = url.indexOf("/", start);
        int endDef = url.indexOf("?", end);
        if (endDef == -1) {
            endDef = url.length();
        }
        String host = url.substring(start, end == -1 ? url.length() : end);
        return user + ":" + pwd + "@" + host;
    }

    @ConvertColumn(ignore = true)
    public String getPwd() {
        return pwd;
    }

}
