ai_predict_model_index.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package services
  2. import (
  3. aiPredictModel "eta/eta_api/models/ai_predict_model"
  4. "eta/eta_api/models/data_manage"
  5. "eta/eta_api/services/data"
  6. "eta/eta_api/utils"
  7. "fmt"
  8. "time"
  9. )
  10. func ImportAiPredictModelIndexAndData(imports []*aiPredictModel.AiPredictModelImportData) (err error) {
  11. if len(imports) == 0 {
  12. return
  13. }
  14. // 查询已存在的标的
  15. indexOb := new(aiPredictModel.AiPredictModelIndex)
  16. indexNameItem := make(map[string]*aiPredictModel.AiPredictModelIndex)
  17. {
  18. list, e := indexOb.GetItemsByCondition("", make([]interface{}, 0), []string{}, "")
  19. if e != nil {
  20. err = fmt.Errorf("获取标的失败, %v", e)
  21. return
  22. }
  23. for _, v := range list {
  24. indexNameItem[v.IndexName] = v
  25. }
  26. }
  27. updateCols := []string{indexOb.Cols().ClassifyId, indexOb.Cols().ModelFramework, indexOb.Cols().PredictDate, indexOb.Cols().PredictValue, indexOb.Cols().DirectionAccuracy, indexOb.Cols().AbsoluteDeviation, indexOb.Cols().SysUserId, indexOb.Cols().SysUserRealName, indexOb.Cols().ModifyTime}
  28. updateIndexes := make([]*aiPredictModel.AiPredictModelImportData, 0)
  29. createIndexes := make([]*aiPredictModel.AiPredictModelImportData, 0)
  30. for _, v := range imports {
  31. exist := indexNameItem[v.Index.IndexName]
  32. // 编辑
  33. if exist != nil {
  34. v.Index.AiPredictModelIndexId = exist.AiPredictModelIndexId
  35. v.Index.IndexCode = exist.IndexCode
  36. updateIndexes = append(updateIndexes, v)
  37. continue
  38. }
  39. // 新增
  40. indexCode, e := utils.GenerateEdbCode(1, "IPM")
  41. if e != nil {
  42. err = fmt.Errorf("生成标的编码失败, %v", e)
  43. return
  44. }
  45. v.Index.IndexCode = indexCode
  46. createIndexes = append(createIndexes, v)
  47. }
  48. // 新增/更新指标
  49. if e := indexOb.ImportIndexAndData(createIndexes, updateIndexes, updateCols); e != nil {
  50. err = fmt.Errorf("导入指标失败, %v", e)
  51. return
  52. }
  53. return
  54. }
  55. func GetAiPredictChartDetailByData(indexItem *aiPredictModel.AiPredictModelIndex, indexData []*aiPredictModel.AiPredictModelData) (resp *data_manage.ChartInfoDetailResp, err error) {
  56. resp = new(data_manage.ChartInfoDetailResp)
  57. // 获取曲线图主题样式
  58. chartView := new(data_manage.ChartInfoView)
  59. chartView.ChartType = utils.CHART_SOURCE_DEFAULT
  60. chartTheme, e := data.GetChartThemeConfig(0, chartView.ChartType, utils.CHART_TYPE_CURVE)
  61. if e != nil {
  62. err = fmt.Errorf("获取图表主题样式失败, %v", e)
  63. return
  64. }
  65. chartView.ChartThemeStyle = chartTheme.Config
  66. chartView.ChartThemeId = chartTheme.ChartThemeId
  67. chartView.ChartName = indexItem.IndexName
  68. chartView.ChartNameEn = indexItem.IndexName
  69. chartView.DateType = 3
  70. chartView.Calendar = "公历"
  71. chartView.ChartSource = "AI预测模型"
  72. chartView.ChartSourceEn = "AI预测模型"
  73. // EdbList-固定一条为标的实际值、一条为预测值
  74. edbList := make([]*data_manage.ChartEdbInfoMapping, 0)
  75. edbActual, edbPredict := new(data_manage.ChartEdbInfoMapping), new(data_manage.ChartEdbInfoMapping)
  76. edbActual.EdbName = indexItem.IndexName
  77. edbActual.EdbNameEn = indexItem.IndexName
  78. edbActual.IsAxis = 1
  79. edbPredict.EdbName = "预测值"
  80. edbPredict.EdbNameEn = "预测值"
  81. edbPredict.IsAxis = 1
  82. actualData, predictData := make([]*data_manage.EdbDataList, 0), make([]*data_manage.EdbDataList, 0)
  83. var startDate, endDate time.Time
  84. var actualValues, predictValues []float64
  85. var actualNewest, predictNewest bool
  86. for k, v := range indexData {
  87. // 如果实际值和预测值都是null那么该日期无效直接忽略
  88. if !v.Value.Valid && !v.PredictValue.Valid {
  89. continue
  90. }
  91. // 将有效值加入[]float64,最后取极值
  92. if v.Value.Valid {
  93. actualValues = append(actualValues, v.Value.Float64)
  94. }
  95. if v.PredictValue.Valid {
  96. predictValues = append(predictValues, v.PredictValue.Float64)
  97. }
  98. // 开始结束时间
  99. if k == 0 {
  100. startDate = v.DataTime
  101. endDate = v.CreateTime
  102. }
  103. if v.DataTime.Before(startDate) {
  104. startDate = v.DataTime
  105. }
  106. if v.DataTime.After(endDate) {
  107. endDate = v.DataTime
  108. }
  109. // 指标数据
  110. if v.Value.Valid {
  111. if !actualNewest {
  112. edbActual.LatestDate = v.DataTime.Format(utils.FormatDate)
  113. edbActual.LatestValue = v.Value.Float64
  114. actualNewest = true
  115. }
  116. actualData = append(actualData, &data_manage.EdbDataList{
  117. DataTime: v.DataTime.Format(utils.FormatDate),
  118. Value: v.Value.Float64,
  119. DataTimestamp: v.DataTimestamp,
  120. })
  121. }
  122. if v.PredictValue.Valid {
  123. if !predictNewest {
  124. edbPredict.LatestDate = v.DataTime.Format(utils.FormatDate)
  125. edbPredict.LatestValue = v.Value.Float64
  126. predictNewest = true
  127. }
  128. predictData = append(predictData, &data_manage.EdbDataList{
  129. DataTime: v.DataTime.Format(utils.FormatDate),
  130. Value: v.PredictValue.Float64,
  131. DataTimestamp: v.DataTimestamp,
  132. })
  133. }
  134. }
  135. // 极值
  136. actualMin, actualMax := utils.FindMinMax(actualValues)
  137. predictMin, predictMax := utils.FindMinMax(predictValues)
  138. edbActual.MinData = actualMin
  139. edbActual.MaxData = actualMax
  140. edbPredict.MinData = predictMin
  141. edbPredict.MaxData = predictMax
  142. edbActual.DataList = actualData
  143. edbPredict.DataList = predictData
  144. edbList = append(edbList, edbActual, edbPredict)
  145. // 上下限
  146. if indexItem.LeftMin != "" {
  147. chartView.LeftMin = indexItem.LeftMin
  148. } else {
  149. leftMin := actualMin
  150. if leftMin > predictMin {
  151. leftMin = predictMin
  152. }
  153. chartView.LeftMin = fmt.Sprint(leftMin)
  154. }
  155. if indexItem.LeftMax != "" {
  156. chartView.LeftMax = indexItem.LeftMax
  157. } else {
  158. leftMax := actualMax
  159. if leftMax < predictMax {
  160. leftMax = predictMax
  161. }
  162. chartView.LeftMax = fmt.Sprint(leftMax)
  163. }
  164. chartView.StartDate = startDate.Format(utils.FormatDate)
  165. chartView.EndDate = endDate.Format(utils.FormatDate)
  166. resp.ChartInfo = chartView
  167. resp.EdbInfoList = edbList
  168. return
  169. }