base_from_wind.go 8.1 KB

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