base_from_wind.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 WindController struct {
  14. BaseAuthController
  15. }
  16. // @Title 新增wind指标接口
  17. // @Description 新增wind指标接口
  18. // @Success 200 {object} models.AddEdbInfoReq
  19. // @router /add [post]
  20. func (this *WindController) 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.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. // 试用平台的话,需要额外从弘则这边获取下地址
  51. if utils.BusinessCode == utils.HZ_TRIAL_BUSSINESS_CODE {
  52. tmpWindUrl, err, errMsg := services.GetHzWindUrl(req.EdbCode, source)
  53. if err != nil {
  54. br.Msg = "添加失败!"
  55. br.ErrMsg = "添加失败,Err:" + err.Error()
  56. if errMsg != `` {
  57. br.Msg = errMsg
  58. }
  59. return
  60. }
  61. windUrl = tmpWindUrl
  62. }
  63. if windUrl == `` {
  64. // 先配置中获取,如果有指定配置的话,那么取配置中的数据
  65. tmpConfig, err := models.GetConfigDetailByCode("wind_data_url")
  66. if err != nil && err.Error() != utils.ErrNoRow() {
  67. br.Msg = "获取配置的wind地址失败!"
  68. br.ErrMsg = "获取配置的wind地址失败,err:" + err.Error()
  69. return
  70. }
  71. if tmpConfig != nil {
  72. windUrl = tmpConfig.ConfigValue
  73. } else {
  74. windUrl, err = GetServerUrl(req.EdbCode)
  75. if err != nil {
  76. br.Msg = "获取使用的wind地址失败!"
  77. br.ErrMsg = "获取使用的wind地址失败,err:" + err.Error()
  78. return
  79. }
  80. }
  81. }
  82. if windUrl == `` {
  83. br.Msg = "没有配置wind地址!"
  84. br.ErrMsg = "没有配置wind地址"
  85. return
  86. }
  87. //windUrl = utils.Hz_Wind_Data_Url_LIST[1].Url
  88. dataItem, errCode, err := services.GetEdbDataFromWindUrl(windUrl, req.EdbCode, utils.BASE_START_DATE, endDate)
  89. //dataItem, errCode, err := services.GetEdbDataFromWind(req.EdbCode, utils.BASE_START_DATE, endDate)
  90. if errCode == 421 { //指标超限
  91. br.Ret = 421
  92. br.Msg = "指标超限!"
  93. return
  94. }
  95. // 指标下架
  96. if errCode == services.WindNoAuthCode {
  97. br.Ret = 4052
  98. br.Msg = "指标已下架"
  99. return
  100. }
  101. if err != nil {
  102. br.Msg = "获取指标信息失败!"
  103. br.ErrMsg = "获取指标信息失败 GetEdbDataFromWind,Err:" + err.Error()
  104. return
  105. }
  106. err = models.AddEdbDataFromWind(req.EdbCode, dataItem)
  107. if err != nil {
  108. br.Msg = "获取指标信息失败!"
  109. br.ErrMsg = "获取指标信息失败 AddEdbDataFromWind,Err:" + err.Error()
  110. return
  111. }
  112. br.Ret = 200
  113. br.Success = true
  114. br.Msg = "获取成功"
  115. } else {
  116. br.Ret = 501
  117. br.Success = true
  118. br.Msg = "系统处理中,请稍后重试"
  119. }
  120. }
  121. // @Title 刷新wind指标接口
  122. // @Description 刷新wind指标接口
  123. // @Success 200 {object} models.RefreshEdbInfoReq
  124. // @router /refresh [post]
  125. func (this *WindController) Refresh() {
  126. br := new(models.BaseResponse).Init()
  127. var cacheKey string
  128. defer func() {
  129. this.Data["json"] = br
  130. this.ServeJSON()
  131. }()
  132. source := utils.DATA_SOURCE_WIND
  133. var req models.RefreshEdbInfoReq
  134. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  135. if err != nil {
  136. br.Msg = "参数解析异常!"
  137. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  138. return
  139. }
  140. if req.EdbCode == "" {
  141. br.Msg = "请输入指标编码!"
  142. br.ErrMsg = "请输入指标编码,指标编码为空"
  143. return
  144. }
  145. if req.EdbInfoId <= 0 {
  146. br.Msg = "请输入指标ID!"
  147. br.ErrMsg = "请输入指标ID"
  148. return
  149. }
  150. cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(source) + "_" + req.EdbCode
  151. if utils.Rc.IsExist(cacheKey) {
  152. br.Ret = 501
  153. br.Success = true
  154. br.Msg = "系统处理中,请稍后重试"
  155. return
  156. }
  157. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  158. defer func() {
  159. utils.Rc.Delete(cacheKey)
  160. }()
  161. //获取指标信息
  162. edbInfo, err := models.GetEdbInfoById(req.EdbInfoId)
  163. if err != nil {
  164. br.Msg = "查询指标信息失败!"
  165. br.ErrMsg = "查询指标信息失败 Err:" + err.Error()
  166. return
  167. }
  168. //期货数据,就默认到今天,特殊处理下
  169. endDate := utils.BASE_END_DATE
  170. if strings.Contains(req.EdbCode, ".") {
  171. endDate = time.Now().Format(utils.FormatDate)
  172. }
  173. dataItem, errCode, err := services.GetEdbDataFromWindUrl(edbInfo.ServerUrl, req.EdbCode, req.StartDate, endDate)
  174. if errCode == 421 { //指标超限
  175. br.Ret = 421
  176. br.Msg = "指标超限!"
  177. return
  178. }
  179. // 忽略掉指标下架的错误, 并更新指标为停止更新
  180. if errCode == services.WindNoAuthCode {
  181. edbInfo.NoUpdate = 1
  182. edbInfo.ModifyTime = time.Now().Local()
  183. if e := edbInfo.Update([]string{"NoUpdate", "ModifyTime"}); e != nil {
  184. br.Msg = "刷新失败"
  185. br.ErrMsg = "更新wind指标停更失败, Err: " + e.Error()
  186. return
  187. }
  188. br.Ret = 200
  189. br.Msg = "操作成功"
  190. return
  191. }
  192. if err != nil {
  193. br.Msg = "获取指标信息失败!"
  194. br.ErrMsg = "获取指标信息失败 GetEdbDataFromWind,Err:" + err.Error()
  195. return
  196. }
  197. err = models.RefreshEdbDataFromWind(req.EdbInfoId, req.EdbCode, req.StartDate, dataItem)
  198. if err != nil && err.Error() != utils.ErrNoRow() {
  199. br.Msg = "刷新指标信息失败!"
  200. br.ErrMsg = "刷新指标信息失败 RefreshEdbDataFromWind,Err:" + err.Error()
  201. return
  202. }
  203. // 更新指标最大最小值
  204. err, errMsg := models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  205. if err != nil {
  206. br.Msg = errMsg
  207. br.ErrMsg = err.Error()
  208. return
  209. }
  210. // 更新ES
  211. go logic.UpdateEs(edbInfo.EdbInfoId)
  212. br.Ret = 200
  213. br.Success = true
  214. br.Msg = "获取成功"
  215. }
  216. // GetServerUrl 获取服务器地址
  217. func GetServerUrl(edbCode string) (windUrl string, err error) {
  218. list, err := models.GetServerUrlCountGroupByServerUrl()
  219. if err != nil {
  220. return
  221. }
  222. windUrlNum := 0
  223. //现有系统的wind数据服务地址分配
  224. windUrlMap := make(map[string]int)
  225. for _, v := range list {
  226. windUrlMap[v.ServerUrl] = v.Total
  227. }
  228. for _, tmpWind := range utils.Hz_Wind_Data_Url_LIST {
  229. tmpWindUrl := tmpWind.Url
  230. tmpNum := windUrlMap[tmpWindUrl]
  231. if windUrl == `` || (windUrl != `` && tmpNum < windUrlNum) {
  232. windUrl = tmpWindUrl
  233. windUrlNum = tmpNum
  234. }
  235. }
  236. //windUrl = `http://datawind.hzinsights.com:8040/`
  237. // 设置缓存
  238. cacheKey := utils.CACHE_WIND_URL + ":" + edbCode
  239. _ = utils.Rc.SetNX(cacheKey, windUrl, utils.GetTodayLastSecond())
  240. return
  241. }