123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- package mgo
- import (
- "context"
- "errors"
- "fmt"
- "github.com/qiniu/qmgo"
- "go.mongodb.org/mongo-driver/bson"
- "go.mongodb.org/mongo-driver/bson/primitive"
- "hongze/hongze_yb/global"
- "time"
- )
- type BaseFromBusinessData struct {
- ID primitive.ObjectID `json:"_id" bson:"_id,omitempty"`
-
- BaseFromBusinessIndexId int64 `json:"base_from_business_index_id" bson:"base_from_business_index_id"`
- IndexCode string `json:"index_code" bson:"index_code"`
- DataTime time.Time `json:"data_time" bson:"data_time"`
- Value float64 `json:"value" bson:"value"`
- CreateTime time.Time `json:"create_time" bson:"create_time"`
- ModifyTime time.Time `json:"modify_time" bson:"modify_time"`
- }
- func (m *BaseFromBusinessData) CollectionName() string {
- return "base_from_business_data"
- }
- func (m *BaseFromBusinessData) DataBaseName() string {
- return global.CONFIG.Mongo.Database
- }
- func (m *BaseFromBusinessData) GetCollection() *qmgo.Collection {
- db := global.MgoDataCli.Database(m.DataBaseName())
- return db.Collection(m.CollectionName())
- }
- func (m *BaseFromBusinessData) GetAllDataList(whereParams interface{}, sort []string) (result []*BaseFromBusinessData, err error) {
- if global.MgoDataCli == nil {
- err = errors.New("mongodb连接失败")
- return
- }
- db := global.MgoDataCli.Database(m.DataBaseName())
- coll := db.Collection(m.CollectionName())
- ctx := context.TODO()
- if err != nil {
- fmt.Println("MgoGetColl Err:", err.Error())
- return
- }
- err = coll.Find(ctx, whereParams).Sort(sort...).All(&result)
- if err != nil {
- return
- }
- for _, v := range result {
- v.DataTime = v.DataTime.In(time.Local)
- v.CreateTime = v.CreateTime.In(time.Local)
- v.ModifyTime = v.ModifyTime.In(time.Local)
- }
- return
- }
- func (m *BaseFromBusinessData) GetLimitDataList(whereParams interface{}, size int64, sort []string) (result []*BaseFromBusinessData, err error) {
- if global.MgoDataCli == nil {
- err = errors.New("mongodb连接失败")
- return
- }
- db := global.MgoDataCli.Database(m.DataBaseName())
- coll := db.Collection(m.CollectionName())
- ctx := context.TODO()
- if err != nil {
- fmt.Println("MgoGetColl Err:", err.Error())
- return
- }
- err = coll.Find(ctx, whereParams).Sort(sort...).Limit(size).All(&result)
- if err != nil {
- return
- }
- for _, v := range result {
- v.DataTime = v.DataTime.In(time.Local)
- v.CreateTime = v.CreateTime.In(time.Local)
- v.ModifyTime = v.ModifyTime.In(time.Local)
- }
- return
- }
- func (m *BaseFromBusinessData) GetPageDataList(whereParams interface{}, startSize, size int64, sort []string) (result []*BaseFromBusinessData, err error) {
- if global.MgoDataCli == nil {
- err = errors.New("mongodb连接失败")
- return
- }
- db := global.MgoDataCli.Database(m.DataBaseName())
- coll := db.Collection(m.CollectionName())
- ctx := context.TODO()
- if err != nil {
- fmt.Println("MgoGetColl Err:", err.Error())
- return
- }
- err = coll.Find(ctx, whereParams).Sort(sort...).Skip(startSize).Limit(size).All(&result)
- if err != nil {
- return
- }
- for _, v := range result {
- v.DataTime = v.DataTime.In(time.Local)
- v.CreateTime = v.CreateTime.In(time.Local)
- v.ModifyTime = v.ModifyTime.In(time.Local)
- }
- return
- }
- func (m *BaseFromBusinessData) GetCountDataList(whereParams interface{}) (count int64, err error) {
- if global.MgoDataCli == nil {
- err = errors.New("mongodb连接失败")
- return
- }
- db := global.MgoDataCli.Database(m.DataBaseName())
- coll := db.Collection(m.CollectionName())
- ctx := context.TODO()
- if err != nil {
- fmt.Println("MgoGetColl Err:", err.Error())
- return
- }
- count, err = coll.Find(ctx, whereParams).Count()
- return
- }
- func (m *BaseFromBusinessData) BatchInsertData(dataList interface{}) (err error) {
- db := global.MgoDataCli.Database(m.DataBaseName())
- coll := db.Collection(m.CollectionName())
- ctx := context.TODO()
- _, err = coll.InsertMany(ctx, dataList)
- if err != nil {
- fmt.Println("BatchInsertData:Err:" + err.Error())
- return
- }
- return
- }
- func (m *BaseFromBusinessData) UpdateDataByColl(coll *qmgo.Collection, whereParams, updateParams interface{}) (err error) {
- ctx := context.TODO()
- err = coll.UpdateOne(ctx, whereParams, updateParams)
- if err != nil {
- fmt.Println("UpdateDataByColl:Err:" + err.Error())
- return
- }
- return
- }
- func (m *BaseFromBusinessData) UpdateData(whereParams, updateParams interface{}) (err error) {
- db := global.MgoDataCli.Database(m.DataBaseName())
- coll := db.Collection(m.CollectionName())
- ctx := context.TODO()
- err = coll.UpdateOne(ctx, whereParams, updateParams)
- if err != nil {
- fmt.Println("UpdateData:Err:" + err.Error())
- return
- }
- return
- }
- func (m *BaseFromBusinessData) HandleData(addDataList, updateDataList []BaseFromBusinessData) (result interface{}, err error) {
- ctx := context.TODO()
- callback := func(sessCtx context.Context) (interface{}, error) {
-
- db := global.MgoDataCli.Database(m.DataBaseName())
- coll := db.Collection(m.CollectionName())
-
- if len(addDataList) > 0 {
- _, err = coll.InsertMany(sessCtx, addDataList)
- if err != nil {
- return nil, err
- }
- }
-
- if len(updateDataList) > 0 {
- for _, v := range updateDataList {
- err = coll.UpdateOne(ctx, bson.M{"_id": v.ID}, bson.M{"$set": bson.M{"value": v.Value, "modify_time": v.ModifyTime}})
- if err != nil {
- fmt.Println("BatchInsertData:Err:" + err.Error())
- return nil, err
- }
- }
- }
- return nil, nil
- }
- result, err = global.MgoDataCli.DoTransaction(ctx, callback)
- return
- }
- func (m *BaseFromBusinessData) GetEdbInfoMaxAndMinInfo(whereParams interface{}) (result EdbInfoMaxAndMinInfo, err error) {
- if global.MgoDataCli == nil {
- err = errors.New("mongodb连接失败")
- return
- }
- db := global.MgoDataCli.Database(m.DataBaseName())
- coll := db.Collection(m.CollectionName())
- ctx := context.TODO()
- if err != nil {
- fmt.Println("MgoGetColl Err:", err.Error())
- return
- }
- err = coll.Aggregate(ctx, whereParams).One(&result)
- if err != nil {
- return
- }
- result.MinDate = result.MinDate.In(time.Local)
- result.MaxDate = result.MaxDate.In(time.Local)
- result.LatestDate = result.LatestDate.In(time.Local)
- return
- }
- func (m *BaseFromBusinessData) GetLatestValue(whereParams, selectParam interface{}) (latestValue LatestValue, err error) {
- if global.MgoDataCli == nil {
- err = errors.New("mongodb连接失败")
- return
- }
- db := global.MgoDataCli.Database(m.DataBaseName())
- coll := db.Collection(m.CollectionName())
- ctx := context.TODO()
- if err != nil {
- fmt.Println("MgoGetColl Err:", err.Error())
- return
- }
-
-
- err = coll.Find(ctx, whereParams).Select(selectParam).One(&latestValue)
- return
- }
|