base_from_trade_analysis.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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.Msg = "期货数据为空"
  115. return
  116. }
  117. // 转换持仓数据为指标数据
  118. convertData, e := tradeAnalysisService.FormatCompanyTradeData2EdbData(companyTradeData[0], extraConfig.WarehouseChartType)
  119. if e != nil {
  120. br.Msg = "获取失败"
  121. br.ErrMsg = fmt.Sprintf("持仓数据转为指标数据失败, %v", e)
  122. return
  123. }
  124. // 刷新指标
  125. if e = edbOb.Refresh(edbInfo, convertData); e != nil {
  126. br.Msg = "刷新指标失败"
  127. br.ErrMsg = fmt.Sprintf("刷新指标失败, %v", e)
  128. return
  129. }
  130. // 更新指标最值
  131. if e = edbOb.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo); e != nil {
  132. br.Msg = "刷新指标失败"
  133. br.ErrMsg = fmt.Sprintf("更新指标最值失败, %v", e)
  134. return
  135. }
  136. // 更新ES
  137. go logic.UpdateEs(edbInfo.EdbInfoId)
  138. br.Ret = 200
  139. br.Success = true
  140. br.Msg = "操作成功"
  141. }
  142. // TableRefresh
  143. // @Title 表格刷新
  144. // @Description 表格刷新
  145. // @Success 200 {object} tradeAnalysisModel.RefreshTableReq
  146. // @router /table/refresh [post]
  147. func (this *BaseFromTradeAnalysisController) TableRefresh() {
  148. br := new(models.BaseResponse).Init()
  149. defer func() {
  150. if br.ErrMsg == "" {
  151. br.IsSendEmail = false
  152. }
  153. this.Data["json"] = br
  154. this.ServeJSON()
  155. }()
  156. var req tradeAnalysisModel.RefreshTableReq
  157. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  158. br.Msg = "参数解析异常!"
  159. br.ErrMsg = fmt.Sprintf("参数解析失败, %v", e)
  160. return
  161. }
  162. if req.ExcelInfoId <= 0 {
  163. br.Msg = "请输入表格ID"
  164. br.ErrMsg = "请输入表格ID"
  165. return
  166. }
  167. cacheKey := fmt.Sprintf("%s_%d", utils.CACHE_EXCEL_REFRESH, req.ExcelInfoId)
  168. if utils.Rc.IsExist(cacheKey) {
  169. br.Ret = 200
  170. br.Success = true
  171. br.Msg = "系统处理中,请稍后重试"
  172. return
  173. }
  174. utils.Rc.SetNX(cacheKey, 1, 2*time.Minute)
  175. defer func() {
  176. _ = utils.Rc.Delete(cacheKey)
  177. }()
  178. // 获取表格信息
  179. excelOb := new(excelModel.ExcelInfo)
  180. item, e := excelOb.GetItemById(req.ExcelInfoId)
  181. if e != nil {
  182. if e.Error() == utils.ErrNoRow() {
  183. br.Msg = "表格不存在"
  184. br.ErrMsg = fmt.Sprintf("表格不存在, ExcelId: %d", req.ExcelInfoId)
  185. return
  186. }
  187. br.Msg = "操作失败"
  188. br.ErrMsg = fmt.Sprintf("获取表格信息失败, %v", e)
  189. return
  190. }
  191. if item.IsDelete == 1 {
  192. br.Msg = "表格已被删除"
  193. br.ErrMsg = fmt.Sprintf("表格已被删除, ExcelId: %d", req.ExcelInfoId)
  194. return
  195. }
  196. // 获取表格数据
  197. switch item.Source {
  198. case utils.TRADE_ANALYSIS_TABLE:
  199. // 多空分析
  200. var tableConfig tradeAnalysisModel.TableExtraConfig
  201. if item.ExtraConfig == "" {
  202. br.Msg = "操作失败"
  203. br.ErrMsg = fmt.Sprintf("表格配置为空, ExcelId: %d", item.ExcelInfoId)
  204. return
  205. }
  206. if e = json.Unmarshal([]byte(item.ExtraConfig), &tableConfig); e != nil {
  207. br.Msg = "操作失败"
  208. br.ErrMsg = fmt.Sprintf("表格配置解析失败, ExcelId: %d, Err: %v", item.ExcelInfoId, e)
  209. return
  210. }
  211. tableData, e := tradeAnalysisService.GetTableRowsDataByConfig(tableConfig)
  212. if e != nil {
  213. br.Msg = "操作失败"
  214. br.ErrMsg = fmt.Sprintf("获取表格行数据失败, %v", e)
  215. return
  216. }
  217. content, e := json.Marshal(tableData)
  218. if e != nil {
  219. br.Msg = "表格数据JSON格式化失败"
  220. br.ErrMsg = fmt.Sprintf("表格数据JSON格式化失败, %v", e)
  221. return
  222. }
  223. item.Content = string(content)
  224. case utils.TRADE_ANALYSIS_CORRELATION_TABLE:
  225. // 相关性表格
  226. var tableConfig tradeAnalysisModel.CorrelationTableExtraConfig
  227. if item.ExtraConfig == "" {
  228. br.Msg = "操作失败"
  229. br.ErrMsg = fmt.Sprintf("表格配置为空, ExcelId: %d", item.ExcelInfoId)
  230. return
  231. }
  232. if e = json.Unmarshal([]byte(item.ExtraConfig), &tableConfig); e != nil {
  233. br.Msg = "操作失败"
  234. br.ErrMsg = fmt.Sprintf("表格配置解析失败, ExcelId: %d, Err: %v", item.ExcelInfoId, e)
  235. return
  236. }
  237. tableData, e := tradeAnalysisService.GetCorrelationTableRowsDataByConfig(tableConfig)
  238. if e != nil {
  239. br.Msg = "操作失败"
  240. br.ErrMsg = fmt.Sprintf("获取表格数据失败, %v", e)
  241. return
  242. }
  243. content, e := json.Marshal(tableData)
  244. if e != nil {
  245. br.Msg = "表格数据JSON格式化失败"
  246. br.ErrMsg = fmt.Sprintf("表格数据JSON格式化失败, %v", e)
  247. return
  248. }
  249. item.Content = string(content)
  250. default:
  251. br.Msg = "表格来源有误"
  252. br.ErrMsg = fmt.Sprintf("表格来源有误, ExcelId: %d, Source: %d", item.ExcelInfoId, item.Source)
  253. return
  254. }
  255. // 更新内容
  256. updateCols := []string{"Content", "ModifyTime"}
  257. if e = item.Update(updateCols); e != nil {
  258. br.Msg = "操作失败"
  259. br.ErrMsg = fmt.Sprintf("更新表格数据失败, %v", e)
  260. return
  261. }
  262. br.Ret = 200
  263. br.Success = true
  264. br.Msg = "操作成功"
  265. }