forked from FloatTech/ZeroBot-Plugin
/
model.go
135 lines (119 loc) · 3 KB
/
model.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package score
import (
"os"
"time"
"github.com/jinzhu/gorm"
)
// sdb 得分数据库
var sdb *scoredb
// 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.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.Model(&scoretable{}).First(&s, "uid = ? ", uid).Error; err != nil {
// error handling...
if gorm.IsRecordNotFoundError(err) {
err = db.Model(&scoretable{}).Create(&s).Error // newUser not user
}
} else {
err = db.Model(&scoretable{}).Where("uid = ? ", uid).Update(
map[string]any{
"score": score,
}).Error
}
return
}
// GetSignInByUID 取得签到次数
func (sdb *scoredb) GetSignInByUID(uid int64) (si signintable) {
db := (*gorm.DB)(sdb)
db.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.Model(&signintable{}).First(&si, "uid = ? ", uid).Error; err != nil {
// error handling...
if gorm.IsRecordNotFoundError(err) {
err = db.Model(&signintable{}).Create(&si).Error // newUser not user
}
} else {
err = db.Model(&signintable{}).Where("uid = ? ", uid).Update(
map[string]any{
"count": count,
}).Error
}
return
}
func (sdb *scoredb) GetScoreRankByTopN(n int) (st []scoretable, err error) {
db := (*gorm.DB)(sdb)
err = db.Model(&scoretable{}).Order("score desc").Limit(n).Find(&st).Error
return
}
type scdata struct {
drawedfile string
picfile string
uid int64
nickname string
inc int // 增加币
score int // 钱包
level int
rank int
}