base_from_fubao.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_index_lib/logic"
  5. "eta_gn/eta_index_lib/models"
  6. "eta_gn/eta_index_lib/services"
  7. "eta_gn/eta_index_lib/utils"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. // 富宝-数据对接
  13. type FuBaoController struct {
  14. BaseAuthController
  15. }
  16. // @Title 新增富宝指标接口
  17. // @Description 新增富宝指标接口
  18. // @Success 200 {object} models.AddEdbInfoReq
  19. // @router /add [post]
  20. func (this *FuBaoController) 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_FUBAO
  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. dataItem, err := services.GetEdbDataFromFuBao(req.EdbCode, utils.BASE_START_DATE, utils.BASE_END_DATE)
  45. if err != nil {
  46. br.Msg = "获取指标信息失败!"
  47. br.ErrMsg = "获取指标信息失败 GetEdbDataFromLt,Err:" + err.Error()
  48. return
  49. }
  50. if dataItem.Code != 200 {
  51. br.Msg = "获取富宝指标数据失败!"
  52. br.ErrMsg = "获取富宝指标数据失败 GetEdbDataFromLt,Code:" + strconv.Itoa(dataItem.Code)
  53. return
  54. }
  55. if len(dataItem.ReturnValue.Data) > 0 {
  56. priceStr := dataItem.ReturnValue.Data[0].PriceStr
  57. strIndex := strings.Index(priceStr, "-")
  58. if strIndex > 0 {
  59. br.Msg = "此指标的数据格式暂不支持!"
  60. br.ErrMsg = "此指标的数据格式暂不支持,Val:" + priceStr + ";pid:" + req.EdbCode
  61. return
  62. }
  63. }
  64. err = models.AddEdbDataFromFubao(req.EdbCode, dataItem)
  65. if err != nil {
  66. br.Msg = "获取指标信息失败!"
  67. br.ErrMsg = "获取指标信息失败 AddEdbDataFromFubao,Err:" + err.Error()
  68. return
  69. }
  70. br.Ret = 200
  71. br.Success = true
  72. br.Msg = "获取成功"
  73. } else {
  74. br.Ret = 501
  75. br.Success = true
  76. br.Msg = "系统处理中,请稍后重试"
  77. }
  78. }
  79. // @Title 刷新富宝指标接口
  80. // @Description 刷新路透社指标接口
  81. // @Success 200 {object} models.RefreshEdbInfoReq
  82. // @router /refresh [post]
  83. func (this *FuBaoController) Refresh() {
  84. br := new(models.BaseResponse).Init()
  85. var cacheKey string
  86. defer func() {
  87. utils.Rc.Delete(cacheKey)
  88. this.Data["json"] = br
  89. this.ServeJSON()
  90. }()
  91. source := utils.DATA_SOURCE_FUBAO
  92. var req models.RefreshEdbInfoReq
  93. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  94. if err != nil {
  95. br.Msg = "参数解析异常!"
  96. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  97. return
  98. }
  99. if req.EdbCode == "" {
  100. br.Msg = "请输入指标编码!"
  101. br.ErrMsg = "请输入指标编码,指标编码为空"
  102. return
  103. }
  104. if req.EdbInfoId <= 0 {
  105. br.Msg = "请输入指标ID!"
  106. br.ErrMsg = "请输入指标ID"
  107. return
  108. }
  109. cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(source) + "_" + req.EdbCode
  110. if utils.Rc.IsExist(cacheKey) {
  111. br.Ret = 501
  112. br.Success = true
  113. br.Msg = "系统处理中,请稍后重试"
  114. return
  115. }
  116. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  117. // 获取指标详情
  118. edbInfo, err := models.GetEdbInfoByEdbCode(source, req.EdbCode)
  119. if err != nil {
  120. br.Msg = "指标不存在!"
  121. br.ErrMsg = "指标不存在"
  122. return
  123. }
  124. dataItem, err := services.GetEdbDataFromFuBao(req.EdbCode, req.StartDate, utils.BASE_END_DATE)
  125. if err != nil {
  126. br.Msg = "获取指标信息失败!"
  127. br.ErrMsg = "获取指标信息失败 GetEdbDataFromFuBao,Err:" + err.Error()
  128. return
  129. }
  130. err = models.RefreshEdbDataFromFubao(req.EdbInfoId, req.EdbCode, req.StartDate, dataItem)
  131. if err != nil && err.Error() != utils.ErrNoRow() {
  132. br.Msg = "刷新指标信息失败!"
  133. br.ErrMsg = "刷新指标信息失败 RefreshEdbDataFromFubao,Err:" + err.Error()
  134. return
  135. }
  136. // 更新指标最大最小值
  137. err, errMsg := models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  138. if err != nil {
  139. br.Msg = errMsg
  140. br.ErrMsg = err.Error()
  141. return
  142. }
  143. // 更新ES
  144. go logic.UpdateEs(edbInfo.EdbInfoId)
  145. br.Ret = 200
  146. br.Success = true
  147. br.Msg = "获取成功"
  148. }