base_from_business.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package data
  2. import (
  3. "eta/eta_api/models/data_manage"
  4. "eta/eta_api/models/mgo"
  5. "eta/eta_api/utils"
  6. "fmt"
  7. "go.mongodb.org/mongo-driver/bson"
  8. )
  9. type BusinessIndexSource2EdbReq struct {
  10. EdbCode string
  11. EdbName string
  12. Frequency string
  13. Unit string
  14. ClassifyId int
  15. AdminId int
  16. AdminRealName string
  17. }
  18. // BusinessIndexSource2Edb 新增彭博数据源到指标库
  19. func BusinessIndexSource2Edb(req BusinessIndexSource2EdbReq, lang string) (edb *data_manage.EdbInfo, err error, errMsg string, skip bool) {
  20. if req.EdbCode == "" {
  21. err = fmt.Errorf("指标ID为空")
  22. return
  23. }
  24. defer func() {
  25. if err != nil {
  26. tips := fmt.Sprintf("BusinessIndexSource2Edb新增失败, Err: %s", err.Error())
  27. fmt.Println(tips)
  28. utils.FileLog.Info(tips)
  29. }
  30. }()
  31. source := utils.DATA_SOURCE_BUSINESS
  32. // 是否新增过指标
  33. exist, e := data_manage.GetEdbInfoByEdbCode(source, req.EdbCode)
  34. if e != nil && e.Error() != utils.ErrNoRow() {
  35. err = fmt.Errorf("获取指标是否存在失败, err: %s", e.Error())
  36. return
  37. }
  38. if exist != nil {
  39. skip = true
  40. return
  41. }
  42. // 开始结束时间
  43. var startDate, endDate string
  44. // 新增指标库
  45. edbInfo, e, msg, _ := EdbInfoAdd(source, utils.DATA_SUB_SOURCE_EDB, req.ClassifyId, req.EdbCode, req.EdbName, req.Frequency, req.Unit, startDate, endDate, req.AdminId, req.AdminRealName, lang)
  46. if e != nil {
  47. errMsg = msg
  48. err = fmt.Errorf("EdbInfo: 新增指标失败, err: %s", e.Error())
  49. return
  50. }
  51. edb = edbInfo
  52. return
  53. }
  54. // GetPageBaseBusinessIndexData
  55. // @Description: 获取自有数据的分页数据
  56. // @author: Roc
  57. // @datetime 2024-05-07 11:02:51
  58. // @param indexCode int
  59. // @param startSize int
  60. // @param pageSize int
  61. // @return dataCount int
  62. // @return dataList []*data_manage.BaseFromBusinessIndexDataItem
  63. // @return err error
  64. func GetPageBaseBusinessIndexData(indexCode string, startSize, pageSize int) (dataCount int, dataList []*data_manage.BaseFromBusinessIndexDataItem, err error) {
  65. if utils.UseMongo {
  66. dataCount, dataList, err = getPageBaseBusinessIndexDataByMongo(indexCode, startSize, pageSize)
  67. } else {
  68. dataCount, dataList, err = getPageBaseBusinessIndexDataByMysql(indexCode, startSize, pageSize)
  69. }
  70. return
  71. }
  72. // getPageBaseBusinessIndexDataByMongo
  73. // @Description: 获取自有数据的分页数据(从mongo中获取)
  74. // @author: Roc
  75. // @datetime 2024-07-01 14:01:04
  76. // @param indexCode string
  77. // @param startSize int
  78. // @param pageSize int
  79. // @return dataCount int
  80. // @return dataList []*data_manage.BaseFromBusinessIndexDataItem
  81. // @return err error
  82. func getPageBaseBusinessIndexDataByMongo(indexCode string, startSize, pageSize int) (dataCount int, dataList []*data_manage.BaseFromBusinessIndexDataItem, err error) {
  83. dataList = make([]*data_manage.BaseFromBusinessIndexDataItem, 0)
  84. mogDataObj := mgo.BaseFromBusinessData{}
  85. // 构建查询条件
  86. queryConditions := bson.M{
  87. "index_code": indexCode,
  88. }
  89. // 获取数据总量
  90. tmpCount, tmpErr := mogDataObj.GetCountDataList(queryConditions)
  91. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  92. err = tmpErr
  93. return
  94. }
  95. dataCount = int(tmpCount)
  96. // 获取列表数据
  97. tmpDataList, tmpErr := mogDataObj.GetPageDataList(queryConditions, int64(startSize), int64(pageSize), []string{"-data_time"})
  98. if tmpErr != nil {
  99. err = tmpErr
  100. return
  101. }
  102. for _, v := range tmpDataList {
  103. dataList = append(dataList, &data_manage.BaseFromBusinessIndexDataItem{
  104. ID: v.ID.String(),
  105. IndexCode: v.IndexCode,
  106. DataTime: v.DataTime.Format(utils.FormatDate),
  107. Value: v.Value,
  108. })
  109. }
  110. return
  111. }
  112. // getPageBaseBusinessIndexDataByMysql
  113. // @Description: 获取自有数据的分页数据(从mysql中获取)
  114. // @author: Roc
  115. // @datetime 2024-07-01 14:00:41
  116. // @param indexCode string
  117. // @param startSize int
  118. // @param pageSize int
  119. // @return dataCount int
  120. // @return dataList []*data_manage.BaseFromBusinessIndexDataItem
  121. // @return err error
  122. func getPageBaseBusinessIndexDataByMysql(indexCode string, startSize, pageSize int) (dataCount int, dataList []*data_manage.BaseFromBusinessIndexDataItem, err error) {
  123. dataList = make([]*data_manage.BaseFromBusinessIndexDataItem, 0)
  124. businessDataObj := data_manage.BaseFromBusinessData{}
  125. var condition []string
  126. var pars []interface{}
  127. condition = append(condition, "index_code = ?")
  128. pars = append(pars, indexCode)
  129. // 获取数据总量
  130. tmpCount, tmpErr := businessDataObj.GetCountDataList(condition, pars)
  131. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  132. err = tmpErr
  133. return
  134. }
  135. dataCount = int(tmpCount)
  136. // 获取列表数据
  137. tmpDataList, tmpErr := businessDataObj.GetPageDataList(condition, pars, " data_time desc", int64(startSize), int64(pageSize))
  138. if tmpErr != nil {
  139. err = tmpErr
  140. return
  141. }
  142. for _, v := range tmpDataList {
  143. dataList = append(dataList, &data_manage.BaseFromBusinessIndexDataItem{
  144. ID: fmt.Sprint(v.BusinessDataId),
  145. IndexCode: v.IndexCode,
  146. DataTime: v.DataTime.Format(utils.FormatDate),
  147. Value: v.Value,
  148. })
  149. }
  150. return
  151. }