2017-05-03 14:22:05 +08:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2018-01-26 17:17:38 +08:00
|
|
|
"time"
|
2021-03-23 15:09:17 +08:00
|
|
|
|
2021-03-26 11:34:02 +08:00
|
|
|
"github.com/beego/beego/v2/client/orm"
|
2021-03-23 15:09:17 +08:00
|
|
|
"github.com/mindoc-org/mindoc/conf"
|
2017-05-03 14:22:05 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type MemberToken struct {
|
2018-01-26 17:17:38 +08:00
|
|
|
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"`
|
2017-05-03 14:22:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// TableName 获取对应数据库表名.
|
|
|
|
func (m *MemberToken) TableName() string {
|
|
|
|
return "member_token"
|
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
|
2017-05-03 14:22:05 +08:00
|
|
|
// TableEngine 获取数据使用的引擎.
|
|
|
|
func (m *MemberToken) TableEngine() string {
|
|
|
|
return "INNODB"
|
|
|
|
}
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
func (m *MemberToken) TableNameWithPrefix() string {
|
|
|
|
return conf.GetDatabasePrefix() + m.TableName()
|
2017-05-03 14:22:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewMemberToken() *MemberToken {
|
|
|
|
return &MemberToken{}
|
|
|
|
}
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
func (m *MemberToken) InsertOrUpdate() (*MemberToken, error) {
|
2017-05-03 14:22:05 +08:00
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
|
|
if m.TokenId > 0 {
|
2018-01-26 17:17:38 +08:00
|
|
|
_, err := o.Update(m)
|
|
|
|
return m, err
|
2017-05-03 14:22:05 +08:00
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
_, err := o.Insert(m)
|
2017-05-03 14:22:05 +08:00
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
return m, err
|
2017-05-03 14:22:05 +08:00
|
|
|
}
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
func (m *MemberToken) FindByFieldFirst(field string, value interface{}) (*MemberToken, error) {
|
2017-05-03 14:22:05 +08:00
|
|
|
o := orm.NewOrm()
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
err := o.QueryTable(m.TableNameWithPrefix()).Filter(field, value).OrderBy("-token_id").One(m)
|
2017-05-03 14:22:05 +08:00
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
return m, err
|
2017-05-03 14:22:05 +08:00
|
|
|
}
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
func (m *MemberToken) FindSendCount(mail string, start_time time.Time, end_time time.Time) (int, error) {
|
2017-05-03 14:22:05 +08:00
|
|
|
o := orm.NewOrm()
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
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()
|
2017-05-03 14:22:05 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2018-01-26 17:17:38 +08:00
|
|
|
return 0, err
|
2017-05-03 14:22:05 +08:00
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
return int(c), nil
|
|
|
|
}
|