base_from_trade_analysis.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_index_lib/logic"
  5. "eta/eta_index_lib/models"
  6. tradeAnalysisModel "eta/eta_index_lib/models/trade_analysis"
  7. tradeAnalysisService "eta/eta_index_lib/services/trade_analysis"
  8. "eta/eta_index_lib/utils"
  9. "fmt"
  10. "time"
  11. )
  12. // BaseFromTradeAnalysisController 持仓分析指标
  13. type BaseFromTradeAnalysisController struct {
  14. BaseAuthController
  15. }
  16. // EdbRefresh
  17. // @Title 指标库刷新
  18. // @Description 指标库刷新
  19. // @Success 200 {object} models.RefreshEdbInfoReq
  20. // @router /edb/refresh [post]
  21. func (this *BaseFromTradeAnalysisController) EdbRefresh() {
  22. br := new(models.BaseResponse).Init()
  23. defer func() {
  24. if br.ErrMsg == "" {
  25. br.IsSendEmail = false
  26. }
  27. this.Data["json"] = br
  28. this.ServeJSON()
  29. }()
  30. var req models.RefreshEdbInfoReq
  31. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  32. if err != nil {
  33. br.Msg = "参数解析异常!"
  34. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  35. return
  36. }
  37. if req.EdbCode == "" {
  38. br.Msg = "请输入指标编码!"
  39. br.ErrMsg = "请输入指标编码,指标编码为空"
  40. return
  41. }
  42. if req.EdbInfoId < 0 {
  43. br.Msg = "请输入指标ID!"
  44. br.ErrMsg = "请输入指标ID"
  45. return
  46. }
  47. edbOb := new(models.EdbTradeAnalysis)
  48. source := edbOb.GetSource()
  49. cacheKey := fmt.Sprintf("%s_%d_%s", utils.CACHE_EDB_DATA_REFRESH, source, req.EdbCode)
  50. if utils.Rc.IsExist(cacheKey) {
  51. br.Ret = 501
  52. br.Success = true
  53. br.Msg = "系统处理中,请稍后重试"
  54. return
  55. }
  56. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  57. defer func() {
  58. _ = utils.Rc.Delete(cacheKey)
  59. }()
  60. // 获取指标详情
  61. edbInfo, e := models.GetEdbInfoByEdbCode(source, req.EdbCode)
  62. if e != nil {
  63. br.Msg = "指标不存在"
  64. br.ErrMsg = fmt.Sprintf("指标不存在, %v", e)
  65. return
  66. }
  67. formula := edbInfo.CalculateFormula
  68. // 校验计算公式
  69. var extraConfig tradeAnalysisModel.WarehouseExtraConfig
  70. if formula == `` {
  71. br.Msg = "指标计算公式有误"
  72. br.ErrMsg = fmt.Sprintf("指标计算公式有误, conf: %s", formula)
  73. return
  74. }
  75. if e := json.Unmarshal([]byte(formula), &extraConfig); e != nil {
  76. br.Msg = "指标计算公式有误"
  77. br.ErrMsg = fmt.Sprintf("指标计算公式解析失败, err: %v; conf: %s", e, formula)
  78. return
  79. }
  80. if extraConfig.Exchange == "" {
  81. br.Msg = "指标计算公式有误"
  82. br.ErrMsg = fmt.Sprintf("指标计算公式交易所异常, conf: %s", formula)
  83. return
  84. }
  85. if extraConfig.ClassifyName == "" {
  86. br.Msg = "指标计算公式有误"
  87. br.ErrMsg = fmt.Sprintf("指标计算公式品种异常, conf: %s", formula)
  88. return
  89. }
  90. if len(extraConfig.Contracts) == 0 {
  91. br.Msg = "指标计算公式有误"
  92. br.ErrMsg = fmt.Sprintf("指标计算公式合约异常, conf: %s", formula)
  93. return
  94. }
  95. if len(extraConfig.Companies) != 1 {
  96. br.Msg = "指标计算公式有误"
  97. br.ErrMsg = fmt.Sprintf("指标计算公式期货公司异常, conf: %s", formula)
  98. return
  99. }
  100. if extraConfig.PredictRatio < 0 || extraConfig.PredictRatio > 1 {
  101. br.Msg = "指标计算公式有误"
  102. br.ErrMsg = fmt.Sprintf("指标计算公式估计参数异常, conf: %s", formula)
  103. return
  104. }
  105. // 获取指标数据, 该图表未用实际指标, 为了统一数据格式用ChartEdbInfoMapping
  106. companyTradeData, e := tradeAnalysisService.GetOriginTradeData(extraConfig.Exchange, extraConfig.ClassifyName, extraConfig.Contracts, extraConfig.Companies, extraConfig.PredictRatio)
  107. if e != nil {
  108. br.Msg = "获取失败"
  109. br.ErrMsg = fmt.Sprintf("获取期货公司持仓加总数据失败, %v", e)
  110. return
  111. }
  112. if len(companyTradeData) == 0 {
  113. br.Msg = "期货数据为空"
  114. return
  115. }
  116. // 转换持仓数据为指标数据
  117. convertData, e := tradeAnalysisService.FormatCompanyTradeData2EdbData(companyTradeData[0], extraConfig.WarehouseChartType)
  118. if e != nil {
  119. br.Msg = "获取失败"
  120. br.ErrMsg = fmt.Sprintf("持仓数据转为指标数据失败, %v", e)
  121. return
  122. }
  123. // 刷新指标
  124. if e = edbOb.Refresh(edbInfo, convertData); e != nil {
  125. br.Msg = "刷新指标失败"
  126. br.ErrMsg = fmt.Sprintf("刷新指标失败, %v", e)
  127. return
  128. }
  129. // 更新指标最值
  130. if e = edbOb.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo); e != nil {
  131. br.Msg = "刷新指标失败"
  132. br.ErrMsg = fmt.Sprintf("更新指标最值失败, %v", e)
  133. return
  134. }
  135. // 更新ES
  136. go logic.UpdateEs(edbInfo.EdbInfoId)
  137. br.Ret = 200
  138. br.Success = true
  139. br.Msg = "操作成功"
  140. }