base_from_wind.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. // 试用平台的话,需要额外从弘则这边获取下地址
  52. if utils.BusinessCode == utils.HZ_TRIAL_BUSSINESS_CODE {
  53. tmpWindUrl, 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. }
  64. if windUrl == `` {
  65. windUrl, err = GetServerUrl()
  66. if err != nil {
  67. br.Msg = "获取可以使用的wind地址失败!"
  68. br.ErrMsg = "获取可以使用的wind地址失败,err:" + err.Error()
  69. return
  70. }
  71. }
  72. if windUrl == `` {
  73. br.Msg = "没有配置wind地址!"
  74. br.ErrMsg = "没有配置wind地址"
  75. return
  76. }
  77. // 判断是否调用桥接服务
  78. windBridgeUrl, _ := services.GetWindBridgeUrl()
  79. if windBridgeUrl != "" {
  80. windUrl = windBridgeUrl
  81. }
  82. // 设置指标与终端关系的缓存
  83. windUrlCacheKey := utils.CACHE_WIND_URL + ":" + req.EdbCode
  84. _ = utils.Rc.SetNX(windUrlCacheKey, windUrl, utils.GetTodayLastSecond())
  85. var dataList []*models.EdbDataFromWindSimple
  86. if windBridgeUrl != "" {
  87. var errMsg string
  88. dataList, err, errMsg = services.GetEdbDataFromBridge(windUrl, req, endDate)
  89. if err != nil {
  90. br.Msg = "获取指标信息失败!"
  91. br.ErrMsg = "获取桥接服务指标信息失败,Err:" + errMsg + err.Error()
  92. return
  93. }
  94. } else {
  95. dataItem, errCode, err := services.GetEdbDataFromWindUrl(windUrl, req.EdbCode, utils.BASE_START_DATE, endDate)
  96. //dataItem, errCode, err := services.GetEdbDataFromWind(req.EdbCode, utils.BASE_START_DATE, endDate)
  97. if errCode == 421 { //指标超限
  98. br.Ret = 421
  99. br.Msg = "指标超限!"
  100. return
  101. }
  102. // 指标下架
  103. if errCode == services.WindNoAuthCode {
  104. br.Ret = 4052
  105. br.Msg = "指标已下架"
  106. return
  107. }
  108. if err != nil {
  109. br.Msg = "获取指标信息失败!"
  110. br.ErrMsg = "获取指标信息失败 GetEdbDataFromWind,Err:" + err.Error()
  111. return
  112. }
  113. dataList = models.DealWindData(dataItem)
  114. }
  115. err = models.AddEdbDataFromWind(req.EdbCode, dataList)
  116. if err != nil {
  117. br.Msg = "获取指标信息失败!"
  118. br.ErrMsg = "获取指标信息失败 AddEdbDataFromWind,Err:" + err.Error()
  119. return
  120. }
  121. br.Ret = 200
  122. br.Success = true
  123. br.Msg = "获取成功"
  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 /refresh [post]
  134. func (this *WindController) 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. // 判断是否调用桥接服务
  183. windBridgeUrl, _ := services.GetWindBridgeJYUrl()
  184. var dataList []*models.EdbDataFromWindSimple
  185. if windBridgeUrl != "" && windBridgeUrl == edbInfo.ServerUrl {
  186. var errMsg string
  187. dataList, err, errMsg = services.RefreshEdbDataFromWindBridge(edbInfo.ServerUrl, req, edbInfo, endDate)
  188. if err != nil {
  189. br.Msg = "获取指标信息失败!"
  190. br.ErrMsg = "获取桥接服务指标信息失败,Err:" + errMsg + err.Error()
  191. return
  192. }
  193. } else {
  194. dataItem, errCode, err := services.GetEdbDataFromWindUrl(edbInfo.ServerUrl, req.EdbCode, req.StartDate, 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. dataList = models.DealWindData(dataItem)
  219. }
  220. err = models.RefreshEdbDataFromWind(req.EdbInfoId, req.EdbCode, req.StartDate, dataList)
  221. if err != nil && err.Error() != utils.ErrNoRow() {
  222. br.Msg = "刷新指标信息失败!"
  223. br.ErrMsg = "刷新指标信息失败 RefreshEdbDataFromWind,Err:" + err.Error()
  224. return
  225. }
  226. // 更新指标最大最小值
  227. err, errMsg := models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  228. if err != nil {
  229. br.Msg = errMsg
  230. br.ErrMsg = err.Error()
  231. return
  232. }
  233. // 更新ES
  234. go logic.UpdateEs(edbInfo.EdbInfoId)
  235. br.Ret = 200
  236. br.Success = true
  237. br.Msg = "获取成功"
  238. }
  239. // GetServerUrl 获取服务器地址
  240. func GetServerUrl() (windUrl string, err error) {
  241. // 先配置中获取,如果有指定配置的话,那么取配置中的数据
  242. tmpConfig, err := models.GetConfigDetailByCode("wind_data_url")
  243. if err != nil && err.Error() != utils.ErrNoRow() {
  244. err = errors.New("获取配置的wind地址失败,err:" + err.Error())
  245. return
  246. }
  247. err = nil
  248. if tmpConfig != nil {
  249. windUrl = tmpConfig.ConfigValue
  250. return
  251. }
  252. // 配置中没有的话,那么就从分组获取
  253. list, err := models.GetServerUrlCountGroupByServerUrl()
  254. if err != nil {
  255. return
  256. }
  257. windUrlNum := 0
  258. //现有系统的wind数据服务地址分配
  259. windUrlMap := make(map[string]int)
  260. for _, v := range list {
  261. windUrlMap[v.ServerUrl] = v.Total
  262. }
  263. for _, tmpWind := range utils.Hz_Wind_Data_Url_LIST {
  264. tmpWindUrl := tmpWind.Url
  265. tmpNum := windUrlMap[tmpWindUrl]
  266. if windUrl == `` || (windUrl != `` && tmpNum < windUrlNum) {
  267. windUrl = tmpWindUrl
  268. windUrlNum = tmpNum
  269. }
  270. }
  271. return
  272. }