沈阳玫苑物业管理后端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.2 KiB

  1. package models
  2. import (
  3. "lapp_-wy/db"
  4. "lapp_-wy/utils"
  5. "context"
  6. "fmt"
  7. "go.mongodb.org/mongo-driver/bson/primitive"
  8. "go.mongodb.org/mongo-driver/bson"
  9. "go.mongodb.org/mongo-driver/mongo/options"
  10. "log"
  11. "strings"
  12. "time"
  13. )
  14. type LeitServerLog struct {
  15. Level string `bson:"level" json:"leit_server_log-level"` //日志类别
  16. TimeStamp string `bson:"timestamp" json:"leit_server_log-timestamp"` //时间戳
  17. Message string `bson:"message" json:"leit_server_log-message"` //出错信息提示
  18. Function string `bson:"function" json:"leit_server_log-function"` //出错函数
  19. File string `bson:"file" json:"leit_server_log-file"` //出错文件
  20. Operator string `bson:"operator" json:"leit_server_log-operator"` //操作人员
  21. }
  22. type SerchData struct {
  23. Key string
  24. Val string
  25. Userid string
  26. }
  27. func (t *LeitServerLog) InsertRecord() (insertID primitive.ObjectID) {
  28. //1.初始化链接
  29. client := db.MgoDb()
  30. //2.选择数据库,数据表
  31. collect := client.Database("logDb").Collection("leit_server")
  32. insertRest, err := collect.InsertOne(context.TODO(), t)
  33. if err != nil {
  34. fmt.Println(err)
  35. return
  36. }
  37. insertID = insertRest.InsertedID.(primitive.ObjectID)
  38. return insertID
  39. }
  40. func (t *LeitServerLog) FindLog(logred SerchData, skip int64, limit int64) ([]*LeitServerLog, int64) {
  41. res := bson.M{}
  42. // 创建需要过滤的条件
  43. if logred.Key == "level" {
  44. res = bson.M{"level": primitive.Regex{Pattern: logred.Val}}
  45. }
  46. if logred.Key == "message" {
  47. res = bson.M{"message": primitive.Regex{Pattern: logred.Val}}
  48. }
  49. if logred.Key == "function" {
  50. res = bson.M{"function": primitive.Regex{Pattern: logred.Val}}
  51. }
  52. if logred.Key == "file" {
  53. res = bson.M{"file": primitive.Regex{Pattern: logred.Val}}
  54. }
  55. if logred.Key == "operator" {
  56. res = bson.M{"operator": primitive.Regex{Pattern: logred.Val}}
  57. }
  58. if logred.Key == "timestamp" {
  59. times := strings.Split(logred.Val, "-")
  60. stime := time.Unix(int64(utils.ValueToInt(times[0], 0)), 0)
  61. etime := time.Unix(int64(utils.ValueToInt(times[1], 0)), 0)
  62. stimestr := utils.TimeFormat(stime, "yyyyMMddHHmmss")
  63. etimestr := utils.TimeFormat(etime, "yyyyMMddHHmmss")
  64. res = bson.M{"timestamp": bson.M{"$lte": etimestr, "$gte": stimestr}}
  65. }
  66. if logred.Userid != "admin" {
  67. res = bson.M{"operator": logred.Userid}
  68. }
  69. //1.初始化链接
  70. client := db.MgoDb()
  71. //2.选择数据库,数据表
  72. collect := client.Database("logDb").Collection("leit_server")
  73. //var skip int64 = 0//从那个开始
  74. //var limit int64 = 2//炼制几个输出字段
  75. cursor, err := collect.Find(context.TODO(), res, &options.FindOptions{
  76. Skip: &skip,
  77. Limit: &limit,
  78. Sort: bson.D{{"timestamp", -1}},
  79. })
  80. if err != nil {
  81. fmt.Println(err)
  82. return nil, 0
  83. }
  84. defer cursor.Close(context.TODO())
  85. //创建需要反序列化成什么样子的结构体对象
  86. records := make([]*LeitServerLog, 0)
  87. for cursor.Next(context.TODO()) {
  88. record := &LeitServerLog{}
  89. //反序列化
  90. err = cursor.Decode(record)
  91. if err != nil {
  92. fmt.Println(err)
  93. return nil, 0
  94. }
  95. //追加
  96. records = append(records, record)
  97. }
  98. // 获取数据总数
  99. count, err := collect.CountDocuments(context.Background(), bson.D{})
  100. if err != nil {
  101. log.Fatal(count)
  102. }
  103. return records, count
  104. }