edb_data_business.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. // 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. // GetAllDataList
  53. // @Description: 根据条件获取所有数据
  54. // @author: Roc
  55. // @receiver m
  56. // @datetime 2024-04-26 13:42:19
  57. // @param whereParams interface{}
  58. // @param sort []string
  59. // @return result []EdbDataBusiness
  60. // @return err error
  61. func (m *EdbDataBusiness) GetAllDataList(whereParams interface{}, sort []string) (result []*EdbDataBusiness, 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. // @param sort []string
  92. // @return result []*BaseFromBusinessData
  93. // @return err error
  94. func (m *EdbDataBusiness) GetLimitDataList(whereParams interface{}, size int64, sort []string) (result []*EdbDataBusiness, err error) {
  95. if utils.MgoDataCli == nil {
  96. err = errors.New("mongodb连接失败")
  97. return
  98. }
  99. db := utils.MgoDataCli.Database(m.DataBaseName())
  100. coll := db.Collection(m.CollectionName())
  101. ctx := context.TODO()
  102. if err != nil {
  103. fmt.Println("MgoGetColl Err:", err.Error())
  104. return
  105. }
  106. err = coll.Find(ctx, whereParams).Sort(sort...).Limit(size).All(&result)
  107. if err != nil {
  108. return
  109. }
  110. for _, v := range result {
  111. v.DataTime = v.DataTime.In(time.Local)
  112. v.CreateTime = v.CreateTime.In(time.Local)
  113. v.ModifyTime = v.ModifyTime.In(time.Local)
  114. }
  115. return
  116. }
  117. // GetPageDataList
  118. // @Description: 根据条件获取分页数据列表
  119. // @author: Roc
  120. // @receiver m
  121. // @datetime 2024-05-07 10:21:07
  122. // @param whereParams interface{}
  123. // @param startSize int64
  124. // @param size int64
  125. // @param sort []string
  126. // @return result []*EdbDataBusiness
  127. // @return err error
  128. func (m *EdbDataBusiness) GetPageDataList(whereParams interface{}, startSize, size int64, sort []string) (result []*EdbDataBusiness, err error) {
  129. if utils.MgoDataCli == nil {
  130. err = errors.New("mongodb连接失败")
  131. return
  132. }
  133. db := utils.MgoDataCli.Database(m.DataBaseName())
  134. coll := db.Collection(m.CollectionName())
  135. ctx := context.TODO()
  136. if err != nil {
  137. fmt.Println("MgoGetColl Err:", err.Error())
  138. return
  139. }
  140. err = coll.Find(ctx, whereParams).Sort(sort...).Skip(startSize).Limit(size).All(&result)
  141. if err != nil {
  142. return
  143. }
  144. for _, v := range result {
  145. v.DataTime = v.DataTime.In(time.Local)
  146. v.CreateTime = v.CreateTime.In(time.Local)
  147. v.ModifyTime = v.ModifyTime.In(time.Local)
  148. }
  149. return
  150. }
  151. // GetCountDataList
  152. // @Description: 根据条件获取数据列表总数
  153. // @author: Roc
  154. // @receiver m
  155. // @datetime 2024-05-07 10:29:00
  156. // @param whereParams interface{}
  157. // @return count int64
  158. // @return err error
  159. func (m *EdbDataBusiness) GetCountDataList(whereParams interface{}) (count int64, err error) {
  160. if utils.MgoDataCli == nil {
  161. err = errors.New("mongodb连接失败")
  162. return
  163. }
  164. db := utils.MgoDataCli.Database(m.DataBaseName())
  165. coll := db.Collection(m.CollectionName())
  166. ctx := context.TODO()
  167. if err != nil {
  168. fmt.Println("MgoGetColl Err:", err.Error())
  169. return
  170. }
  171. count, err = coll.Find(ctx, whereParams).Count()
  172. return
  173. }
  174. // BatchInsertData
  175. // @Description: 批量写入数据
  176. // @author: Roc
  177. // @receiver m
  178. // @datetime 2024-04-26 14:22:18
  179. // @param dataList interface{}
  180. // @return err error
  181. func (m *EdbDataBusiness) BatchInsertData(dataList []EdbDataBusiness) (err error) {
  182. db := utils.MgoDataCli.Database(m.DataBaseName())
  183. coll := db.Collection(m.CollectionName())
  184. return m.BatchInsertDataByColl(coll, dataList)
  185. }
  186. // BatchInsertDataByColl
  187. // @Description: 批量写入数据(外部传入集合)
  188. // @author: Roc
  189. // @receiver m
  190. // @datetime 2024-04-26 14:22:18
  191. // @param dataList interface{}
  192. // @return err error
  193. func (m *EdbDataBusiness) BatchInsertDataByColl(coll *qmgo.Collection, dataList []EdbDataBusiness) (err error) {
  194. ctx := context.TODO()
  195. _, err = coll.InsertMany(ctx, dataList)
  196. if err != nil {
  197. fmt.Println("BatchInsertData:Err:" + err.Error())
  198. return
  199. }
  200. return
  201. }
  202. // UpdateData
  203. // @Description: 单条数据修改
  204. // @author: Roc
  205. // @receiver m
  206. // @datetime 2024-04-26 15:01:51
  207. // @param whereParams interface{}
  208. // @param updateParams interface{}
  209. // @return err error
  210. func (m *EdbDataBusiness) UpdateData(whereParams, updateParams interface{}) (err error) {
  211. db := utils.MgoDataCli.Database(m.DataBaseName())
  212. coll := db.Collection(m.CollectionName())
  213. return m.UpdateDataByColl(coll, whereParams, updateParams)
  214. }
  215. // UpdateDataByColl
  216. // @Description: 单条数据修改(外部传入集合)
  217. // @author: Roc
  218. // @receiver m
  219. // @datetime 2024-04-26 15:01:51
  220. // @param whereParams interface{}
  221. // @param updateParams interface{}
  222. // @return err error
  223. func (m *EdbDataBusiness) UpdateDataByColl(coll *qmgo.Collection, whereParams, updateParams interface{}) (err error) {
  224. ctx := context.TODO()
  225. err = coll.UpdateOne(ctx, whereParams, updateParams)
  226. if err != nil {
  227. fmt.Println("UpdateDataByColl:Err:" + err.Error())
  228. return
  229. }
  230. return
  231. }
  232. // RemoveMany
  233. // @Description: 根据条件删除多条数据
  234. // @author: Roc
  235. // @receiver m
  236. // @datetime 2024-04-30 13:17:02
  237. // @param whereParams interface{}
  238. // @return err error
  239. func (m *EdbDataBusiness) RemoveMany(whereParams interface{}) (err error) {
  240. db := utils.MgoDataCli.Database(m.DataBaseName())
  241. coll := db.Collection(m.CollectionName())
  242. return m.RemoveManyByColl(coll, whereParams)
  243. }
  244. // RemoveManyByColl
  245. // @Description: 根据条件删除多条数据(外部传入集合)
  246. // @author: Roc
  247. // @receiver m
  248. // @datetime 2024-04-30 13:18:42
  249. // @param coll *qmgo.Collection
  250. // @param whereParams interface{}
  251. // @return err error
  252. func (m *EdbDataBusiness) RemoveManyByColl(coll *qmgo.Collection, whereParams interface{}) (err error) {
  253. ctx := context.TODO()
  254. _, err = coll.RemoveAll(ctx, whereParams)
  255. if err != nil {
  256. fmt.Println("RemoveManyByColl:Err:" + err.Error())
  257. return
  258. }
  259. return
  260. }
  261. // HandleData
  262. // @Description: 事务处理数据
  263. // @author: Roc
  264. // @receiver m
  265. // @datetime 2024-04-30 10:39:01
  266. // @param addDataList []AddEdbDataBusiness
  267. // @param updateDataList []EdbDataBusiness
  268. // @return result interface{}
  269. // @return err error
  270. func (m *EdbDataBusiness) HandleData(addDataList, updateDataList []EdbDataBusiness) (result interface{}, err error) {
  271. ctx := context.TODO()
  272. callback := func(sessCtx context.Context) (interface{}, error) {
  273. // 重要:确保事务中的每一个操作,都使用传入的sessCtx参数
  274. db := utils.MgoDataCli.Database(m.DataBaseName())
  275. coll := db.Collection(m.CollectionName())
  276. // 插入数据
  277. if len(addDataList) > 0 {
  278. _, err = coll.InsertMany(sessCtx, addDataList)
  279. if err != nil {
  280. return nil, err
  281. }
  282. }
  283. // 修改
  284. if len(updateDataList) > 0 {
  285. for _, v := range updateDataList {
  286. err = coll.UpdateOne(ctx, bson.M{"_id": v.ID}, bson.M{"$set": bson.M{"value": v.Value, "modify_time": v.ModifyTime}})
  287. if err != nil {
  288. fmt.Println("BatchInsertData:Err:" + err.Error())
  289. return nil, err
  290. }
  291. }
  292. }
  293. return nil, nil
  294. }
  295. result, err = utils.MgoDataCli.DoTransaction(ctx, callback)
  296. return
  297. }
  298. // EdbInfoMaxAndMinInfo 指标最新数据记录结构体
  299. type EdbInfoMaxAndMinInfo struct {
  300. MinDate time.Time `description:"最小日期" bson:"min_date"`
  301. MaxDate time.Time `description:"最大日期" bson:"max_date"`
  302. MinValue float64 `description:"最小值" bson:"min_value"`
  303. MaxValue float64 `description:"最大值" bson:"max_value"`
  304. LatestValue float64 `description:"最新值" bson:"latest_value"`
  305. LatestDate time.Time `description:"实际数据最新日期" bson:"latest_date"`
  306. EndValue float64 `description:"最新值" bson:"end_value"`
  307. }
  308. // GetEdbInfoMaxAndMinInfo
  309. // @Description: 获取当前指标的最大最小值
  310. // @author: Roc
  311. // @receiver m
  312. // @datetime 2024-04-30 17:15:39
  313. // @param whereParams interface{}
  314. // @return result EdbInfoMaxAndMinInfo
  315. // @return err error
  316. func (m *EdbDataBusiness) GetEdbInfoMaxAndMinInfo(whereParams interface{}) (result EdbInfoMaxAndMinInfo, err error) {
  317. if utils.MgoDataCli == nil {
  318. err = errors.New("mongodb连接失败")
  319. return
  320. }
  321. db := utils.MgoDataCli.Database(m.DataBaseName())
  322. coll := db.Collection(m.CollectionName())
  323. ctx := context.TODO()
  324. if err != nil {
  325. fmt.Println("MgoGetColl Err:", err.Error())
  326. return
  327. }
  328. err = coll.Aggregate(ctx, whereParams).One(&result)
  329. if err != nil {
  330. return
  331. }
  332. result.MinDate = result.MinDate.In(time.Local)
  333. result.MaxDate = result.MaxDate.In(time.Local)
  334. result.LatestDate = result.LatestDate.In(time.Local)
  335. return
  336. }
  337. // LatestValue 指标最新数据记录结构体
  338. type LatestValue struct {
  339. Value float64 `description:"值" bson:"value"`
  340. }
  341. // GetLatestValue
  342. // @Description: 获取当前指标的最新数据记录
  343. // @author: Roc
  344. // @receiver m
  345. // @datetime 2024-04-30 17:16:15
  346. // @param whereParams interface{}
  347. // @param selectParam interface{}
  348. // @return latestValue LatestValue
  349. // @return err error
  350. func (m *EdbDataBusiness) GetLatestValue(whereParams, selectParam interface{}) (latestValue LatestValue, err error) {
  351. if utils.MgoDataCli == nil {
  352. err = errors.New("mongodb连接失败")
  353. return
  354. }
  355. db := utils.MgoDataCli.Database(m.DataBaseName())
  356. coll := db.Collection(m.CollectionName())
  357. ctx := context.TODO()
  358. if err != nil {
  359. fmt.Println("MgoGetColl Err:", err.Error())
  360. return
  361. }
  362. //var result interface{}
  363. //err = coll.Find(ctx, whereParams).Select(selectParam).One(&result)
  364. err = coll.Find(ctx, whereParams).Select(selectParam).One(&latestValue)
  365. return
  366. }