ZeroBot-Plugin/plugin_score/model.go
2022-01-12 11:33:12 +08:00

117 lines
2.7 KiB
Go

package score
import (
"os"
"time"
"github.com/jinzhu/gorm"
_ "github.com/logoove/sqlite" // import sql
)
// scoredb 分数数据库
type scoredb gorm.DB
// scoretable 分数结构体
type scoretable struct {
UID int64 `gorm:"column:uid;primary_key"`
Score int `gorm:"column:score;default:0"`
}
// TableName ...
func (scoretable) TableName() string {
return "score"
}
// signintable 签到结构体
type signintable struct {
UID int64 `gorm:"column:uid;primary_key"`
Count int `gorm:"column:count;default:0"`
UpdatedAt time.Time
}
// TableName ...
func (signintable) TableName() string {
return "sign_in"
}
// initialize 初始化ScoreDB数据库
func initialize(dbpath string) *scoredb {
var err error
if _, err = os.Stat(dbpath); err != nil || os.IsNotExist(err) {
// 生成文件
f, err := os.Create(dbpath)
if err != nil {
return nil
}
defer f.Close()
}
gdb, err := gorm.Open("sqlite3", dbpath)
if err != nil {
panic(err)
}
gdb.AutoMigrate(&scoretable{}).AutoMigrate(&signintable{})
return (*scoredb)(gdb)
}
// Close ...
func (sdb *scoredb) Close() error {
db := (*gorm.DB)(sdb)
return db.Close()
}
// GetScoreByUID 取得分数
func (sdb *scoredb) GetScoreByUID(uid int64) (s scoretable) {
db := (*gorm.DB)(sdb)
db.Debug().Model(&scoretable{}).FirstOrCreate(&s, "uid = ? ", uid)
return s
}
// InsertOrUpdateScoreByUID 插入或更新分数
func (sdb *scoredb) InsertOrUpdateScoreByUID(uid int64, score int) (err error) {
db := (*gorm.DB)(sdb)
s := scoretable{
UID: uid,
Score: score,
}
if err = db.Debug().Model(&scoretable{}).First(&s, "uid = ? ", uid).Error; err != nil {
// error handling...
if gorm.IsRecordNotFoundError(err) {
db.Debug().Model(&scoretable{}).Create(&s) // newUser not user
}
} else {
err = db.Debug().Model(&scoretable{}).Where("uid = ? ", uid).Update(
map[string]interface{}{
"score": score,
}).Error
}
return
}
// GetSignInByUID 取得签到次数
func (sdb *scoredb) GetSignInByUID(uid int64) (si signintable) {
db := (*gorm.DB)(sdb)
db.Debug().Model(&signintable{}).FirstOrCreate(&si, "uid = ? ", uid)
return si
}
// InsertOrUpdateSignInCountByUID 插入或更新签到次数
func (sdb *scoredb) InsertOrUpdateSignInCountByUID(uid int64, count int) (err error) {
db := (*gorm.DB)(sdb)
si := signintable{
UID: uid,
Count: count,
}
if err = db.Debug().Model(&signintable{}).First(&si, "uid = ? ", uid).Error; err != nil {
// error handling...
if gorm.IsRecordNotFoundError(err) {
db.Debug().Model(&signintable{}).Create(&si) // newUser not user
}
} else {
err = db.Debug().Model(&signintable{}).Where("uid = ? ", uid).Update(
map[string]interface{}{
"count": count,
}).Error
}
return
}