base_from_lt.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. "time"
  10. )
  11. // 路透社
  12. type LtController struct {
  13. BaseAuthController
  14. }
  15. // @Title 新增路透指标接口
  16. // @Description 新增路透指标接口
  17. // @Success 200 {object} models.AddEdbInfoReq
  18. // @router /add [post]
  19. func (this *LtController) Add() {
  20. br := new(models.BaseResponse).Init()
  21. var cacheKey string
  22. defer func() {
  23. utils.Rc.Delete(cacheKey)
  24. this.Data["json"] = br
  25. this.ServeJSON()
  26. }()
  27. source := utils.DATA_SOURCE_LT
  28. var req models.AddEdbInfoReq
  29. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  30. if err != nil {
  31. br.Msg = "参数解析异常!"
  32. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  33. return
  34. }
  35. if req.EdbCode == "" {
  36. br.Msg = "请输入指标编码!"
  37. br.ErrMsg = "请输入指标编码,指标编码为空"
  38. return
  39. }
  40. cacheKey = utils.CACHE_EDB_DATA_ADD + strconv.Itoa(source) + "_" + req.EdbCode
  41. if !utils.Rc.IsExist(cacheKey) {
  42. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  43. dataItem, err := services.GetEdbDataFromLt(req.EdbCode, utils.BASE_START_DATE, utils.BASE_END_DATE, req.Frequency, "")
  44. if err != nil {
  45. br.Msg = "获取指标信息失败!"
  46. br.ErrMsg = "获取指标信息失败 GetEdbDataFromLt,Err:" + err.Error()
  47. return
  48. }
  49. err = models.AddEdbDataFromLt(req.EdbCode, dataItem)
  50. if err != nil {
  51. br.Msg = "获取指标信息失败!"
  52. br.ErrMsg = "获取指标信息失败 AddEdbDataFromLt,Err:" + err.Error()
  53. return
  54. }
  55. br.Ret = 200
  56. br.Success = true
  57. br.Msg = "获取成功"
  58. } else {
  59. br.Ret = 501
  60. br.Success = true
  61. br.Msg = "系统处理中,请稍后重试"
  62. }
  63. }
  64. // @Title 刷新路透社指标接口
  65. // @Description 刷新路透社指标接口
  66. // @Success 200 {object} models.RefreshEdbInfoReq
  67. // @router /refresh [post]
  68. func (this *LtController) Refresh() {
  69. br := new(models.BaseResponse).Init()
  70. var cacheKey string
  71. defer func() {
  72. utils.Rc.Delete(cacheKey)
  73. this.Data["json"] = br
  74. this.ServeJSON()
  75. }()
  76. source := utils.DATA_SOURCE_LT
  77. var req models.RefreshEdbInfoReq
  78. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  79. if err != nil {
  80. br.Msg = "参数解析异常!"
  81. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  82. return
  83. }
  84. if req.EdbCode == "" {
  85. br.Msg = "请输入指标编码!"
  86. br.ErrMsg = "请输入指标编码,指标编码为空"
  87. return
  88. }
  89. if req.EdbInfoId <= 0 {
  90. br.Msg = "请输入指标ID!"
  91. br.ErrMsg = "请输入指标ID"
  92. return
  93. }
  94. cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(source) + "_" + req.EdbCode
  95. if utils.Rc.IsExist(cacheKey) {
  96. br.Ret = 501
  97. br.Success = true
  98. br.Msg = "系统处理中,请稍后重试"
  99. return
  100. }
  101. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  102. // 获取指标详情
  103. edbInfo, err := models.GetEdbInfoByEdbCode(source, req.EdbCode)
  104. if err != nil {
  105. br.Msg = "指标不存在!"
  106. br.ErrMsg = "指标不存在"
  107. return
  108. }
  109. dataItem, err := services.GetEdbDataFromLt(req.EdbCode, utils.GetEdbRefreshStartDate(req.StartDate), utils.BASE_END_DATE, edbInfo.Frequency, edbInfo.TerminalCode)
  110. if err != nil {
  111. br.Msg = "获取指标信息失败!"
  112. br.ErrMsg = "获取指标信息失败 GetEdbDataFromLt,Err:" + err.Error()
  113. return
  114. }
  115. err = models.RefreshEdbDataFromLt(req.EdbInfoId, req.EdbCode, utils.GetEdbRefreshStartDate(req.StartDate), dataItem)
  116. if err != nil && err.Error() != utils.ErrNoRow() {
  117. br.Msg = "刷新指标信息失败!"
  118. br.ErrMsg = "刷新指标信息失败 RefreshEdbDataFromLt,Err:" + err.Error()
  119. return
  120. }
  121. // 更新指标最大最小值
  122. err, errMsg := models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  123. if err != nil {
  124. br.Msg = errMsg
  125. br.ErrMsg = err.Error()
  126. return
  127. }
  128. // 更新ES
  129. go logic.UpdateEs(edbInfo.EdbInfoId)
  130. br.Ret = 200
  131. br.Success = true
  132. br.Msg = "获取成功"
  133. }