-
Notifications
You must be signed in to change notification settings - Fork 14
/
note_annex.go
87 lines (67 loc) · 2.12 KB
/
note_annex.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
package dao
import (
"context"
"github.com/gogf/gf/v2/os/gtime"
"github.com/iimeta/iim-client/internal/config"
"github.com/iimeta/iim-client/internal/model/do"
"github.com/iimeta/iim-client/internal/model/entity"
"github.com/iimeta/iim-client/utility/db"
"github.com/iimeta/iim-client/utility/filesystem"
"go.mongodb.org/mongo-driver/bson"
)
var NoteAnnex = NewNoteAnnexDao()
type NoteAnnexDao struct {
*MongoDB[entity.NoteAnnex]
filesystem *filesystem.Filesystem
}
func NewNoteAnnexDao(database ...string) *NoteAnnexDao {
if len(database) == 0 {
database = append(database, db.DefaultDatabase)
}
return &NoteAnnexDao{
MongoDB: NewMongoDB[entity.NoteAnnex](database[0], do.NOTE_ANNEX_COLLECTION),
filesystem: filesystem.NewFilesystem(config.Cfg),
}
}
func (d *NoteAnnexDao) AnnexList(ctx context.Context, uid int, articleId string) ([]*entity.NoteAnnex, error) {
return d.Find(ctx, bson.M{"user_id": uid, "article_id": articleId, "status": 1})
}
func (d *NoteAnnexDao) RecoverList(ctx context.Context, uid int) ([]*entity.NoteAnnex, []*entity.Note, error) {
noteAnnexList, err := d.Find(ctx, bson.M{"user_id": uid, "status": 2})
if err != nil {
return nil, nil, err
}
articleIds := make([]string, 0)
for _, noteAnnex := range noteAnnexList {
articleIds = append(articleIds, noteAnnex.ArticleId)
}
noteList, err := Note.FindByIds(ctx, articleIds)
if err != nil {
return nil, nil, err
}
return noteAnnexList, noteList, nil
}
// 更新附件状态
func (d *NoteAnnexDao) UpdateStatus(ctx context.Context, uid int, id string, status int) error {
data := map[string]any{
"status": status,
}
if status == 2 {
data["deleted_at"] = gtime.Timestamp()
}
return d.UpdateOne(ctx, bson.M{"_id": id, "user_id": uid}, data)
}
// 永久删除笔记附件
func (d *NoteAnnexDao) ForeverDelete(ctx context.Context, uid int, id string) error {
noteAnnex, err := d.FindOne(ctx, bson.M{"_id": id, "user_id": uid})
if err != nil {
return err
}
switch noteAnnex.Drive {
case 1:
_ = d.filesystem.Local.Delete(noteAnnex.Path)
case 2:
_ = d.filesystem.Cos.Delete(noteAnnex.Path)
}
return d.DeleteById(ctx, id)
}