edb_data_ths_hf.go 14 KB

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