1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- package mgodb
- import (
- "context"
- "eta/eta_forum_admin/utils"
- "go.mongodb.org/mongo-driver/bson"
- "go.mongodb.org/mongo-driver/bson/primitive"
- "go.mongodb.org/mongo-driver/mongo/options"
- "time"
- )
- type EdbDataBase struct {
- EdbDataId primitive.ObjectID `bson:"_id,omitempty"`
- EdbInfoId int `bson:"edb_info_id"`
- EdbCode string `bson:"edb_code"`
- DataTime time.Time `bson:"data_time"`
- Value float64 `bson:"value"`
- Status int `bson:"status"`
- CreateTime time.Time `bson:"create_time"`
- ModifyTime time.Time `bson:"modify_time"`
- DataTimestamp int64 `bson:"data_timestamp"`
- }
- func GetEdbDataBaseByEdbInfoId(edbInfoId int) (items []*EdbDataBase, err error) {
- findOptions := options.Find()
- db := NewMgo(utils.MgoDataDbName, "edb_data_base", utils.MgoDataCli)
- filter := bson.D{{"edb_info_id", edbInfoId}}
- ctx := context.TODO()
- cur, err := db.Find(filter, findOptions)
- if err != nil {
- return
- }
- // Close the cursor once finished
- defer cur.Close(ctx)
- for cur.Next(ctx) {
- // create a value into which the single document can be decoded
- var elem EdbDataBase
- err = cur.Decode(&elem)
- if err != nil {
- return
- }
- items = append(items, &elem)
- }
- if err = cur.Err(); err != nil {
- return
- }
- return
- }
- // GetEdbDataList 获取指标的数据(日期正序返回)
- func GetEdbDataList(endInfoId int, startDate, endDate time.Time) (list []*EdbDataBase, err error) {
- findOptions := options.Find()
- findOptions.SetSort(bson.D{{"data_time", 1}})
- db := NewMgo(utils.MgoDataDbName, "edb_data_base", utils.MgoDataCli)
- filter := bson.D{{"edb_info_id", endInfoId}}
- if !startDate.IsZero() {
- filter = append(filter, bson.E{"data_time", bson.M{"$gte": startDate}})
- }
- if !endDate.IsZero() {
- filter = append(filter, bson.E{"data_time", bson.M{"$lte": endDate}})
- }
- ctx := context.TODO()
- cur, err := db.Find(filter, findOptions)
- if err != nil {
- return
- }
- // Close the cursor once finished
- defer cur.Close(ctx)
- for cur.Next(ctx) {
- // create a value into which the single document can be decoded
- var elem EdbDataBase
- err = cur.Decode(&elem)
- if err != nil {
- return
- }
- list = append(list, &elem)
- }
- if err = cur.Err(); err != nil {
- return
- }
- return
- }
|