base_from_ths_hf_data.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. package mgo
  2. import (
  3. "context"
  4. "errors"
  5. "eta/eta_api/utils"
  6. "fmt"
  7. "github.com/qiniu/qmgo"
  8. "go.mongodb.org/mongo-driver/bson"
  9. "go.mongodb.org/mongo-driver/bson/primitive"
  10. "time"
  11. )
  12. // BaseFromThsHfData
  13. // @Description: 同花顺高频集合
  14. type BaseFromThsHfData struct {
  15. ID primitive.ObjectID `json:"_id" bson:"_id,omitempty"` // 文档id
  16. BaseFromThsHfDataId int64 `json:"base_from_ths_hf_data_id" bson:"base_from_ths_hf_data_id"` // 指标数据ID
  17. BaseFromThsHfIndexId int64 `json:"base_from_ths_hf_index_id" bson:"base_from_ths_hf_index_id"` // 指标ID
  18. IndexCode string `json:"index_code" bson:"index_code"` // 指标编码
  19. DataTime time.Time `json:"data_time" bson:"data_time"` // 数据日期
  20. Value float64 `json:"value" bson:"value"` // 数据值
  21. UniqueCode string `json:"unique_code" bson:"unique_code"` // 唯一编码
  22. CreateTime time.Time `json:"create_time" bson:"create_time"` // 创建时间
  23. ModifyTime time.Time `json:"modify_time" bson:"modify_time"` // 修改时间
  24. DataTimestamp int64 `json:"data_timestamp" bson:"data_timestamp"` // 数据日期时间戳
  25. }
  26. // CollectionName
  27. // @Description: 获取集合名称
  28. func (m *BaseFromThsHfData) CollectionName() string {
  29. return "base_from_ths_hf_data"
  30. }
  31. // DataBaseName
  32. // @Description: 获取数据库名称
  33. func (m *BaseFromThsHfData) DataBaseName() string {
  34. return utils.MgoDataDbName
  35. }
  36. // GetCollection
  37. // @Description: 获取mongodb集合的句柄
  38. func (m *BaseFromThsHfData) GetCollection() *qmgo.Collection {
  39. db := utils.MgoDataCli.Database(m.DataBaseName())
  40. return db.Collection(m.CollectionName())
  41. }
  42. // GetAllDataList
  43. // @Description: 根据条件获取所有数据
  44. // @author: Roc
  45. // @receiver m
  46. // @datetime 2024-04-26 13:42:19
  47. // @param sort []string
  48. // @param whereParams interface{}
  49. // @return result []BaseFromThsHfData
  50. // @return err error
  51. func (m *BaseFromThsHfData) GetAllDataList(whereParams interface{}, sort []string) (result []*BaseFromThsHfData, err error) {
  52. if utils.MgoDataCli == nil {
  53. err = errors.New("mongodb连接失败")
  54. return
  55. }
  56. db := utils.MgoDataCli.Database(m.DataBaseName())
  57. coll := db.Collection(m.CollectionName())
  58. ctx := context.TODO()
  59. if err != nil {
  60. fmt.Println("MgoGetColl Err:", err.Error())
  61. return
  62. }
  63. err = coll.Find(ctx, whereParams).Sort(sort...).All(&result)
  64. if err != nil {
  65. return
  66. }
  67. for _, v := range result {
  68. v.DataTime = v.DataTime.In(time.Local)
  69. v.CreateTime = v.CreateTime.In(time.Local)
  70. v.ModifyTime = v.ModifyTime.In(time.Local)
  71. }
  72. return
  73. }
  74. // GetLimitDataList
  75. // @Description: 根据条件获取指定数量数据列表
  76. // @author: Roc
  77. // @receiver m
  78. // @datetime 2024-05-06 17:08:32
  79. // @param whereParams interface{}
  80. // @param size int64
  81. // @return result []*BaseFromThsHfData
  82. // @return err error
  83. func (m *BaseFromThsHfData) GetLimitDataList(whereParams interface{}, size int64, sort []string) (result []*BaseFromThsHfData, err error) {
  84. if utils.MgoDataCli == nil {
  85. err = errors.New("mongodb连接失败")
  86. return
  87. }
  88. db := utils.MgoDataCli.Database(m.DataBaseName())
  89. coll := db.Collection(m.CollectionName())
  90. ctx := context.TODO()
  91. if err != nil {
  92. fmt.Println("MgoGetColl Err:", err.Error())
  93. return
  94. }
  95. err = coll.Find(ctx, whereParams).Sort(sort...).Limit(size).All(&result)
  96. if err != nil {
  97. return
  98. }
  99. for _, v := range result {
  100. v.DataTime = v.DataTime.In(time.Local)
  101. v.CreateTime = v.CreateTime.In(time.Local)
  102. v.ModifyTime = v.ModifyTime.In(time.Local)
  103. }
  104. return
  105. }
  106. // GetPageDataList
  107. // @Description: 根据条件获取分页数据列表
  108. // @author: Roc
  109. // @receiver m
  110. // @datetime 2024-05-07 10:21:07
  111. // @param whereParams interface{}
  112. // @param startSize int64
  113. // @param size int64
  114. // @param sort []string
  115. // @return result []*BaseFromThsHfData
  116. // @return err error
  117. func (m *BaseFromThsHfData) GetPageDataList(whereParams interface{}, startSize, size int64, sort []string) (result []*BaseFromThsHfData, err error) {
  118. if utils.MgoDataCli == nil {
  119. err = errors.New("mongodb连接失败")
  120. return
  121. }
  122. db := utils.MgoDataCli.Database(m.DataBaseName())
  123. coll := db.Collection(m.CollectionName())
  124. ctx := context.TODO()
  125. if err != nil {
  126. fmt.Println("MgoGetColl Err:", err.Error())
  127. return
  128. }
  129. err = coll.Find(ctx, whereParams).Sort(sort...).Skip(startSize).Limit(size).All(&result)
  130. if err != nil {
  131. return
  132. }
  133. for _, v := range result {
  134. v.DataTime = v.DataTime.In(time.Local)
  135. v.CreateTime = v.CreateTime.In(time.Local)
  136. v.ModifyTime = v.ModifyTime.In(time.Local)
  137. }
  138. return
  139. }
  140. // GetCountDataList
  141. // @Description: 根据条件获取数据列表总数
  142. // @author: Roc
  143. // @receiver m
  144. // @datetime 2024-05-07 10:29:00
  145. // @param whereParams interface{}
  146. // @return count int64
  147. // @return err error
  148. func (m *BaseFromThsHfData) GetCountDataList(whereParams interface{}) (count int64, err error) {
  149. if utils.MgoDataCli == nil {
  150. err = errors.New("mongodb连接失败")
  151. return
  152. }
  153. db := utils.MgoDataCli.Database(m.DataBaseName())
  154. coll := db.Collection(m.CollectionName())
  155. ctx := context.TODO()
  156. if err != nil {
  157. fmt.Println("MgoGetColl Err:", err.Error())
  158. return
  159. }
  160. count, err = coll.Find(ctx, whereParams).Count()
  161. return
  162. }
  163. // InsertDataByColl
  164. // @Description: 写入单条数据(外部传入集合)
  165. // @author: Roc
  166. // @receiver m
  167. // @datetime 2024-04-26 14:22:18
  168. // @param addData interface{}
  169. // @return err error
  170. func (m *BaseFromThsHfData) InsertDataByColl(coll *qmgo.Collection, addData interface{}) (err error) {
  171. ctx := context.TODO()
  172. _, err = coll.InsertOne(ctx, addData)
  173. if err != nil {
  174. fmt.Println("InsertDataByColl:Err:" + err.Error())
  175. return
  176. }
  177. return
  178. }
  179. // BatchInsertData
  180. // @Description: 批量写入数据
  181. // @author: Roc
  182. // @receiver m
  183. // @datetime 2024-04-26 14:22:18
  184. // @param bulk int 每次请求保存的数据量
  185. // @param dataList []interface{}
  186. // @return err error
  187. func (m *BaseFromThsHfData) BatchInsertData(bulk int, dataList []interface{}) (err error) {
  188. db := utils.MgoDataCli.Database(m.DataBaseName())
  189. coll := db.Collection(m.CollectionName())
  190. return m.BatchInsertDataByColl(coll, bulk, dataList)
  191. }
  192. // BatchInsertDataByColl
  193. // @Description: 批量写入数据(外部传入集合)
  194. // @author: Roc
  195. // @receiver m
  196. // @datetime 2024-04-26 14:22:18
  197. // @param coll *qmgo.Collection
  198. // @param bulk int 每次请求保存的数据量
  199. // @param dataList []interface{}
  200. // @return err error
  201. func (m *BaseFromThsHfData) BatchInsertDataByColl(coll *qmgo.Collection, bulk int, dataList []interface{}) (err error) {
  202. ctx := context.TODO()
  203. dataNum := len(dataList)
  204. if dataNum <= 0 {
  205. return
  206. }
  207. // 不设置每次保存切片数量大小,或者实际数据量小于设置的切片数量大小,那么就直接保存吧
  208. if bulk <= 0 || dataNum <= bulk {
  209. _, err = coll.InsertMany(ctx, dataList)
  210. if err != nil {
  211. fmt.Println("BatchInsertData:Err:" + err.Error())
  212. return
  213. }
  214. return
  215. }
  216. // 分批保存
  217. i := 0
  218. tmpAddDataList := make([]interface{}, 0)
  219. for _, v := range dataList {
  220. tmpAddDataList = append(tmpAddDataList, v)
  221. i++
  222. if i >= bulk {
  223. _, err = coll.InsertMany(ctx, tmpAddDataList)
  224. if err != nil {
  225. fmt.Println("BatchInsertData:Err:" + err.Error())
  226. return
  227. }
  228. i = 0
  229. tmpAddDataList = make([]interface{}, 0)
  230. }
  231. }
  232. if len(tmpAddDataList) > 0 {
  233. _, err = coll.InsertMany(ctx, tmpAddDataList)
  234. if err != nil {
  235. fmt.Println("BatchInsertData:Err:" + err.Error())
  236. return
  237. }
  238. }
  239. return
  240. }
  241. // UpdateDataByColl
  242. // @Description: 单条数据修改
  243. // @author: Roc
  244. // @receiver m
  245. // @datetime 2024-04-26 15:01:51
  246. // @param whereParams interface{}
  247. // @param updateParams interface{}
  248. // @return err error
  249. func (m *BaseFromThsHfData) UpdateDataByColl(coll *qmgo.Collection, whereParams, updateParams interface{}) (err error) {
  250. ctx := context.TODO()
  251. err = coll.UpdateOne(ctx, whereParams, updateParams)
  252. if err != nil {
  253. fmt.Println("UpdateDataByColl:Err:" + err.Error())
  254. return
  255. }
  256. return
  257. }
  258. // UpdateData
  259. // @Description: 单条数据修改
  260. // @author: Roc
  261. // @receiver m
  262. // @datetime 2024-04-26 15:01:51
  263. // @param whereParams interface{}
  264. // @param updateParams interface{}
  265. // @return err error
  266. func (m *BaseFromThsHfData) UpdateData(whereParams, updateParams interface{}) (err error) {
  267. db := utils.MgoDataCli.Database(m.DataBaseName())
  268. coll := db.Collection(m.CollectionName())
  269. ctx := context.TODO()
  270. err = coll.UpdateOne(ctx, whereParams, updateParams)
  271. if err != nil {
  272. fmt.Println("UpdateData:Err:" + err.Error())
  273. return
  274. }
  275. return
  276. }
  277. // HandleData
  278. // @Description: 事务处理数据
  279. // @author: Roc
  280. // @receiver m
  281. // @datetime 2024-04-30 10:40:20
  282. // @param addDataList []BaseAddFromBusinessData
  283. // @param updateDataList []BaseFromThsHfData
  284. // @return result interface{}
  285. // @return err error
  286. func (m *BaseFromThsHfData) HandleData(addDataList, updateDataList []BaseFromThsHfData) (result interface{}, err error) {
  287. ctx := context.TODO()
  288. callback := func(sessCtx context.Context) (interface{}, error) {
  289. // 重要:确保事务中的每一个操作,都使用传入的sessCtx参数
  290. db := utils.MgoDataCli.Database(m.DataBaseName())
  291. coll := db.Collection(m.CollectionName())
  292. // 插入数据
  293. if len(addDataList) > 0 {
  294. _, err = coll.InsertMany(sessCtx, addDataList)
  295. if err != nil {
  296. return nil, err
  297. }
  298. }
  299. // 修改
  300. if len(updateDataList) > 0 {
  301. for _, v := range updateDataList {
  302. err = coll.UpdateOne(ctx, bson.M{"_id": v.ID}, bson.M{"$set": bson.M{"value": v.Value, "modify_time": v.ModifyTime}})
  303. if err != nil {
  304. fmt.Println("BatchInsertData:Err:" + err.Error())
  305. return nil, err
  306. }
  307. }
  308. }
  309. return nil, nil
  310. }
  311. result, err = utils.MgoDataCli.DoTransaction(ctx, callback)
  312. return
  313. }
  314. // GetEdbInfoMaxAndMinInfo
  315. // @Description: 获取当前指标的最大最小值
  316. // @author: Roc
  317. // @receiver m
  318. // @datetime 2024-04-30 17:15:39
  319. // @param whereParams interface{}
  320. // @return result EdbInfoMaxAndMinInfo
  321. // @return err error
  322. func (m *BaseFromThsHfData) GetEdbInfoMaxAndMinInfo(whereParams interface{}) (result EdbInfoMaxAndMinInfo, err error) {
  323. if utils.MgoDataCli == nil {
  324. err = errors.New("mongodb连接失败")
  325. return
  326. }
  327. db := utils.MgoDataCli.Database(m.DataBaseName())
  328. coll := db.Collection(m.CollectionName())
  329. ctx := context.TODO()
  330. if err != nil {
  331. fmt.Println("MgoGetColl Err:", err.Error())
  332. return
  333. }
  334. err = coll.Aggregate(ctx, whereParams).One(&result)
  335. if err != nil {
  336. return
  337. }
  338. result.MinDate = result.MinDate.In(time.Local)
  339. result.MaxDate = result.MaxDate.In(time.Local)
  340. result.LatestDate = result.LatestDate.In(time.Local)
  341. return
  342. }
  343. // GetLatestValue
  344. // @Description: 获取当前指标的最新数据记录
  345. // @author: Roc
  346. // @receiver m
  347. // @datetime 2024-04-30 17:16:15
  348. // @param whereParams interface{}
  349. // @param selectParam interface{}
  350. // @return latestValue LatestValue
  351. // @return err error
  352. func (m *BaseFromThsHfData) GetLatestValue(whereParams, selectParam interface{}) (latestValue LatestValue, err error) {
  353. if utils.MgoDataCli == nil {
  354. err = errors.New("mongodb连接失败")
  355. return
  356. }
  357. db := utils.MgoDataCli.Database(m.DataBaseName())
  358. coll := db.Collection(m.CollectionName())
  359. ctx := context.TODO()
  360. if err != nil {
  361. fmt.Println("MgoGetColl Err:", err.Error())
  362. return
  363. }
  364. //var result interface{}
  365. //err = coll.Find(ctx, whereParams).Select(selectParam).One(&result)
  366. err = coll.Find(ctx, whereParams).Select(selectParam).One(&latestValue)
  367. return
  368. }
  369. func (m *BaseFromThsHfData) RemoveMany(whereParams interface{}) (err error) {
  370. db := utils.MgoDataCli.Database(m.DataBaseName())
  371. coll := db.Collection(m.CollectionName())
  372. return m.RemoveManyByColl(coll, whereParams)
  373. }
  374. func (m *BaseFromThsHfData) RemoveManyByColl(coll *qmgo.Collection, whereParams interface{}) (err error) {
  375. ctx := context.TODO()
  376. _, err = coll.RemoveAll(ctx, whereParams)
  377. if err != nil {
  378. fmt.Println("RemoveManyByColl:Err:" + err.Error())
  379. return
  380. }
  381. return
  382. }