base_from_zz.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 ZzController struct {
  12. BaseAuthController
  13. }
  14. // @Title 新增郑商所指标接口
  15. // @Description 新增郑商所指标接口
  16. // @Success 200 {object} models.AddEdbInfoReq
  17. // @router /add [post]
  18. func (this *ZzController) 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_ZZ
  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.AddEdbDataFromZz(req.EdbCode)
  43. if err != nil {
  44. br.Msg = "获取指标信息失败!"
  45. br.ErrMsg = "获取指标信息失败 AddEdbDataFromZz,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 *ZzController) Refresh() {
  62. br := new(models.BaseResponse).Init()
  63. var cacheKey string
  64. defer func() {
  65. this.Data["json"] = br
  66. this.ServeJSON()
  67. }()
  68. source := utils.DATA_SOURCE_ZZ
  69. var req models.RefreshEdbInfoReq
  70. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  71. if err != nil {
  72. br.Msg = "参数解析异常!"
  73. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  74. return
  75. }
  76. if req.EdbCode == "" {
  77. br.Msg = "请输入指标编码!"
  78. br.ErrMsg = "请输入指标编码,指标编码为空"
  79. return
  80. }
  81. if req.EdbInfoId <= 0 {
  82. br.Msg = "请输入指标ID!"
  83. br.ErrMsg = "请输入指标ID"
  84. return
  85. }
  86. // 获取指标详情
  87. edbInfo, err := models.GetEdbInfoByEdbCode(source, req.EdbCode)
  88. if err != nil {
  89. br.Msg = "指标不存在!"
  90. br.ErrMsg = "指标不存在"
  91. return
  92. }
  93. cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(source) + "_" + req.EdbCode
  94. if utils.Rc.IsExist(cacheKey) {
  95. br.Ret = 501
  96. br.Success = true
  97. br.Msg = "系统处理中,请稍后重试"
  98. return
  99. }
  100. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  101. defer func() {
  102. utils.Rc.Delete(cacheKey)
  103. }()
  104. err = models.RefreshEdbDataFromZz(req.EdbInfoId, req.EdbCode, req.StartDate)
  105. if err != nil && err.Error() != utils.ErrNoRow() {
  106. br.Msg = "刷新指标信息失败!"
  107. br.ErrMsg = "刷新指标信息失败 RefreshEdbDataFromZz,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. }