package model

import (
	"os"

	_ "github.com/go-sql-driver/mysql"
	"go.uber.org/zap"
	"xorm.io/xorm"
	"xorm.io/xorm/dialects"

	"data_minio/global"
)

type Statistics struct {
	Total int64
}

func New(driveName, dsn string, showSql bool) (db *xorm.Engine) {
	db, _ = NewDbConnection(driveName, dsn)
	db.ShowSQL(showSql)
	db.Dialect().SetQuotePolicy(dialects.QuotePolicyReserved)
	err := db.Sync2(
		&Dataset{},
		&DatasetDetail{},
		&FileManager{},
	)
	if err != nil {
		global.Logger.Error("同步数据库表结构", zap.Error(err))
		os.Exit(1)
	}
	return db
}

func NewDbConnection(driveName, dsn string) (db *xorm.Engine, err error) {
	db, err = xorm.NewEngine(driveName, dsn)
	if err != nil {
		zap.L().Error("创建数据库连接", zap.Error(err))
		os.Exit(-1)
	}
	db.SetMaxOpenConns(300)
	return
}
