forked from phachon/mm-wiki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collection.go
193 lines (172 loc) · 4.5 KB
/
collection.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package models
import (
"mm-wiki/app/utils"
"github.com/snail007/go-activerecord/mysql"
)
const (
Collection_Type_Doc = 1
Collection_Type_Space = 2
)
const Table_Collection_Name = "collection"
type Collection struct {
}
var CollectionModel = Collection{}
// get collection by collection_id
func (c *Collection) GetCollectionByCollectionId(collectionId string) (collection map[string]string, err error) {
db := G.DB()
var rs *mysql.ResultSet
rs, err = db.Query(db.AR().From(Table_Collection_Name).Where(map[string]interface{}{
"collection_id": collectionId,
}))
if err != nil {
return
}
collection = rs.Row()
return
}
// get collections by user_id
func (c *Collection) GetCollectionsByUserId(userId string) (collections []map[string]string, err error) {
db := G.DB()
var rs *mysql.ResultSet
rs, err = db.Query(db.AR().From(Table_Collection_Name).Where(map[string]interface{}{
"user_id": userId,
}))
if err != nil {
return
}
collections = rs.Rows()
return
}
// get collections by user_id
func (c *Collection) GetCollectionsByUserIdAndType(userId string, typeS int) (collections []map[string]string, err error) {
db := G.DB()
var rs *mysql.ResultSet
rs, err = db.Query(db.AR().From(Table_Collection_Name).Where(map[string]interface{}{
"user_id": userId,
"type": typeS,
}))
if err != nil {
return
}
collections = rs.Rows()
return
}
// get collections by user_id type and resource_id
func (c *Collection) GetCollectionByUserIdTypeAndResourceId(userId string, typeS int, resId string) (collection map[string]string, err error) {
db := G.DB()
var rs *mysql.ResultSet
rs, err = db.Query(db.AR().From(Table_Collection_Name).Where(map[string]interface{}{
"user_id": userId,
"type": typeS,
"resource_id": resId,
}).Limit(0, 1))
if err != nil {
return
}
collection = rs.Row()
return
}
// delete collection by collection_id
func (c *Collection) Delete(collectionId string) (err error) {
db := G.DB()
_, err = db.Exec(db.AR().Delete(Table_Collection_Name, map[string]interface{}{
"collection_id": collectionId,
}))
if err != nil {
return
}
return
}
// delete collection by type and object_id
func (c *Collection) DeleteByResourceIdType(resourceId string, collectType string) (err error) {
db := G.DB()
_, err = db.Exec(db.AR().Delete(Table_Collection_Name, map[string]interface{}{
"type": collectType,
"resource_id": resourceId,
}))
if err != nil {
return
}
return
}
// insert collection
func (c *Collection) Insert(collectionValue map[string]interface{}) (id int64, err error) {
db := G.DB()
var rs *mysql.ResultSet
rs, err = db.Exec(db.AR().Insert(Table_Collection_Name, collectionValue))
if err != nil {
return
}
id = rs.LastInsertId
return
}
// get all collections
func (c *Collection) GetCollections() (collections []map[string]string, err error) {
db := G.DB()
var rs *mysql.ResultSet
rs, err = db.Query(
db.AR().From(Table_Collection_Name))
if err != nil {
return
}
collections = rs.Rows()
return
}
// get collection count
func (c *Collection) CountCollections() (count int64, err error) {
db := G.DB()
var rs *mysql.ResultSet
rs, err = db.Query(
db.AR().
Select("count(*) as total").
From(Table_Collection_Name))
if err != nil {
return
}
count = utils.NewConvert().StringToInt64(rs.Value("total"))
return
}
// get collections by many collection_id
func (c *Collection) GetCollectionsByCollectionIds(collectionIds []string) (collections []map[string]string, err error) {
db := G.DB()
var rs *mysql.ResultSet
rs, err = db.Query(db.AR().From(Table_Collection_Name).Where(map[string]interface{}{
"collection_id": collectionIds,
}))
if err != nil {
return
}
collections = rs.Rows()
return
}
func (c *Collection) GetResourceIdsOrderByCountLimit(limit int, collectType int) (collects []map[string]string, err error) {
db := G.DB()
var rs *mysql.ResultSet
sql := db.AR().Select("resource_id, count('resource_id') as total").
From(Table_Collection_Name).Where(map[string]interface{}{
"type": collectType,
}).
GroupBy("resource_id").
OrderBy("total", "DESC").
Limit(0, limit)
rs, err = db.Query(sql)
if err != nil {
return
}
collects = rs.Rows()
return
}
func (c *Collection) GetCollectionGroupUserId(colType int) (collects []map[string]string, err error) {
db := G.DB()
var rs *mysql.ResultSet
sql := db.AR().Select("user_id, count('user_id') as total").
From(Table_Collection_Name).Where(map[string]interface{}{
"type": colType,
}).GroupBy("user_id")
rs, err = db.Query(sql)
if err != nil {
return
}
collects = rs.Rows()
return
}