package initialize

import (
	"os"
	"server/dao"

	"go.uber.org/zap"
	"gorm.io/gorm"
	"server/global"
	"server/model/example"
)

func Gorm() *gorm.DB {
	switch global.GVA_CONFIG.System.DbType {
	case "mysql":
		return GormMysql()
	case "pgsql":
		return GormPgSql()
	case "oracle":
		return GormOracle()
	case "mssql":
		return GormMssql()
	case "sqlite":
		return GormSqlite()
	default:
		return GormMysql()
	}
}

func RegisterTables() {
	db := global.GVA_DB
	err := db.AutoMigrate(

		dao.SysApi{},
		dao.SysUser{},
		dao.SysBaseMenu{},
		dao.JwtBlacklist{},
		dao.SysAuthority{},
		dao.SysDictionary{},
		dao.SysOperationRecord{},
		dao.SysDictionaryDetail{},
		dao.SysBaseMenuParameter{},
		dao.SysBaseMenuBtn{},
		dao.SysAuthorityBtn{},

		example.ExaFile{},
		example.ExaFileChunk{},
		dao.ExaFileUploadAndDownload{},
		dao.Department{},
		dao.Project{},
		dao.ProjectState{},
		dao.ProjectFile{},
		dao.FileGenre{},
		dao.NecessaryDocument{},
		dao.ProjectWorkingHours{},
		dao.Collection{},
		dao.Borrowing{},
		dao.ExpensesGenre{},
		dao.DailyExpenses{},
		dao.DailyFeeDetails{},
		dao.DailyFile{},
		dao.Reimbursement{},
		dao.FeeDetails{},
		dao.Constructor{},
		dao.Expenses{},
		dao.MonthExpenses{},
		dao.ProjectFee{},
		dao.ProjectFeeGenre{},
		dao.ProjectMonthFee{},

		dao.Node{},
		dao.Approver{},
		dao.Process{},
		dao.Description{},
		dao.ProjectProcess{},
	)
	if err != nil {
		global.GVA_LOG.Error("register table failed", zap.Error(err))
		os.Exit(0)
	}
	global.GVA_LOG.Info("register table success")
}
