forked from mindoc-org/mindoc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
member_token.go
66 lines (51 loc) · 1.78 KB
/
member_token.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
package models
import (
"github.com/astaxie/beego/orm"
"github.com/lifei6671/mindoc/conf"
"time"
)
type MemberToken struct {
TokenId int `orm:"column(token_id);pk;auto;unique" json:"token_id"`
MemberId int `orm:"column(member_id);type(int)" json:"member_id"`
Token string `orm:"column(token);size(150);index" json:"token"`
Email string `orm:"column(email);size(255)" json:"email"`
IsValid bool `orm:"column(is_valid)" json:"is_valid"`
ValidTime time.Time `orm:"column(valid_time);null" json:"valid_time"`
SendTime time.Time `orm:"column(send_time);auto_now_add;type(datetime)" json:"send_time"`
}
// TableName 获取对应数据库表名.
func (m *MemberToken) TableName() string {
return "member_token"
}
// TableEngine 获取数据使用的引擎.
func (m *MemberToken) TableEngine() string {
return "INNODB"
}
func (m *MemberToken) TableNameWithPrefix() string {
return conf.GetDatabasePrefix() + m.TableName()
}
func NewMemberToken() *MemberToken {
return &MemberToken{}
}
func (m *MemberToken) InsertOrUpdate() (*MemberToken, error) {
o := orm.NewOrm()
if m.TokenId > 0 {
_, err := o.Update(m)
return m, err
}
_, err := o.Insert(m)
return m, err
}
func (m *MemberToken) FindByFieldFirst(field string, value interface{}) (*MemberToken, error) {
o := orm.NewOrm()
err := o.QueryTable(m.TableNameWithPrefix()).Filter(field, value).OrderBy("-token_id").One(m)
return m, err
}
func (m *MemberToken) FindSendCount(mail string, start_time time.Time, end_time time.Time) (int, error) {
o := orm.NewOrm()
c, err := o.QueryTable(m.TableNameWithPrefix()).Filter("send_time__gte", start_time.Format("2006-01-02 15:04:05")).Filter("send_time__lte", end_time.Format("2006-01-02 15:04:05")).Count()
if err != nil {
return 0, err
}
return int(c), nil
}