edb_data_base.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package mgodb
  2. import (
  3. "context"
  4. "eta/eta_forum_admin/utils"
  5. "go.mongodb.org/mongo-driver/bson"
  6. "go.mongodb.org/mongo-driver/bson/primitive"
  7. "go.mongodb.org/mongo-driver/mongo/options"
  8. "time"
  9. )
  10. type EdbDataBase struct {
  11. EdbDataId primitive.ObjectID `bson:"_id,omitempty"`
  12. EdbInfoId int `bson:"edb_info_id"`
  13. EdbCode string `bson:"edb_code"`
  14. DataTime time.Time `bson:"data_time"`
  15. Value float64 `bson:"value"`
  16. Status int `bson:"status"`
  17. CreateTime time.Time `bson:"create_time"`
  18. ModifyTime time.Time `bson:"modify_time"`
  19. DataTimestamp int64 `bson:"data_timestamp"`
  20. }
  21. func GetEdbDataBaseByEdbInfoId(edbInfoId int) (items []*EdbDataBase, err error) {
  22. findOptions := options.Find()
  23. db := NewMgo(utils.MgoDataDbName, "edb_data_base", utils.MgoDataCli)
  24. filter := bson.D{{"edb_info_id", edbInfoId}}
  25. ctx := context.TODO()
  26. cur, err := db.Find(filter, findOptions)
  27. if err != nil {
  28. return
  29. }
  30. // Close the cursor once finished
  31. defer cur.Close(ctx)
  32. for cur.Next(ctx) {
  33. // create a value into which the single document can be decoded
  34. var elem EdbDataBase
  35. err = cur.Decode(&elem)
  36. if err != nil {
  37. return
  38. }
  39. items = append(items, &elem)
  40. }
  41. if err = cur.Err(); err != nil {
  42. return
  43. }
  44. return
  45. }
  46. // GetEdbDataList 获取指标的数据(日期正序返回)
  47. func GetEdbDataList(endInfoId int, startDate, endDate time.Time) (list []*EdbDataBase, err error) {
  48. findOptions := options.Find()
  49. findOptions.SetSort(bson.D{{"data_time", 1}})
  50. db := NewMgo(utils.MgoDataDbName, "edb_data_base", utils.MgoDataCli)
  51. filter := bson.D{{"edb_info_id", endInfoId}}
  52. if !startDate.IsZero() {
  53. filter = append(filter, bson.E{"data_time", bson.M{"$gte": startDate}})
  54. }
  55. if !endDate.IsZero() {
  56. filter = append(filter, bson.E{"data_time", bson.M{"$lte": endDate}})
  57. }
  58. ctx := context.TODO()
  59. cur, err := db.Find(filter, findOptions)
  60. if err != nil {
  61. return
  62. }
  63. // Close the cursor once finished
  64. defer cur.Close(ctx)
  65. for cur.Next(ctx) {
  66. // create a value into which the single document can be decoded
  67. var elem EdbDataBase
  68. err = cur.Decode(&elem)
  69. if err != nil {
  70. return
  71. }
  72. list = append(list, &elem)
  73. }
  74. if err = cur.Err(); err != nil {
  75. return
  76. }
  77. return
  78. }