base_from_wind_wsd.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 == `` {
  76. br.Msg = "没有配置wind终端!"
  77. br.ErrMsg = "没有配置wind终端"
  78. return
  79. }
  80. if windUrl == `` {
  81. windUrl, err = GetServerUrl()
  82. if err != nil {
  83. br.Msg = "获取可以使用的wind地址失败!"
  84. br.ErrMsg = "获取可以使用的wind地址失败,err:" + err.Error()
  85. return
  86. }
  87. }
  88. if windUrl == `` {
  89. br.Msg = "没有配置wind地址!"
  90. br.ErrMsg = "没有配置wind地址"
  91. return
  92. }
  93. // 设置指标与终端关系的缓存
  94. windUrlCacheKey := utils.CACHE_WIND_URL + ":" + req.StockCode + req.EdbCode
  95. _ = utils.Rc.SetNX(windUrlCacheKey, windUrl, utils.GetTodayLastSecond())
  96. //windUrl = utils.Hz_Wind_Data_Url_LIST[1].Url
  97. dataItem, errCode, err := services.GetEdbDataFromWindUrlWsd(windUrl, req.StockCode, req.EdbCode, utils.BASE_START_DATE, endDate)
  98. if errCode == 421 { //指标超限
  99. br.Ret = 421
  100. br.Msg = "指标超限!"
  101. return
  102. }
  103. // 指标下架
  104. if errCode == services.WindNoAuthCode {
  105. br.Ret = 4052
  106. br.Msg = "指标已下架"
  107. return
  108. }
  109. if err != nil {
  110. br.Msg = "获取指标信息失败!"
  111. br.ErrMsg = "获取指标信息失败 GetEdbDataFromWind,Err:" + err.Error()
  112. return
  113. }
  114. err = models.AddEdbDataFromWindWsd(req.StockCode, dataItem)
  115. if err != nil {
  116. br.Msg = "获取指标信息失败!"
  117. br.ErrMsg = "获取指标信息失败 AddEdbDataFromWindWsd,Err:" + err.Error()
  118. return
  119. }
  120. br.Ret = 200
  121. br.Success = true
  122. br.Msg = "获取成功"
  123. //br.Data = dataItem
  124. } else {
  125. br.Ret = 501
  126. br.Success = true
  127. br.Msg = "系统处理中,请稍后重试"
  128. }
  129. }
  130. // @Title 刷新wind指标接口
  131. // @Description 刷新wind指标接口
  132. // @Success 200 {object} models.RefreshEdbInfoReq
  133. // @router /wsd/refresh [post]
  134. func (this *WindWsdController) Refresh() {
  135. br := new(models.BaseResponse).Init()
  136. var cacheKey string
  137. defer func() {
  138. this.Data["json"] = br
  139. this.ServeJSON()
  140. }()
  141. source := utils.DATA_SOURCE_WIND
  142. var req models.RefreshEdbInfoReq
  143. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  144. if err != nil {
  145. br.Msg = "参数解析异常!"
  146. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  147. return
  148. }
  149. if req.EdbCode == "" {
  150. br.Msg = "请输入指标编码!"
  151. br.ErrMsg = "请输入指标编码,指标编码为空"
  152. return
  153. }
  154. if req.EdbInfoId <= 0 {
  155. br.Msg = "请输入指标ID!"
  156. br.ErrMsg = "请输入指标ID"
  157. return
  158. }
  159. cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(source) + "_" + req.EdbCode
  160. if utils.Rc.IsExist(cacheKey) {
  161. br.Ret = 501
  162. br.Success = true
  163. br.Msg = "系统处理中,请稍后重试"
  164. return
  165. }
  166. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  167. defer func() {
  168. utils.Rc.Delete(cacheKey)
  169. }()
  170. //获取指标信息
  171. edbInfo, err := models.GetEdbInfoById(req.EdbInfoId)
  172. if err != nil {
  173. br.Msg = "查询指标信息失败!"
  174. br.ErrMsg = "查询指标信息失败 Err:" + err.Error()
  175. return
  176. }
  177. //期货数据,就默认到今天,特殊处理下
  178. endDate := utils.BASE_END_DATE
  179. if strings.Contains(req.EdbCode, ".") {
  180. endDate = time.Now().Format(utils.FormatDate)
  181. }
  182. dataItem, errCode, err := services.GetEdbDataFromWindUrlWsd(edbInfo.ServerUrl, edbInfo.StockCode, edbInfo.IndicatorCode, utils.GetEdbRefreshStartDate(req.StartDate), utils.GetEdbRefreshEndDate(endDate))
  183. if errCode == 421 { //指标超限
  184. br.Ret = 421
  185. br.Msg = "指标超限!"
  186. return
  187. }
  188. // 忽略掉指标下架的错误, 并更新指标为停止更新
  189. if errCode == services.WindNoAuthCode {
  190. edbInfo.NoUpdate = 1
  191. edbInfo.ModifyTime = time.Now().Local()
  192. if e := edbInfo.Update([]string{"NoUpdate", "ModifyTime"}); e != nil {
  193. br.Msg = "刷新失败"
  194. br.ErrMsg = "更新wind指标停更失败, Err: " + e.Error()
  195. return
  196. }
  197. br.Ret = 200
  198. br.Msg = "操作成功"
  199. return
  200. }
  201. if err != nil {
  202. br.Msg = "获取指标信息失败!"
  203. br.ErrMsg = "获取指标信息失败 GetEdbDataFromWind,Err:" + err.Error()
  204. return
  205. }
  206. err = models.RefreshEdbDataFromWindWsd(req.EdbInfoId, edbInfo.EdbCode, utils.GetEdbRefreshStartDate(req.StartDate), dataItem)
  207. if err != nil && err.Error() != utils.ErrNoRow() {
  208. br.Msg = "刷新指标信息失败!"
  209. br.ErrMsg = "刷新指标信息失败 RefreshEdbDataFromWind,Err:" + err.Error()
  210. return
  211. }
  212. // 更新指标最大最小值
  213. err, errMsg := models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  214. if err != nil {
  215. br.Msg = errMsg
  216. br.ErrMsg = err.Error()
  217. return
  218. }
  219. // 更新ES
  220. go logic.UpdateEs(edbInfo.EdbInfoId)
  221. br.Ret = 200
  222. br.Success = true
  223. br.Msg = "获取成功"
  224. }