-
Notifications
You must be signed in to change notification settings - Fork 745
/
Copy pathRecycleModel.go
379 lines (317 loc) · 9.88 KB
/
RecycleModel.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package models
import (
"fmt"
"strings"
"time"
"github.com/TruthHun/DocHub/helper"
"github.com/astaxie/beego/orm"
)
//文档回收站
type DocumentRecycle struct {
Id int `orm:"column(Id)"` //对应的文档id
Uid int `orm:"default(0);column(Uid)"` //操作用户
Date int `orm:"default(0);column(Date)"` //操作时间
Self bool `orm:"default(false);column(Self)"` //是否是文档上传用户删除的,默认为false。如果是文档上传者删除的,设置为true
}
func NewDocumentRecycle() *DocumentRecycle {
return &DocumentRecycle{}
}
func GetTableDocumentRecycle() string {
return getTable("document_recycle")
}
//将文档从回收站中恢复过来,文档的状态必须是-1才可以
//@param ids 文档id
//@return err 返回错误,nil表示恢复成功,否则恢复失败
func (this *DocumentRecycle) RecoverFromRecycle(ids ...interface{}) (err error) {
var (
docInfo []DocumentInfo
dsId []interface{} //document_store id
o = orm.NewOrm()
md5Arr []interface{}
)
if len(ids) == 0 {
return
}
o.Begin()
defer func() {
if err != nil {
o.Rollback()
} else {
o.Commit()
}
}()
qs := o.QueryTable(GetTableDocumentInfo()).Filter("Id__in", ids...).Filter("Status", DocStatusDeleted)
qs.All(&docInfo)
_, err = qs.Update(orm.Params{"Status": DocStatusNormal})
if err != nil {
return
}
//总文档数量增加
sqlSys := fmt.Sprintf("update %v set `CntDoc`=`CntDoc`+? where Id = 1", GetTableSys())
_, err = o.Raw(sqlSys, len(docInfo)).Exec()
if err != nil {
return
}
reward := NewSys().GetByField("Reward").Reward
client := NewElasticSearchClient()
sqlUser := fmt.Sprintf("update %v set `Document`=`Document`+?,`Coin`=`Coin`+? where Id=?", GetTableUserInfo())
sqlCate := fmt.Sprintf("update %v set `Cnt`=`Cnt`+? where `Id` in(?,?,?)", GetTableCategory())
now := int(time.Now().Unix())
doc := NewDocument()
for _, v := range docInfo {
dsId = append(dsId, v.DsId)
_, err = o.Raw(sqlUser, 1, reward, v.Uid).Exec()
if err != nil {
return
}
// 积分变更
log := &CoinLog{Uid: v.Uid, Coin: reward, TimeCreate: now}
log.Log = fmt.Sprintf("系统恢复《%v》文档,获得 %v 个金币奖励", doc.GetDocument(v.Id, "Title").Title, reward)
_, err = o.Insert(log)
if err != nil {
return
}
_, err = o.Raw(sqlCate, 1, v.ChanelId, v.Cid, v.Pid).Exec()
if err != nil {
return
}
client.BuildIndexById(v.Id) //新增索引
}
//从回收站中删除记录
_, err = o.QueryTable(GetTableDocumentRecycle()).Filter("Id__in", ids...).Delete()
if store, _, _ := NewDocument().GetDocStoreByDsId(dsId...); len(store) > 0 {
for _, item := range store {
md5Arr = append(md5Arr, item.Md5)
}
}
_, err = o.QueryTable(GetTableDocumentIllegal()).Filter("Md5__in", md5Arr...).Delete()
return
}
//回收站文档列表
func (this *DocumentRecycle) RecycleList(p, listRows int) (params []orm.Params, rows int64, err error) {
var sql string
tables := []string{GetTableDocumentRecycle() + " dr", GetTableDocument() + " d", GetTableDocumentInfo() + " di", GetTableUser() + " u", GetTableDocumentStore() + " ds"}
on := []map[string]string{
{"dr.Id": "d.Id"},
{"d.Id": "di.Id"},
{"u.Id": "di.Uid"},
{"di.DsId": "ds.Id"},
}
fields := map[string][]string{
"dr": {"Date", "Self"},
"d": {"Title", "Filename", "Id"},
"ds": {"Md5", "Ext", "ExtCate", "Page", "Size"},
"u": {"Username", "Id Uid"},
}
if sql, err = LeftJoinSqlBuild(tables, on, fields, p, listRows, []string{"dr.Date desc"}, nil, "dr.Id>0"); err == nil {
rows, err = orm.NewOrm().Raw(sql).Values(¶ms)
}
return
}
//将文档移入回收站(软删除)
//@param uid 操作人,即将文档移入回收站的人
//@param self 是否是用户自己操作
//@param ids 文档id,即需要删除的文档id
//@return errs 错误
func (this *DocumentRecycle) RemoveToRecycle(uid interface{}, self bool, ids ...interface{}) (err error) {
//软删除
//1、将文档状态标记为-1
//2、将文档id录入到回收站
//3、用户文档数量减少
//4、整站文档数量减少
//5、分类下的文档减少
//不需要删除用户的收藏记录
//不需要删除文档的评分记录
var docInfo []DocumentInfo
sys, _ := NewSys().Get()
if len(ids) == 0 {
return
}
o := orm.NewOrm()
o.QueryTable(GetTableDocumentInfo()).Filter("Id__in", ids...).Filter("Status__in", DocStatusNormal, DocStatusConverting).All(&docInfo)
if len(docInfo) == 0 {
return
}
o.Begin()
defer func() {
if err == nil {
o.Commit()
} else {
o.Rollback()
}
}()
sqlSys := fmt.Sprintf("update %v set `CntDoc`=`CntDoc`-? where Id=1", GetTableSys())
_, err = o.Raw(sqlSys, len(docInfo)).Exec()
if err != nil {
return
}
sqlUser := fmt.Sprintf("update %v set `Document`=`Document`-? ,`Coin`=`Coin`-? where Id=?", GetTableUserInfo())
sqlCate := fmt.Sprintf("update %v set `Cnt`=`Cnt`-? where `Id` in(?,?,?)", GetTableCategory())
doc := NewDocument()
now := int(time.Now().Unix())
for _, info := range docInfo {
_, err = o.Raw(sqlCate, 1, info.ChanelId, info.Pid, info.Cid).Exec()
if err != nil {
return
}
_, err = o.Raw(sqlUser, 1, sys.Reward, info.Uid).Exec() //用户个人的文档数量和金币减少
if err != nil {
return
}
log := CoinLog{
Uid: info.Uid,
Coin: -sys.Reward,
TimeCreate: now,
}
log.Log = fmt.Sprintf("删除《%v》,扣除 %v 个金币", doc.GetDocument(info.Id, "Title").Title, sys.Reward)
if !self {
log.Log = "系统" + log.Log
}
_, err = o.Insert(&log)
if err != nil {
return
}
}
//变更文档状态
if _, err := UpdateByIds(GetTableDocumentInfo(), "Status", -1, ids...); err != nil {
helper.Logger.Error(err.Error())
}
marks := strings.Join(make([]string, len(ids)+1), "?")
sqlDocStatus := fmt.Sprintf("update %v set `Status`=-1 where Id in(%v)", GetTableDocumentInfo(), marks)
_, err = o.Raw(sqlDocStatus, ids...).Exec()
if err != nil {
return
}
for _, id := range ids {
var rc DocumentRecycle
rc.Id = helper.Interface2Int(id)
rc.Uid = helper.Interface2Int(uid)
rc.Date = now
rc.Self = self
if _, err = o.Insert(&rc); err != nil { // 移入回收站
return
}
}
client := NewElasticSearchClient()
for _, id := range ids { //删除索引
client.DeleteIndex(helper.Interface2Int(id))
}
return
}
// 彻底删除文档,包括删除文档记录(被收藏的记录、用户的发布记录、扣除用户获得的积分(如果此刻文档的状态不是待删除)),删除文档文件
func (this *DocumentRecycle) DeepDel(ids ...interface{}) (err error) {
// 文档id找到文档的dsId,再根据dsId查找到全部的文档id
// 根据文档id,将文档全部移入回收站
// 删除文档记录
//
var (
dsId []interface{}
info []DocumentInfo
store []DocumentStore
o = orm.NewOrm()
)
info, _, err = NewDocument().GetDocInfoById(ids...)
if err != nil && err != orm.ErrNoRows {
return
}
if len(info) == 0 {
return
}
for _, item := range info {
dsId = append(dsId, item.DsId)
}
info, _, _ = NewDocument().GetDocInfoByDsId(dsId)
if len(info) > 0 {
ids = []interface{}{}
for _, item := range info {
ids = append(ids, item.Id)
}
}
err = this.RemoveToRecycle(0, false, ids...)
if err != nil {
return
}
if err = this.DelRows(ids...); err != nil && err != orm.ErrNoRows {
return
}
if store, _, err = NewDocument().GetDocStoreByDsId(dsId...); err != orm.ErrNoRows && err != nil {
return
}
if _, err = o.QueryTable(GetTableDocumentStore()).Filter("Id__in", dsId).Delete(); err != orm.ErrNoRows && err != nil {
return
}
go func() {
for _, item := range store {
this.DelFile(item.Md5, item.Ext, item.PreviewExt, item.PreviewPage)
}
}()
return
}
//删除文档记录
func (this *DocumentRecycle) DelRows(ids ...interface{}) (err error) {
//1、删除被收藏的收藏记录
//2、删除文档的评论(评分)记录
//3、删除document表的记录
//4、删除document_info表的记录
//【这个不删除】5、删除document_store表的记录
//6、删除回收站中的记录
var (
o = orm.NewOrm()
)
if err = NewCollect().DelByDocId(ids...); err != orm.ErrNoRows && err != nil {
return
}
//删除评论
if err = NewDocumentComment().DelCommentByDocId(ids...); err != orm.ErrNoRows && err != nil {
return
}
if _, err = o.QueryTable(GetTableDocumentInfo()).Filter("Id__in", ids).Delete(); err != orm.ErrNoRows && err != nil {
return
}
if _, err = o.QueryTable(GetTableDocument()).Filter("Id__in", ids...).Delete(); err != orm.ErrNoRows && err != nil {
return
}
if _, err = o.QueryTable(GetTableDocumentRecycle()).Filter("Id__in", ids...).Delete(); err != orm.ErrNoRows && err != nil {
return
}
return
}
//根据md5,删除文档、封面、预览文件等
//@param md5 文档md5
func (this *DocumentRecycle) DelFile(md5, oriExt, prevExt string, previewPagesCount int) (err error) {
var (
cover = md5 + ".jpg" //封面文件
pdfFile = md5 + helper.ExtPDF
oriFile = md5 + "." + strings.TrimLeft(oriExt, ".")
svgFormat = md5 + "/%v." + strings.TrimLeft(prevExt, ".")
clientPublic *CloudStore
clientPrivate *CloudStore
)
if previewPagesCount <= 0 {
previewPagesCount = 1000 // default
}
clientPublic, err = NewCloudStore(false)
if err != nil {
helper.Logger.Error(err.Error())
return
}
clientPrivate, err = NewCloudStore(true)
if err != nil {
helper.Logger.Error(err.Error())
return
}
if err = clientPrivate.Delete(oriFile, pdfFile); err != nil {
helper.Logger.Error(err.Error())
}
if err = clientPublic.Delete(cover); err != nil {
helper.Logger.Error(err.Error())
}
var svgs []string
for i := 0; i < previewPagesCount; i++ {
svgs = append(svgs, fmt.Sprintf(svgFormat, i+1))
}
if err = clientPublic.Delete(svgs...); err != nil {
helper.Logger.Error(err.Error())
}
return
}