-
Notifications
You must be signed in to change notification settings - Fork 20
/
db_models.go
132 lines (111 loc) · 2.73 KB
/
db_models.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package ppp
import (
proto "github.com/panjjo/ppp/proto"
)
const (
tradeTable = "trades"
)
func getTrade(q interface{}) *proto.Trade {
session := DBPool.Get()
defer session.Close()
trade := &proto.Trade{}
res := session.FindOne(tradeTable, q, trade)
if res != nil {
trade = res.(*proto.Trade)
}
return trade
}
func saveTrade(trade *proto.Trade) error {
session := DBPool.Get()
defer session.Close()
return session.Save(tradeTable, trade)
}
func updateTrade(query, update interface{}) error {
session := DBPool.Get()
defer session.Close()
return session.Update(tradeTable, query, update)
}
func upsertTrade(query, update interface{}) (interface{}, error) {
session := DBPool.Get()
defer session.Close()
return session.UpSert(tradeTable, query, update)
}
const (
refundTable = "refunds"
)
func saveRefund(refund *proto.Refund) error {
session := DBPool.Get()
defer session.Close()
return session.Save(refundTable, refund)
}
const (
userTable = "users"
)
// 查询用户
func getUser(userid, t string) *proto.User {
session := DBPool.Get()
defer session.Close()
user := &proto.User{}
res := session.FindOne(userTable, map[string]interface{}{"userid": userid, "from": t}, user)
if res != nil {
user = res.(*proto.User)
}
return user
}
// 更新用户
func updateUser(query, update interface{}) error {
session := DBPool.Get()
defer session.Close()
return session.Update(userTable, query, update)
}
//
func updateUserMulti(query, update interface{}) error {
session := DBPool.Get()
defer session.Close()
_, err := session.UpAll(userTable, query, update)
return err
}
// 保存用户
func saveUser(user *proto.User) error {
session := DBPool.Get()
defer session.Close()
return session.Save(userTable, user)
}
const (
accountTable = "accounts"
)
// 获取授权
func getToken(mchid, appid string) *proto.Account {
session := DBPool.Get()
defer session.Close()
auth := &proto.Account{}
res := session.FindOne(accountTable, map[string]interface{}{"mchid": mchid, "appid": appid}, auth)
if res != nil {
auth = res.(*proto.Account)
}
return auth
}
// 刷新授权
func updateToken(mchid, appid string, update interface{}) error {
session := DBPool.Get()
defer session.Close()
return session.Update(accountTable, map[string]interface{}{"mchid": mchid, "appid": appid}, update)
}
// 保存授权
func saveToken(auth *proto.Account) error {
session := DBPool.Get()
defer session.Close()
return session.Save(accountTable, auth)
}
// 通过userid 或者 mchid 获取授权信息
func token(userid, mchid, t, appid string) *proto.Account {
auth := &proto.Account{}
if mchid == "" {
user := getUser(userid, t)
if user.Status != proto.Accountstatus_ok {
return auth
}
mchid = user.MchID
}
return getToken(mchid, appid)
}