base_from_sh.go 3.2 KB

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