factor_edb_series.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_index_lib/models"
  6. "eta/eta_index_lib/services/alarm_msg"
  7. "eta/eta_index_lib/utils"
  8. "fmt"
  9. "time"
  10. )
  11. // FactorEdbStepCalculate 因子指标-多公式计算
  12. func FactorEdbStepCalculate(seriesId, edbInfoId int, edbCode string, edbData []*models.EdbInfoSearchData, calculates []models.CalculatesReq) (err error) {
  13. if len(edbData) == 0 || len(calculates) == 0 {
  14. return
  15. }
  16. defer func() {
  17. if err != nil {
  18. tips := fmt.Sprintf("FactorEdbStepCalculate计算失败, EdbCode: %s, ErrMsg: %v", edbCode, err)
  19. utils.FileLog.Info(tips)
  20. go alarm_msg.SendAlarmMsg(tips, 2)
  21. }
  22. }()
  23. // 重新计算-先清除原数据
  24. calculateDataOb := new(models.FactorEdbSeriesCalculateData)
  25. {
  26. cond := fmt.Sprintf("%s = ?", calculateDataOb.Cols().FactorEdbSeriesId)
  27. pars := make([]interface{}, 0)
  28. pars = append(pars, seriesId)
  29. if e := calculateDataOb.RemoveByCondition(cond, pars); e != nil {
  30. err = fmt.Errorf("清除原数据失败, err: %v", e)
  31. return
  32. }
  33. }
  34. // 多公式嵌套计算
  35. dataMap, dateList, _, e := StepCalculate(edbData, calculates)
  36. if e != nil {
  37. err = fmt.Errorf("嵌套计算失败, err: %v", e)
  38. return
  39. }
  40. // 计算成功的保存结果
  41. dataArr := make([]*models.FactorEdbSeriesCalculateData, 0)
  42. for _, d := range dateList {
  43. val, ok := dataMap[d]
  44. if !ok {
  45. continue
  46. }
  47. dataTime, e := time.ParseInLocation(utils.FormatDate, d, time.Local)
  48. if e != nil {
  49. err = fmt.Errorf("解析计算结果日期失败, err: %v", e)
  50. return
  51. }
  52. dataArr = append(dataArr, &models.FactorEdbSeriesCalculateData{
  53. FactorEdbSeriesId: seriesId,
  54. EdbInfoId: edbInfoId,
  55. EdbCode: edbCode,
  56. DataTime: dataTime,
  57. Value: val,
  58. CreateTime: time.Now().Local(),
  59. ModifyTime: time.Now().Local(),
  60. DataTimestamp: dataTime.UnixNano() / 1e6,
  61. })
  62. }
  63. if len(dataArr) == 0 {
  64. //err = fmt.Errorf("计算结果无数据")
  65. return
  66. }
  67. if e = calculateDataOb.CreateMulti(dataArr); e != nil {
  68. err = fmt.Errorf("保存计算结果失败, err: %v", e)
  69. return
  70. }
  71. return
  72. }
  73. func RangeAnalysisChartCalculate(seriesId, chartInfoId int, seriesMappingItem *models.FactorEdbSeriesChartMapping, edbInfoList []*models.EdbInfo) (err error, errMsg string) {
  74. defer func() {
  75. if err != nil {
  76. tips := fmt.Sprintf("RangeAnalysisChartCalculate 区间分析图表计算失败, 图表ID:%d, ErrMsg: %v", chartInfoId, err)
  77. fmt.Println(tips)
  78. utils.FileLog.Info(tips)
  79. go alarm_msg.SendAlarmMsg(tips, 3)
  80. }
  81. }()
  82. chartInfo, err := models.GetChartInfoById(chartInfoId)
  83. if err != nil {
  84. if err.Error() == utils.ErrNoRow() {
  85. errMsg = "图表已被删除,请刷新页面"
  86. err = errors.New(errMsg)
  87. return
  88. }
  89. errMsg = "获取图表信息失败"
  90. err = errors.New("获取图表信息失败,Err:" + err.Error())
  91. return
  92. }
  93. // 区间计算图表配置校验
  94. var extraConfig models.RangeAnalysisCalculateFormula
  95. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &extraConfig)
  96. if err != nil {
  97. errMsg = "配置信息错误"
  98. err = errors.New(errMsg + ", Err: " + err.Error())
  99. return
  100. }
  101. if seriesMappingItem.FactorEdbSeriesChartMappingId > 0 {
  102. // 重新计算-先清除原数据
  103. calculateDataOb := new(models.FactorEdbSeriesCalculateDataQjjs)
  104. cond := fmt.Sprintf("%s = ?", calculateDataOb.Cols().FactorEdbSeriesId)
  105. pars := make([]interface{}, 0)
  106. pars = append(pars, seriesId)
  107. if e := calculateDataOb.RemoveByCondition(cond, pars); e != nil {
  108. err = fmt.Errorf("清除原数据失败, err: %v", e)
  109. return
  110. }
  111. // 计算成功的保存结果
  112. dataArr := make([]*models.FactorEdbSeriesCalculateDataQjjs, 0)
  113. for _, fromEdbInfo := range edbInfoList {
  114. dataList, e := models.GetRangeAnalysisChartDataByEdbInfo(fromEdbInfo, extraConfig)
  115. if e != nil {
  116. errMsg = "获取区间分析图表数据失败"
  117. err = errors.New(errMsg + ", Err: " + e.Error())
  118. return
  119. }
  120. for _, dataItem := range dataList {
  121. dataTime, _ := time.ParseInLocation(utils.FormatDate, dataItem.DataTime, time.Local)
  122. dataArr = append(dataArr, &models.FactorEdbSeriesCalculateDataQjjs{
  123. FactorEdbSeriesId: seriesId,
  124. EdbInfoId: fromEdbInfo.EdbInfoId,
  125. EdbCode: fromEdbInfo.EdbCode,
  126. DataTime: dataTime,
  127. Value: dataItem.Value,
  128. CreateTime: time.Now().Local(),
  129. ModifyTime: time.Now().Local(),
  130. DataTimestamp: dataItem.DataTimestamp,
  131. })
  132. }
  133. }
  134. if len(dataArr) == 0 {
  135. err = fmt.Errorf("计算结果无数据, seriesId: %d", seriesId)
  136. return
  137. }
  138. if e := calculateDataOb.CreateMulti(dataArr); e != nil {
  139. err = fmt.Errorf("保存计算结果失败, seriesId: %d, err: %v, ", seriesId, e)
  140. return
  141. }
  142. }
  143. return
  144. }