package initialization

import (
	"fmt"
	"gorm.io/driver/postgres"
	"gorm.io/gorm"
	"log"
	"testData/global"
	"testData/model"
	"time"
)

func InitPostgres() {
	dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=Asia/Shanghai",
		"192.168.0.56",
		"tpdb",
		"TpMysql12#$",
		"test_data",
		"5432",
	)
	if db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
		DisableForeignKeyConstraintWhenMigrating: true,
		//Logger:                                   logger.Default.LogMode(logger.Silent),
	}); err != nil {
		log.Printf("PostgreSQL Connection error: %s", err)
		panic(err)
	} else {
		postgresDB, _ := db.DB()
		postgresDB.SetMaxIdleConns(10)
		postgresDB.SetMaxOpenConns(100)
		postgresDB.SetConnMaxLifetime(time.Hour)
		_ = db.AutoMigrate(&model.FileHandled{})
		global.PostGreSQL = db
		_ = global.PostGreSQL.AutoMigrate(&model.FileHandled{})
	}
}
