|
@@ -5,12 +5,14 @@ import (
|
|
|
"math"
|
|
|
"time"
|
|
|
|
|
|
+ mhayaTime "github.com/mhaya/extend/time"
|
|
|
"github.com/mhaya/game/game_cluster/internal/code"
|
|
|
"github.com/mhaya/game/game_cluster/internal/constant"
|
|
|
"github.com/mhaya/game/game_cluster/internal/mdb"
|
|
|
"github.com/mhaya/game/game_cluster/internal/mdb/models"
|
|
|
"github.com/mhaya/game/game_cluster/nodes/webadmin/common"
|
|
|
"github.com/mhaya/game/game_cluster/nodes/webadmin/entity"
|
|
|
+ "github.com/mhaya/game/game_cluster/nodes/webadmin/model"
|
|
|
mhayaLogger "github.com/mhaya/logger"
|
|
|
"github.com/spf13/cast"
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
@@ -457,3 +459,102 @@ func (s *Synthesis) FindUserLevel() (*entity.UserLevelCountResp, *code.Result) {
|
|
|
Details: results,
|
|
|
}, nil
|
|
|
}
|
|
|
+
|
|
|
+func (s *Synthesis) InsertRecord(param model.UserOperationLog) {
|
|
|
+ mhayaLogger.Warnf("InsertRecord param:%#v", param)
|
|
|
+
|
|
|
+ record := new(model.UserOperationLog)
|
|
|
+ collection := mdb.MDB.Collection(record.TableName())
|
|
|
+
|
|
|
+ insertData := bson.M{}
|
|
|
+ insertData["user_name"] = param.Username
|
|
|
+ insertData["role_id"] = param.RoleId
|
|
|
+ insertData["url"] = param.Path
|
|
|
+ insertData["method"] = param.Method
|
|
|
+ insertData["status_code"] = param.StatusCode
|
|
|
+ insertData["dur"] = param.Dur
|
|
|
+ insertData["client_ip"] = param.ClientIP
|
|
|
+ insertData["error_message"] = param.ErrorMessage
|
|
|
+ insertData["created_at"] = mhayaTime.Now().Unix()
|
|
|
+
|
|
|
+ _, err := collection.InsertOne(context.Background(), insertData)
|
|
|
+ if err != nil {
|
|
|
+ mhayaLogger.Warnf("InsertRecord InsertOne error:%v", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func (s *Synthesis) RecordList(req entity.RecordListReq) (*entity.RecordListResp, *code.Result) {
|
|
|
+ record := new(model.UserOperationLog)
|
|
|
+ collection := mdb.MDB.Collection(record.TableName())
|
|
|
+
|
|
|
+ // 构建过滤器
|
|
|
+ filter := bson.M{}
|
|
|
+
|
|
|
+ if req.UserName != "" {
|
|
|
+ filter["userName"] = req.UserName
|
|
|
+ }
|
|
|
+ if req.RoleId != "" {
|
|
|
+ filter["role_id"] = req.RoleId
|
|
|
+ }
|
|
|
+ if req.ID != "" {
|
|
|
+ id, err := primitive.ObjectIDFromHex(req.ID)
|
|
|
+ if err != nil {
|
|
|
+ mhayaLogger.Warnf("RecordList ObjectIDFromHex error:%v, req.ID:%s", err, req.ID)
|
|
|
+ return nil, common.NewResult(code.ParamError)
|
|
|
+ }
|
|
|
+
|
|
|
+ filter["id"] = id
|
|
|
+ }
|
|
|
+
|
|
|
+ if req.StartTime != 0 {
|
|
|
+ filter["createAt"] = bson.M{
|
|
|
+ "$gte": req.StartTime,
|
|
|
+ "$lte": req.EndTime,
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置分页选项
|
|
|
+ findOptions := options.Find()
|
|
|
+ findOptions.SetSkip(int64((req.Page - 1) * req.Size))
|
|
|
+ findOptions.SetLimit(int64(req.Size))
|
|
|
+ findOptions.SetSort(bson.D{{"created_at", -1}})
|
|
|
+
|
|
|
+ // 获取总数total
|
|
|
+ ctx, cancel := context.WithTimeout(context.Background(), 100*time.Second)
|
|
|
+ defer cancel()
|
|
|
+ count, err := collection.CountDocuments(ctx, filter)
|
|
|
+ if err != nil {
|
|
|
+ mhayaLogger.Warnf("RecordList CountDocuments error:%v", err)
|
|
|
+ return nil, common.NewResult(code.InternalError)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询数据
|
|
|
+ var results []*entity.RecordListDetail
|
|
|
+ cursor, err := collection.Find(ctx, filter, findOptions)
|
|
|
+ if err != nil {
|
|
|
+ mhayaLogger.Warnf("RecordList Find error:%v", err)
|
|
|
+ return nil, common.NewResult(code.InternalError)
|
|
|
+ }
|
|
|
+ defer cursor.Close(ctx)
|
|
|
+
|
|
|
+ // 解析结果
|
|
|
+ for cursor.Next(ctx) {
|
|
|
+ var result entity.RecordListDetail
|
|
|
+ if err := cursor.Decode(&result); err != nil {
|
|
|
+ mhayaLogger.Warnf("RecordList Decode error:%v", err)
|
|
|
+ return nil, common.NewResult(code.InternalError)
|
|
|
+ }
|
|
|
+ results = append(results, &result)
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := cursor.Err(); err != nil {
|
|
|
+ mhayaLogger.Warnf("RecordList cursor error:%v", err)
|
|
|
+ return nil, common.NewResult(code.InternalError)
|
|
|
+ }
|
|
|
+
|
|
|
+ return &entity.RecordListResp{
|
|
|
+ Details: results,
|
|
|
+ Total: count,
|
|
|
+ }, nil
|
|
|
+}
|