package com.purui.prims.headless.api.pojo.request;

import com.purui.prims.common.pojo.RecordInfo;
import com.purui.prims.headless.api.pojo.enums.DataType;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;


@Data
@ApiModel(value = "数据源Req")
public class DatabaseReq extends RecordInfo {

    @ApiModelProperty(value = "ID")
    private Long id;

    @ApiModelProperty(value = "名称")
    private String name;

    @ApiModelProperty(value = "类型")
    private String type;

    @ApiModelProperty(value = "主机")
    private String host;

    @ApiModelProperty(value = "端口")
    private String port;

    @ApiModelProperty(value = "用户名")
    private String username;

    @ApiModelProperty(value = "密码")
    private String password;

    @ApiModelProperty(value = "数据库")
    private String database;

    @ApiModelProperty(value = "版本")
    private String version;

    @ApiModelProperty(value = "描述")
    private String description;

    @ApiModelProperty(value = "模式")
    private String schema;

    @ApiModelProperty(value = "连接地址")
    private String url;

    public String getConnectUrl() {
        if (StringUtils.isNotBlank(url)) {
            return url;
        }
        String databaseUrl = database;
        if (StringUtils.isBlank(databaseUrl)) {
            databaseUrl = "";
        } else {
            databaseUrl = "/" + database;
        }
        if (type.equalsIgnoreCase(DataType.MYSQL.getFeature())) {
            return String.format("jdbc:%s://%s:%s%s?sessionVariables=sql_mode='IGNORE_SPACE'&allowMultiQueries=true",
                    type, host, port, databaseUrl);
        }
        return String.format("jdbc:%s://%s:%s%s", type, host, port, databaseUrl);
    }
}