package dbsql

import (
	"fmt"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var DSN = ""

const DSN_local = "root:flatincbr@tcp(qa.flatincbr.work:3306)/qa?charset=utf8&parseTime=true"

// 初始化数据库
func GetConn(dsn string) (*gorm.DB, error) {
	// D_airecommen:NbI40G9iTK@tcp(test-nimodatatag.cluster-cxxsbkavtbvo.ap-southeast-1.rds.amazonaws.com:3306)/labeldb?charset=utf8&parseTime=true
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		fmt.Println(err.Error())
	}
	return db, nil
}

func Close(db *gorm.DB) {
	sqlDB, err := db.DB()
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	sqlDB.Close()
}

func SetPageQuery(db *gorm.DB, pageIndex int, pageSize int) *gorm.DB {
	return db.Limit(pageSize).Offset((pageIndex - 1) * pageSize)
}
