edb_data_base.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. type EdbDataBase struct {
  10. EdbInfoId int `bson:"edb_info_id"`
  11. EdbCode string `bson:"edb_code"`
  12. DataTime string `bson:"data_time"`
  13. Value string `bson:"value"`
  14. Status int `bson:"status"`
  15. CreateTime string `bson:"create_time"`
  16. ModifyTime string `bson:"modify_time"`
  17. DataTimestamp int64 `bson:"data_timestamp"`
  18. }
  19. type EdbInfoSearchData struct {
  20. DataTime string `description:"数据日期" bson:"data_time"`
  21. Value float64 `description:"数据" bson:"value"`
  22. EdbCode string `description:"指标编码" bson:"edb_code"`
  23. }
  24. func GetEdbDataBaseByEdbInfoId(edbInfoId int) (items []*EdbDataBase, err error) {
  25. findOptions := options.Find()
  26. db := NewMgo(utils.MONGODB_COMMUNITY, "edb_data_base", MgoClient)
  27. filter := bson.D{{"edb_info_id", edbInfoId}}
  28. ctx := context.TODO()
  29. cur, err := db.Find(filter, findOptions)
  30. if err != nil {
  31. return
  32. }
  33. // Close the cursor once finished
  34. defer cur.Close(ctx)
  35. for cur.Next(ctx) {
  36. // create a value into which the single document can be decoded
  37. var elem EdbDataBase
  38. err = cur.Decode(&elem)
  39. if err != nil {
  40. return
  41. }
  42. items = append(items, &elem)
  43. }
  44. if err = cur.Err(); err != nil {
  45. return
  46. }
  47. return
  48. }
  49. func GetEdbDataBaseByEdbCode(edbCode string) (items []*EdbDataBase, err error) {
  50. findOptions := options.Find()
  51. db := NewMgo(utils.MONGODB_COMMUNITY, "edb_data_base", MgoClient)
  52. filter := bson.D{{"edb_code", edbCode}}
  53. ctx := context.TODO()
  54. cur, err := db.Find(filter, findOptions)
  55. if err != nil {
  56. return
  57. }
  58. // Close the cursor once finished
  59. defer cur.Close(ctx)
  60. for cur.Next(ctx) {
  61. // create a value into which the single document can be decoded
  62. var elem EdbDataBase
  63. err = cur.Decode(&elem)
  64. if err != nil {
  65. return
  66. }
  67. items = append(items, &elem)
  68. }
  69. if err = cur.Err(); err != nil {
  70. return
  71. }
  72. return
  73. }
  74. // GetEdbDataList 获取指标的数据(日期正序返回)
  75. func GetEdbDataList(endInfoId int, startDate, endDate string) (list []*EdbDataBase, err error) {
  76. findOptions := options.Find()
  77. findOptions.SetSort(bson.D{{"data_time", 1}})
  78. db := NewMgo(utils.MONGODB_COMMUNITY, "edb_data_base", MgoClient)
  79. filter := bson.D{{"edb_info_id", endInfoId}}
  80. if startDate != "" {
  81. filter = append(filter, bson.E{"data_time", bson.M{"$gte": startDate}})
  82. }
  83. if endDate != "" {
  84. filter = append(filter, bson.E{"data_time", bson.M{"$lte": endDate}})
  85. }
  86. ctx := context.TODO()
  87. cur, err := db.Find(filter, findOptions)
  88. if err != nil {
  89. return
  90. }
  91. // Close the cursor once finished
  92. defer cur.Close(ctx)
  93. for cur.Next(ctx) {
  94. // create a value into which the single document can be decoded
  95. var elem EdbDataBase
  96. err = cur.Decode(&elem)
  97. if err != nil {
  98. return
  99. }
  100. list = append(list, &elem)
  101. }
  102. if err = cur.Err(); err != nil {
  103. return
  104. }
  105. return
  106. }
  107. func InsertBatch(items []interface{}) (err error) {
  108. db := NewMgo(utils.MONGODB_COMMUNITY, "edb_data_base", MgoClient)
  109. _, err = db.InsertMany(items)
  110. if err != nil {
  111. return
  112. }
  113. return
  114. }
  115. func ModifyEdbInfoDataStatus(edbInfoId int64, edbCode string) (err error) {
  116. filter := bson.D{{"edb_code", edbCode}}
  117. update := bson.D{{"$set", bson.D{{"edb_info_id", edbInfoId}, {"modify_time", time.Now().Format("2006-01-02 15:04:05")}}}}
  118. db := NewMgo(utils.MONGODB_COMMUNITY, "edb_data_base", MgoClient)
  119. _, err = db.UpdateMany(filter, update)
  120. if err != nil {
  121. return
  122. }
  123. return
  124. }
  125. // 删除
  126. func DeleteEdbInfoDataByEdbInfoId(edbInfoId int) (err error) {
  127. filter := bson.D{{"edb_info_id", edbInfoId}}
  128. db := NewMgo(utils.MONGODB_COMMUNITY, "edb_data_base", MgoClient)
  129. _, err = db.DeleteMany(filter)
  130. return
  131. }
  132. // 更新指标的值
  133. func ModifyValueEdbInfoDataStatus(edbCode string, dataTime string, value string) (err error) {
  134. filter := bson.D{{"edb_code", edbCode}, {"data_time", dataTime}}
  135. update := bson.D{{"$set", bson.D{{"value", value}, {"modify_time", time.Now().Format("2006-01-02 15:04:05")}}}}
  136. db := NewMgo(utils.MONGODB_COMMUNITY, "edb_data_base", MgoClient)
  137. _, err = db.UpdateOne(filter, update)
  138. return
  139. }