variety_edb.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package supply_analysis
  2. import (
  3. "errors"
  4. "eta_gn/eta_api/models/data_manage"
  5. "eta_gn/eta_api/models/data_manage/supply_analysis"
  6. "eta_gn/eta_api/utils"
  7. "fmt"
  8. "time"
  9. )
  10. // GetCustomEdbData 通过品种id获取自定义ETA指标库格式的数据列表
  11. func GetCustomEdbData(varietyEdbId int, startDate, endDate string) (dataList []*data_manage.EdbDataList, minVal, maxVal float64, err error) {
  12. // 根据品种指标id获取所有的数据
  13. tmpVarietyDataList, err := supply_analysis.GetVarietyEdbData(varietyEdbId, startDate, endDate)
  14. if err != nil {
  15. return
  16. }
  17. dataList = make([]*data_manage.EdbDataList, 0)
  18. for _, v := range tmpVarietyDataList {
  19. dataList = append(dataList, &data_manage.EdbDataList{
  20. EdbDataId: v.StockPlantDataId,
  21. EdbInfoId: v.VarietyEdbId,
  22. DataTime: v.DataTime.Format(utils.FormatDate),
  23. DataTimestamp: v.DataTime.UnixNano() / 1e6,
  24. Value: v.Value,
  25. })
  26. if v.Value < minVal {
  27. minVal = v.Value
  28. }
  29. if v.Value > maxVal {
  30. maxVal = v.Value
  31. }
  32. }
  33. return
  34. }
  35. // GetChartEdbSeasonalData 获取指标的季节性数据
  36. func GetChartEdbSeasonalData(varietyEdbId int, calendar, startDate, endDate, latestDateStr string) (resultData interface{}, minVal, maxVal float64, err error) {
  37. var startDateReal string
  38. startDateReal = startDate
  39. calendarPreYear := 0
  40. if calendar == "农历" {
  41. newStartDateReal, tmpErr := time.Parse(utils.FormatDate, startDateReal)
  42. if tmpErr != nil {
  43. err = tmpErr
  44. fmt.Println("time.Parse:" + err.Error())
  45. return
  46. }
  47. calendarPreYear = newStartDateReal.Year() - 1
  48. newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
  49. startDateReal = newStartDateReal.Format(utils.FormatDate)
  50. }
  51. dataList, minVal, maxVal, err := GetCustomEdbData(varietyEdbId, startDateReal, endDate)
  52. if err != nil {
  53. return
  54. }
  55. //latestDateStr := edbInfo.LatestDate //实际数据的截止日期
  56. latestDate, tmpErr := time.Parse(utils.FormatDate, latestDateStr)
  57. if tmpErr != nil {
  58. err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + latestDateStr))
  59. return
  60. }
  61. latestDateYear := latestDate.Year() //实际数据截止年份
  62. if calendar == "农历" {
  63. if len(dataList) <= 0 {
  64. resultData = new(data_manage.EdbDataResult)
  65. } else {
  66. result, tmpErr := data_manage.AddCalculateQuarterV4(dataList)
  67. if tmpErr != nil {
  68. err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
  69. return
  70. }
  71. // 处理季节图的截止日期
  72. for k, edbDataItems := range result.List {
  73. var cuttingDataTimestamp int64
  74. // 切割的日期时间字符串
  75. cuttingDataTimeStr := latestDate.AddDate(0, 0, edbDataItems.BetweenDay).Format(utils.FormatDate)
  76. //如果等于最后的实际日期,那么遍历找到该日期对应的时间戳,并将其赋值为 切割时间戳
  77. if edbDataItems.Year >= latestDateYear {
  78. for _, tmpData := range edbDataItems.Items {
  79. if tmpData.DataTime == cuttingDataTimeStr {
  80. cuttingDataTimestamp = tmpData.DataTimestamp
  81. break
  82. }
  83. }
  84. }
  85. edbDataItems.CuttingDataTimestamp = cuttingDataTimestamp
  86. result.List[k] = edbDataItems
  87. }
  88. if result.List[0].Year != calendarPreYear {
  89. itemList := make([]*data_manage.EdbDataList, 0)
  90. items := new(data_manage.EdbDataItems)
  91. //items.Year = calendarPreYear
  92. items.Items = itemList
  93. newResult := new(data_manage.EdbDataResult)
  94. newResult.List = append(newResult.List, items)
  95. newResult.List = append(newResult.List, result.List...)
  96. resultData = newResult
  97. } else {
  98. resultData = result
  99. }
  100. }
  101. } else {
  102. currentYear := time.Now().Year()
  103. quarterDataList := make([]*data_manage.QuarterData, 0)
  104. quarterMap := make(map[int][]*data_manage.EdbDataList)
  105. var quarterArr []int
  106. for _, v := range dataList {
  107. itemDate, tmpErr := time.Parse(utils.FormatDate, v.DataTime)
  108. if tmpErr != nil {
  109. err = errors.New("季度指标日期转换,Err:" + tmpErr.Error() + ";DataTime:" + v.DataTime)
  110. return
  111. }
  112. year := itemDate.Year()
  113. newItemDate := itemDate.AddDate(currentYear-year, 0, 0)
  114. timestamp := newItemDate.UnixNano() / 1e6
  115. v.DataTimestamp = timestamp
  116. if findVal, ok := quarterMap[year]; !ok {
  117. quarterArr = append(quarterArr, year)
  118. findVal = append(findVal, v)
  119. quarterMap[year] = findVal
  120. } else {
  121. findVal = append(findVal, v)
  122. quarterMap[year] = findVal
  123. }
  124. }
  125. for _, v := range quarterArr {
  126. itemList := quarterMap[v]
  127. quarterItem := new(data_manage.QuarterData)
  128. quarterItem.Year = v
  129. quarterItem.DataList = itemList
  130. //如果等于最后的实际日期,那么将切割时间戳记录
  131. if v == latestDateYear {
  132. var cuttingDataTimestamp int64
  133. for _, tmpData := range itemList {
  134. if tmpData.DataTime == latestDateStr {
  135. cuttingDataTimestamp = tmpData.DataTimestamp
  136. break
  137. }
  138. }
  139. quarterItem.CuttingDataTimestamp = cuttingDataTimestamp
  140. } else if v > latestDateYear {
  141. //如果大于最后的实际日期,那么第一个点就是切割的时间戳
  142. if len(itemList) > 0 {
  143. quarterItem.CuttingDataTimestamp = itemList[0].DataTimestamp - 100
  144. }
  145. }
  146. quarterDataList = append(quarterDataList, quarterItem)
  147. }
  148. resultData = quarterDataList
  149. }
  150. return
  151. }