123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- package mgodb
- import (
- "context"
- "eta/eta_forum_admin/utils"
- "go.mongodb.org/mongo-driver/bson"
- "go.mongodb.org/mongo-driver/mongo/options"
- "time"
- )
- type EdbDataBase struct {
- EdbInfoId int `bson:"edb_info_id"`
- EdbCode string `bson:"edb_code"`
- DataTime string `bson:"data_time"`
- Value string `bson:"value"`
- Status int `bson:"status"`
- CreateTime string `bson:"create_time"`
- ModifyTime string `bson:"modify_time"`
- DataTimestamp int64 `bson:"data_timestamp"`
- }
- type EdbInfoSearchData struct {
- DataTime string `description:"数据日期" bson:"data_time"`
- Value float64 `description:"数据" bson:"value"`
- EdbCode string `description:"指标编码" bson:"edb_code"`
- }
- func GetEdbDataBaseByEdbInfoId(edbInfoId int) (items []*EdbDataBase, err error) {
- findOptions := options.Find()
- db := NewMgo(utils.MONGODB_COMMUNITY, "edb_data_base", MgoClient)
- 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 GetEdbDataBaseByEdbCode(edbCode string) (items []*EdbDataBase, err error) {
- findOptions := options.Find()
- db := NewMgo(utils.MONGODB_COMMUNITY, "edb_data_base", MgoClient)
- 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
- }
- // GetEdbDataList 获取指标的数据(日期正序返回)
- func GetEdbDataList(endInfoId int, startDate, endDate string) (list []*EdbDataBase, err error) {
- findOptions := options.Find()
- findOptions.SetSort(bson.D{{"data_time", 1}})
- db := NewMgo(utils.MONGODB_COMMUNITY, "edb_data_base", MgoClient)
- filter := bson.D{{"edb_info_id", endInfoId}}
- if startDate != "" {
- filter = append(filter, bson.E{"data_time", bson.M{"$gte": startDate}})
- }
- if endDate != "" {
- 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
- }
- func InsertBatch(items []interface{}) (err error) {
- db := NewMgo(utils.MONGODB_COMMUNITY, "edb_data_base", MgoClient)
- _, err = db.InsertMany(items)
- if err != nil {
- return
- }
- return
- }
- func ModifyEdbInfoDataStatus(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().Format("2006-01-02 15:04:05")}}}}
- db := NewMgo(utils.MONGODB_COMMUNITY, "edb_data_base", MgoClient)
- _, 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.MONGODB_COMMUNITY, "edb_data_base", MgoClient)
- _, err = db.DeleteMany(filter)
- return
- }
- // 更新指标的值
- func ModifyValueEdbInfoDataStatus(edbCode string, dataTime string, value string) (err error) {
- filter := bson.D{{"edb_code", edbCode}, {"data_time", dataTime}}
- update := bson.D{{"$set", bson.D{{"value", value}, {"modify_time", time.Now().Format("2006-01-02 15:04:05")}}}}
- db := NewMgo(utils.MONGODB_COMMUNITY, "edb_data_base", MgoClient)
- _, err = db.UpdateOne(filter, update)
- return
- }
|