forked from mindoc-org/mindoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Itemsets.go
274 lines (228 loc) · 8.17 KB
/
Itemsets.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package models
import (
"errors"
"strings"
"time"
"github.com/beego/beego/v2/client/orm"
"github.com/beego/beego/v2/core/logs"
"github.com/mindoc-org/mindoc/conf"
"github.com/mindoc-org/mindoc/utils"
"github.com/mindoc-org/mindoc/utils/cryptil"
)
//项目空间
type Itemsets struct {
ItemId int `orm:"column(item_id);pk;auto;unique" json:"item_id"`
ItemName string `orm:"column(item_name);size(500);description(项目空间名称)" json:"item_name"`
ItemKey string `orm:"column(item_key);size(100);unique;description(项目空间标识)" json:"item_key"`
Description string `orm:"column(description);type(text);null;description(描述)" json:"description"`
MemberId int `orm:"column(member_id);size(100);description(所属用户)" json:"member_id"`
CreateTime time.Time `orm:"column(create_time);type(datetime);auto_now_add;description(创建时间)" json:"create_time"`
ModifyTime time.Time `orm:"column(modify_time);type(datetime);null;auto_now;description(修改时间)" json:"modify_time"`
ModifyAt int `orm:"column(modify_at);type(int);description(修改人id)" json:"modify_at"`
BookNumber int `orm:"-" json:"book_number"`
CreateTimeString string `orm:"-" json:"create_time_string"`
CreateName string `orm:"-" json:"create_name"`
}
// TableName 获取对应数据库表名.
func (item *Itemsets) TableName() string {
return "itemsets"
}
// TableEngine 获取数据使用的引擎.
func (item *Itemsets) TableEngine() string {
return "INNODB"
}
func (item *Itemsets) TableNameWithPrefix() string {
return conf.GetDatabasePrefix() + item.TableName()
}
func (item *Itemsets) QueryTable() orm.QuerySeter {
return orm.NewOrm().QueryTable(item.TableNameWithPrefix())
}
func NewItemsets() *Itemsets {
return &Itemsets{}
}
func (item *Itemsets) First(itemId int) (*Itemsets, error) {
if itemId <= 0 {
return nil, ErrInvalidParameter
}
err := item.QueryTable().Filter("item_id", itemId).One(item)
if err != nil {
logs.Error("查询项目空间失败 -> item_id=", itemId, err)
} else {
item.Include()
}
return item, err
}
func (item *Itemsets) FindFirst(itemKey string) (*Itemsets, error) {
err := item.QueryTable().Filter("item_key", itemKey).One(item)
if err != nil {
logs.Error("查询项目空间失败 -> itemKey=", itemKey, err)
} else {
item.Include()
}
return item, err
}
func (item *Itemsets) Exist(itemId int) bool {
return item.QueryTable().Filter("item_id", itemId).Exist()
}
//保存
func (item *Itemsets) Save() (err error) {
item.ItemName = strings.TrimSpace(utils.StripTags(item.ItemName))
item.Description = strings.TrimSpace(utils.StripTags(item.Description))
item.ItemKey = strings.TrimSpace(item.ItemKey)
if item.ItemName == "" {
return errors.New("项目空间名称不能为空")
}
if item.ItemKey == "" {
item.ItemKey = cryptil.NewRandChars(16)
}
if item.QueryTable().Filter("item_id__ne", item.ItemId).Filter("item_key", item.ItemKey).Exist() {
return errors.New("项目空间标识已存在")
}
if item.ItemId > 0 {
_, err = orm.NewOrm().Update(item)
} else {
_, err = orm.NewOrm().Insert(item)
}
return
}
//删除.
func (item *Itemsets) Delete(itemId int) (err error) {
if itemId <= 0 {
return ErrInvalidParameter
}
if itemId == 1 {
return errors.New("默认项目空间不能删除")
}
if !item.Exist(itemId) {
return errors.New("项目空间不存在")
}
ormer := orm.NewOrm()
o, err := ormer.Begin()
if err != nil {
logs.Error("开启事物失败 ->", err)
return err
}
_, err = o.QueryTable(item.TableNameWithPrefix()).Filter("item_id", itemId).Delete()
if err != nil {
logs.Error("删除项目空间失败 -> item_id=", itemId, err)
o.Rollback()
}
_, err = o.Raw("update md_books set item_id=1 where item_id=?;", itemId).Exec()
if err != nil {
logs.Error("删除项目空间失败 -> item_id=", itemId, err)
o.Rollback()
}
return o.Commit()
}
func (item *Itemsets) Include() (*Itemsets, error) {
item.CreateTimeString = item.CreateTime.Format("2006-01-02 15:04:05")
if item.MemberId > 0 {
if m, err := NewMember().Find(item.MemberId, "account", "real_name"); err == nil {
if m.RealName != "" {
item.CreateName = m.RealName
} else {
item.CreateName = m.Account
}
}
}
i, err := NewBook().QueryTable().Filter("item_id", item.ItemId).Count()
if err != nil && err != orm.ErrNoRows {
return item, err
}
item.BookNumber = int(i)
return item, nil
}
//分页查询.
func (item *Itemsets) FindToPager(pageIndex, pageSize int) (list []*Itemsets, totalCount int, err error) {
offset := (pageIndex - 1) * pageSize
_, err = item.QueryTable().OrderBy("-item_id").Offset(offset).Limit(pageSize).All(&list)
if err != nil {
return
}
c, err := item.QueryTable().Count()
if err != nil {
return
}
totalCount = int(c)
for _, item := range list {
item.Include()
}
return
}
//根据项目空间名称查询.
func (item *Itemsets) FindItemsetsByName(name string, limit int) (*SelectMemberResult, error) {
result := SelectMemberResult{}
var itemsets []*Itemsets
var err error
if name == "" {
_, err = item.QueryTable().Limit(limit).All(&itemsets)
} else {
_, err = item.QueryTable().Filter("item_name__icontains", name).Limit(limit).All(&itemsets)
}
if err != nil {
logs.Error("查询项目空间失败 ->", err)
return &result, err
}
items := make([]KeyValueItem, 0)
for _, m := range itemsets {
item := KeyValueItem{}
item.Id = m.ItemId
item.Text = m.ItemName
items = append(items, item)
}
result.Result = items
return &result, err
}
//根据项目空间标识查询项目空间的项目列表.
func (item *Itemsets) FindItemsetsByItemKey(key string, pageIndex, pageSize, memberId int) (books []*BookResult, totalCount int, err error) {
o := orm.NewOrm()
err = item.QueryTable().Filter("item_key", key).One(item)
if err != nil {
logs.Error("查询项目空间时出错 ->", key, err)
return nil, 0, err
}
offset := (pageIndex - 1) * pageSize
//如果是登录用户
if memberId > 0 {
sql1 := `SELECT COUNT(*)
FROM md_books AS book
LEFT JOIN md_relationship AS rel ON rel.book_id = book.book_id AND rel.member_id = ?
left join (select book_id,min(role_id) as role_id
from (select book_id,role_id
from md_team_relationship as mtr
left join md_team_member as mtm on mtm.team_id=mtr.team_id and mtm.member_id=? order by role_id desc )
as t group by book_id) as team on team.book_id = book.book_id
WHERE book.item_id = ? AND (book.privately_owned = 0 or rel.role_id >= 0 or team.role_id >= 0)`
err = o.Raw(sql1, memberId, memberId, item.ItemId).QueryRow(&totalCount)
if err != nil {
logs.Error("查询项目空间时出错 ->", key, err)
return
}
sql2 := `SELECT book.*,rel1.*,mdmb.account AS create_name FROM md_books AS book
LEFT JOIN md_relationship AS rel ON rel.book_id = book.book_id AND rel.member_id = ?
left join (select book_id,min(role_id) as role_id from (select book_id,role_id
from md_team_relationship as mtr
left join md_team_member as mtm on mtm.team_id=mtr.team_id and mtm.member_id=? order by role_id desc )
as t group by book_id) as team
on team.book_id = book.book_id
LEFT JOIN md_relationship AS rel1 ON rel1.book_id = book.book_id AND rel1.role_id = 0
LEFT JOIN md_members AS mdmb ON rel1.member_id = mdmb.member_id
WHERE book.item_id = ? AND (book.privately_owned = 0 or rel.role_id >= 0 or team.role_id >= 0)
ORDER BY order_index desc,book.book_id DESC limit ? offset ?`
_, err = o.Raw(sql2, memberId, memberId, item.ItemId, pageSize, offset).QueryRows(&books)
return
} else {
count, err1 := o.QueryTable(NewBook().TableNameWithPrefix()).Filter("privately_owned", 0).Filter("item_id", item.ItemId).Count()
if err1 != nil {
err = err1
return
}
totalCount = int(count)
sql := `SELECT book.*,rel.*,mdmb.account AS create_name FROM md_books AS book
LEFT JOIN md_relationship AS rel ON rel.book_id = book.book_id AND rel.role_id = 0
LEFT JOIN md_members AS mdmb ON rel.member_id = mdmb.member_id
WHERE book.item_id = ? AND book.privately_owned = 0 ORDER BY order_index desc,book.book_id DESC limit ? offset ?`
_, err = o.Raw(sql, item.ItemId, pageSize, offset).QueryRows(&books)
return
}
}