123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510 |
- package mgo
- import (
- "context"
- "errors"
- "eta/eta_mobile/utils"
- "fmt"
- "github.com/qiniu/qmgo"
- "go.mongodb.org/mongo-driver/bson"
- "go.mongodb.org/mongo-driver/bson/primitive"
- "time"
- )
- // EdbDataThsHf
- // @Description: 同花顺高频集合(指标库)
- type EdbDataThsHf struct {
- ID primitive.ObjectID `json:"_id" bson:"_id,omitempty" ` // 文档id
- EdbInfoId int `json:"edb_info_id" bson:"edb_info_id"` // 指标ID
- EdbCode string `json:"edb_code" bson:"edb_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"` // 修改时间
- DataTimestamp int64 `json:"data_timestamp" bson:"data_timestamp"` // 数据日期时间戳
- }
- // CollectionName
- // @Description: 获取集合名称
- // @author: Roc
- // @receiver m
- // @datetime 2024-04-26 13:41:36
- // @return string
- func (m *EdbDataThsHf) CollectionName() string {
- return "edb_data_ths_hf"
- }
- // DataBaseName
- // @Description: 获取数据库名称
- // @author: Roc
- // @receiver m
- // @datetime 2024-04-26 13:41:33
- // @return string
- func (m *EdbDataThsHf) DataBaseName() string {
- return utils.MgoDataDbName
- }
- // GetCollection
- // @Description: 获取mongodb集合的句柄
- // @author: Roc
- // @receiver m
- // @datetime 2024-04-26 13:41:33
- // @return string
- func (m *EdbDataThsHf) GetCollection() *qmgo.Collection {
- db := utils.MgoDataCli.Database(m.DataBaseName())
- return db.Collection(m.CollectionName())
- }
- // GetItem
- // @Description: 根据条件获取单条数据
- // @author: Roc
- // @receiver m
- // @datetime 2024-05-09 10:00:49
- // @param whereParams interface{}
- // @return item *EdbDataThsHf
- // @return err error
- func (m *EdbDataThsHf) GetItem(whereParams interface{}) (item *EdbDataThsHf, err error) {
- if utils.MgoDataCli == nil {
- err = errors.New("mongodb连接失败")
- return
- }
- db := utils.MgoDataCli.Database(m.DataBaseName())
- coll := db.Collection(m.CollectionName())
- return m.GetItemByColl(coll, whereParams)
- }
- // GetItemByColl
- // @Description: 根据条件获取单条数据
- // @author: Roc
- // @receiver m
- // @datetime 2024-05-09 13:22:06
- // @param coll *qmgo.Collection
- // @param whereParams interface{}
- // @return item *EdbDataThsHf
- // @return err error
- func (m *EdbDataThsHf) GetItemByColl(coll *qmgo.Collection, whereParams interface{}) (item *EdbDataThsHf, err error) {
- ctx := context.TODO()
- if err != nil {
- fmt.Println("MgoGetColl Err:", err.Error())
- return
- }
- err = coll.Find(ctx, whereParams).One(&item)
- if err != nil {
- return
- }
- item.DataTime = item.DataTime.In(time.Local)
- item.CreateTime = item.CreateTime.In(time.Local)
- item.ModifyTime = item.ModifyTime.In(time.Local)
- return
- }
- // GetAllDataList
- // @Description: 根据条件获取所有数据
- // @author: Roc
- // @receiver m
- // @datetime 2024-04-26 13:42:19
- // @param whereParams interface{}
- // @param sort []string
- // @return result []EdbDataThsHf
- // @return err error
- func (m *EdbDataThsHf) GetAllDataList(whereParams interface{}, sort []string) (result []*EdbDataThsHf, err error) {
- if utils.MgoDataCli == nil {
- err = errors.New("mongodb连接失败")
- return
- }
- db := utils.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
- }
- // GetLimitDataList
- // @Description: 根据条件获取指定数量数据列表
- // @author: Roc
- // @receiver m
- // @datetime 2024-05-06 17:08:32
- // @param whereParams interface{}
- // @param size int64
- // @param sort []string
- // @return result []*BaseFromBusinessData
- // @return err error
- func (m *EdbDataThsHf) GetLimitDataList(whereParams interface{}, size int64, sort []string) (result []*EdbDataThsHf, err error) {
- if utils.MgoDataCli == nil {
- err = errors.New("mongodb连接失败")
- return
- }
- db := utils.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
- }
- // GetPageDataList
- // @Description: 根据条件获取分页数据列表
- // @author: Roc
- // @receiver m
- // @datetime 2024-05-07 10:21:07
- // @param whereParams interface{}
- // @param startSize int64
- // @param size int64
- // @param sort []string
- // @return result []*EdbDataThsHf
- // @return err error
- func (m *EdbDataThsHf) GetPageDataList(whereParams interface{}, startSize, size int64, sort []string) (result []*EdbDataThsHf, err error) {
- if utils.MgoDataCli == nil {
- err = errors.New("mongodb连接失败")
- return
- }
- db := utils.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
- }
- // GetCountDataList
- // @Description: 根据条件获取数据列表总数
- // @author: Roc
- // @receiver m
- // @datetime 2024-05-07 10:29:00
- // @param whereParams interface{}
- // @return count int64
- // @return err error
- func (m *EdbDataThsHf) GetCountDataList(whereParams interface{}) (count int64, err error) {
- if utils.MgoDataCli == nil {
- err = errors.New("mongodb连接失败")
- return
- }
- db := utils.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
- }
- // InsertDataByColl
- // @Description: 写入单条数据(外部传入集合)
- // @author: Roc
- // @receiver m
- // @datetime 2024-04-26 14:22:18
- // @param addData interface{}
- // @return err error
- func (m *EdbDataThsHf) InsertDataByColl(coll *qmgo.Collection, addData interface{}) (err error) {
- ctx := context.TODO()
- _, err = coll.InsertOne(ctx, addData)
- if err != nil {
- fmt.Println("InsertDataByColl:Err:" + err.Error())
- return
- }
- return
- }
- // BatchInsertData
- // @Description: 批量写入数据
- // @author: Roc
- // @receiver m
- // @datetime 2024-04-26 14:22:18
- // @param bulk int 每次请求保存的数据量
- // @param dataList []interface{}
- // @return err error
- func (m *EdbDataThsHf) BatchInsertData(bulk int, dataList []interface{}) (err error) {
- db := utils.MgoDataCli.Database(m.DataBaseName())
- coll := db.Collection(m.CollectionName())
- return m.BatchInsertDataByColl(coll, bulk, dataList)
- }
- // BatchInsertDataByColl
- // @Description: 批量写入数据(外部传入集合)
- // @author: Roc
- // @receiver m
- // @datetime 2024-04-26 14:22:18
- // @param coll *qmgo.Collection
- // @param bulk int 每次请求保存的数据量
- // @param dataList []interface{}
- // @return err error
- func (m *EdbDataThsHf) BatchInsertDataByColl(coll *qmgo.Collection, bulk int, dataList []interface{}) (err error) {
- ctx := context.TODO()
- dataNum := len(dataList)
- if dataNum <= 0 {
- return
- }
- // 不设置每次保存切片数量大小,或者实际数据量小于设置的切片数量大小,那么就直接保存吧
- if bulk <= 0 || dataNum <= bulk {
- _, err = coll.InsertMany(ctx, dataList)
- if err != nil {
- fmt.Println("BatchInsertData:Err:" + err.Error())
- return
- }
- return
- }
- // 分批保存
- i := 0
- tmpAddDataList := make([]interface{}, 0)
- for _, v := range dataList {
- tmpAddDataList = append(tmpAddDataList, v)
- i++
- if i >= bulk {
- _, err = coll.InsertMany(ctx, tmpAddDataList)
- if err != nil {
- fmt.Println("BatchInsertData:Err:" + err.Error())
- return
- }
- i = 0
- tmpAddDataList = make([]interface{}, 0)
- }
- }
- if len(tmpAddDataList) > 0 {
- _, err = coll.InsertMany(ctx, tmpAddDataList)
- if err != nil {
- fmt.Println("BatchInsertData:Err:" + err.Error())
- return
- }
- }
- return
- }
- // UpdateData
- // @Description: 单条数据修改
- // @author: Roc
- // @receiver m
- // @datetime 2024-04-26 15:01:51
- // @param whereParams interface{}
- // @param updateParams interface{}
- // @return err error
- func (m *EdbDataThsHf) UpdateData(whereParams, updateParams interface{}) (err error) {
- db := utils.MgoDataCli.Database(m.DataBaseName())
- coll := db.Collection(m.CollectionName())
- return m.UpdateDataByColl(coll, whereParams, updateParams)
- }
- // UpdateDataByColl
- // @Description: 单条数据修改(外部传入集合)
- // @author: Roc
- // @receiver m
- // @datetime 2024-04-26 15:01:51
- // @param whereParams interface{}
- // @param updateParams interface{}
- // @return err error
- func (m *EdbDataThsHf) 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
- }
- // RemoveMany
- // @Description: 根据条件删除多条数据
- // @author: Roc
- // @receiver m
- // @datetime 2024-04-30 13:17:02
- // @param whereParams interface{}
- // @return err error
- func (m *EdbDataThsHf) RemoveMany(whereParams interface{}) (err error) {
- db := utils.MgoDataCli.Database(m.DataBaseName())
- coll := db.Collection(m.CollectionName())
- return m.RemoveManyByColl(coll, whereParams)
- }
- // RemoveManyByColl
- // @Description: 根据条件删除多条数据(外部传入集合)
- // @author: Roc
- // @receiver m
- // @datetime 2024-04-30 13:18:42
- // @param coll *qmgo.Collection
- // @param whereParams interface{}
- // @return err error
- func (m *EdbDataThsHf) RemoveManyByColl(coll *qmgo.Collection, whereParams interface{}) (err error) {
- ctx := context.TODO()
- _, err = coll.RemoveAll(ctx, whereParams)
- if err != nil {
- fmt.Println("RemoveManyByColl:Err:" + err.Error())
- return
- }
- return
- }
- // HandleData
- // @Description: 事务处理数据
- // @author: Roc
- // @receiver m
- // @datetime 2024-04-30 10:39:01
- // @param addDataList []AddEdbDataThsHf
- // @param updateDataList []EdbDataThsHf
- // @return result interface{}
- // @return err error
- func (m *EdbDataThsHf) HandleData(addDataList, updateDataList []EdbDataThsHf) (result interface{}, err error) {
- ctx := context.TODO()
- callback := func(sessCtx context.Context) (interface{}, error) {
- // 重要:确保事务中的每一个操作,都使用传入的sessCtx参数
- db := utils.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 = utils.MgoDataCli.DoTransaction(ctx, callback)
- return
- }
- // EdbInfoMaxAndMinInfo 指标最新数据记录结构体
- //type EdbInfoMaxAndMinInfo struct {
- // MinDate time.Time `description:"最小日期" bson:"min_date"`
- // MaxDate time.Time `description:"最大日期" bson:"max_date"`
- // MinValue float64 `description:"最小值" bson:"min_value"`
- // MaxValue float64 `description:"最大值" bson:"max_value"`
- // LatestValue float64 `description:"最新值" bson:"latest_value"`
- // LatestDate time.Time `description:"实际数据最新日期" bson:"latest_date"`
- // EndValue float64 `description:"最新值" bson:"end_value"`
- //}
- // GetEdbInfoMaxAndMinInfo
- // @Description: 获取当前指标的最大最小值
- // @author: Roc
- // @receiver m
- // @datetime 2024-04-30 17:15:39
- // @param whereParams interface{}
- // @return result EdbInfoMaxAndMinInfo
- // @return err error
- func (m *EdbDataThsHf) GetEdbInfoMaxAndMinInfo(whereParams interface{}) (result EdbInfoMaxAndMinInfo, err error) {
- if utils.MgoDataCli == nil {
- err = errors.New("mongodb连接失败")
- return
- }
- db := utils.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
- }
- // LatestValue 指标最新数据记录结构体
- //type LatestValue struct {
- // Value float64 `description:"值" bson:"value"`
- //}
- // GetLatestValue
- // @Description: 获取当前指标的最新数据记录
- // @author: Roc
- // @receiver m
- // @datetime 2024-04-30 17:16:15
- // @param whereParams interface{}
- // @param selectParam interface{}
- // @return latestValue LatestValue
- // @return err error
- func (m *EdbDataThsHf) GetLatestValue(whereParams, selectParam interface{}) (latestValue LatestValue, err error) {
- if utils.MgoDataCli == nil {
- err = errors.New("mongodb连接失败")
- return
- }
- db := utils.MgoDataCli.Database(m.DataBaseName())
- coll := db.Collection(m.CollectionName())
- ctx := context.TODO()
- if err != nil {
- fmt.Println("MgoGetColl Err:", err.Error())
- return
- }
- //var result interface{}
- //err = coll.Find(ctx, whereParams).Select(selectParam).One(&result)
- err = coll.Find(ctx, whereParams).Select(selectParam).One(&latestValue)
- return
- }
|