package utilities

import (
	"database/sql"
	_ "github.com/go-sql-driver/mysql"
	_ "github.com/godror/godror"
)

var db *sql.DB
type connect struct {
	HOST string
	PORT string
	DATABASE string
	USERNAME string
	PASSWORD string
	CHARSET string
	PARSETIME string
	SID string
}

func Mysql()(baseDB *sql.DB, err error)  {
	mysql := connect{
		HOST:     "was.baoshi56.com",
		PORT:     "3306",
		DATABASE: "bswas",
		USERNAME: "developer",
		PASSWORD: "developer",
		CHARSET: "utf8mb4",
		PARSETIME: "True",
	}
	driver := mysql.USERNAME+":"+mysql.PASSWORD+"@"+"tcp("+mysql.HOST+":"+mysql.PORT+")/"+mysql.DATABASE+"?charset="+mysql.CHARSET
	if mysql.PARSETIME != "" {
		driver += "&parseTime="+mysql.PARSETIME
	}
	db, err = sql.Open("mysql",driver)
	if err != nil {
		WriteLog("/utilities/connect.go:37   Mysql数据库连接错误!","ERROR")
		return
	}
	db.SetMaxOpenConns(20)
	db.SetMaxIdleConns(10)
	return db,nil
}

func Oracle()(baseDB *sql.DB, err error){
	oracle := connect{
		HOST:      "47.103.12.61",
		PORT:      "1521",
		USERNAME:  "WMS_USER",
		PASSWORD:  "WMS_USER",
		SID: 	   "orcl",
	}
	db, err := sql.Open("godror",`user="`+oracle.USERNAME+`" password="`+oracle.PASSWORD+`" connectString="`+oracle.HOST+`:`+oracle.PORT+`/`+oracle.SID+`"`)
	if err != nil {
		WriteLog("/utilities/connect.go:55   Oracle数据库连接错误!","ERROR")
		return
	}
	db.SetMaxOpenConns(20)
	db.SetMaxIdleConns(10)
	return db,nil
}
