package system

import (
	"fmt"
	"os"
)

// InitDBRequest 初始化数据库请求
type InitDBRequest struct {
	AdminPassword string `json:"adminPassword" binding:"required"`
	DBType        string `json:"dbType"`
	Host          string `json:"host"`
	Port          string `json:"port"`
	UserName      string `json:"userName"`
	Password      string `json:"password"`
	DBName        string `json:"dbName" binding:"required"`
	DBPath        string `json:"dbPath"`
	Template      string `json:"template"`
}

// MysqlEmptyDsn MySQL空数据库建库链接
func (i *InitDBRequest) MysqlEmptyDsn() string {
	if i.Host == "" {
		i.Host = "127.0.0.1"
	}
	if i.Port == "" {
		i.Port = "3306"
	}
	return fmt.Sprintf("%s:%s@tcp(%s:%s)/", i.UserName, i.Password, i.Host, i.Port)
}

// PgsqlEmptyDsn PostgreSQL空数据库建库链接
func (i *InitDBRequest) PgsqlEmptyDsn() string {
	if i.Host == "" {
		i.Host = "127.0.0.1"
	}
	if i.Port == "" {
		i.Port = "5432"
	}
	return "host=" + i.Host + " user=" + i.UserName + " password=" + i.Password + " port=" + i.Port + " dbname=" + "postgres" + " " + "sslmode=disable TimeZone=Asia/Shanghai"
}

// SqliteEmptyDsn SQLite空数据库建库链接
func (i *InitDBRequest) SqliteEmptyDsn() string {
	separator := string(os.PathSeparator)
	return i.DBPath + separator + i.DBName + ".db"
}

// MssqlEmptyDsn MSSQL空数据库建库链接
func (i *InitDBRequest) MssqlEmptyDsn() string {
	return "sqlserver://" + i.UserName + ":" + i.Password + "@" + i.Host + ":" + i.Port + "?database=" + i.DBName + "&encrypt=disable"
}

// CheckDBResponse 检查数据库响应
type CheckDBResponse struct {
	NeedInit bool `json:"needInit"`
}
