base_from_com_trade.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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/utils"
  7. "strconv"
  8. "time"
  9. )
  10. // ComTradeController Eia Steo报告
  11. type ComTradeController struct {
  12. BaseAuthController
  13. }
  14. // Add
  15. // @Title 新增Eia Steo指标接口
  16. // @Description 新增Eia Steo指标接口
  17. // @Success 200 {object} models.AddEdbInfoReq
  18. // @router /add [post]
  19. func (this *ComTradeController) 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_COM_TRADE
  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. err = models.AddEdbDataFromComTrade(req.EdbCode)
  44. if err != nil {
  45. br.Msg = "获取指标信息失败!"
  46. br.ErrMsg = "获取指标信息失败 AddEdbDataFromComTrade,Err:" + err.Error()
  47. return
  48. }
  49. br.Ret = 200
  50. br.Success = true
  51. br.Msg = "获取成功"
  52. } else {
  53. br.Ret = 501
  54. br.Success = true
  55. br.Msg = "系统处理中,请稍后重试"
  56. }
  57. }
  58. // Refresh
  59. // @Title 刷新Eia Steo指标接口
  60. // @Description 刷新Eia Steo指标接口
  61. // @Success 200 {object} models.RefreshEdbInfoReq
  62. // @router /refresh [post]
  63. func (this *ComTradeController) Refresh() {
  64. br := new(models.BaseResponse).Init()
  65. var cacheKey string
  66. defer func() {
  67. utils.Rc.Delete(cacheKey)
  68. this.Data["json"] = br
  69. this.ServeJSON()
  70. }()
  71. source := utils.DATA_SOURCE_COM_TRADE
  72. var req models.RefreshEdbInfoReq
  73. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  74. if err != nil {
  75. br.Msg = "参数解析异常!"
  76. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  77. return
  78. }
  79. if req.EdbCode == "" {
  80. br.Msg = "请输入指标编码!"
  81. br.ErrMsg = "请输入指标编码,指标编码为空"
  82. return
  83. }
  84. if req.EdbInfoId <= 0 {
  85. br.Msg = "请输入指标ID!"
  86. br.ErrMsg = "请输入指标ID"
  87. return
  88. }
  89. // 获取指标详情
  90. edbInfo, err := models.GetEdbInfoByEdbCode(source, req.EdbCode)
  91. if err != nil {
  92. br.Msg = "指标不存在!"
  93. br.ErrMsg = "指标不存在"
  94. return
  95. }
  96. cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(source) + "_" + req.EdbCode
  97. if utils.Rc.IsExist(cacheKey) {
  98. br.Ret = 501
  99. br.Success = true
  100. br.Msg = "系统处理中,请稍后重试"
  101. return
  102. }
  103. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  104. err = models.RefreshEdbDataFromComTrade(req.EdbInfoId, req.EdbCode, req.StartDate)
  105. if err != nil && err.Error() != utils.ErrNoRow() {
  106. br.Msg = "刷新指标信息失败!"
  107. br.ErrMsg = "刷新指标信息失败 RefreshEdbDataFromComTrade,Err:" + err.Error()
  108. return
  109. }
  110. // 更新指标最大最小值
  111. err, errMsg := models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  112. if err != nil {
  113. br.Msg = errMsg
  114. br.ErrMsg = err.Error()
  115. return
  116. }
  117. // 更新ES
  118. go logic.UpdateEs(edbInfo.EdbInfoId)
  119. br.Ret = 200
  120. br.Success = true
  121. br.Msg = "获取成功"
  122. }