edb_data_calculate.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. )
  8. func GetEdbDataCalculateByEdbInfoId(edbInfoId int) (items []*EdbDataBase, err error) {
  9. findOptions := options.Find()
  10. db := NewMgo(utils.MONGODB_COMMUNITY, "edb_data_calculate", MgoClient)
  11. filter := bson.D{{"edb_info_id", edbInfoId}}
  12. ctx := context.TODO()
  13. cur, err := db.Find(filter, findOptions)
  14. if err != nil {
  15. return
  16. }
  17. // Close the cursor once finished
  18. defer cur.Close(ctx)
  19. for cur.Next(ctx) {
  20. // create a value into which the single document can be decoded
  21. var elem EdbDataBase
  22. err = cur.Decode(&elem)
  23. if err != nil {
  24. return
  25. }
  26. items = append(items, &elem)
  27. }
  28. if err = cur.Err(); err != nil {
  29. return
  30. }
  31. return
  32. }
  33. // GetEdbCalculateDataList 获取指标的数据(日期正序返回)
  34. func GetEdbCalculateDataList(endInfoId int, startDate, endDate string) (list []*EdbDataBase, err error) {
  35. findOptions := options.Find()
  36. findOptions.SetSort(bson.D{{"data_time", 1}})
  37. db := NewMgo(utils.MONGODB_COMMUNITY, "edb_data_calculate", MgoClient)
  38. filter := bson.D{{"edb_info_id", endInfoId}}
  39. if startDate != "" {
  40. filter = append(filter, bson.E{"data_time", bson.M{"$gte": startDate}})
  41. }
  42. if endDate != "" {
  43. filter = append(filter, bson.E{"data_time", bson.M{"$lte": endDate}})
  44. }
  45. ctx := context.TODO()
  46. cur, err := db.Find(filter, findOptions)
  47. if err != nil {
  48. return
  49. }
  50. // Close the cursor once finished
  51. defer cur.Close(ctx)
  52. for cur.Next(ctx) {
  53. // create a value into which the single document can be decoded
  54. var elem EdbDataBase
  55. err = cur.Decode(&elem)
  56. if err != nil {
  57. return
  58. }
  59. list = append(list, &elem)
  60. }
  61. if err = cur.Err(); err != nil {
  62. return
  63. }
  64. return
  65. }