package mgodb import ( "context" "eta/eta_forum_hub/utils" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo/options" "time" ) func GetEdbDataCalculateByEdbInfoId(edbInfoId int) (items []*EdbDataBase, err error) { findOptions := options.Find() db := NewMgo(utils.MgoDataDbName, "edb_data_calculate", 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 } func GetEdbDataCalculateByEdbCode(edbCode string) (items []*EdbDataBase, err error) { findOptions := options.Find() db := NewMgo(utils.MgoDataDbName, "edb_data_calculate", utils.MgoDataCli) filter := bson.D{{"edb_code", edbCode}} 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 } func InsertEdbCalculateDataBatch(items []interface{}) (err error) { db := NewMgo(utils.MgoDataDbName, "edb_data_calculate", utils.MgoDataCli) _, err = db.InsertMany(items) if err != nil { return } return } func ModifyEdbCalculateDataEdbInfoId(edbInfoId int64, edbCode string) (err error) { filter := bson.D{{"edb_code", edbCode}} update := bson.D{{"$set", bson.D{{"edb_info_id", edbInfoId}, {"modify_time", time.Now()}}}} db := NewMgo(utils.MgoDataDbName, "edb_data_calculate", utils.MgoDataCli) _, err = db.UpdateMany(filter, update) if err != nil { return } return } // 删除 func DeleteEdbInfoCalculateDataByEdbInfoId(edbInfoId int) (err error) { filter := bson.D{{"edb_info_id", edbInfoId}} db := NewMgo(utils.MgoDataDbName, "edb_data_calculate", utils.MgoDataCli) _, err = db.DeleteMany(filter) return } // 更新指标的值 func ModifyValueEdbCalculateDataValue(edbDataId primitive.ObjectID, value float64) (err error) { filter := bson.D{{"_id", edbDataId}} update := bson.D{{"$set", bson.D{{"value", value}, {"modify_time", time.Now()}}}} db := NewMgo(utils.MgoDataDbName, "edb_data_calculate", utils.MgoDataCli) _, err = db.UpdateOne(filter, update) return } func DeleteEdbCalculateDataByEdbInfoIdAndDate(edbCode string, dataTime []time.Time) (err error) { filter := bson.D{{"edb_code", edbCode}, {"data_time", bson.D{{"$in", dataTime}}}} db := NewMgo(utils.MgoDataDbName, "edb_data_calculate", utils.MgoDataCli) _, err = db.DeleteMany(filter) return }