xdd-plus/models/user.go
2021-08-19 19:50:25 +08:00

57 lines
1.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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)
}