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.

169 lines
4.8 KiB

4 years ago
  1. package models
  2. import (
  3. "leit.com/LAPP_GAAS_GFrame/conf"
  4. "leit.com/LAPP_GAAS_GFrame/db"
  5. "leit.com/LAPP_GAAS_GFrame/utils"
  6. "fmt"
  7. "context"
  8. "go.mongodb.org/mongo-driver/bson"
  9. "go.mongodb.org/mongo-driver/bson/primitive"
  10. "go.mongodb.org/mongo-driver/mongo/options"
  11. "log"
  12. "strings"
  13. "time"
  14. )
  15. type Buffer struct {
  16. Orderid string `bson:"orderid" json:"buffer-orderid"` //流水号id
  17. Eid string `bson:"eid" json:"buffer-eid"` //映射关系
  18. Data string `bson:"data" json:"buffer-data"` //json串
  19. TimeStamp string `bson:"timestamp" json:"buffer-timestamp"` //时间戳
  20. Todb string `bson:"todb" json:"buffer-todb"` //导入数据库
  21. Todrivername string `bson:"todrivername" json:"buffer-todrivername"` //导入数据库引擎
  22. Totable string `bson:"totable" json:"buffer-totable"` //导入数据表
  23. Flag int `bson:"flag" json:"buffer-flag"` //是否导入状态
  24. Status string `bson:"status" json:"buffer-status"` //ok or error
  25. Message string `bson:"message" json:"buffer-message"` //错误信息
  26. }
  27. func (t *Buffer) TableName() string {
  28. return "buffer"
  29. }
  30. func (t *Buffer) InsertRecord() (insertID primitive.ObjectID) {
  31. //1.初始化链接
  32. client := db.MgoDb()
  33. //2.选择数据库,数据表
  34. collect := client.Database(conf.DbConfig.Mongdbname).Collection("buffer")
  35. insertRest, err := collect.InsertOne(context.TODO(), t)
  36. if err != nil {
  37. fmt.Println(err)
  38. return
  39. }
  40. insertID = insertRest.InsertedID.(primitive.ObjectID)
  41. return insertID
  42. }
  43. func (t *Buffer) FindList(logred SerchData, skip int64, limit int64) ([]*Buffer, int64) {
  44. res := bson.M{}
  45. // 创建需要过滤的条件
  46. if logred.Key == "message" {
  47. res = bson.M{"message": primitive.Regex{Pattern: logred.Val}}
  48. }
  49. if logred.Key == "totable" {
  50. res = bson.M{"totable": primitive.Regex{Pattern: logred.Val}}
  51. }
  52. if logred.Key == "status" {
  53. res = bson.M{"status": primitive.Regex{Pattern: logred.Val}}
  54. }
  55. if logred.Key == "timestamp" {
  56. times := strings.Split(logred.Val, "-")
  57. stime := time.Unix(int64(utils.ValueToInt(times[0], 0)), 0)
  58. etime := time.Unix(int64(utils.ValueToInt(times[1], 0)), 0)
  59. stimestr := utils.TimeFormat(stime, "yyyyMMddHHmmss")
  60. etimestr := utils.TimeFormat(etime, "yyyyMMddHHmmss")
  61. res = bson.M{"timestamp": bson.M{"$lte": etimestr, "$gte": stimestr}}
  62. }
  63. //1.初始化链接
  64. client := db.MgoDb()
  65. //2.选择数据库,数据表
  66. collect := client.Database(conf.DbConfig.Mongdbname).Collection("buffer")
  67. //var skip int64 = 0//从那个开始
  68. //var limit int64 = 2//炼制几个输出字段
  69. cursor, err := collect.Find(context.TODO(), res, &options.FindOptions{
  70. Skip: &skip,
  71. Limit: &limit,
  72. Sort: bson.D{{"timestamp", -1}},
  73. })
  74. if err != nil {
  75. fmt.Println(err)
  76. return nil, 0
  77. }
  78. defer cursor.Close(context.TODO())
  79. //创建需要反序列化成什么样子的结构体对象
  80. records := make([]*Buffer, 0)
  81. for cursor.Next(context.TODO()) {
  82. record := &Buffer{}
  83. //反序列化
  84. err = cursor.Decode(record)
  85. if err != nil {
  86. fmt.Println(err)
  87. return nil, 0
  88. }
  89. //追加
  90. records = append(records, record)
  91. }
  92. // 获取数据总数
  93. count, err := collect.CountDocuments(context.Background(), bson.D{})
  94. if err != nil {
  95. log.Fatal(count)
  96. }
  97. return records, count
  98. }
  99. func (t *Buffer) FindData() []*Buffer {
  100. limit := int64(500)
  101. skip := int64(0)
  102. //1.初始化链接
  103. client := db.MgoDb()
  104. //2.选择数据库,数据表
  105. collect := client.Database(conf.DbConfig.Mongdbname).Collection("buffer")
  106. //var skip int64 = 0//从那个开始
  107. //var limit int64 = 2//炼制几个输出字段
  108. cursor, err := collect.Find(context.TODO(), bson.D{
  109. {"flag", 0},
  110. {"status", "ok"},
  111. }, &options.FindOptions{
  112. Skip: &skip,
  113. Limit: &limit,
  114. Sort: bson.D{{"timestamp", -1}},
  115. })
  116. fmt.Println(cursor)
  117. if err != nil {
  118. return nil
  119. }
  120. defer cursor.Close(context.TODO())
  121. //创建需要反序列化成什么样子的结构体对象
  122. records := make([]*Buffer, 0)
  123. for cursor.Next(context.TODO()) {
  124. record := &Buffer{}
  125. //反序列化
  126. err = cursor.Decode(record)
  127. if err != nil {
  128. fmt.Println(err)
  129. return nil
  130. }
  131. //追加
  132. records = append(records, record)
  133. }
  134. return records
  135. }
  136. func (t *Buffer) UpdateData() error {
  137. //1.初始化链接
  138. client := db.MgoDb()
  139. //2.选择数据库,数据表
  140. collect := client.Database(conf.DbConfig.Mongdbname).Collection("buffer")
  141. // 修改一条数据,如果不存在则插入
  142. new := &Buffer{
  143. Orderid: t.Orderid,
  144. Data: t.Data,
  145. TimeStamp: t.TimeStamp,
  146. Totable: t.Totable,
  147. Todb: t.Todb,
  148. Status: t.Status,
  149. Message: t.Message,
  150. Flag: 1,
  151. }
  152. update := bson.M{"$set": new}
  153. _, err := collect.UpdateOne(context.Background(), bson.M{"orderid": new.Orderid}, update)
  154. if err != nil {
  155. return err
  156. }
  157. return nil
  158. }