base_from_pb.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 PbController struct {
  13. BaseAuthController
  14. }
  15. // @Title 新增彭博指标接口
  16. // @Description 新增彭博指标接口
  17. // @Success 200 {object} models.AddEdbInfoReq
  18. // @router /add [post]
  19. func (this *PbController) 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_PB
  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.GetEdbDataFromPb(req.EdbCode, utils.BASE_START_DATE_UnSpace, utils.BASE_END_DATE_UnSpace, "")
  44. if err != nil {
  45. br.Msg = "获取指标信息失败!"
  46. br.ErrMsg = "获取指标信息失败 GetEdbDataFromPb,Err:" + err.Error()
  47. return
  48. }
  49. err = models.AddEdbDataFromPb(req.EdbCode, dataItem)
  50. if err != nil {
  51. br.Msg = "获取指标信息失败!"
  52. br.ErrMsg = "获取指标信息失败 AddEdbDataFromPb,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 *PbController) 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_PB
  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. // 获取指标详情
  95. edbInfo, err := models.GetEdbInfoByEdbCode(source, req.EdbCode)
  96. if err != nil {
  97. br.Msg = "指标不存在!"
  98. br.ErrMsg = "指标不存在"
  99. return
  100. }
  101. cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(source) + "_" + req.EdbCode
  102. if utils.Rc.IsExist(cacheKey) {
  103. br.Ret = 501
  104. br.Success = true
  105. br.Msg = "系统处理中,请稍后重试"
  106. return
  107. }
  108. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  109. //开始日期格式化
  110. startDateTime, _ := time.ParseInLocation(utils.FormatDate, req.StartDate, time.Local)
  111. dataItem, err := services.GetEdbDataFromPb(req.EdbCode, startDateTime.Format(utils.FormatDateUnSpace), utils.BASE_END_DATE_UnSpace, edbInfo.TerminalCode)
  112. if err != nil {
  113. br.Msg = "获取指标信息失败!"
  114. br.ErrMsg = "获取指标信息失败 GetEdbDataFromPb,Err:" + err.Error()
  115. return
  116. }
  117. err = models.RefreshEdbDataFromPb(req.EdbInfoId, req.EdbCode, req.StartDate, dataItem)
  118. if err != nil && err.Error() != utils.ErrNoRow() {
  119. br.Msg = "刷新指标信息失败!"
  120. br.ErrMsg = "刷新指标信息失败 RefreshEdbDataFromPb,Err:" + err.Error()
  121. return
  122. }
  123. err, errMsg := models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  124. if err != nil {
  125. br.Msg = errMsg
  126. br.ErrMsg = err.Error()
  127. return
  128. }
  129. // 更新ES
  130. go logic.UpdateEs(edbInfo.EdbInfoId)
  131. br.Ret = 200
  132. br.Success = true
  133. br.Msg = "获取成功"
  134. }