-
Notifications
You must be signed in to change notification settings - Fork 2
/
model.go
102 lines (90 loc) · 2.22 KB
/
model.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"github.com/jinzhu/gorm"
"github.com/pkg/errors"
"time"
)
type User struct {
ID int
Username string
Nickname string
Email string
Tel string
LastGetNewMessageTime int
LastSendEmailTime int
LastSendWxTime int
WechatOpenID string
}
type Message struct {
ID int
ReceiverID int
IsRead bool
WxSent bool
Content string
SenderID int
Sender User `gorm:"ForeignKey:SenderID"`
CreatedAt time.Time
UpdatedAt time.Time
}
type SendConfig struct {
FromName string
SendID string
To string
Body string
Title string
}
type TextConfig struct {
To string
Body string
}
func (Message) TableName() string {
return "message"
}
func ListUser(db *gorm.DB) (users []User, err error) {
err = db.Find(&users).Error
if err != nil {
err = errors.Wrap(err, "ListUser")
return
}
return
}
func MessagesByUserID(db *gorm.DB, userID int) (msgs []Message, err error) {
err = db.Where("receiver_id = ?", userID).Where("is_read = ?", false).Find(&msgs).Error
if err != nil {
err = errors.Wrap(err, "MessagesByUserID")
return
}
return
}
func WeChatMessagesByUserID(db *gorm.DB, userID int) (msgs []Message, err error) {
err = db.Where("receiver_id = ?", userID).Where("is_read = ?", false).Where("wx_sent = ?", false).Preload("Sender").Find(&msgs).Error
if err != nil {
err = errors.Wrap(err, "MessagesByUserID")
return
}
return
}
func SetUserEmailLock(db *gorm.DB, user *User) (err error) {
err = db.Model(user).Where("id = ?", user.ID).Update("last_send_email_time", int(time.Now().Unix())).Error
if err != nil {
err = errors.Wrap(err, "SetUserEmailLock")
return
}
return
}
func SetUserWxLock(db *gorm.DB, user *User) (err error) {
err = db.Model(user).Where("id = ?", user.ID).Update("last_send_wx_time", int(time.Now().Unix())).Error
if err != nil {
err = errors.Wrap(err, "SetUserWxLock")
return
}
return
}
func SetMsgWechatSent(db *gorm.DB, msg *Message) (err error) {
err = db.Table("message").Where("id = ?", msg.ID).Updates(map[string]interface{}{"wx_sent": true}).Error
if err != nil {
err = errors.Wrap(err, "SetMsgWechatSent")
return
}
return
}