edb_data_base.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. 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. type EdbInfoSearchData struct {
  22. DataTime time.Time `description:"数据日期" bson:"data_time"`
  23. Value float64 `description:"数据" bson:"value"`
  24. EdbCode string `description:"指标编码" bson:"edb_code"`
  25. }
  26. func GetEdbDataBaseByEdbCode(edbCode string) (items []*EdbDataBase, err error) {
  27. findOptions := options.Find()
  28. db := NewMgo(utils.MgoDataDbName, "edb_data_base", utils.MgoDataCli)
  29. filter := bson.D{{"edb_code", edbCode}}
  30. ctx := context.TODO()
  31. cur, err := db.Find(filter, findOptions)
  32. if err != nil {
  33. return
  34. }
  35. // Close the cursor once finished
  36. defer cur.Close(ctx)
  37. for cur.Next(ctx) {
  38. // create a value into which the single document can be decoded
  39. var elem EdbDataBase
  40. err = cur.Decode(&elem)
  41. if err != nil {
  42. return
  43. }
  44. elem.DataTime = elem.DataTime.In(time.Local)
  45. elem.CreateTime = elem.CreateTime.In(time.Local)
  46. elem.ModifyTime = elem.ModifyTime.In(time.Local)
  47. items = append(items, &elem)
  48. }
  49. if err = cur.Err(); err != nil {
  50. return
  51. }
  52. return
  53. }
  54. func InsertEdbDataBatch(items []interface{}) (err error) {
  55. db := NewMgo(utils.MgoDataDbName, "edb_data_base", utils.MgoDataCli)
  56. _, err = db.InsertMany(items)
  57. if err != nil {
  58. return
  59. }
  60. return
  61. }
  62. func ModifyEdbDataEdbInfoId(edbInfoId int64, edbCode string) (err error) {
  63. filter := bson.D{{"edb_code", edbCode}}
  64. update := bson.D{{"$set", bson.D{{"edb_info_id", edbInfoId}, {"modify_time", time.Now()}}}}
  65. db := NewMgo(utils.MgoDataDbName, "edb_data_base", utils.MgoDataCli)
  66. _, err = db.UpdateMany(filter, update)
  67. if err != nil {
  68. return
  69. }
  70. return
  71. }
  72. // 删除
  73. func DeleteEdbInfoDataByEdbInfoId(edbInfoId int) (err error) {
  74. filter := bson.D{{"edb_info_id", edbInfoId}}
  75. db := NewMgo(utils.MgoDataDbName, "edb_data_base", utils.MgoDataCli)
  76. _, err = db.DeleteMany(filter)
  77. return
  78. }
  79. // 删除
  80. func DeleteEdbInfoDataByEdbInfoIdAndDate(edbCode string, dataTime []time.Time) (err error) {
  81. filter := bson.D{{"edb_code", edbCode}, {"data_time", bson.D{{"$in", dataTime}}}}
  82. db := NewMgo(utils.MgoDataDbName, "edb_data_base", utils.MgoDataCli)
  83. _, err = db.DeleteMany(filter)
  84. return
  85. }
  86. // 更新指标的值
  87. func ModifyValueEdbDataValue(edbDataId primitive.ObjectID, value float64) (err error) {
  88. filter := bson.D{{"_id", edbDataId}}
  89. update := bson.D{{"$set", bson.D{{"value", value}, {"modify_time", time.Now()}}}}
  90. db := NewMgo(utils.MgoDataDbName, "edb_data_base", utils.MgoDataCli)
  91. _, err = db.UpdateOne(filter, update)
  92. return
  93. }