handle_data.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package models
  2. import (
  3. "errors"
  4. "eta_gn/eta_index_lib/utils"
  5. "github.com/shopspring/decimal"
  6. "math"
  7. "time"
  8. )
  9. func HandleDataByLinearRegression(edbInfoDataList []*EdbInfoSearchData, handleDataMap map[string]float64) (newList []*EdbInfoSearchData, err error) {
  10. if len(edbInfoDataList) < 2 {
  11. return
  12. }
  13. var startEdbInfoData *EdbInfoSearchData
  14. for _, v := range edbInfoDataList {
  15. handleDataMap[v.DataTime] = v.Value
  16. if startEdbInfoData == nil {
  17. startEdbInfoData = v
  18. newList = append(newList, &EdbInfoSearchData{
  19. EdbDataId: v.EdbDataId,
  20. DataTime: v.DataTime,
  21. Value: v.Value,
  22. })
  23. continue
  24. }
  25. startDataTime, _ := time.ParseInLocation(utils.FormatDate, startEdbInfoData.DataTime, time.Local)
  26. currDataTime, _ := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  27. betweenHour := int(currDataTime.Sub(startDataTime).Hours())
  28. betweenDay := betweenHour / 24
  29. if betweenDay <= 1 {
  30. startEdbInfoData = v
  31. newList = append(newList, &EdbInfoSearchData{
  32. EdbDataId: v.EdbDataId,
  33. DataTime: v.DataTime,
  34. Value: v.Value,
  35. })
  36. continue
  37. }
  38. var a, b float64
  39. {
  40. coordinateData := make([]utils.Coordinate, 0)
  41. tmpCoordinate1 := utils.Coordinate{
  42. X: 1,
  43. Y: startEdbInfoData.Value,
  44. }
  45. coordinateData = append(coordinateData, tmpCoordinate1)
  46. tmpCoordinate2 := utils.Coordinate{
  47. X: float64(betweenDay) + 1,
  48. Y: v.Value,
  49. }
  50. coordinateData = append(coordinateData, tmpCoordinate2)
  51. a, b = utils.GetLinearResult(coordinateData)
  52. if math.IsNaN(a) || math.IsNaN(b) {
  53. err = errors.New("线性方程公式生成失败")
  54. return
  55. }
  56. }
  57. {
  58. for i := 1; i < betweenDay; i++ {
  59. tmpDataTime := startDataTime.AddDate(0, 0, i)
  60. aDecimal := decimal.NewFromFloat(a)
  61. xDecimal := decimal.NewFromInt(int64(i) + 1)
  62. bDecimal := decimal.NewFromFloat(b)
  63. val, _ := aDecimal.Mul(xDecimal).Add(bDecimal).Round(4).Float64()
  64. handleDataMap[tmpDataTime.Format(utils.FormatDate)] = val
  65. newList = append(newList, &EdbInfoSearchData{
  66. DataTime: tmpDataTime.Format(utils.FormatDate),
  67. Value: val,
  68. })
  69. }
  70. }
  71. newList = append(newList, &EdbInfoSearchData{
  72. EdbDataId: v.EdbDataId,
  73. DataTime: v.DataTime,
  74. Value: v.Value,
  75. })
  76. startEdbInfoData = v
  77. }
  78. return
  79. }
  80. func HandleDataByPreviousData(edbInfoDataList []*EdbInfoSearchData, handleDataMap map[string]float64) (err error) {
  81. if len(edbInfoDataList) < 2 {
  82. return
  83. }
  84. var startEdbInfoData *EdbInfoSearchData
  85. for _, v := range edbInfoDataList {
  86. handleDataMap[v.DataTime] = v.Value
  87. if startEdbInfoData == nil {
  88. startEdbInfoData = v
  89. continue
  90. }
  91. startDataTime, _ := time.ParseInLocation(utils.FormatDate, startEdbInfoData.DataTime, time.Local)
  92. currDataTime, _ := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  93. betweenHour := int(currDataTime.Sub(startDataTime).Hours())
  94. betweenDay := betweenHour / 24
  95. if betweenDay <= 1 {
  96. startEdbInfoData = v
  97. continue
  98. }
  99. {
  100. for i := 1; i < betweenDay; i++ {
  101. tmpDataTime := startDataTime.AddDate(0, 0, i)
  102. handleDataMap[tmpDataTime.Format(utils.FormatDate)] = startEdbInfoData.Value
  103. }
  104. }
  105. startEdbInfoData = v
  106. }
  107. return
  108. }
  109. func HandleDataByLinearRegressionByTime(edbInfoDataList []*EdbInfoData, handleDataMap map[time.Time]float64) (newList []*EdbInfoData, err error) {
  110. if len(edbInfoDataList) < 2 {
  111. return
  112. }
  113. var startEdbInfoData *EdbInfoData
  114. for _, v := range edbInfoDataList {
  115. handleDataMap[v.DataTime] = v.Value
  116. if startEdbInfoData == nil {
  117. startEdbInfoData = v
  118. newList = append(newList, &EdbInfoData{
  119. EdbDataId: v.EdbDataId,
  120. DataTime: v.DataTime,
  121. Value: v.Value,
  122. })
  123. continue
  124. }
  125. startDataTime := startEdbInfoData.DataTime
  126. currDataTime := v.DataTime
  127. betweenHour := int(currDataTime.Sub(startDataTime).Hours())
  128. betweenDay := betweenHour / 24
  129. if betweenDay <= 1 {
  130. startEdbInfoData = v
  131. newList = append(newList, &EdbInfoData{
  132. EdbDataId: v.EdbDataId,
  133. DataTime: v.DataTime,
  134. Value: v.Value,
  135. })
  136. continue
  137. }
  138. var a, b float64
  139. {
  140. coordinateData := make([]utils.Coordinate, 0)
  141. tmpCoordinate1 := utils.Coordinate{
  142. X: 1,
  143. Y: startEdbInfoData.Value,
  144. }
  145. coordinateData = append(coordinateData, tmpCoordinate1)
  146. tmpCoordinate2 := utils.Coordinate{
  147. X: float64(betweenDay) + 1,
  148. Y: v.Value,
  149. }
  150. coordinateData = append(coordinateData, tmpCoordinate2)
  151. a, b = utils.GetLinearResult(coordinateData)
  152. if math.IsNaN(a) || math.IsNaN(b) {
  153. err = errors.New("线性方程公式生成失败")
  154. return
  155. }
  156. }
  157. {
  158. for i := 1; i < betweenDay; i++ {
  159. tmpDataTime := startDataTime.AddDate(0, 0, i)
  160. aDecimal := decimal.NewFromFloat(a)
  161. xDecimal := decimal.NewFromInt(int64(i) + 1)
  162. bDecimal := decimal.NewFromFloat(b)
  163. val, _ := aDecimal.Mul(xDecimal).Add(bDecimal).Round(4).Float64()
  164. handleDataMap[tmpDataTime] = val
  165. newList = append(newList, &EdbInfoData{
  166. DataTime: tmpDataTime,
  167. Value: val,
  168. })
  169. }
  170. }
  171. newList = append(newList, &EdbInfoData{
  172. EdbDataId: v.EdbDataId,
  173. DataTime: v.DataTime,
  174. Value: v.Value,
  175. })
  176. startEdbInfoData = v
  177. }
  178. return
  179. }