base_from_wind.go 8.1 KB

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