71 lines
1.5 KiB
Go
71 lines
1.5 KiB
Go
package operationlogmod
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"91porn-server/common/db"
|
|
"91porn-server/common/log"
|
|
"91porn-server/models"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
var mdb *db.MongoDB
|
|
|
|
const table = models.OperationLog
|
|
|
|
func coll(t *db.MongoTool) *db.MongoTool {
|
|
if t == nil {
|
|
return mdb.Coll(table)
|
|
}
|
|
return t.Coll(table)
|
|
}
|
|
|
|
func initIndex() {
|
|
many := []mongo.IndexModel{ //batch set indexes //value is the type 1 or -1
|
|
{
|
|
Keys: bson.D{{Key: "createdAt", Value: 1}},
|
|
},
|
|
}
|
|
if _, err := coll(nil).CreateIndex(many); err != nil {
|
|
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
|
}
|
|
}
|
|
|
|
// 添加
|
|
func Insert(l OperationLog) error {
|
|
l.CreatedAt = time.Now()
|
|
if _, err := coll(nil).InsertOne(l); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Insert", table, "InsertOne", err))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func InsertMany(logs []OperationLog) error {
|
|
_, err := coll(nil).InsertMany(logs)
|
|
return err
|
|
}
|
|
|
|
// FindMany
|
|
func FindManyOperationLog(filter bson.M, opts *options.FindOptions) (data []OperationLog, err error) {
|
|
err = coll(nil).Find(&data, filter, opts)
|
|
if err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindManyOperationLog", table, "FindMany", err), log.Any("blockType", filter))
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func Count(query bson.M) (total int64) {
|
|
total, err := coll(nil).Count(query)
|
|
if err != nil {
|
|
log.Error("models operationLog Count error", log.E(err))
|
|
return
|
|
}
|
|
return
|
|
}
|