base_from_business.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package data
  2. import (
  3. "eta_gn/eta_api/models/data_manage"
  4. "eta_gn/eta_api/models/mgo"
  5. "eta_gn/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. func BusinessIndexSource2Edb(req BusinessIndexSource2EdbReq, lang string) (edb *data_manage.EdbInfo, err error, errMsg string, skip bool) {
  19. if req.EdbCode == "" {
  20. err = fmt.Errorf("指标ID为空")
  21. return
  22. }
  23. defer func() {
  24. if err != nil {
  25. tips := fmt.Sprintf("BusinessIndexSource2Edb新增失败, Err: %s", err.Error())
  26. fmt.Println(tips)
  27. utils.FileLog.Info(tips)
  28. }
  29. }()
  30. source := utils.DATA_SOURCE_BUSINESS
  31. exist, e := data_manage.GetEdbInfoByEdbCode(source, req.EdbCode)
  32. if e != nil && !utils.IsErrNoRow(e) {
  33. err = fmt.Errorf("获取指标是否存在失败, err: %s", e.Error())
  34. return
  35. }
  36. if exist != nil {
  37. skip = true
  38. return
  39. }
  40. var startDate, endDate string
  41. 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)
  42. if e != nil {
  43. errMsg = msg
  44. err = fmt.Errorf("EdbInfo: 新增指标失败, err: %s", e.Error())
  45. return
  46. }
  47. edb = edbInfo
  48. return
  49. }
  50. func GetPageBaseBusinessIndexData(indexCode string, startSize, pageSize int) (dataCount int, dataList []*data_manage.BaseFromBusinessIndexDataItem, err error) {
  51. if utils.UseMongo {
  52. dataCount, dataList, err = getPageBaseBusinessIndexDataByMongo(indexCode, startSize, pageSize)
  53. } else {
  54. dataCount, dataList, err = getPageBaseBusinessIndexDataByMysql(indexCode, startSize, pageSize)
  55. }
  56. return
  57. }
  58. func getPageBaseBusinessIndexDataByMongo(indexCode string, startSize, pageSize int) (dataCount int, dataList []*data_manage.BaseFromBusinessIndexDataItem, err error) {
  59. dataList = make([]*data_manage.BaseFromBusinessIndexDataItem, 0)
  60. mogDataObj := mgo.BaseFromBusinessData{}
  61. queryConditions := bson.M{
  62. "index_code": indexCode,
  63. }
  64. tmpCount, tmpErr := mogDataObj.GetCountDataList(queryConditions)
  65. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  66. err = tmpErr
  67. return
  68. }
  69. dataCount = int(tmpCount)
  70. tmpDataList, tmpErr := mogDataObj.GetPageDataList(queryConditions, int64(startSize), int64(pageSize), []string{"-data_time"})
  71. if tmpErr != nil {
  72. err = tmpErr
  73. return
  74. }
  75. for _, v := range tmpDataList {
  76. dataList = append(dataList, &data_manage.BaseFromBusinessIndexDataItem{
  77. ID: v.ID.String(),
  78. IndexCode: v.IndexCode,
  79. DataTime: v.DataTime.Format(utils.FormatDate),
  80. Value: v.Value,
  81. })
  82. }
  83. return
  84. }
  85. func getPageBaseBusinessIndexDataByMysql(indexCode string, startSize, pageSize int) (dataCount int, dataList []*data_manage.BaseFromBusinessIndexDataItem, err error) {
  86. dataList = make([]*data_manage.BaseFromBusinessIndexDataItem, 0)
  87. businessDataObj := data_manage.BaseFromBusinessData{}
  88. var condition []string
  89. var pars []interface{}
  90. condition = append(condition, "index_code = ?")
  91. pars = append(pars, indexCode)
  92. tmpCount, tmpErr := businessDataObj.GetCountDataList(condition, pars)
  93. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  94. err = tmpErr
  95. return
  96. }
  97. dataCount = int(tmpCount)
  98. tmpDataList, tmpErr := businessDataObj.GetPageDataList(condition, pars, " data_time desc", int64(startSize), int64(pageSize))
  99. if tmpErr != nil {
  100. err = tmpErr
  101. return
  102. }
  103. for _, v := range tmpDataList {
  104. dataList = append(dataList, &data_manage.BaseFromBusinessIndexDataItem{
  105. ID: fmt.Sprint(v.BusinessDataId),
  106. IndexCode: v.IndexCode,
  107. DataTime: v.DataTime.Format(utils.FormatDate),
  108. Value: v.Value,
  109. })
  110. }
  111. return
  112. }