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