base_from_wind_wsd.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_index_lib/logic"
  5. "eta/eta_index_lib/models"
  6. "eta/eta_index_lib/services"
  7. "eta/eta_index_lib/utils"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. // wind
  13. type WindWsdController struct {
  14. BaseAuthController
  15. }
  16. // @Title 新增wind指标接口
  17. // @Description 新增wind指标接口
  18. // @Success 200 {object} models.AddEdbInfoReq
  19. // @router /wsd/add [post]
  20. func (this *WindWsdController) Add() {
  21. br := new(models.BaseResponse).Init()
  22. var cacheKey string
  23. defer func() {
  24. utils.Rc.Delete(cacheKey)
  25. this.Data["json"] = br
  26. this.ServeJSON()
  27. }()
  28. source := utils.DATA_SOURCE_WIND
  29. var req models.AddEdbInfoReq
  30. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  31. if err != nil {
  32. br.Msg = "参数解析异常!"
  33. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  34. return
  35. }
  36. if req.EdbCode == "" {
  37. br.Msg = "请输入指标编码!"
  38. br.ErrMsg = "请输入指标编码,指标编码为空"
  39. return
  40. }
  41. cacheKey = utils.CACHE_EDB_DATA_ADD + strconv.Itoa(source) + "_" + req.StockCode + req.EdbCode
  42. if !utils.Rc.IsExist(cacheKey) {
  43. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  44. //期货数据,就默认到今天,特殊处理下
  45. endDate := utils.BASE_END_DATE
  46. if strings.Contains(req.EdbCode, ".") {
  47. endDate = time.Now().Format(utils.FormatDate)
  48. }
  49. windUrl := ``
  50. terminalCode := ""
  51. // 试用平台的话,需要额外从弘则这边获取下地址
  52. if utils.BusinessCode == utils.HZ_TRIAL_BUSSINESS_CODE {
  53. tmpWindUrl, tmpTerminalCode, err, errMsg := services.GetHzWindUrl(req.EdbCode, source)
  54. if err != nil {
  55. br.Msg = "添加失败!"
  56. br.ErrMsg = "添加失败,Err:" + err.Error()
  57. if errMsg != `` {
  58. br.Msg = errMsg
  59. }
  60. return
  61. }
  62. windUrl = tmpWindUrl
  63. terminalCode = tmpTerminalCode
  64. }
  65. if terminalCode == `` {
  66. terminalInfo, err := services.GetTerminal(utils.DATA_SOURCE_WIND, "")
  67. if err != nil {
  68. br.Msg = "获取可以使用的wind地址失败!"
  69. br.ErrMsg = "获取可以使用的wind地址失败,err:" + err.Error()
  70. return
  71. }
  72. windUrl = terminalInfo.ServerUrl
  73. terminalCode = terminalInfo.TerminalCode
  74. }
  75. if terminalCode == `` || windUrl == `` {
  76. br.Msg = "没有配置wind终端!"
  77. br.ErrMsg = "没有配置wind终端"
  78. return
  79. }
  80. // 设置指标与终端关系的缓存
  81. windUrlCacheKey := utils.CACHE_WIND_URL + ":" + req.StockCode + req.EdbCode
  82. _ = utils.Rc.SetNX(windUrlCacheKey, windUrl, utils.GetTodayLastSecond())
  83. // 设置指标与终端关系的缓存
  84. terminalCodeCacheKey := utils.CACHE_EDB_TERMINAL_CODE_URL + req.StockCode
  85. _ = utils.Rc.Put(terminalCodeCacheKey, terminalCode, utils.GetTodayLastSecond())
  86. //windUrl = utils.Hz_Wind_Data_Url_LIST[1].Url
  87. dataItem, errCode, err := services.GetEdbDataFromWindUrlWsd(windUrl, req.StockCode, req.EdbCode, utils.BASE_START_DATE, endDate)
  88. if errCode == 421 { //指标超限
  89. br.Ret = 421
  90. br.Msg = "指标超限!"
  91. return
  92. }
  93. // 指标下架
  94. if errCode == services.WindNoAuthCode {
  95. br.Ret = 4052
  96. br.Msg = "指标已下架"
  97. return
  98. }
  99. if err != nil {
  100. br.Msg = "获取指标信息失败!"
  101. br.ErrMsg = "获取指标信息失败 GetEdbDataFromWind,Err:" + err.Error()
  102. return
  103. }
  104. err = models.AddEdbDataFromWindWsd(req.StockCode, dataItem)
  105. if err != nil {
  106. br.Msg = "获取指标信息失败!"
  107. br.ErrMsg = "获取指标信息失败 AddEdbDataFromWindWsd,Err:" + err.Error()
  108. return
  109. }
  110. br.Ret = 200
  111. br.Success = true
  112. br.Msg = "获取成功"
  113. //br.Data = dataItem
  114. } else {
  115. br.Ret = 501
  116. br.Success = true
  117. br.Msg = "系统处理中,请稍后重试"
  118. }
  119. }
  120. // @Title 刷新wind指标接口
  121. // @Description 刷新wind指标接口
  122. // @Success 200 {object} models.RefreshEdbInfoReq
  123. // @router /wsd/refresh [post]
  124. func (this *WindWsdController) Refresh() {
  125. br := new(models.BaseResponse).Init()
  126. var cacheKey string
  127. defer func() {
  128. this.Data["json"] = br
  129. this.ServeJSON()
  130. }()
  131. source := utils.DATA_SOURCE_WIND
  132. var req models.RefreshEdbInfoReq
  133. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  134. if err != nil {
  135. br.Msg = "参数解析异常!"
  136. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  137. return
  138. }
  139. if req.EdbCode == "" {
  140. br.Msg = "请输入指标编码!"
  141. br.ErrMsg = "请输入指标编码,指标编码为空"
  142. return
  143. }
  144. if req.EdbInfoId <= 0 {
  145. br.Msg = "请输入指标ID!"
  146. br.ErrMsg = "请输入指标ID"
  147. return
  148. }
  149. cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(source) + "_" + req.EdbCode
  150. if utils.Rc.IsExist(cacheKey) {
  151. br.Ret = 501
  152. br.Success = true
  153. br.Msg = "系统处理中,请稍后重试"
  154. return
  155. }
  156. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  157. defer func() {
  158. utils.Rc.Delete(cacheKey)
  159. }()
  160. //获取指标信息
  161. edbInfo, err := models.GetEdbInfoById(req.EdbInfoId)
  162. if err != nil {
  163. br.Msg = "查询指标信息失败!"
  164. br.ErrMsg = "查询指标信息失败 Err:" + err.Error()
  165. return
  166. }
  167. //期货数据,就默认到今天,特殊处理下
  168. endDate := utils.BASE_END_DATE
  169. if strings.Contains(req.EdbCode, ".") {
  170. endDate = time.Now().Format(utils.FormatDate)
  171. }
  172. //获取对应的终端信息
  173. terminal, err := services.GetTerminal(utils.DATA_SOURCE_WIND, edbInfo.TerminalCode)
  174. if err != nil {
  175. br.Msg = "终端未配置"
  176. return
  177. }
  178. dataItem, errCode, err := services.GetEdbDataFromWindUrlWsd(terminal.ServerUrl, edbInfo.StockCode, edbInfo.IndicatorCode, utils.GetEdbRefreshStartDate(req.StartDate), utils.GetEdbRefreshEndDate(endDate))
  179. if errCode == 421 { //指标超限
  180. br.Ret = 421
  181. br.Msg = "指标超限!"
  182. return
  183. }
  184. // 忽略掉指标下架的错误, 并更新指标为停止更新
  185. if errCode == services.WindNoAuthCode {
  186. edbInfo.NoUpdate = 1
  187. edbInfo.ModifyTime = time.Now().Local()
  188. if e := edbInfo.Update([]string{"NoUpdate", "ModifyTime"}); e != nil {
  189. br.Msg = "刷新失败"
  190. br.ErrMsg = "更新wind指标停更失败, Err: " + e.Error()
  191. return
  192. }
  193. br.Ret = 200
  194. br.Msg = "操作成功"
  195. return
  196. }
  197. if err != nil {
  198. br.Msg = "获取指标信息失败!"
  199. br.ErrMsg = "获取指标信息失败 GetEdbDataFromWind,Err:" + err.Error()
  200. return
  201. }
  202. err = models.RefreshEdbDataFromWindWsd(req.EdbInfoId, edbInfo.EdbCode, utils.GetEdbRefreshStartDate(req.StartDate), dataItem)
  203. if err != nil && err.Error() != utils.ErrNoRow() {
  204. br.Msg = "刷新指标信息失败!"
  205. br.ErrMsg = "刷新指标信息失败 RefreshEdbDataFromWind,Err:" + err.Error()
  206. return
  207. }
  208. // 更新指标最大最小值
  209. err, errMsg := models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  210. if err != nil {
  211. br.Msg = errMsg
  212. br.ErrMsg = err.Error()
  213. return
  214. }
  215. // 更新ES
  216. go logic.UpdateEs(edbInfo.EdbInfoId)
  217. br.Ret = 200
  218. br.Success = true
  219. br.Msg = "获取成功"
  220. }