Initial commit
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
package opLogMod
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"stat/common/log"
|
||||
"stat/common/mongos"
|
||||
)
|
||||
|
||||
// 初始化model
|
||||
func Init() {
|
||||
// 获取数据库表
|
||||
db = mongos.New(mongos.MgoManager, CollOpLog)
|
||||
|
||||
indexes := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "createdAt", Value: 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "router", Value: 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "operator", Value: 1}},
|
||||
},
|
||||
}
|
||||
|
||||
if _, err := db.CreateIndex(context.Background(), indexes); err != nil {
|
||||
panic(fmt.Sprintf("Model %s InitIndex error %+v", CollOpLog, err))
|
||||
}
|
||||
}
|
||||
|
||||
// 增加操作日志
|
||||
func AddOpLog(ctx context.Context, doc *OpLog) (err error) {
|
||||
if _, err = db.InsertOne(ctx, doc); err != nil {
|
||||
log.Error("opLog AddOpLog InsertOne err", log.E(err))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 获取操作日志列表
|
||||
func GetOpLogList(ctx context.Context, operator *string, router *string, isAdmin *bool, skip, limit int64) (count int64, data []OpLog, err error) {
|
||||
filter := bson.M{}
|
||||
if operator != nil {
|
||||
filter["operator"] = operator
|
||||
}
|
||||
if router != nil {
|
||||
filter["router"] = router
|
||||
}
|
||||
if isAdmin != nil {
|
||||
filter["isAdmin"] = isAdmin
|
||||
}
|
||||
|
||||
sort := bson.D{{"createdAt", -1}}
|
||||
|
||||
opts := options.Find().SetSort(sort).SetSkip(skip).SetLimit(limit)
|
||||
|
||||
if err = db.Find(ctx, &data, filter, opts); err != nil {
|
||||
log.Error("opLog GetOpLogList Find err", log.E(err))
|
||||
return
|
||||
}
|
||||
|
||||
if count, err = db.Count(ctx, filter); err != nil {
|
||||
log.Error("opLog GetOpLogList Count err", log.E(err))
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package opLogMod
|
||||
|
||||
import (
|
||||
"stat/common/mongos"
|
||||
"time"
|
||||
)
|
||||
|
||||
const CollOpLog = "op_log"
|
||||
|
||||
var db *mongos.MongoTool
|
||||
|
||||
type OpLog struct {
|
||||
Operator string `bson:"operator"` // 操作人员
|
||||
Router string `bson:"router"` // 接口路由
|
||||
Data string `bson:"data"` // 操作数据
|
||||
IsAdmin bool `bson:"isAdmin"` // 是否是管理员
|
||||
CreatedAt time.Time `bson:"createdAt"` // 创建时间
|
||||
}
|
||||
Reference in New Issue
Block a user