/
mongo_overtime_dao.go
304 lines (244 loc) · 8.28 KB
/
mongo_overtime_dao.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
package mongodb_repository
import (
"log"
"github.com/zapscloud/golib-dbutils/db_common"
"github.com/zapscloud/golib-dbutils/mongo_utils"
"github.com/zapscloud/golib-hr-repository/hr_common"
"github.com/zapscloud/golib-utils/utils"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
)
// OvertimeMongoDBDao - Leave DAO Repository
type OvertimeMongoDBDao struct {
client utils.Map
businessId string
}
func init() {
log.SetFlags(log.Lshortfile | log.LstdFlags | log.Lmicroseconds)
}
func (p *OvertimeMongoDBDao) InitializeDao(client utils.Map, businessId string) {
log.Println("Initialize Leave Mongodb DAO")
p.client = client
p.businessId = businessId
}
// List - List all Collections
func (p *OvertimeMongoDBDao) List(filter string, sort string, skip int64, limit int64) (utils.Map, error) {
var results []utils.Map
log.Println("Begin - Find All Collection Dao", hr_common.DbHrOvertimes)
collection, ctx, err := mongo_utils.GetMongoDbCollection(p.client, hr_common.DbHrOvertimes)
if err != nil {
return nil, err
}
log.Println("Get Collection - Find All Collection Dao", filter, len(filter), sort, len(sort))
opts := options.Find()
filterdoc := bson.D{}
if len(filter) > 0 {
// filters, _ := strconv.Unquote(string(filter))
err = bson.UnmarshalExtJSON([]byte(filter), true, &filterdoc)
if err != nil {
log.Println("Unmarshal Ext JSON error", err)
log.Println(filterdoc)
}
}
if len(sort) > 0 {
var sortdoc interface{}
err = bson.UnmarshalExtJSON([]byte(sort), true, &sortdoc)
if err != nil {
log.Println("Sort Unmarshal Error ", sort)
} else {
opts.SetSort(sortdoc)
}
}
if skip > 0 {
log.Println(filterdoc)
opts.SetSkip(skip)
}
if limit > 0 {
log.Println(filterdoc)
opts.SetLimit(limit)
}
filterdoc = append(filterdoc,
bson.E{Key: hr_common.FLD_BUSINESS_ID, Value: p.businessId},
bson.E{Key: db_common.FLD_IS_DELETED, Value: false})
log.Println("Parameter values ", filterdoc, opts)
cursor, err := collection.Find(ctx, filterdoc, opts)
if err != nil {
return nil, err
}
// get a list of all returned documents and print them out
// see the mongo.Cursor documentation for more examples of using cursors
if err = cursor.All(ctx, &results); err != nil {
return nil, err
}
//log.Println("End - Find All Collection Dao", results)
listdata := []utils.Map{}
for _, value := range results {
//log.Println("Item ", idx)
value = db_common.AmendFldsForGet(value)
listdata = append(listdata, value)
}
//log.Println("End - Find All Collection Dao", listdata)
log.Println("Parameter values ", filterdoc)
filtercount, err := collection.CountDocuments(ctx, filterdoc)
if err != nil {
return nil, err
}
basefilterdoc := bson.D{
{Key: hr_common.FLD_BUSINESS_ID, Value: p.businessId},
{Key: db_common.FLD_IS_DELETED, Value: false}}
totalcount, err := collection.CountDocuments(ctx, basefilterdoc)
if err != nil {
return nil, err
}
response := utils.Map{
db_common.LIST_SUMMARY: utils.Map{
db_common.LIST_TOTALSIZE: totalcount,
db_common.LIST_FILTEREDSIZE: filtercount,
db_common.LIST_RESULTSIZE: len(listdata),
},
db_common.LIST_RESULT: listdata,
}
return response, nil
}
// Get - Get account details
func (p *OvertimeMongoDBDao) Get(OvertimeeId string) (utils.Map, error) {
// Find a single document
var result utils.Map
log.Println("accountMongoDao::Get:: Begin ", OvertimeeId)
collection, ctx, err := mongo_utils.GetMongoDbCollection(p.client, hr_common.DbHrOvertimes)
log.Println("Find:: Got Collection ")
filter := bson.D{
{Key: hr_common.FLD_OVERTIME_ID, Value: OvertimeeId},
{Key: hr_common.FLD_BUSINESS_ID, Value: p.businessId},
{Key: db_common.FLD_IS_DELETED, Value: false}}
log.Println("Get:: Got filter ", filter)
singleResult := collection.FindOne(ctx, filter)
if singleResult.Err() != nil {
log.Println("Get:: Record not found ", singleResult.Err())
return result, singleResult.Err()
}
singleResult.Decode(&result)
if err != nil {
log.Println("Error in decode", err)
return result, err
}
// Remove fields from result
result = db_common.AmendFldsForGet(result)
log.Println("accountMongoDao::Get:: End Found a single document: \n", err)
return result, nil
}
// Find - Find by code
func (p *OvertimeMongoDBDao) Find(filter string) (utils.Map, error) {
// Find a single document
var result utils.Map
log.Println("accountMongoDao::Find:: Begin ", filter)
collection, ctx, err := mongo_utils.GetMongoDbCollection(p.client, hr_common.DbHrOvertimes)
log.Println("Find:: Got Collection ", err)
bfilter := bson.D{}
err = bson.UnmarshalExtJSON([]byte(filter), true, &bfilter)
if err != nil {
log.Println("Error on filter Unmarshal", err)
}
bfilter = append(bfilter,
bson.E{Key: hr_common.FLD_BUSINESS_ID, Value: p.businessId},
bson.E{Key: db_common.FLD_IS_DELETED, Value: false})
log.Println("Find:: Got filter ", bfilter)
singleResult := collection.FindOne(ctx, bfilter)
if singleResult.Err() != nil {
log.Println("Find:: Record not found ", singleResult.Err())
return result, singleResult.Err()
}
singleResult.Decode(&result)
if err != nil {
log.Println("Error in decode", err)
return result, err
}
// Remove fields from result
result = db_common.AmendFldsForGet(result)
log.Println("accountMongoDao::Find:: End Found a single document: \n", err)
return result, nil
}
// Create - Create Collection
func (p *OvertimeMongoDBDao) Create(indata utils.Map) (utils.Map, error) {
log.Println("Business Leave Save - Begin", indata)
collection, ctx, err := mongo_utils.GetMongoDbCollection(p.client, hr_common.DbHrOvertimes)
if err != nil {
return indata, err
}
// Add Fields for Create
indata = db_common.AmendFldsforCreate(indata)
// Insert a single document
insertResult, err := collection.InsertOne(ctx, indata)
if err != nil {
log.Println("Error in insert ", err)
return indata, err
}
log.Println("Inserted a single document: ", insertResult.InsertedID)
log.Println("Save - End", indata[hr_common.FLD_OVERTIME_ID])
return indata, err
}
// Update - Update Collection
func (p *OvertimeMongoDBDao) Update(OvertimeeId string, indata utils.Map) (utils.Map, error) {
log.Println("Update - Begin")
collection, ctx, err := mongo_utils.GetMongoDbCollection(p.client, hr_common.DbHrOvertimes)
if err != nil {
return utils.Map{}, err
}
// Modify Fields for Update
indata = db_common.AmendFldsforUpdate(indata)
log.Printf("Update - Values %v", indata)
filter := bson.D{
{Key: hr_common.FLD_OVERTIME_ID, Value: OvertimeeId},
{Key: hr_common.FLD_BUSINESS_ID, Value: p.businessId}}
updateResult, err := collection.UpdateOne(ctx, filter, bson.D{{Key: hr_common.MONGODB_SET, Value: indata}})
if err != nil {
return utils.Map{}, err
}
log.Println("Update a single document: ", updateResult.ModifiedCount)
log.Println("Update - End")
return indata, nil
}
// Delete - Delete Collection
func (p *OvertimeMongoDBDao) Delete(OvertimeeId string) (int64, error) {
log.Println("accountMongoDao::Delete - Begin ", OvertimeeId)
collection, ctx, err := mongo_utils.GetMongoDbCollection(p.client, hr_common.DbHrOvertimes)
if err != nil {
return 0, err
}
opts := options.Delete().SetCollation(&options.Collation{
Locale: db_common.LOCALE,
Strength: 1,
CaseLevel: false,
})
filter := bson.D{
{Key: hr_common.FLD_OVERTIME_ID, Value: OvertimeeId},
{Key: hr_common.FLD_BUSINESS_ID, Value: p.businessId}}
res, err := collection.DeleteOne(ctx, filter, opts)
if err != nil {
log.Println("Error in delete ", err)
return 0, err
}
log.Printf("accountMongoDao::Delete - End deleted %v documents\n", res.DeletedCount)
return res.DeletedCount, nil
}
// DeleteAll - Delete All Collection
func (p *OvertimeMongoDBDao) DeleteAll() (int64, error) {
log.Println("accountMongoDao::DeleteAll - Begin ")
collection, ctx, err := mongo_utils.GetMongoDbCollection(p.client, hr_common.DbHrOvertimes)
if err != nil {
return 0, err
}
opts := options.Delete().SetCollation(&options.Collation{
Locale: db_common.LOCALE,
Strength: 1,
CaseLevel: false,
})
filter := bson.E{Key: hr_common.FLD_BUSINESS_ID, Value: p.businessId}
res, err := collection.DeleteMany(ctx, filter, opts)
if err != nil {
log.Println("Error in delete ", err)
return 0, err
}
log.Printf("accountMongoDao::DeleteAll - End deleted %v documents\n", res.DeletedCount)
return res.DeletedCount, nil
}