base_from_business_data.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. package mgo
  2. import (
  3. "context"
  4. "errors"
  5. "eta/eta_chart_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. // 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. // BatchInsertData
  174. // @Description: 批量写入数据
  175. // @author: Roc
  176. // @receiver m
  177. // @datetime 2024-04-26 14:22:18
  178. // @param dataList interface{}
  179. // @return err error
  180. func (m *BaseFromBusinessData) BatchInsertData(dataList interface{}) (err error) {
  181. db := utils.MgoDataCli.Database(m.DataBaseName())
  182. coll := db.Collection(m.CollectionName())
  183. ctx := context.TODO()
  184. _, err = coll.InsertMany(ctx, dataList)
  185. if err != nil {
  186. fmt.Println("BatchInsertData:Err:" + err.Error())
  187. return
  188. }
  189. return
  190. }
  191. // UpdateDataByColl
  192. // @Description: 单条数据修改
  193. // @author: Roc
  194. // @receiver m
  195. // @datetime 2024-04-26 15:01:51
  196. // @param whereParams interface{}
  197. // @param updateParams interface{}
  198. // @return err error
  199. func (m *BaseFromBusinessData) UpdateDataByColl(coll *qmgo.Collection, whereParams, updateParams interface{}) (err error) {
  200. ctx := context.TODO()
  201. err = coll.UpdateOne(ctx, whereParams, updateParams)
  202. if err != nil {
  203. fmt.Println("UpdateDataByColl:Err:" + err.Error())
  204. return
  205. }
  206. return
  207. }
  208. // UpdateData
  209. // @Description: 单条数据修改
  210. // @author: Roc
  211. // @receiver m
  212. // @datetime 2024-04-26 15:01:51
  213. // @param whereParams interface{}
  214. // @param updateParams interface{}
  215. // @return err error
  216. func (m *BaseFromBusinessData) UpdateData(whereParams, updateParams interface{}) (err error) {
  217. db := utils.MgoDataCli.Database(m.DataBaseName())
  218. coll := db.Collection(m.CollectionName())
  219. ctx := context.TODO()
  220. err = coll.UpdateOne(ctx, whereParams, updateParams)
  221. if err != nil {
  222. fmt.Println("UpdateData:Err:" + err.Error())
  223. return
  224. }
  225. return
  226. }
  227. // HandleData
  228. // @Description: 事务处理数据
  229. // @author: Roc
  230. // @receiver m
  231. // @datetime 2024-04-30 10:40:20
  232. // @param addDataList []BaseAddFromBusinessData
  233. // @param updateDataList []BaseFromBusinessData
  234. // @return result interface{}
  235. // @return err error
  236. func (m *BaseFromBusinessData) HandleData(addDataList, updateDataList []BaseFromBusinessData) (result interface{}, err error) {
  237. ctx := context.TODO()
  238. callback := func(sessCtx context.Context) (interface{}, error) {
  239. // 重要:确保事务中的每一个操作,都使用传入的sessCtx参数
  240. db := utils.MgoDataCli.Database(m.DataBaseName())
  241. coll := db.Collection(m.CollectionName())
  242. // 插入数据
  243. if len(addDataList) > 0 {
  244. _, err = coll.InsertMany(sessCtx, addDataList)
  245. if err != nil {
  246. return nil, err
  247. }
  248. }
  249. // 修改
  250. if len(updateDataList) > 0 {
  251. for _, v := range updateDataList {
  252. err = coll.UpdateOne(ctx, bson.M{"_id": v.ID}, bson.M{"$set": bson.M{"value": v.Value, "modify_time": v.ModifyTime}})
  253. if err != nil {
  254. fmt.Println("BatchInsertData:Err:" + err.Error())
  255. return nil, err
  256. }
  257. }
  258. }
  259. return nil, nil
  260. }
  261. result, err = utils.MgoDataCli.DoTransaction(ctx, callback)
  262. return
  263. }
  264. // GetEdbInfoMaxAndMinInfo
  265. // @Description: 获取当前指标的最大最小值
  266. // @author: Roc
  267. // @receiver m
  268. // @datetime 2024-04-30 17:15:39
  269. // @param whereParams interface{}
  270. // @return result EdbInfoMaxAndMinInfo
  271. // @return err error
  272. func (m *BaseFromBusinessData) GetEdbInfoMaxAndMinInfo(whereParams interface{}) (result EdbInfoMaxAndMinInfo, err error) {
  273. if utils.MgoDataCli == nil {
  274. err = errors.New("mongodb连接失败")
  275. return
  276. }
  277. db := utils.MgoDataCli.Database(m.DataBaseName())
  278. coll := db.Collection(m.CollectionName())
  279. ctx := context.TODO()
  280. if err != nil {
  281. fmt.Println("MgoGetColl Err:", err.Error())
  282. return
  283. }
  284. err = coll.Aggregate(ctx, whereParams).One(&result)
  285. if err != nil {
  286. return
  287. }
  288. result.MinDate = result.MinDate.In(time.Local)
  289. result.MaxDate = result.MaxDate.In(time.Local)
  290. result.LatestDate = result.LatestDate.In(time.Local)
  291. return
  292. }
  293. // GetLatestValue
  294. // @Description: 获取当前指标的最新数据记录
  295. // @author: Roc
  296. // @receiver m
  297. // @datetime 2024-04-30 17:16:15
  298. // @param whereParams interface{}
  299. // @param selectParam interface{}
  300. // @return latestValue LatestValue
  301. // @return err error
  302. func (m *BaseFromBusinessData) GetLatestValue(whereParams, selectParam interface{}) (latestValue LatestValue, err error) {
  303. if utils.MgoDataCli == nil {
  304. err = errors.New("mongodb连接失败")
  305. return
  306. }
  307. db := utils.MgoDataCli.Database(m.DataBaseName())
  308. coll := db.Collection(m.CollectionName())
  309. ctx := context.TODO()
  310. if err != nil {
  311. fmt.Println("MgoGetColl Err:", err.Error())
  312. return
  313. }
  314. //var result interface{}
  315. //err = coll.Find(ctx, whereParams).Select(selectParam).One(&result)
  316. err = coll.Find(ctx, whereParams).Select(selectParam).One(&latestValue)
  317. return
  318. }