base_from_business_data.go 11 KB

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