59 lines
1.1 KiB
Go
59 lines
1.1 KiB
Go
package model
|
|
|
|
import (
|
|
"database/sql/driver"
|
|
"encoding/json"
|
|
"fmt"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type RecentValue struct {
|
|
// 最后登录时间 (这是一个int字符串)
|
|
LastLoginTime string `json:"t"`
|
|
}
|
|
|
|
func (r *RecentValue) Scan(v any) error {
|
|
if r == nil {
|
|
*r = RecentValue{}
|
|
}
|
|
switch js := v.(type) {
|
|
case string:
|
|
if len(js) <= 0 {
|
|
return nil
|
|
}
|
|
return json.Unmarshal([]byte(js), r)
|
|
case []byte:
|
|
if len(js) <= 0 {
|
|
return nil
|
|
}
|
|
return json.Unmarshal(js, r)
|
|
default:
|
|
return fmt.Errorf(fmt.Sprintf("rule is %T but not string", v))
|
|
}
|
|
}
|
|
func (r *RecentValue) Value() (driver.Value, error) {
|
|
data, err := json.Marshal(r)
|
|
return string(data), err
|
|
}
|
|
|
|
type UserRecent struct {
|
|
Uid int64 `gorm:"column:uid"`
|
|
Value *RecentValue `gorm:"column:value"`
|
|
}
|
|
|
|
func (r *UserRecent) TableName() string {
|
|
return fmt.Sprintf("t_recent%d", r.Uid%10)
|
|
}
|
|
|
|
type UserRecentOp struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewUserRecentOp() *UserRecentOp {
|
|
return &UserRecentOp{db: dataDB}
|
|
}
|
|
func (op *UserRecentOp) Get(uid int64) (*UserRecent, error) {
|
|
res := &UserRecent{Uid: uid}
|
|
return res, op.db.Table(res.TableName()).Where("uid=?", uid).First(res).Error
|
|
}
|