edb_data.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package services
  2. import (
  3. "eta/eta_forum_admin/models"
  4. "eta/eta_forum_admin/models/mgodb"
  5. "eta/eta_forum_admin/utils"
  6. "fmt"
  7. "github.com/shopspring/decimal"
  8. "time"
  9. )
  10. // GetEdbDataList 获取指标的数据(日期正序返回)
  11. func GetEdbDataList(endInfoId, edbType int, startDate, endDate string) (list []*models.EdbDataList, err error) {
  12. dataList := make([]*mgodb.EdbDataBase, 0)
  13. var startDateT, endDateT time.Time
  14. if startDate != "" {
  15. startDateT, err = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  16. if err != nil {
  17. err = fmt.Errorf("日期格式错误 error, %v", err)
  18. return
  19. }
  20. }
  21. if endDate != "" {
  22. endDateT, err = time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  23. if err != nil {
  24. err = fmt.Errorf("日期格式错误 error, %v", err)
  25. return
  26. }
  27. }
  28. if edbType == 1 {
  29. dataList, err = mgodb.GetEdbDataList(endInfoId, startDateT, endDateT)
  30. if err != nil {
  31. err = fmt.Errorf("查询指标数据出错 error, %v", err)
  32. return
  33. }
  34. } else {
  35. dataList, err = mgodb.GetEdbCalculateDataList(endInfoId, startDateT, endDateT)
  36. if err != nil {
  37. err = fmt.Errorf("查询指标数据出错 error, %v", err)
  38. return
  39. }
  40. }
  41. list = make([]*models.EdbDataList, 0)
  42. for _, v := range dataList {
  43. // 字符串转成浮点数
  44. list = append(list, &models.EdbDataList{
  45. EdbInfoId: v.EdbInfoId,
  46. DataTime: v.DataTime.Format(utils.FormatDate),
  47. DataTimestamp: v.DataTimestamp,
  48. Value: v.Value,
  49. },
  50. )
  51. }
  52. return
  53. }
  54. // TbzDiv 同比值计算
  55. func TbzDiv(a, b float64) float64 {
  56. var valFloat float64
  57. if b != 0 {
  58. af := decimal.NewFromFloat(a)
  59. bf := decimal.NewFromFloat(b)
  60. val, _ := af.Div(bf).Float64()
  61. val = val - 1
  62. valFloat, _ = decimal.NewFromFloat(val).RoundCeil(4).Float64()
  63. } else {
  64. valFloat = 0
  65. }
  66. return valFloat
  67. }
  68. // GetEdbDataTbzForSeason 获取指标的同比值数据
  69. func GetEdbDataTbzForSeason(frequency string, tmpDataList []*models.EdbDataList, startDateTime time.Time) (dataList []*models.EdbDataList, minValue, maxValue float64, err error) {
  70. dataList = make([]*models.EdbDataList, 0)
  71. // 数据处理
  72. var dateArr []string
  73. dataMap := make(map[string]*models.EdbDataList)
  74. for _, v := range tmpDataList {
  75. dateArr = append(dateArr, v.DataTime)
  76. dataMap[v.DataTime] = v
  77. }
  78. for _, av := range dateArr {
  79. currentItem, ok := dataMap[av]
  80. // 如果找不到当前日期的数据,那么终止当前循环,进入下一循环
  81. if !ok {
  82. continue
  83. }
  84. tmpItem := *currentItem
  85. var isOk bool //是否计算出来结果
  86. //当前日期
  87. currentDate, tmpErr := time.ParseInLocation(utils.FormatDate, av, time.Local)
  88. if tmpErr != nil {
  89. err = tmpErr
  90. return
  91. }
  92. // 如果存在开始日期,同时,当前日期早于开始日期,那么终止当前循环,进入下一循环
  93. if !startDateTime.IsZero() && currentDate.Before(startDateTime) {
  94. continue
  95. }
  96. //上一年的日期
  97. preDate := currentDate.AddDate(-1, 0, 0)
  98. preDateStr := preDate.Format(utils.FormatDate)
  99. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期找到
  100. tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
  101. isOk = true
  102. } else {
  103. if frequency == "月度" { //向上和向下,各找一个月
  104. for i := 0; i <= 35; i++ {
  105. nextDateDay := preDate.AddDate(0, 0, i)
  106. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  107. if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  108. tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
  109. isOk = true
  110. break
  111. } else {
  112. preDateDay := preDate.AddDate(0, 0, -i)
  113. preDateDayStr := preDateDay.Format(utils.FormatDate)
  114. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  115. tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
  116. isOk = true
  117. break
  118. }
  119. }
  120. }
  121. } else if frequency == "季度" || frequency == "年度" {
  122. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期->下一个月找到
  123. tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
  124. isOk = true
  125. break
  126. }
  127. } else {
  128. nextDateDay := preDate.AddDate(0, 0, 1)
  129. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  130. preDateDay := preDate.AddDate(0, 0, -1)
  131. preDateDayStr := preDateDay.Format(utils.FormatDate)
  132. for i := 0; i < 35; i++ {
  133. if i >= 1 {
  134. nextDateDay = nextDateDay.AddDate(0, 0, i)
  135. nextDateDayStr = nextDateDay.Format(utils.FormatDate)
  136. }
  137. if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  138. tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
  139. isOk = true
  140. break
  141. } else {
  142. if i >= 1 {
  143. preDateDay = preDate.AddDate(0, 0, -i)
  144. preDateDayStr = nextDateDay.Format(utils.FormatDate)
  145. }
  146. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  147. tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
  148. isOk = true
  149. break
  150. }
  151. }
  152. }
  153. }
  154. }
  155. if isOk {
  156. if tmpItem.Value > maxValue {
  157. maxValue = tmpItem.Value
  158. }
  159. if tmpItem.Value < minValue {
  160. minValue = tmpItem.Value
  161. }
  162. dataList = append(dataList, &tmpItem)
  163. }
  164. }
  165. return
  166. }