From bef52f11302bce261f431beecdcf54cf5eced3a1 Mon Sep 17 00:00:00 2001 From: cdle <798731886@qq.com> Date: Thu, 19 Aug 2021 19:50:25 +0800 Subject: [PATCH] x --- models/bot.go | 3 +++ models/db.go | 1 + models/user.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 models/user.go diff --git a/models/bot.go b/models/bot.go index 6b1917b..a8c6dea 100644 --- a/models/bot.go +++ b/models/bot.go @@ -100,6 +100,9 @@ var handleMessage = func(msgs ...interface{}) interface{} { if len(msgs) == 4 { gid = msgs[3].(int) } + if new, rt := NewActiveUser(tp, uid); new { + sendMessagee(rt, msgs...) + } switch msg { case "status", "状态": if !isAdmin(msgs...) { diff --git a/models/db.go b/models/db.go index 93824bc..171e107 100644 --- a/models/db.go +++ b/models/db.go @@ -38,6 +38,7 @@ func initDB() { db.AutoMigrate( &JdCookie{}, &JdCookiePool{}, + &User{}, ) keys = make(map[string]bool) pins = make(map[string]bool) diff --git a/models/user.go b/models/user.go new file mode 100644 index 0000000..85c71f7 --- /dev/null +++ b/models/user.go @@ -0,0 +1,56 @@ +package models + +import ( + "fmt" + "time" + + "gorm.io/gorm" +) + +type User struct { + ID int + Number int + Class string + ActiveAt time.Time + Coin int +} + +func NewActiveUser(class string, uid int) (bool, string) { + if class == "tgg" { + class = "tg" + } + if class == "qqg" { + class = "qq" + } + zero, _ := time.ParseInLocation("2006-01-02", time.Now().Local().Format("2006-01-02"), time.Local) + var u User + var ntime = time.Now() + var first = false + total := []int{} + err := db.Where("class = ? and number = ?", class, uid).First(&u).Error + if err != nil { + first = true + u = User{ + Class: class, + Number: uid, + Coin: 1, + ActiveAt: ntime, + } + if err := db.Create(u).Error; err != nil { + return true, err.Error() + } + } else { + if zero.After(u.ActiveAt) { + first = true + db.Updates(map[string]interface{}{ + "active_at": ntime, + "coin": gorm.Expr("coin+1"), + }) + u.Coin++ + } + } + if first { + db.Select("count(id) as total").Where("active_at > ?", u.ActiveAt).Pluck("total", &total) + } + return first, fmt.Sprintf("你是今天第%d个发言的用户,奖励%d个心愿币,心愿币余额%d", total[0]+1, 1, u.Coin) +}