edb_data.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package data
  2. import (
  3. "eta/eta_chart_lib/models"
  4. "eta/eta_chart_lib/utils"
  5. "time"
  6. )
  7. // GetEdbDataTbzForSeason 获取指标的同比值数据
  8. func GetEdbDataTbzForSeason(frequency string, tmpDataList []*models.EdbDataList, startDateTime time.Time) (dataList []*models.EdbDataList, minValue, maxValue float64, err error) {
  9. dataList = make([]*models.EdbDataList, 0)
  10. // 数据处理
  11. var dateArr []string
  12. dataMap := make(map[string]*models.EdbDataList)
  13. for _, v := range tmpDataList {
  14. dateArr = append(dateArr, v.DataTime)
  15. dataMap[v.DataTime] = v
  16. }
  17. for _, av := range dateArr {
  18. currentItem, ok := dataMap[av]
  19. // 如果找不到当前日期的数据,那么终止当前循环,进入下一循环
  20. if !ok {
  21. continue
  22. }
  23. tmpItem := *currentItem
  24. var isOk bool //是否计算出来结果
  25. //当前日期
  26. currentDate, tmpErr := time.ParseInLocation(utils.FormatDate, av, time.Local)
  27. if tmpErr != nil {
  28. err = tmpErr
  29. return
  30. }
  31. // 如果存在开始日期,同时,当前日期早于开始日期,那么终止当前循环,进入下一循环
  32. if !startDateTime.IsZero() && currentDate.Before(startDateTime) {
  33. continue
  34. }
  35. //上一年的日期
  36. preDate := currentDate.AddDate(-1, 0, 0)
  37. preDateStr := preDate.Format(utils.FormatDate)
  38. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期找到
  39. tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
  40. isOk = true
  41. } else {
  42. if frequency == "月度" { //向上和向下,各找一个月
  43. for i := 0; i <= 35; i++ {
  44. nextDateDay := preDate.AddDate(0, 0, i)
  45. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  46. if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  47. tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
  48. isOk = true
  49. break
  50. } else {
  51. preDateDay := preDate.AddDate(0, 0, -i)
  52. preDateDayStr := preDateDay.Format(utils.FormatDate)
  53. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  54. tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
  55. isOk = true
  56. break
  57. }
  58. }
  59. }
  60. } else if frequency == "季度" || frequency == "年度" {
  61. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期->下一个月找到
  62. tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
  63. isOk = true
  64. break
  65. }
  66. } else {
  67. nextDateDay := preDate.AddDate(0, 0, 1)
  68. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  69. preDateDay := preDate.AddDate(0, 0, -1)
  70. preDateDayStr := preDateDay.Format(utils.FormatDate)
  71. for i := 0; i < 35; i++ {
  72. if i >= 1 {
  73. nextDateDay = nextDateDay.AddDate(0, 0, i)
  74. nextDateDayStr = nextDateDay.Format(utils.FormatDate)
  75. }
  76. if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  77. tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
  78. isOk = true
  79. break
  80. } else {
  81. if i >= 1 {
  82. preDateDay = preDate.AddDate(0, 0, -i)
  83. preDateDayStr = nextDateDay.Format(utils.FormatDate)
  84. }
  85. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  86. tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
  87. isOk = true
  88. break
  89. }
  90. }
  91. }
  92. }
  93. }
  94. if isOk {
  95. if tmpItem.Value > maxValue {
  96. maxValue = tmpItem.Value
  97. }
  98. if tmpItem.Value < minValue {
  99. minValue = tmpItem.Value
  100. }
  101. dataList = append(dataList, &tmpItem)
  102. }
  103. }
  104. return
  105. }