base_from_trade_analysis.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_index_lib/logic"
  5. "eta/eta_index_lib/models"
  6. excelModel "eta/eta_index_lib/models/excel"
  7. tradeAnalysisModel "eta/eta_index_lib/models/trade_analysis"
  8. tradeAnalysisService "eta/eta_index_lib/services/trade_analysis"
  9. "eta/eta_index_lib/utils"
  10. "fmt"
  11. "time"
  12. )
  13. // BaseFromTradeAnalysisController 持仓分析指标
  14. type BaseFromTradeAnalysisController struct {
  15. BaseAuthController
  16. }
  17. // EdbRefresh
  18. // @Title 指标库刷新
  19. // @Description 指标库刷新
  20. // @Success 200 {object} models.RefreshEdbInfoReq
  21. // @router /edb/refresh [post]
  22. func (this *BaseFromTradeAnalysisController) EdbRefresh() {
  23. br := new(models.BaseResponse).Init()
  24. defer func() {
  25. if br.ErrMsg == "" {
  26. br.IsSendEmail = false
  27. }
  28. this.Data["json"] = br
  29. this.ServeJSON()
  30. }()
  31. var req models.RefreshEdbInfoReq
  32. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  33. if err != nil {
  34. br.Msg = "参数解析异常!"
  35. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  36. return
  37. }
  38. if req.EdbCode == "" {
  39. br.Msg = "请输入指标编码!"
  40. br.ErrMsg = "请输入指标编码,指标编码为空"
  41. return
  42. }
  43. if req.EdbInfoId < 0 {
  44. br.Msg = "请输入指标ID!"
  45. br.ErrMsg = "请输入指标ID"
  46. return
  47. }
  48. edbOb := new(models.EdbTradeAnalysis)
  49. source := edbOb.GetSource()
  50. cacheKey := fmt.Sprintf("%s_%d_%s", utils.CACHE_EDB_DATA_REFRESH, source, req.EdbCode)
  51. if utils.Rc.IsExist(cacheKey) {
  52. br.Ret = 501
  53. br.Success = true
  54. br.Msg = "系统处理中,请稍后重试"
  55. return
  56. }
  57. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  58. defer func() {
  59. _ = utils.Rc.Delete(cacheKey)
  60. }()
  61. // 获取指标详情
  62. edbInfo, e := models.GetEdbInfoByEdbCode(source, req.EdbCode)
  63. if e != nil {
  64. br.Msg = "指标不存在"
  65. br.ErrMsg = fmt.Sprintf("指标不存在, %v", e)
  66. return
  67. }
  68. formula := edbInfo.CalculateFormula
  69. // 校验计算公式
  70. var extraConfig tradeAnalysisModel.WarehouseExtraConfig
  71. if formula == `` {
  72. br.Msg = "指标计算公式有误"
  73. br.ErrMsg = fmt.Sprintf("指标计算公式有误, conf: %s", formula)
  74. return
  75. }
  76. if e := json.Unmarshal([]byte(formula), &extraConfig); e != nil {
  77. br.Msg = "指标计算公式有误"
  78. br.ErrMsg = fmt.Sprintf("指标计算公式解析失败, err: %v; conf: %s", e, formula)
  79. return
  80. }
  81. if extraConfig.Exchange == "" {
  82. br.Msg = "指标计算公式有误"
  83. br.ErrMsg = fmt.Sprintf("指标计算公式交易所异常, conf: %s", formula)
  84. return
  85. }
  86. if extraConfig.ClassifyName == "" {
  87. br.Msg = "指标计算公式有误"
  88. br.ErrMsg = fmt.Sprintf("指标计算公式品种异常, conf: %s", formula)
  89. return
  90. }
  91. if len(extraConfig.Contracts) == 0 {
  92. br.Msg = "指标计算公式有误"
  93. br.ErrMsg = fmt.Sprintf("指标计算公式合约异常, conf: %s", formula)
  94. return
  95. }
  96. if len(extraConfig.Companies) != 1 {
  97. br.Msg = "指标计算公式有误"
  98. br.ErrMsg = fmt.Sprintf("指标计算公式期货公司异常, conf: %s", formula)
  99. return
  100. }
  101. if extraConfig.PredictRatio < 0 || extraConfig.PredictRatio > 1 {
  102. br.Msg = "指标计算公式有误"
  103. br.ErrMsg = fmt.Sprintf("指标计算公式估计参数异常, conf: %s", formula)
  104. return
  105. }
  106. // 获取持仓数据
  107. companyTradeData, e := tradeAnalysisService.GetWarehouseTradeData(extraConfig.Exchange, extraConfig.ClassifyName, extraConfig.Contracts, extraConfig.Companies, extraConfig.PredictRatio)
  108. if e != nil {
  109. br.Msg = "获取失败"
  110. br.ErrMsg = fmt.Sprintf("获取期货公司持仓加总数据失败, %v", e)
  111. return
  112. }
  113. if len(companyTradeData) == 0 {
  114. br.Ret = 200
  115. br.Success = true
  116. br.Msg = "期货数据为空"
  117. return
  118. }
  119. // 转换持仓数据为指标数据
  120. convertData, e := tradeAnalysisService.FormatCompanyTradeData2EdbData(companyTradeData[0], extraConfig.WarehouseChartType)
  121. if e != nil {
  122. br.Msg = "获取失败"
  123. br.ErrMsg = fmt.Sprintf("持仓数据转为指标数据失败, %v", e)
  124. return
  125. }
  126. // 刷新指标
  127. if e = edbOb.Refresh(edbInfo, convertData); e != nil {
  128. br.Msg = "刷新指标失败"
  129. br.ErrMsg = fmt.Sprintf("刷新指标失败, %v", e)
  130. return
  131. }
  132. // 更新指标最值
  133. if e = edbOb.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo); e != nil {
  134. br.Msg = "刷新指标失败"
  135. br.ErrMsg = fmt.Sprintf("更新指标最值失败, %v", e)
  136. return
  137. }
  138. // 更新ES
  139. go logic.UpdateEs(edbInfo.EdbInfoId)
  140. br.Ret = 200
  141. br.Success = true
  142. br.Msg = "操作成功"
  143. }
  144. // TableRefresh
  145. // @Title 表格刷新
  146. // @Description 表格刷新
  147. // @Success 200 {object} tradeAnalysisModel.RefreshTableReq
  148. // @router /table/refresh [post]
  149. func (this *BaseFromTradeAnalysisController) TableRefresh() {
  150. br := new(models.BaseResponse).Init()
  151. defer func() {
  152. if br.ErrMsg == "" {
  153. br.IsSendEmail = false
  154. }
  155. this.Data["json"] = br
  156. this.ServeJSON()
  157. }()
  158. var req tradeAnalysisModel.RefreshTableReq
  159. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  160. br.Msg = "参数解析异常!"
  161. br.ErrMsg = fmt.Sprintf("参数解析失败, %v", e)
  162. return
  163. }
  164. if req.ExcelInfoId <= 0 {
  165. br.Msg = "请输入表格ID"
  166. br.ErrMsg = "请输入表格ID"
  167. return
  168. }
  169. cacheKey := fmt.Sprintf("%s_%d", utils.CACHE_EXCEL_REFRESH, req.ExcelInfoId)
  170. if utils.Rc.IsExist(cacheKey) {
  171. br.Ret = 200
  172. br.Success = true
  173. br.Msg = "系统处理中,请稍后重试"
  174. return
  175. }
  176. utils.Rc.SetNX(cacheKey, 1, 2*time.Minute)
  177. defer func() {
  178. _ = utils.Rc.Delete(cacheKey)
  179. }()
  180. // 获取表格信息
  181. excelOb := new(excelModel.ExcelInfo)
  182. item, e := excelOb.GetItemById(req.ExcelInfoId)
  183. if e != nil {
  184. if e.Error() == utils.ErrNoRow() {
  185. br.Msg = "表格不存在"
  186. br.ErrMsg = fmt.Sprintf("表格不存在, ExcelId: %d", req.ExcelInfoId)
  187. return
  188. }
  189. br.Msg = "操作失败"
  190. br.ErrMsg = fmt.Sprintf("获取表格信息失败, %v", e)
  191. return
  192. }
  193. if item.IsDelete == 1 {
  194. br.Msg = "表格已被删除"
  195. br.ErrMsg = fmt.Sprintf("表格已被删除, ExcelId: %d", req.ExcelInfoId)
  196. return
  197. }
  198. // 获取表格数据
  199. switch item.Source {
  200. case utils.TRADE_ANALYSIS_TABLE:
  201. // 多空分析
  202. var tableConfig tradeAnalysisModel.TableExtraConfig
  203. if item.ExtraConfig == "" {
  204. br.Msg = "操作失败"
  205. br.ErrMsg = fmt.Sprintf("表格配置为空, ExcelId: %d", item.ExcelInfoId)
  206. return
  207. }
  208. if e = json.Unmarshal([]byte(item.ExtraConfig), &tableConfig); e != nil {
  209. br.Msg = "操作失败"
  210. br.ErrMsg = fmt.Sprintf("表格配置解析失败, ExcelId: %d, Err: %v", item.ExcelInfoId, e)
  211. return
  212. }
  213. tableData, e := tradeAnalysisService.GetTableRowsDataByConfig(tableConfig)
  214. if e != nil {
  215. br.Msg = "操作失败"
  216. br.ErrMsg = fmt.Sprintf("获取表格行数据失败, %v", e)
  217. return
  218. }
  219. content, e := json.Marshal(tableData)
  220. if e != nil {
  221. br.Msg = "表格数据JSON格式化失败"
  222. br.ErrMsg = fmt.Sprintf("表格数据JSON格式化失败, %v", e)
  223. return
  224. }
  225. item.Content = string(content)
  226. case utils.TRADE_ANALYSIS_CORRELATION_TABLE:
  227. // 相关性表格
  228. var tableConfig tradeAnalysisModel.CorrelationTableExtraConfig
  229. if item.ExtraConfig == "" {
  230. br.Msg = "操作失败"
  231. br.ErrMsg = fmt.Sprintf("表格配置为空, ExcelId: %d", item.ExcelInfoId)
  232. return
  233. }
  234. if e = json.Unmarshal([]byte(item.ExtraConfig), &tableConfig); e != nil {
  235. br.Msg = "操作失败"
  236. br.ErrMsg = fmt.Sprintf("表格配置解析失败, ExcelId: %d, Err: %v", item.ExcelInfoId, e)
  237. return
  238. }
  239. tableData, e := tradeAnalysisService.GetCorrelationTableRowsDataByConfig(tableConfig)
  240. if e != nil {
  241. br.Msg = "操作失败"
  242. br.ErrMsg = fmt.Sprintf("获取表格数据失败, %v", e)
  243. return
  244. }
  245. content, e := json.Marshal(tableData)
  246. if e != nil {
  247. br.Msg = "表格数据JSON格式化失败"
  248. br.ErrMsg = fmt.Sprintf("表格数据JSON格式化失败, %v", e)
  249. return
  250. }
  251. item.Content = string(content)
  252. default:
  253. br.Msg = "表格来源有误"
  254. br.ErrMsg = fmt.Sprintf("表格来源有误, ExcelId: %d, Source: %d", item.ExcelInfoId, item.Source)
  255. return
  256. }
  257. // 更新内容
  258. updateCols := []string{"Content", "ModifyTime"}
  259. if e = item.Update(updateCols); e != nil {
  260. br.Msg = "操作失败"
  261. br.ErrMsg = fmt.Sprintf("更新表格数据失败, %v", e)
  262. return
  263. }
  264. br.Ret = 200
  265. br.Success = true
  266. br.Msg = "操作成功"
  267. }