Initial commit: web_hghl_server
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,175 @@
|
||||
package oplog
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
"videoweb/pub/common/log"
|
||||
"videoweb/pub/common/srvdef"
|
||||
"videoweb/pub/common/utils"
|
||||
"videoweb/pub/mongos"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
// Init 初始化数据
|
||||
func Init(ctx context.Context) {
|
||||
opLogColl = mongos.ShopLog.Coll(TN.OpLog)
|
||||
initIndex(ctx)
|
||||
return
|
||||
}
|
||||
|
||||
func initIndex(ctx context.Context) {
|
||||
indexes := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{
|
||||
{FN.OpLog.ActId, 1},
|
||||
{FN.OpLog.Action, 1},
|
||||
{FN.OpLog.Path, 1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{FN.OpLog.CreatedAt, -1},
|
||||
},
|
||||
},
|
||||
}
|
||||
if _, err := opLogColl.CreateIndex(ctx, indexes); err != nil {
|
||||
panic(fmt.Sprintf("(oplog:initIndex) Create adLog Index failed! err:%+v:", err))
|
||||
}
|
||||
}
|
||||
|
||||
// 添加日志
|
||||
func AddLog(ctx context.Context, path string, desc string, actId int64, rawData, ip string) (err error) {
|
||||
_, err = opLogColl.InsertOne(ctx, primitive.M{
|
||||
FN.OpLog.Id: mongos.Lid.MwOpLog.Get(),
|
||||
FN.OpLog.Action: desc,
|
||||
FN.OpLog.Path: path,
|
||||
FN.OpLog.ActId: actId,
|
||||
FN.OpLog.RawData: rawData,
|
||||
FN.OpLog.CreatedAt: utils.Now(),
|
||||
FN.OpLog.IP: ip,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
type LogReq struct {
|
||||
PageNum int64 `json:"pageNum"` // 页码
|
||||
PageSize int64 `json:"pageSize"` //
|
||||
Path *string `json:"path"` // 请求接口
|
||||
Action *string `json:"action"` // 操作目的
|
||||
ActId *int64 `json:"actId"` // 操作人Id
|
||||
StartTime *time.Time `json:"startTime"` // 开始时间
|
||||
EndTime *time.Time `json:"endTime"` // 开始时间
|
||||
}
|
||||
|
||||
// 操作日志信息
|
||||
type OpLogInfo struct {
|
||||
Path string `json:"path" bson:"path"` // 请求接口
|
||||
ActId int64 `json:"actId" bson:"actId"` // 操作人Id
|
||||
Action string `json:"action" bson:"action"` // 操作目的
|
||||
Param string `json:"param" bson:"rawData"` // 操作内容
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt"` // 操作时间
|
||||
ActName string `json:"actName" bson:"actName"` // 操作人
|
||||
}
|
||||
|
||||
type OpPageRes struct {
|
||||
Total int64 `json:"total" bson:"total"`
|
||||
List []*OpLogInfo `json:"list" bson:"list"`
|
||||
}
|
||||
|
||||
// 获取日志列表
|
||||
func PageLogList(ctx context.Context, req *LogReq) (res *OpPageRes, err error) {
|
||||
filter := bson.M{}
|
||||
if req.Path != nil && *req.Path != "" {
|
||||
filter[FN.OpLog.Path] = bson.M{"$regex": req.Path}
|
||||
}
|
||||
if req.Action != nil && *req.Action != "" {
|
||||
filter[FN.OpLog.Action] = req.Action
|
||||
}
|
||||
if req.ActId != nil && *req.ActId > 0 {
|
||||
filter[FN.OpLog.ActId] = req.ActId
|
||||
}
|
||||
if req.StartTime != nil && req.EndTime != nil {
|
||||
filter[FN.OpLog.CreatedAt] = bson.M{"$gte": req.StartTime, "$lt": req.EndTime}
|
||||
}
|
||||
res = new(OpPageRes)
|
||||
res.Total, err = mongos.ShopLog.Coll(TN.OpLog).Count(ctx, filter)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
sort := bson.M{FN.OpLog.CreatedAt: -1}
|
||||
opt := &options.FindOptions{}
|
||||
opt.SetSort(sort)
|
||||
skip := (req.PageNum - 1) * req.PageSize
|
||||
opt.SetSkip(skip)
|
||||
opt.SetLimit(req.PageSize)
|
||||
list := make(OpLogList, 0)
|
||||
err = mongos.ShopLog.Coll(TN.OpLog).Find(ctx, &list, filter, opt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
actIds := make([]int64, 0)
|
||||
for _, v := range list {
|
||||
actIds = append(actIds, v.ActId)
|
||||
}
|
||||
// err = opLogColl.Aggregate(ctx, &res.List, []bson.M{
|
||||
// {"$match": conf},
|
||||
// {"$sort": bson.M{FN.OpLog.CreatedAt: -1}},
|
||||
// {"$skip": (req.PageNum - 1) * req.PageSize},
|
||||
// {"$limit": req.PageSize},
|
||||
// {"$lookup": bson.M{
|
||||
// "localField": FN.OpLog.ActId,
|
||||
// "from": mwacc.TN.MwAcc,
|
||||
// "foreignField": mwacc.FN.MwAcc.Id,
|
||||
// "as": "admins",
|
||||
// }},
|
||||
// {"$unwind": "$admins"},
|
||||
// {"$project": bson.M{
|
||||
// FN.OpLog.Path: 1,
|
||||
// FN.OpLog.Action: 1,
|
||||
// FN.OpLog.ActId: 1,
|
||||
// FN.OpLog.RawData: 1,
|
||||
// FN.OpLog.CreatedAt: 1,
|
||||
// "actName": "$admins.userName",
|
||||
// }},
|
||||
// })
|
||||
return
|
||||
}
|
||||
|
||||
// CountOpLogs 计算某个查询的数量
|
||||
func CountOpLogs(ctx context.Context, filter bson.M) (int64, *srvdef.SrvErr) {
|
||||
total, err := mongos.ShopLog.Coll(TN.OpLog).Count(ctx, filter)
|
||||
if err != nil {
|
||||
log.Info("oplog:CountOpLogs db.Count failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return 0, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return total, nil
|
||||
}
|
||||
|
||||
// InsertOneUser 插入一个用户
|
||||
func InsertOneUser(ctx context.Context, opLog *OpLog) *srvdef.SrvErr {
|
||||
if _, err := mongos.ShopLog.Coll(TN.OpLog).InsertOne(ctx, opLog); err != nil {
|
||||
log.Warn("oplog:InsertOneUser db.InsertOne failed", log.E(err))
|
||||
return srvdef.New(srvdef.ErrDbInsertError, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindManyOpLogs 查找一批数据
|
||||
func FindManyOpLogs(ctx context.Context,
|
||||
filter bson.M, opts ...*options.FindOptions) (
|
||||
OpLogList, *srvdef.SrvErr) {
|
||||
data := make(OpLogList, 0)
|
||||
if err := mongos.ShopLog.Coll(TN.OpLog).Find(ctx, &data, filter, opts...); err != nil {
|
||||
log.Warn("oplog:FindManyOpLogs db.Find failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
Reference in New Issue
Block a user