edb_data_calculate.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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/mongo/options"
  7. "time"
  8. )
  9. // GetEdbCalculateDataList 获取指标的数据(日期正序返回)
  10. func GetEdbCalculateDataList(endInfoId int, startDate, endDate time.Time) (list []*EdbDataBase, err error) {
  11. findOptions := options.Find()
  12. findOptions.SetSort(bson.D{{"data_time", 1}})
  13. db := NewMgo(utils.MgoDataDbName, "edb_data_calculate", utils.MgoDataCli)
  14. filter := bson.D{{"edb_info_id", endInfoId}}
  15. if !startDate.IsZero() {
  16. filter = append(filter, bson.E{"data_time", bson.M{"$gte": startDate}})
  17. }
  18. if !endDate.IsZero() {
  19. filter = append(filter, bson.E{"data_time", bson.M{"$lte": endDate}})
  20. }
  21. ctx := context.TODO()
  22. cur, err := db.Find(filter, findOptions)
  23. if err != nil {
  24. return
  25. }
  26. // Close the cursor once finished
  27. defer cur.Close(ctx)
  28. for cur.Next(ctx) {
  29. // create a value into which the single document can be decoded
  30. var elem EdbDataBase
  31. err = cur.Decode(&elem)
  32. if err != nil {
  33. return
  34. }
  35. elem.DataTime = elem.DataTime.In(time.Local)
  36. elem.CreateTime = elem.CreateTime.In(time.Local)
  37. elem.ModifyTime = elem.ModifyTime.In(time.Local)
  38. list = append(list, &elem)
  39. }
  40. if err = cur.Err(); err != nil {
  41. return
  42. }
  43. return
  44. }