edb_data_base.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. // GetEdbDataList 获取指标的数据(日期正序返回)
  22. func GetEdbDataList(endInfoId int, startDate, endDate time.Time) (list []*EdbDataBase, err error) {
  23. findOptions := options.Find()
  24. findOptions.SetSort(bson.D{{"data_time", 1}})
  25. db := NewMgo(utils.MgoDataDbName, "edb_data_base", utils.MgoDataCli)
  26. filter := bson.D{{"edb_info_id", endInfoId}}
  27. if !startDate.IsZero() {
  28. filter = append(filter, bson.E{"data_time", bson.M{"$gte": startDate}})
  29. }
  30. if !endDate.IsZero() {
  31. filter = append(filter, bson.E{"data_time", bson.M{"$lte": endDate}})
  32. }
  33. ctx := context.TODO()
  34. cur, err := db.Find(filter, findOptions)
  35. if err != nil {
  36. return
  37. }
  38. // Close the cursor once finished
  39. defer cur.Close(ctx)
  40. for cur.Next(ctx) {
  41. // create a value into which the single document can be decoded
  42. var elem EdbDataBase
  43. err = cur.Decode(&elem)
  44. if err != nil {
  45. return
  46. }
  47. elem.DataTime = elem.DataTime.In(time.Local)
  48. elem.CreateTime = elem.CreateTime.In(time.Local)
  49. elem.ModifyTime = elem.ModifyTime.In(time.Local)
  50. list = append(list, &elem)
  51. }
  52. if err = cur.Err(); err != nil {
  53. return
  54. }
  55. return
  56. }