-
Notifications
You must be signed in to change notification settings - Fork 3
/
db.go
150 lines (134 loc) · 3.09 KB
/
db.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
package gomongo
import (
"errors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/options"
"reflect"
"strings"
"time"
)
var obj = Instance
type DB struct {
colName string
doc interface{}
}
func New(colName string, doc interface{}) *DB {
return &DB{
colName: colName,
doc: doc,
}
}
// Parse 转换数据
func (my *DB) Parse(data bson.M, schema interface{}) error {
if data == nil {
return errors.New("数据不存在")
}
if err := obj.ToStruct(data, schema); err != nil {
return err
}
return nil
}
// AddOne 添加
func (my *DB) Save() error {
var data bson.M
bytes, err := bson.Marshal(my.doc)
if err != nil {
return err
}
err = bson.Unmarshal(bytes, &data)
if err != nil {
return err
}
id, ok := data["_id"].(primitive.ObjectID)
if !ok {
createTime, ok := data["create_time"].(int64)
if !ok || createTime == 0 {
data["create_time"] = time.Now().UnixNano() / 1e6
}
success := obj.InsertOne(my.colName, data)
if !success {
return errors.New("保存失败")
}
} else {
ok := obj.UpdateOne(my.colName, bson.M{"_id": id}, bson.M{"$set": my.doc})
if !ok {
return errors.New("保存失败")
}
}
return nil
}
// DeleteOneByID 删除
func (my *DB) Delete() error {
v := struct2Map(my.doc)
id := v["_id"].(primitive.ObjectID)
if id.Hex() == "000000000000000000000000" {
return errors.New("删除失败")
}
b := obj.DeleteOne(my.colName, bson.M{"_id": id})
if !b {
return errors.New("删除失败")
}
return nil
}
// Load 加载文档
func (my *DB) LoadDoc(filter bson.M) error {
userF := obj.FindOne(my.colName, filter)
if userF == nil {
return errors.New("数据不存在!")
}
err := my.Parse(userF, my.doc)
if err != nil {
return errors.New("数据解析失败!")
}
return nil
}
// FindMany 查询多个
func (my *DB) Find(pageNum int64, filter bson.M) ([]bson.M, error) {
pageNum = pageNum - 1
if pageNum < 0 {
pageNum = 0
}
opts := new(options.FindOptions)
list := obj.FindMany(my.colName, filter,
opts.SetSkip(pageNum*20),
opts.SetLimit(20),
opts.SetSort(bson.M{
"create_time": 1,
}))
return list, nil
}
func (my *DB) Aggregate(filter interface{}, opts ...*options.AggregateOptions) ([]bson.M, error) {
list := obj.Aggregate(my.colName, filter)
return list, nil
}
// FindMany 查询多个
func (my *DB) All(filter bson.M) ([]bson.M, error) {
list := obj.FindMany(my.colName, filter)
return list, nil
}
// Has 文档是否存在
func (my *DB) Exist(filter bson.M) bool {
userF := obj.FindOne(my.colName, filter)
if userF == nil {
return false
}
return true
}
// Count 数量
func (my *DB) Count(filter bson.M) (int64, error) {
return obj.Count(my.colName, filter), nil
}
func (my *DB) SetDoc(doc interface{}) {
my.doc = doc
}
func struct2Map(obj interface{}) map[string]interface{} {
elem := reflect.ValueOf(obj).Elem()
relType := elem.Type()
var data = make(map[string]interface{})
for i := 0; i < relType.NumField(); i++ {
name := strings.SplitN(relType.Field(i).Tag.Get("bson"), ",", 2)[0]
data[name] = elem.Field(i).Interface()
}
return data
}