package database

import (
	_ "github.com/mattn/go-sqlite3"
	"xorm.io/xorm"
)

var engine *xorm.Engine

type Wol struct {
	ID   int64
	Name string `xorm:"varchar(64)"`
	Mac  string `xorm:"varchar(64)"`
}

func init() {
	var err error
	file := "./wol.db"
	engine, err = xorm.NewEngine("sqlite3", file)
	if err != nil {
		println(err.Error())

	}
	res, err := engine.IsTableExist("wol")
	if !res {
		CreateTable()
	}
	if err != nil {
		println(err.Error())

	}

}
func CreateTable() int {
	err := engine.Sync2(new(Wol))
	if err != nil {
		println(err.Error())
	}
	return 0
}
func Getallwol(everyone *[]Wol) {
	err := engine.Find(&(*everyone))
	if err != nil {
		println("Getallwol err", err.Error())
	}
}

func Getallwoltext() {
	everyone := make([]Wol, 0)
	err := engine.Find(&everyone)
	if err != nil {
		println("Getallwoltext", err.Error())
	}
	println(len(everyone))
}

func Insert(wol *Wol) {
	affected, err := engine.Insert(wol)
	if err != nil {
		println(err.Error())
	}
	println(affected)
}
func Del(id int64) {
	affected, err := engine.Where("i_d = ?", id).Delete(&Wol{})
	if err != nil {
		println(err)
	}
	println(affected, "ok")
}
