edb_data_calculate.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package mgodb
  2. import (
  3. "context"
  4. "eta/eta_forum_hub/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. func GetEdbDataCalculateByEdbCode(edbCode string) (items []*EdbDataBase, err error) {
  11. findOptions := options.Find()
  12. db := NewMgo(utils.MgoDataDbName, "edb_data_calculate", utils.MgoDataCli)
  13. filter := bson.D{{"edb_code", edbCode}}
  14. ctx := context.TODO()
  15. cur, err := db.Find(filter, findOptions)
  16. if err != nil {
  17. return
  18. }
  19. // Close the cursor once finished
  20. defer cur.Close(ctx)
  21. for cur.Next(ctx) {
  22. // create a value into which the single document can be decoded
  23. var elem EdbDataBase
  24. err = cur.Decode(&elem)
  25. if err != nil {
  26. return
  27. }
  28. elem.DataTime = elem.DataTime.In(time.Local)
  29. elem.CreateTime = elem.CreateTime.In(time.Local)
  30. elem.ModifyTime = elem.ModifyTime.In(time.Local)
  31. items = append(items, &elem)
  32. }
  33. if err = cur.Err(); err != nil {
  34. return
  35. }
  36. return
  37. }
  38. func InsertEdbCalculateDataBatch(items []interface{}) (err error) {
  39. db := NewMgo(utils.MgoDataDbName, "edb_data_calculate", utils.MgoDataCli)
  40. _, err = db.InsertMany(items)
  41. if err != nil {
  42. return
  43. }
  44. return
  45. }
  46. func ModifyEdbCalculateDataEdbInfoId(edbInfoId int64, edbCode string) (err error) {
  47. filter := bson.D{{"edb_code", edbCode}}
  48. update := bson.D{{"$set", bson.D{{"edb_info_id", edbInfoId}, {"modify_time", time.Now()}}}}
  49. db := NewMgo(utils.MgoDataDbName, "edb_data_calculate", utils.MgoDataCli)
  50. _, err = db.UpdateMany(filter, update)
  51. if err != nil {
  52. return
  53. }
  54. return
  55. }
  56. // 删除
  57. func DeleteEdbInfoCalculateDataByEdbInfoId(edbInfoId int) (err error) {
  58. filter := bson.D{{"edb_info_id", edbInfoId}}
  59. db := NewMgo(utils.MgoDataDbName, "edb_data_calculate", utils.MgoDataCli)
  60. _, err = db.DeleteMany(filter)
  61. return
  62. }
  63. // 更新指标的值
  64. func ModifyValueEdbCalculateDataValue(edbDataId primitive.ObjectID, value float64, modifyTime time.Time) (err error) {
  65. filter := bson.D{{"_id", edbDataId}}
  66. update := bson.D{{"$set", bson.D{{"value", value}, {"modify_time", modifyTime}}}}
  67. db := NewMgo(utils.MgoDataDbName, "edb_data_calculate", utils.MgoDataCli)
  68. _, err = db.UpdateOne(filter, update)
  69. return
  70. }
  71. func DeleteEdbCalculateDataByEdbCodeAndDateList(edbCode string, dataTime []time.Time) (err error) {
  72. filter := bson.D{{"edb_code", edbCode}, {"data_time", bson.D{{"$in", dataTime}}}}
  73. db := NewMgo(utils.MgoDataDbName, "edb_data_calculate", utils.MgoDataCli)
  74. _, err = db.DeleteMany(filter)
  75. return
  76. }
  77. func DeleteEdbCalculateDataByEdbInfoIdAndDateList(edbInfoId int, dataTime []time.Time) (err error) {
  78. filter := bson.D{{"edb_info_id", edbInfoId}, {"data_time", bson.D{{"$in", dataTime}}}}
  79. db := NewMgo(utils.MgoDataDbName, "edb_data_calculate", utils.MgoDataCli)
  80. _, err = db.DeleteMany(filter)
  81. return
  82. }
  83. // GetEdbCalculateDataList 获取指标的数据(日期正序返回)
  84. func GetEdbCalculateDataList(endInfoId int, startDate, endDate time.Time) (list []*EdbDataBase, err error) {
  85. findOptions := options.Find()
  86. findOptions.SetSort(bson.D{{"data_time", 1}})
  87. db := NewMgo(utils.MgoDataDbName, "edb_data_calculate", utils.MgoDataCli)
  88. filter := bson.D{{"edb_info_id", endInfoId}}
  89. if !startDate.IsZero() {
  90. filter = append(filter, bson.E{"data_time", bson.M{"$gte": startDate}})
  91. }
  92. if !endDate.IsZero() {
  93. filter = append(filter, bson.E{"data_time", bson.M{"$lte": endDate}})
  94. }
  95. ctx := context.TODO()
  96. //fmt.Printf("edb_info_id %d, start find %v\n", endInfoId, time.Now())
  97. cur, err := db.Find(filter, findOptions)
  98. if err != nil {
  99. return
  100. }
  101. //fmt.Printf("end find %v\n", time.Now())
  102. // Close the cursor once finished
  103. defer cur.Close(ctx)
  104. //fmt.Printf("start cur.Next %v\n", time.Now())
  105. for cur.Next(ctx) {
  106. // create a value into which the single document can be decoded
  107. var elem EdbDataBase
  108. err = cur.Decode(&elem)
  109. if err != nil {
  110. return
  111. }
  112. elem.DataTime = elem.DataTime.In(time.Local)
  113. elem.CreateTime = elem.CreateTime.In(time.Local)
  114. elem.ModifyTime = elem.ModifyTime.In(time.Local)
  115. list = append(list, &elem)
  116. }
  117. //fmt.Printf("end cur.Next %v\n", time.Now())
  118. if err = cur.Err(); err != nil {
  119. return
  120. }
  121. return
  122. }