package com.njcn.gather.systemops.database.pojo.param;

import com.fasterxml.jackson.annotation.JsonAlias;
import com.njcn.web.pojo.param.BaseParam;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * 数据库连接配置参数。
 */
@Data
@ApiModel("数据库连接配置参数")
public class DatabaseConnectionParam {

    @ApiModelProperty("连接名称")
    @NotBlank(message = "连接名称不能为空")
    private String connectionName;

    @ApiModelProperty("数据库类型：ORACLE、MYSQL")
    private String dbType;

    @ApiModelProperty("数据库主机地址")
    @NotBlank(message = "数据库主机地址不能为空")
    private String host;

    @ApiModelProperty("数据库端口")
    @NotNull(message = "数据库端口不能为空")
    private Integer port;

    @ApiModelProperty("连接类型：SERVICE_NAME、SID")
    private String connectType;

    @ApiModelProperty("服务名")
    private String serviceName;

    @ApiModelProperty("SID")
    private String sid;

    @ApiModelProperty("数据库名，MySQL 使用")
    private String databaseName;

    @ApiModelProperty("Schema")
    private String schemaName;

    @ApiModelProperty("用户名")
    @NotBlank(message = "用户名不能为空")
    private String username;

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

    @ApiModelProperty("是否保存密码：0-否，1-是")
    private Integer savePassword;

    @ApiModelProperty("Oracle Directory 名称")
    private String directoryName;

    @ApiModelProperty("Oracle Directory 物理路径")
    private String directoryPath;

    @ApiModelProperty("扩展配置 JSON")
    private String extraConfigJson;

    @ApiModelProperty("备注")
    private String remark;

    @Data
    @EqualsAndHashCode(callSuper = true)
    @ApiModel("数据库连接更新参数")
    public static class UpdateParam extends DatabaseConnectionParam {
        @ApiModelProperty("连接 ID")
        @NotBlank(message = "连接 ID 不能为空")
        private String id;
    }

    @Data
    @EqualsAndHashCode(callSuper = true)
    @ApiModel("数据库连接查询参数")
    public static class QueryParam extends BaseParam {
        @ApiModelProperty("连接名称")
        private String connectionName;
        @ApiModelProperty("数据库类型")
        private String dbType;
        @ApiModelProperty("Schema")
        private String schemaName;
    }

    @Data
    @ApiModel("数据库连接删除参数")
    public static class DeleteParam {
        @ApiModelProperty("连接 ID")
        @NotBlank(message = "连接 ID 不能为空")
        private String id;
    }

    @Data
    @ApiModel("数据库连接测试参数")
    public static class TestParam {
        @ApiModelProperty("连接 ID，已有连接测试时传入")
        private String connectionId;
        @ApiModelProperty("临时连接参数，新增前测试时传入")
        private DatabaseConnectionParam connection;
        @ApiModelProperty("临时密码，测试时允许只传该字段而不写入 connection.password")
        private String temporaryPassword;
    }

    @Data
    @ApiModel("数据库表查询参数")
    public static class TablesParam {
        @ApiModelProperty("连接 ID")
        @NotBlank(message = "连接 ID 不能为空")
        private String connectionId;
        @ApiModelProperty("临时密码，不保存密码时传入")
        private String temporaryPassword;
        @ApiModelProperty("兼容前端传入的运行时密码；为空时复用数据库 password_cipher")
        private String password;
        @JsonAlias("password_cipher")
        @ApiModelProperty("兼容前端传入的已保存密码")
        private String passwordCipher;
        @ApiModelProperty("兼容前端传入的临时连接参数")
        private DatabaseConnectionParam connection;
        @ApiModelProperty("Schema 或数据库名，不传则使用连接默认值")
        private String schemaName;
    }
}
