123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- 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"
- )
- 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"`
- }
- type EdbInfoSearchData struct {
- DataTime time.Time `description:"数据日期" bson:"data_time"`
- Value float64 `description:"数据" bson:"value"`
- EdbCode string `description:"指标编码" bson:"edb_code"`
- }
- func GetEdbDataBaseByEdbCode(edbCode string) (items []*EdbDataBase, err error) {
- findOptions := options.Find()
- db := NewMgo(utils.MgoDataDbName, "edb_data_base", 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
- }
- elem.DataTime = elem.DataTime.In(time.Local)
- elem.CreateTime = elem.CreateTime.In(time.Local)
- elem.ModifyTime = elem.ModifyTime.In(time.Local)
- items = append(items, &elem)
- }
- if err = cur.Err(); err != nil {
- return
- }
- return
- }
- func InsertEdbDataBatch(items []interface{}) (err error) {
- db := NewMgo(utils.MgoDataDbName, "edb_data_base", utils.MgoDataCli)
- _, err = db.InsertMany(items)
- if err != nil {
- return
- }
- return
- }
- func ModifyEdbDataEdbInfoId(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_base", utils.MgoDataCli)
- _, err = db.UpdateMany(filter, update)
- if err != nil {
- return
- }
- return
- }
- // 删除
- func DeleteEdbInfoDataByEdbInfoId(edbInfoId int) (err error) {
- filter := bson.D{{"edb_info_id", edbInfoId}}
- db := NewMgo(utils.MgoDataDbName, "edb_data_base", utils.MgoDataCli)
- _, err = db.DeleteMany(filter)
- return
- }
- // 删除
- func DeleteEdbInfoDataByEdbInfoIdAndDate(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_base", utils.MgoDataCli)
- _, err = db.DeleteMany(filter)
- return
- }
- // 更新指标的值
- func ModifyValueEdbDataValue(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_base", utils.MgoDataCli)
- _, err = db.UpdateOne(filter, update)
- return
- }
|