factor_edb_series.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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) (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. ob := new(models.FactorEdbSeriesMapping)
  103. seriesEdbMappingList, e := ob.GetItemBySeriesId(seriesMappingItem.FactorEdbSeriesId)
  104. if e != nil { // 没有数据,则不计算
  105. return
  106. }
  107. edbInfoIds := make([]int, 0)
  108. for _, item := range seriesEdbMappingList {
  109. edbInfoIds = append(edbInfoIds, item.EdbInfoId)
  110. }
  111. edbInfoList, e := models.GetEdbInfoByIdList(edbInfoIds)
  112. if e != nil {
  113. errMsg = "获取指标信息失败"
  114. err = errors.New(errMsg + ", Err: " + e.Error())
  115. return
  116. }
  117. // 重新计算-先清除原数据
  118. calculateDataOb := new(models.FactorEdbSeriesCalculateDataQjjs)
  119. cond := fmt.Sprintf("%s = ?", calculateDataOb.Cols().FactorEdbSeriesId)
  120. pars := make([]interface{}, 0)
  121. pars = append(pars, seriesId)
  122. if e := calculateDataOb.RemoveByCondition(cond, pars); e != nil {
  123. err = fmt.Errorf("清除原数据失败, err: %v", e)
  124. return
  125. }
  126. // 计算成功的保存结果
  127. dataArr := make([]*models.FactorEdbSeriesCalculateDataQjjs, 0)
  128. for _, fromEdbInfo := range edbInfoList {
  129. dataList, e := models.GetRangeAnalysisChartDataByEdbInfo(fromEdbInfo, extraConfig)
  130. if e != nil {
  131. errMsg = "获取区间分析图表数据失败"
  132. err = errors.New(errMsg + ", Err: " + e.Error())
  133. return
  134. }
  135. for _, dataItem := range dataList {
  136. dataTime, _ := time.ParseInLocation(utils.FormatDate, dataItem.DataTime, time.Local)
  137. dataArr = append(dataArr, &models.FactorEdbSeriesCalculateDataQjjs{
  138. FactorEdbSeriesId: seriesId,
  139. EdbInfoId: fromEdbInfo.EdbInfoId,
  140. EdbCode: fromEdbInfo.EdbCode,
  141. DataTime: dataTime,
  142. Value: dataItem.Value,
  143. CreateTime: time.Now().Local(),
  144. ModifyTime: time.Now().Local(),
  145. DataTimestamp: dataItem.DataTimestamp,
  146. })
  147. }
  148. }
  149. if len(dataArr) == 0 {
  150. err = fmt.Errorf("计算结果无数据, seriesId: %d", seriesId)
  151. return
  152. }
  153. if e := calculateDataOb.CreateMulti(dataArr); e != nil {
  154. err = fmt.Errorf("保存计算结果失败, seriesId: %d, err: %v, ", seriesId, e)
  155. return
  156. }
  157. }
  158. return
  159. }