package models

import (
	"fmt"
	"git.wanbits.cc/sin/flytalk/config"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"time"
)

var DB *gorm.DB

type Model struct {
	ID        uint       `gorm:"primary_key" json:"id"`
	CreatedAt time.Time  `json:"created_at"`
	UpdatedAt time.Time  `json:"updated_at"`
	DeletedAt *time.Time `sql:"index" json:"deleted_at"`
}

func InitDb() {
	dbc := config.C.Database
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		dbc.Username, dbc.Password, dbc.Server, dbc.Port, dbc.Db)

	var err error
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic("connect to database failed")
	}
}

func Execute(sql string) {
	if len(sql) > 0 {
		DB.Exec(sql)
	}
}

func CloseDB() {
}
