edb_data_business.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. package mgo
  2. import (
  3. "context"
  4. "errors"
  5. "eta/eta_index_lib/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. // EdbDataBusiness
  13. // @Description: 自有数据集合(指标库)
  14. type EdbDataBusiness struct {
  15. ID primitive.ObjectID `json:"_id" bson:"_id,omitempty" ` // 文档id
  16. EdbInfoId int `json:"edb_info_id" bson:"edb_info_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 *EdbDataBusiness) CollectionName() string {
  31. return "edb_data_business"
  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 *EdbDataBusiness) 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 *EdbDataBusiness) 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 *EdbDataBusiness
  59. // @return err error
  60. func (m *EdbDataBusiness) GetItem(whereParams interface{}) (item *EdbDataBusiness, 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 *EdbDataBusiness
  77. // @return err error
  78. func (m *EdbDataBusiness) GetItemByColl(coll *qmgo.Collection, whereParams interface{}) (item *EdbDataBusiness, 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 []EdbDataBusiness
  101. // @return err error
  102. func (m *EdbDataBusiness) GetAllDataList(whereParams interface{}, sort []string) (result []*EdbDataBusiness, 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 *EdbDataBusiness) GetLimitDataList(whereParams interface{}, size int64, sort []string) (result []*EdbDataBusiness, 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 []*EdbDataBusiness
  168. // @return err error
  169. func (m *EdbDataBusiness) GetPageDataList(whereParams interface{}, startSize, size int64, sort []string) (result []*EdbDataBusiness, 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 *EdbDataBusiness) 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. // BatchInsertData
  216. // @Description: 批量写入数据
  217. // @author: Roc
  218. // @receiver m
  219. // @datetime 2024-04-26 14:22:18
  220. // @param dataList interface{}
  221. // @return err error
  222. func (m *EdbDataBusiness) BatchInsertData(dataList []EdbDataBusiness) (err error) {
  223. db := utils.MgoDataCli.Database(m.DataBaseName())
  224. coll := db.Collection(m.CollectionName())
  225. return m.BatchInsertDataByColl(coll, dataList)
  226. }
  227. // BatchInsertDataByColl
  228. // @Description: 批量写入数据(外部传入集合)
  229. // @author: Roc
  230. // @receiver m
  231. // @datetime 2024-04-26 14:22:18
  232. // @param dataList interface{}
  233. // @return err error
  234. func (m *EdbDataBusiness) BatchInsertDataByColl(coll *qmgo.Collection, dataList []EdbDataBusiness) (err error) {
  235. ctx := context.TODO()
  236. _, err = coll.InsertMany(ctx, dataList)
  237. if err != nil {
  238. fmt.Println("BatchInsertData:Err:" + err.Error())
  239. return
  240. }
  241. return
  242. }
  243. // UpdateData
  244. // @Description: 单条数据修改
  245. // @author: Roc
  246. // @receiver m
  247. // @datetime 2024-04-26 15:01:51
  248. // @param whereParams interface{}
  249. // @param updateParams interface{}
  250. // @return err error
  251. func (m *EdbDataBusiness) UpdateData(whereParams, updateParams interface{}) (err error) {
  252. db := utils.MgoDataCli.Database(m.DataBaseName())
  253. coll := db.Collection(m.CollectionName())
  254. return m.UpdateDataByColl(coll, whereParams, updateParams)
  255. }
  256. // UpdateDataByColl
  257. // @Description: 单条数据修改(外部传入集合)
  258. // @author: Roc
  259. // @receiver m
  260. // @datetime 2024-04-26 15:01:51
  261. // @param whereParams interface{}
  262. // @param updateParams interface{}
  263. // @return err error
  264. func (m *EdbDataBusiness) UpdateDataByColl(coll *qmgo.Collection, whereParams, updateParams interface{}) (err error) {
  265. ctx := context.TODO()
  266. err = coll.UpdateOne(ctx, whereParams, updateParams)
  267. if err != nil {
  268. fmt.Println("UpdateDataByColl:Err:" + err.Error())
  269. return
  270. }
  271. return
  272. }
  273. // RemoveMany
  274. // @Description: 根据条件删除多条数据
  275. // @author: Roc
  276. // @receiver m
  277. // @datetime 2024-04-30 13:17:02
  278. // @param whereParams interface{}
  279. // @return err error
  280. func (m *EdbDataBusiness) RemoveMany(whereParams interface{}) (err error) {
  281. db := utils.MgoDataCli.Database(m.DataBaseName())
  282. coll := db.Collection(m.CollectionName())
  283. return m.RemoveManyByColl(coll, whereParams)
  284. }
  285. // RemoveManyByColl
  286. // @Description: 根据条件删除多条数据(外部传入集合)
  287. // @author: Roc
  288. // @receiver m
  289. // @datetime 2024-04-30 13:18:42
  290. // @param coll *qmgo.Collection
  291. // @param whereParams interface{}
  292. // @return err error
  293. func (m *EdbDataBusiness) RemoveManyByColl(coll *qmgo.Collection, whereParams interface{}) (err error) {
  294. ctx := context.TODO()
  295. _, err = coll.RemoveAll(ctx, whereParams)
  296. if err != nil {
  297. fmt.Println("RemoveManyByColl:Err:" + err.Error())
  298. return
  299. }
  300. return
  301. }
  302. // HandleData
  303. // @Description: 事务处理数据
  304. // @author: Roc
  305. // @receiver m
  306. // @datetime 2024-04-30 10:39:01
  307. // @param addDataList []AddEdbDataBusiness
  308. // @param updateDataList []EdbDataBusiness
  309. // @return result interface{}
  310. // @return err error
  311. func (m *EdbDataBusiness) HandleData(addDataList, updateDataList []EdbDataBusiness) (result interface{}, err error) {
  312. ctx := context.TODO()
  313. callback := func(sessCtx context.Context) (interface{}, error) {
  314. // 重要:确保事务中的每一个操作,都使用传入的sessCtx参数
  315. db := utils.MgoDataCli.Database(m.DataBaseName())
  316. coll := db.Collection(m.CollectionName())
  317. // 插入数据
  318. if len(addDataList) > 0 {
  319. _, err = coll.InsertMany(sessCtx, addDataList)
  320. if err != nil {
  321. return nil, err
  322. }
  323. }
  324. // 修改
  325. if len(updateDataList) > 0 {
  326. for _, v := range updateDataList {
  327. err = coll.UpdateOne(ctx, bson.M{"_id": v.ID}, bson.M{"$set": bson.M{"value": v.Value, "modify_time": v.ModifyTime}})
  328. if err != nil {
  329. fmt.Println("BatchInsertData:Err:" + err.Error())
  330. return nil, err
  331. }
  332. }
  333. }
  334. return nil, nil
  335. }
  336. result, err = utils.MgoDataCli.DoTransaction(ctx, callback)
  337. return
  338. }
  339. // EdbInfoMaxAndMinInfo 指标最新数据记录结构体
  340. type EdbInfoMaxAndMinInfo struct {
  341. MinDate time.Time `description:"最小日期" bson:"min_date"`
  342. MaxDate time.Time `description:"最大日期" bson:"max_date"`
  343. MinValue float64 `description:"最小值" bson:"min_value"`
  344. MaxValue float64 `description:"最大值" bson:"max_value"`
  345. LatestValue float64 `description:"最新值" bson:"latest_value"`
  346. LatestDate time.Time `description:"实际数据最新日期" bson:"latest_date"`
  347. EndValue float64 `description:"最新值" bson:"end_value"`
  348. }
  349. // GetEdbInfoMaxAndMinInfo
  350. // @Description: 获取当前指标的最大最小值
  351. // @author: Roc
  352. // @receiver m
  353. // @datetime 2024-04-30 17:15:39
  354. // @param whereParams interface{}
  355. // @return result EdbInfoMaxAndMinInfo
  356. // @return err error
  357. func (m *EdbDataBusiness) GetEdbInfoMaxAndMinInfo(whereParams interface{}) (result EdbInfoMaxAndMinInfo, err error) {
  358. if utils.MgoDataCli == nil {
  359. err = errors.New("mongodb连接失败")
  360. return
  361. }
  362. db := utils.MgoDataCli.Database(m.DataBaseName())
  363. coll := db.Collection(m.CollectionName())
  364. ctx := context.TODO()
  365. if err != nil {
  366. fmt.Println("MgoGetColl Err:", err.Error())
  367. return
  368. }
  369. err = coll.Aggregate(ctx, whereParams).One(&result)
  370. if err != nil {
  371. return
  372. }
  373. result.MinDate = result.MinDate.In(time.Local)
  374. result.MaxDate = result.MaxDate.In(time.Local)
  375. result.LatestDate = result.LatestDate.In(time.Local)
  376. return
  377. }
  378. // LatestValue 指标最新数据记录结构体
  379. type LatestValue struct {
  380. Value float64 `description:"值" bson:"value"`
  381. }
  382. // GetLatestValue
  383. // @Description: 获取当前指标的最新数据记录
  384. // @author: Roc
  385. // @receiver m
  386. // @datetime 2024-04-30 17:16:15
  387. // @param whereParams interface{}
  388. // @param selectParam interface{}
  389. // @return latestValue LatestValue
  390. // @return err error
  391. func (m *EdbDataBusiness) GetLatestValue(whereParams, selectParam interface{}) (latestValue LatestValue, err error) {
  392. if utils.MgoDataCli == nil {
  393. err = errors.New("mongodb连接失败")
  394. return
  395. }
  396. db := utils.MgoDataCli.Database(m.DataBaseName())
  397. coll := db.Collection(m.CollectionName())
  398. ctx := context.TODO()
  399. if err != nil {
  400. fmt.Println("MgoGetColl Err:", err.Error())
  401. return
  402. }
  403. //var result interface{}
  404. //err = coll.Find(ctx, whereParams).Select(selectParam).One(&result)
  405. err = coll.Find(ctx, whereParams).Select(selectParam).One(&latestValue)
  406. return
  407. }